feat: add auth package with file-based token support for K8s projected volumes#3244
feat: add auth package with file-based token support for K8s projected volumes#3244akshat-kumar-singhal wants to merge 17 commits into
Conversation
…file based token (for k8s)
Added observability
|
Hey @akshat-kumar-singhal ! Thanks for the PR.
|
Addresses PR gofr-dev#3244 review from @Umang01-hash: 1. Reverts the service/ restructure, sub-package migration of existing auth types, and deprecations. BasicAuth/APIKey/OAuth and their tests are restored untouched. 2. Adds FileTokenAuthConfig in a new pkg/gofr/service/auth/ sub-package containing only new code, so existing types are unaffected. The decorator exposes Unwrap() service.HTTP so ConnectionPool / CircuitBreaker / Retry can still reach the base *httpService. 3. Adds a generic `interface{ Unwrap() HTTP }` case to extractHTTPService in connection_pool.go. This fixes the silent no-op the reviewer flagged where pool/CB/retry options stopped working when combined with an auth decorator defined outside the service package, and future-proofs the unwrap path against the same class of bug. Tests include a regression lock-in that combines FileTokenAuthConfig with ConnectionPoolConfig end-to-end. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- err113: replace fmt.Errorf with static errAuthHeaderPresent - bodyclose: close response bodies in tests - wsl_v5: required blank lines in tests Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- NewFileTokenAuthConfig now requires a file.FileSystem (typically file.NewLocalFileSystem(app.Logger())) and reads the token through it, dropping the direct os.ReadFile dependency. Mock filesystems can be injected in tests. - Adds examples/using-file-token-auth demonstrating the setup in a gofr app pointed at the Kubernetes projected SA token path. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Let the example use gofr's default port instead of hardcoding 9016. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Description:
pkg/gofr/service/authpackage that consolidates authentication for outgoing HTTP service callsProviderinterface (header key + value) andTokenSourceinterface (raw token) to cleanly separate auth header injection from token acquisitionNewBearerAuthOption(TokenSource)for bearer-style auth andNewAuthOption(Provider)for custom header-based authNewFileTokenAuthConfig) supporting Kubernetes projected service account tokens with automatic periodic refresh andsync.Once-guarded shutdownTokenSourceat construction time, leveraginggolang.org/x/oauth2's built-in token refresh instead of re-creating a token source per requestObservableinterface toservice.Optionsenabling automatic logger/metrics injection fromNewHTTPServiceinto options that support it. WhenAddOptionis called directly, callers must set logger/metrics manuallyErrtype implementsUnwrap()for proper error chain traversal witherrors.Is/errors.Asservice/(BasicAuthConfig,APIKeyConfig,OAuthConfig,AuthErr) — deprecated code remains fully functionalauth/package with table-driven tests; all concrete types are unexported to minimize API surfaceBreaking Changes: None. All existing
service.NewBasicAuthConfig,service.NewAPIKeyConfig,service.NewOAuthConfigcontinue to work unchanged.Additional Information:
/var/run/secrets/kubernetes.io/serviceaccount/tokenand 30s refresh intervalChecklist:
goimportandgolangci-lint.