Skip to content

Conversation

@LaurenceJJones
Copy link
Member

@LaurenceJJones LaurenceJJones commented Dec 10, 2025

Remove Gin dependency and migrate to net/http

Replaced gin-gonic/gin with Go's standard net/http, eliminating 3 direct dependencies (plus ~36 transitive) while maintaining 100% API functionality.

Dependencies Removed

  • github.com/gin-gonic/gin
  • github.com/gin-contrib/gzip
  • github.com/appleboy/gin-jwt/v2

Changes

  • Custom router using net/http.ServeMux (Go 1.22+ patterns)
  • Standard http.HandlerFunc handlers
  • JWT middleware using golang-jwt/jwt/v4
  • IP handling migrated to net/netip

draft PR needs to be fully tested and edge cases before approval

- Replace gin-gonic/gin with standard net/http.ServeMux (Go 1.22+ patterns)
- Implement custom router with middleware chaining support
- Migrate all handlers from gin.Context to standard http.HandlerFunc
- Rewrite JWT middleware using golang-jwt/jwt/v4
- Fix trusted proxies vs TrustedIPs confusion (store resolved IP in context)
- Fix middleware bypass for 404/405 requests
- Remove strict JSON validation for backwards compatibility
- Fix double middleware execution
- All requests now properly go through middleware pipeline
- Replace net.IPNet with netip.Prefix for CIDR networks
- Replace net.ParseIP() with netip.ParseAddr() for IP addresses
- Replace net.ParseCIDR() with netip.ParsePrefix() for CIDR parsing
- Update ResolveClientIP to accept both netip.Prefix and net.IPNet for compatibility
- Update convertTrustedProxies to return []netip.Prefix
- Update ClientIPMiddleware to use netip.Prefix
- Update controllers to use netip for IP parsing and comparison
- Maintain backwards compatibility with existing net.IPNet types from config
@github-actions
Copy link

@LaurenceJJones: There are no 'kind' label on this PR. You need a 'kind' label to generate the release automatically.

  • /kind feature
  • /kind enhancement
  • /kind refactoring
  • /kind fix
  • /kind chore
  • /kind dependencies
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@github-actions
Copy link

@LaurenceJJones: There are no area labels on this PR. You can add as many areas as you see fit.

  • /area agent
  • /area local-api
  • /area cscli
  • /area appsec
  • /area security
  • /area configuration
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@LaurenceJJones LaurenceJJones changed the title feat(api): Remove gin depedancy feat(api): Remove gin dependancy Dec 10, 2025
- Remove trailing newline from JSON responses (use json.Marshal instead of Encoder)
- Return 'incorrect Username or Password' for auth failures (security: don't reveal if machine exists)
- Add specific error for empty cookie tokens
- Reject certificates signed directly by root CA when intermediate is expected
- Update tests to match new error messages
- Iterate backwards (right to left) through X-Forwarded-For and X-Real-IP headers
- Extract helper functions: isTrustedProxy, findFirstUntrustedIP, isRemoteAddrTrusted
- Handle multiple IPs in X-Real-IP header (comma-separated)
- Simplify type handling: only accept []netip.Prefix (removed []net.IPNet compatibility)
- Fix empty RemoteAddr handling in tests
@codecov
Copy link

codecov bot commented Dec 10, 2025

Codecov Report

❌ Patch coverage is 61.13744% with 410 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.64%. Comparing base (874cf66) to head (ad9e91f).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
pkg/apiserver/controllers/v1/decisions.go 22.65% 98 Missing and 1 partial ⚠️
pkg/apiserver/middlewares/v1/jwt.go 62.14% 68 Missing and 13 partials ⚠️
pkg/apiserver/router/helpers.go 50.36% 57 Missing and 11 partials ⚠️
pkg/apiserver/router/router.go 69.02% 28 Missing and 7 partials ⚠️
pkg/apiserver/middlewares/logging.go 61.36% 16 Missing and 1 partial ⚠️
pkg/apiserver/controllers/v1/alerts.go 71.42% 10 Missing and 4 partials ⚠️
pkg/apiserver/apiserver.go 65.71% 9 Missing and 3 partials ⚠️
pkg/apiserver/middlewares/gzip.go 52.00% 11 Missing and 1 partial ⚠️
pkg/apiserver/controllers/v1/machines.go 65.62% 10 Missing and 1 partial ⚠️
pkg/apiserver/router/middleware.go 31.25% 11 Missing ⚠️
... and 9 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4128      +/-   ##
==========================================
- Coverage   62.86%   62.64%   -0.22%     
==========================================
  Files         464      471       +7     
  Lines       33290    33748     +458     
==========================================
+ Hits        20927    21143     +216     
- Misses      10241    10449     +208     
- Partials     2122     2156      +34     
Flag Coverage Δ
bats 46.06% <51.75%> (-0.15%) ⬇️
unit-linux 35.77% <52.22%> (+0.16%) ⬆️
unit-windows 24.63% <1.70%> (-0.42%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Fix contextcheck: Add nolint for intentional context.Background() usage
- Fix errcheck: Remove unused nolint directives
- Fix nilerr: Add nolint for correct false, nil return pattern
- Fix revive: Change MachineIDKey to typed context key
- Fix unused: Remove unused realm field and isBrokenConnection function
- Fix unconvert: Remove unnecessary conversion
- Fix ineffassign: Remove unused path variable
- Update test: Change expected error from 'cookie token is empty' to 'token not found' to match actual behavior when no cookie is present
Access logs should only be written when the logger level allows info-level messages.
This ensures that when log level is set to 'error', access logs don't appear in stderr,
matching the expected behavior in tests.
@LaurenceJJones LaurenceJJones changed the title feat(api): Remove gin dependancy feat(api): Remove gin dependency Dec 10, 2025
Set Code: 200 in successful login and refresh token responses to match
the swagger spec and fix lp-get-token test that expects code field.
- Refactor StreamDecisionChunked to extract helper functions
- Simplify ResolveClientIP and extract helper functions
- Fix Unix socket handling to check forwarded headers before fallback
- Treat Unix socket (@) as 127.0.0.1 for trusted proxy validation
- Reduce verbose comments throughout
- Fix all golangci-lint issues (nestif, errcheck, contextcheck, etc.)
- Set route patterns before middleware runs to fix Prometheus cardinality
- Fix gzip middleware to properly close original body (prevents socket leaks)
- Forward optional ResponseWriter interfaces (Flusher, Hijacker, Pusher, ReaderFrom)
- Fix filters map mutation by working on copies
- Pre-wrap router middleware chain for better performance
- Modernize loops (range over int, maps.Copy)
- Extract findPatternWithVariables helper to reduce nesting
- Change matchesPattern to standalone function (unused receiver)
- Fix intrange: use range over slice directly
- All golangci-lint errors resolved
- Fix nolintlint error by placing directive before function declaration
- All golangci-lint errors resolved
- Remove locks and atomic.Value since routes are registered once at startup
- Add Build() method to finalize router after all routes are registered
- Add built flag to prevent modifications after Build()
- Store route patterns in map for routePatternMiddleware
- Set unknown routes to 'invalid-endpoint' to bound metrics cardinality
- Move startTime in PrometheusMiddleware to just before handler execution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant