Repository navigation
feat(auth): Add deprecated scope translation layer - #128663
DominikB2014 merged 6 commits into
Conversation
Sentry Snapshot Testing
|
|
@cursor review |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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
d5fc12a to
552daa5
Compare
edwardgou-sentry
left a comment
There was a problem hiding this comment.
I'm not as familiar with the permissions code area, but the changes and translations look reasonable to me.
…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", |
| 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()} |
There was a problem hiding this comment.
~ Curious why we wouldn't show the translated scopes here, since that's what would be used for permissions
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
~ also couldn't we use the translated scopes here as well?
There was a problem hiding this comment.
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
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]>

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_TRANSLATIONSinconf/server.py):org:read→org:read+dashboard:read/create/update/delete. Each deprecated scope is replaced by its mapping.org:readmaps 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→Accessresolution andrequest.auth.has_scope()all see them.AuthenticatedToken.get_stored_scopes()returns the stored scopes. It is used by the translation metric, theapi.has_deprecated_scopesattribution metric and the/api/0/index response.access.py, as their access comes from the app's scope list rather thanrequest.auth.request.auththrough 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 withorganizations:granular-permission-scopes.Metric:
auth.deprecated_scope_translation.applied(taggeddeprecated_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.enabledis 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 acceptingorg:readonly when this option andorganizations:granular-permission-scopesare 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]