Repository navigation
ref(service-accounts): Split resolve_service_account out of viewer-context auth - #130679
Merged
Merged
Conversation
…ntext auth Move the account lookup and checks in authenticate_service_account into resolve_service_account(service_account_id, organization_id), and keep authenticate_service_account as a thin wrapper that reads the viewer context. The service account token authenticator will call the same helper, so both paths apply the same active, org and feature checks in both silos. No behavior change.
sentry-junior
Bot
force-pushed
the
ref/resolve-service-account
branch
from
November 12, 2026 14:43
bb670d2 to
21ec3ce
Compare
gricha
approved these changes
Nov 12, 2026
skaasten
added a commit
that referenced
this pull request
Nov 13, 2026
…kens (#130914) Allows service account access tokens to be authenticated ------- Stacked on #130679. Adds `ServiceAccountTokenAuthentication`, which accepts `sntrym_` bearer tokens. It's registered right after `OrgAuthTokenAuthentication`, both in the API's default authenticators and in the auth middleware (so rate limits key on the same identity the endpoint sees). How a token becomes a request credential: - It looks up the active token by hash: `ServiceAccountTokenReplica` in the cell, `ServiceAccountToken` in control. Revoked (`date_deactivated` set) and expired tokens return 401. - It resolves the account with `resolve_service_account`. That's the same helper the viewer-context path uses, so it applies the same checks: the account is active and in the token's org, and the org has `organizations:service-accounts`. We reuse that flag instead of adding a separate token flag, since service accounts aren't available outside the internal org yet. - It returns the same `service_account` `AuthenticatedToken` as viewer-context auth, with `entity_id` set to the **account** ID. Access, project reach, `service_account:<id>` rate limits and creator attribution work without changes. - The credential's scopes are the token's scopes intersected with the account's current scopes. Narrowing the account narrows its existing tokens, but widening the account doesn't widen them. - On URLs with `organization_id_or_slug`, a token for another org fails with a 401. Access would deny it anyway, but this gives a clear error and covers endpoints that never build access. Not in this PR: throttled `date_last_used` updates, which need a cell-to-control RPC, the management endpoints for creating and revoking tokens, and the token column in the service-account auth matrix. <!-- junior-request-attribution:start --> via @skaasten. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:slack%3AC0CA89MBDQB%3A1794249904.362549 --> -- [View Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CA89MBDQB%3A1794249904.362549) [[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CA89MBDQB%3A1794249904.362549/?project=4510944073809921) <!-- junior-session-footer:end --> --------- Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: Shaun Kaasten <[email protected]>
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.
A small refactor to make way for service account token authentication
Moves the account lookup and checks out of
authenticate_service_account(vc)intoresolve_service_account(service_account_id, organization_id).authenticate_service_accountis now a thin wrapper that only reads the actor and org off the viewer context.This sets up the service account token authenticator. After it looks up the token hash, it will call the same helper, so both ways of authenticating as a service account apply the same checks in one place: the account is active and in that org, the org has
organizations:service-accounts, and the account is read from the control replica or the cell replica depending on the silo.No behavior change. The existing viewer-context tests in
tests/sentry/api/test_authentication.pypass unchanged. New tests intests/sentry/service_accounts/test_auth.pycall the helper directly in both silos and cover a deleted account, another org, a missing feature, and parity with the viewer-context wrapper.via @skaasten.
--
View Session [Sentry]