fix(api): accept service account keys on the Bearer auth path - #263
Closed
wangzhengzhuo05 wants to merge 3 commits into
Closed
wangzhengzhuo05 wants to merge 3 commits into
wangzhengzhuo05 wants to merge 3 commits into
Conversation
The Authorization: Bearer fallback in WithAuth only validated user API keys, so a service account key that works in X-API-Key got a 401 as a bearer token. Extract the service-account validation into a shared helper and fall through to it on the bearer path too. Fixes marmotdata#253
Member
|
/assign |
Author
|
Closing this out \u2014 I'm wrapping up my open-source contribution activity and don't want to leave work sitting in maintainers' queues indefinitely.\n\nThe change itself is still valid and CI is green: feel free to cherry-pick the patch, take just the test, or adapt it in whatever direction fits this codebase best. I'm happy to reopen if you'd rather review it as-is, and I can answer questions on the approach for as long as the branch stays around.\n\nThanks for taking the time to look at it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A valid service-account key sent as
Authorization: Bearer <key>got a401 Invalid token. TheX-API-Keypath accepted the same key, so the two carriers disagreed.Why
WithAuthhas two API-key carriers.X-API-Keyvalidated the user key and then fell back toglobalServiceAccountService.ValidateAPIKey, but theAuthorization: Bearerfallback called onlyuserService.ValidateAPIKey. A service-account key is not a user key, souser.ErrInvalidAPIKeypropagated straight to the 401.How
serviceAccountPrincipal(ctx, apiKey), which returns the matchingauth.Principaland anokflag. It preserves the existing role/permission projection and theglobalServiceAccountService == nilguard.X-API-Keypath now calls that helper (behaviour unchanged — same guard, same order, same 401).X-API-Keypath: user key first, then service-account key, then the refusal. The K8s ServiceAccount-token branch, OIDC exchange, anonymous branch, JWT and user-key behaviour are untouched.Tests
New
internal/api/v1/common/middleware_serviceaccount_test.go, using the existingmockUserService/mockAuthServicefrommiddleware_oidc_test.goplus amockServiceAccountService. Four subtests:service_account(401 before this change)user(the user path is not displaced)Mutation check
Disabling the new bearer fallback (
if errors.Is(err, user.ErrInvalidAPIKey)→if false) makes the new test fail withexpected 200, got 401while the other three subtests still pass — so the test guards this fix and not the surrounding behaviour. Restoring the helper returns the package to green.Fixes #253