Skip to content

feat(service-accounts): Generate service account tokens and scan for leaks - #130456

Merged
skaasten merged 2 commits into
masterfrom
feat/service-account-token-generation
Nov 10, 2026
Merged

skaasten merged 2 commits into
masterfrom
feat/service-account-token-generation

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

This is PR 2 of the service account access tokens plan. It adds the token format and leak detection. Nothing creates or authenticates these tokens yet.

  • AuthTokenType.SERVICE_ACCOUNT = "sntrym_". The m is for machine, because s is already used by org auth tokens. sentry.service_accounts.tokens generates and parses tokens in the org auth token layout (prefix + base64({iat, url, region_url, org}) + "_" + secret). To support that, orgauthtoken_token.generate_token and parse_token take an optional prefix, and the default leaves org tokens unchanged. stored_token_fields() returns only the SHA-256 hash and the last 4 characters. The plaintext isn't stored on any object, and the hash is repr=False.
  • UserAuthTokenAuthentication.accepts_auth skips sntrym_, the same way it skips sntrys_. Until a service account authenticator lands, these tokens authenticate as nothing.
  • When the GitHub secret-scanning webhook receives a sntrym_ token, it looks up the hash in active ServiceAccountToken rows (date_deactivated null) and emails the org owners. It only notifies and never revokes. Revoked or unknown tokens are labeled false_positive.

No migration: the ApiToken.token_type choices change doesn't produce one (makemigrations --check reports no changes).

Open items and deviations:

  • Registering sntrym_ with GitHub's secret scanning partner program is a manual step outside this PR.
  • Service account tokens have no management UI yet, so the revoke link in the leak email falls back to the org's root URL.

Checks run: prek on the changed files, mypy (SENTRY_MYPY_PRE_PUSH=1 … --stage pre-push), makemigrations --check, and pytest on tests/sentry/service_accounts/test_tokens.py, tests/sentry/utils/security/test_orgauthtoken_token.py, tests/sentry/api/endpoints/secret_scanning/test_github.py, tests/sentry/api/test_authentication.py and tests/sentry/service_accounts/test_models.py (167 passed).

via @skaasten.

--

View Session [Sentry]

@sentry-junior
sentry-junior Bot requested review from a team as code owners November 9, 2026 17:23
@sentry-junior
sentry-junior Bot requested review from a team as code owners November 9, 2026 17:23
@mchen-sentry mchen-sentry added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Nov 9, 2026
Comment thread src/sentry/types/token.py Resolved

@michelletran-sentry michelletran-sentry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM!

sentry-junior Bot and others added 2 commits November 10, 2026 09:55
…leaks

Add a sntrym_ token type that reuses the org auth token layout. A token
carries its org and region, and only its SHA-256 hash and last four
characters are stored.

User auth token authentication skips sntrym_ tokens. The GitHub
secret-scanning webhook notifies org owners when an active service
account token leaks.

Co-Authored-By: Shaun Kaasten <[email protected]>
@skaasten
skaasten force-pushed the feat/service-account-token-generation branch from 6f916c8 to bd255bc Compare November 10, 2026 14:55
@skaasten
skaasten merged commit 6a8b307 into master Nov 10, 2026
98 checks passed
@skaasten
skaasten deleted the feat/service-account-token-generation branch November 10, 2026 15:33
skaasten pushed a commit that referenced this pull request Nov 10, 2026
#130483)

Adds a new endpoint for listing service account tokens, gated by service
account feature flag

------

Adds `GET /organizations/{org}/service-accounts/{id}/tokens/`. This is
the first of the small token endpoint PRs, one per endpoint. It's
stacked on #130456, which adds token generation:

1. **List tokens (this PR)**
2. Create a token: #130507
3. Revoke a token: #130508

The endpoint runs in control, is private, and is gated on
`organizations:service-accounts` like the other service account
endpoints. It needs `service_account:read`.

It returns the account's active tokens, newest first: `id`, `name`,
`scopes`, `tokenLastCharacters`, `dateCreated`, `dateLastUsed`,
`dateExpires`. Revoked tokens aren't listed, and the response never
includes a token's value. Pagination works the same way as the service
account list. The account in the URL must be active and in the URL's
org, otherwise it returns 404.

Only a person signed in as themselves can call it. A service account
gets 403 `user-required` (`requires_user`).

The account lookup from the details endpoint moved into a shared
`ServiceAccountScopedEndpointBase`. The details endpoint behaves the
same.

The new URL is added to
`static/app/utils/api/knownSentryApiUrls.generated.ts`, so the frontend
PR (#130485) can type-check against it.

<!-- junior-request-attribution:start -->
via @skaasten.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CA89MBDQB%3A1793911581.772709 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CA89MBDQB%3A1793911581.772709)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CA89MBDQB%3A1793911581.772709/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
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 Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants