Skip to content

ref(service-accounts): Split resolve_service_account out of viewer-context auth - #130679

Merged
skaasten merged 1 commit into
masterfrom
ref/resolve-service-account
Nov 12, 2026
Merged

skaasten merged 1 commit into
masterfrom
ref/resolve-service-account

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

A small refactor to make way for service account token authentication


Moves the account lookup and checks out of authenticate_service_account(vc) into resolve_service_account(service_account_id, organization_id). authenticate_service_account is 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.py pass unchanged. New tests in tests/sentry/service_accounts/test_auth.py call 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]

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 10, 2026
@sentry-junior
sentry-junior Bot added this pull request to stack #130915 November 12, 2026 14:38
…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
sentry-junior Bot force-pushed the ref/resolve-service-account branch from bb670d2 to 21ec3ce Compare November 12, 2026 14:43
@skaasten
skaasten requested a review from gricha November 12, 2026 15:07
@skaasten
skaasten merged commit 5943789 into master Nov 12, 2026
98 checks passed
@skaasten
skaasten deleted the ref/resolve-service-account branch November 12, 2026 17:28
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants