Skip to content

feat(auth): Add deprecated scope translation layer - #128663

Merged
DominikB2014 merged 6 commits into
masterfrom
dominikbuszowiecki/eng-8841-create-permission-scope-translation-layer
Oct 28, 2026
Merged

DominikB2014 merged 6 commits into
masterfrom
dominikbuszowiecki/eng-8841-create-permission-scope-translation-layer

Conversation

@DominikB2014

@DominikB2014 DominikB2014 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Adds a request-time translation from deprecated token scopes to the granular scopes replacing them. Existing tokens keep working as endpoints move to granular scopes. Stored token scopes, and what the token UI/API shows, are unchanged.

For now there is one mapping (DEPRECATED_SCOPE_TRANSLATIONS in conf/server.py): org:read → org:read + dashboard:read/create/update/delete. Each deprecated scope is replaced by its mapping. org:read maps to itself until it has a full 1:1 translation to granular scopes.

Where it applies

  • AuthenticatedToken.get_scopes() returns translated scopes, so the endpoint scope check, token→Access resolution and request.auth.has_scope() all see them.
  • AuthenticatedToken.get_stored_scopes() returns the stored scopes. It is used by the translation metric, the api.has_deprecated_scopes attribution metric and the /api/0/ index response.
  • Sentry app installations translate explicitly in access.py, as their access comes from the app's scope list rather than request.auth.
  • Raw token models passed as request.auth through the internal API client (e.g. the release webhook) are not translated.

Behavior today: no endpoint changes what it allows, since dashboard endpoints still accept org:read. User tokens are still capped by the member's role, so the dashboard scopes only take effect for orgs with organizations:granular-permission-scopes.

Metric: auth.deprecated_scope_translation.applied (tagged deprecated_scope, endpoint) is emitted from the permission check, at most once per request, when a translation adds scopes the token didn't already hold.

Option: the translation and its metric only run while auth.deprecated-scope-translation.enabled is on. It defaults to off, and a sentry-options-automator PR turns it on after this deploys. Turning it off is safe while endpoints still accept deprecated scopes. A follow-up will stop dashboard endpoints accepting org:read only when this option and organizations:granular-permission-scopes are both on. Before any endpoint stops accepting a deprecated scope regardless of the option, it has to be enabled everywhere and removed.

Refs ENG-8841

via @DominikB2014.

--

View Junior Session [Sentry]

@linear-code

linear-code Bot commented Oct 23, 2026

Copy link
Copy Markdown

ENG-8841

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 23, 2026
@sentry

sentry Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Sentry Snapshot Testing

Name Added Removed Changed Renamed Unchanged Skipped Status
sentry-frontend
sentry-frontend
0 0 0 0 760 0 ✅ Unchanged

⚙️ sentry-frontend Snapshot Settings

@DominikB2014

Copy link
Copy Markdown
Contributor Author

@cursor review

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 73dcb83. Configure here.

Translate deprecated token scopes into the granular scopes replacing them
at request time, so existing tokens keep working as endpoints move to
granular scopes. For now org:read translates to itself plus
dashboard:read/create/write/delete. Stored token scopes are unchanged.

The translation applies to the token scope check in ScopedPermission and
to the token scope cap in Access. User tokens are still capped by the
member's role, so dashboard scopes only take effect once the org has
granular role scopes. auth.deprecated_scope_translation.applied counts
requests where translation added scopes the token did not already hold.

Refs ENG-8841
Move the deprecated scope translation into AuthenticatedToken.get_scopes()
so every token scope check, including direct request.auth.has_scope()
calls, sees translated scopes. get_stored_scopes() returns the scopes
stored on the token for the translation metric, the
api.has_deprecated_scopes attribution metric and the API index response.

Sentry app installations still translate explicitly, as their access is
built from the app's scope list rather than request.auth.

Refs ENG-8841
@DominikB2014
DominikB2014 force-pushed the dominikbuszowiecki/eng-8841-create-permission-scope-translation-layer branch from d5fc12a to 552daa5 Compare October 23, 2026 18:01
@DominikB2014
DominikB2014 marked this pull request as ready for review October 26, 2026 14:23
@DominikB2014
DominikB2014 requested review from a team as code owners October 26, 2026 14:24

@edwardgou-sentry edwardgou-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.

I'm not as familiar with the permissions code area, but the changes and translations look reasonable to me.

sentry-junior Bot and others added 3 commits October 27, 2026 13:10
…eng-8841-create-permission-scope-translation-layer

Co-Authored-By: Dominik Buszowiecki <[email protected]>
dashboard:write was renamed to dashboard:update on master (#128801).
Update the org:read translation and tests to match.

Refs ENG-8841

Co-Authored-By: Dominik Buszowiecki <[email protected]>
GET /api/0/ should list the scopes stored on the token, not the granular scopes that deprecated scopes like org:read are translated to.

Refs ENG-8841
"""Count the deprecated scopes that added granular scopes for this caller."""
for deprecated_scope in get_deprecated_scope_translations(scopes):
metrics.incr(
"auth.deprecated_scope_translation.applied",

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.

Neat 👍

if request.auth:
auth = {"scopes": request.auth.get_scopes()}
get_stored_scopes = getattr(request.auth, "get_stored_scopes", request.auth.get_scopes)
auth = {"scopes": get_stored_scopes()}

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.

~ Curious why we wouldn't show the translated scopes here, since that's what would be used for permissions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was planning on showing the translated scopes once all the new scopes are in place

# A token's stored (untranslated) scopes; otherwise the access resolved for the
# request. Not every `request.auth` is a token: HMAC signature authentication sets
# it to a string, and the internal API client can pass a raw token model.
get_token_scopes = getattr(request.auth, "get_stored_scopes", None) or getattr(

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.

~ also couldn't we use the translated scopes here as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is when we are logging the stored scopes, we need the stored scopes here to track deprecated scope usage.

Add auth.deprecated-scope-translation.enabled, off by default, so the translation can be rolled out and turned off from sentry-options-automator. The translation metric is also skipped while the option is off. Turning it off is safe while endpoints still accept deprecated scopes.

Refs ENG-8841
@DominikB2014
DominikB2014 merged commit b36da6d into master Oct 28, 2026
91 checks passed
@DominikB2014
DominikB2014 deleted the dominikbuszowiecki/eng-8841-create-permission-scope-translation-layer branch October 28, 2026 15:23
tustanivsky pushed a commit to tustanivsky/sentry that referenced this pull request Oct 28, 2026
BYK added a commit that referenced this pull request Nov 9, 2026
Part of the #130351 proposal to publish endpoints that the Sentry CLI
(getsentry/toolkit) calls with raw requests today, so they appear in the
public OpenAPI schema and in the generated `@sentry/api` SDK.

This PR makes `GET /api/0/` (`IndexEndpoint`, operation
`retrieveApiIndex`) `PUBLIC` and documents it.

**Evidence**
- The endpoint has existed since 2015. #76975 marked it `PRIVATE` in
2024 because it "basically does nothing". Its response hasn't changed
since then, apart from #128663, which made it report stored scopes.
- Current caller: the CLI calls `GET /` after a 403 to read the current
token's scopes (`getCurrentAuthScopes`). That's why it needs to be in
the SDK.

**Ownership**
- The endpoint is assigned to `ApiOwner.FOUNDATIONS`. Foundations
already owns the other auth and token introspection endpoints, such as
`AuthIndexEndpoint` and `ApiAuthorizationsEndpoint`, and this endpoint
does the same kind of job: it reports who you are and what your
credentials allow. `ApiOwner` has no API-platform team to pick instead.
- `/api/0/` is removed from `api_ownership_allowlist_dont_modify.py`.
`api_ownership_stats_dont_modify.json` is gitignored, so it doesn't
change.

**Changes**
- New `ApiIndexResponse` type: `version`, `auth: {scopes} | null` and
`user: UserSerializerResponseSelf | null`. I checked each field against
the code. `user` is null for anonymous requests and for API keys and org
auth tokens, because neither belongs to a user. `auth` is null for
session and anonymous requests.
- The user type is reused. The endpoint calls `serialize(user, user)`,
so it always returns the requester variant. That variant's `options`
fields (`_UserOptions`) get descriptions, and `flags`, which was `Any`,
becomes a `_UserFlags` TypedDict. `UserSerializerResponseSelf` wasn't in
the public schema before, and `_UserOptions` only appears in private
endpoints, so no other public operation changes.
- Authentication is optional, so the operation declares `security:
[{auth_token: []}, {}]`, which lets SDK clients send a token without
requiring one. drf-spectacular passes the `auth` list through to
`security` unchanged, but its type annotation only allows scheme names.
That's why that one line has a `# type: ignore[list-item]`, with a
comment saying why.
- Adds the `UserExamples.RETRIEVE_API_INDEX` example and an apidocs test
that validates the schema for a user token, an org auth token, and an
anonymous request.

**Verification**
- I ran `make build-api-docs` on this branch and on master (ac2d476)
and diffed `openapi-derefed.json`. The only changes are the new `GET
/api/0/` operation and the `ApiIndexResponse` component.
- `python -m sentry.apidocs.response_coverage`: 0 new gaps.
- `pnpm run validate-api-examples`: no errors.
- New apidocs test, `test_index.py`, and the user serializer, user
details and display preferences tests: 108 passed.
- `pytest tests/apidocs tests/sentry/apidocs` (`-n4`): 324 passed, 1
failed. The failure was `test_group_integration_details.py::test_get`
(500), which passes when run alone on this branch and on master. It's
likely xdist flakiness unrelated to this change.
- `tests/sentry/seer/endpoints/test_organization_agent_token.py`: 325
passed, 23 xfailed.
- prek and mypy (pre-push stage) pass on the changed files. Plain mypy
on `src/sentry/users/api/` and the organization serializer, which use
the changed user types, also passes.

The "(Optional) Shows the difference between the prod and dev schema"
check is expected to fail because this PR changes the schema.

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A22c5e57fa755cece5122380dcb72ec8e -->

--

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

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

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[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.

3 participants