Skip to content

ref(auth): Read actor contexts through one ViewerContext (reader) - #128080

Merged
gricha merged 2 commits into
masterfrom
feat/viewer-context-actor-poc
Oct 22, 2026
Merged

gricha merged 2 commits into
masterfrom
feat/viewer-context-actor-poc

Conversation

@gricha

@gricha gricha commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

1/7 — READER: Sentry. Deploy first; current GetSentry keeps working.

Use one frozen, actor-backed ViewerContext. Actor owns identity, delegation, and elevation. actor.id is an ActorId(type, id) named tuple; actor.actor_id is the scalar ID. Construction sites migrate mechanically without changing endpoint authorization policy.

Read V1 and V2 into this model, while continuing to write V1. Temporarily preserve the human user_id accessor and the constructor/token replacement API used by current GetSentry. Legacy token input is discarded. The bridge is removed together in Sentry cleanup #128112, after the GetSentry migration deploys.

Fully deploy across web, workers, consumers, and all silos, then deploy 2/7: Seer reader #8941. DB models and service-account issuance are separate work; new actor producers wait until both writer PRs deploy.

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 19, 2026
@gricha gricha changed the title feat(auth): Prototype service-account actors in ViewerContext feat(auth): Make Actor the identity in ViewerContext Oct 19, 2026
@gricha gricha changed the title feat(auth): Make Actor the identity in ViewerContext feat(auth): Add V2 ViewerContext compatibility readers Oct 19, 2026
Comment thread src/sentry/taskworker/adapters.py Resolved
@gricha
gricha force-pushed the feat/viewer-context-actor-poc branch from f79a656 to 5a5516b Compare October 20, 2026 19:48
@gricha gricha changed the title feat(auth): Add V2 ViewerContext compatibility readers feat(auth): Read actor contexts through one ViewerContext Oct 20, 2026
@gricha
gricha force-pushed the feat/viewer-context-actor-poc branch from ebf233b to 4a436fe Compare October 20, 2026 21:45
@gricha gricha changed the title feat(auth): Read actor contexts through one ViewerContext feat(auth): Read actor contexts through one ViewerContext (reader) Oct 20, 2026
@gricha
gricha force-pushed the feat/viewer-context-actor-poc branch 3 times, most recently from 0f1210f to 43fe642 Compare October 20, 2026 23:56
@gricha
gricha marked this pull request as ready for review October 21, 2026 18:44
@gricha
gricha requested review from a team as code owners October 21, 2026 18:44
@gricha
gricha requested review from a team as code owners October 21, 2026 18:44
@gricha
gricha force-pushed the feat/viewer-context-actor-poc branch from 43fe642 to 0eadcfc Compare October 21, 2026 19:01
…lers

Keep one actor-backed ViewerContext, read both wire formats, and continue emitting V1 until reader rollout. Temporarily accept the constructor arguments and token replacement used by current GetSentry; remove that bridge after its caller migration deploys.

Co-Authored-By: Codex <[email protected]>
@gricha
gricha force-pushed the feat/viewer-context-actor-poc branch from 0eadcfc to bf9263d Compare October 21, 2026 23:57
@gricha gricha changed the title feat(auth): Read actor contexts through one ViewerContext (reader) ref(auth): Read actor contexts through one ViewerContext (reader) Oct 22, 2026

@sehr-m sehr-m left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems straightforward :)

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

First pass. The mechanical migration looks right to me, and I checked the GetSentry bridge against the current callers: ViewerContext(actor_type=...), ViewerContext(user_id=..., actor_type=USER), replace(ctx, ..., token=...), and reading ctx.token all still work, and getsentry / backend is green. Nothing in src/ still reads ViewerContext.token. Two things to look at before this lands:

  • _resolve_viewer_context in signed_seer_api.py: AGENT contexts already exist in prod (agent tokens, and now JWT agent callbacks through the new middleware branch). For those contexts, a legacy SeerViewerContext mismatch used to log, merge, and strip elevation. Now it raises ValueError. That raise happens before the encode_viewer_context try in make_signed_seer_api_request, so the outbound Seer call fails instead of degrading, which goes beyond a mechanical change for an existing actor type. No test covers this branch. In the normal path, the org binding in bind_organization_context should keep the orgs aligned, so I expect this to be rare. Still, I'd either keep log + metric + strip elevation for AGENT in this reader PR and save the hard failure for later in the stack, or add a test and some evidence (for example, seer.viewer_context_mismatch volume for agent contexts) that the raise can't fire today.
  • Re-emitting actors that V1 can't represent: after a V2 SERVICE_ACCOUNT (or AGENT with an actor_id) context has been read, the three V1 writers behave differently. ViewerContextHook.on_dispatch and the hybrid-cloud RPC meta["viewer_context"] = vc.serialize() raise, so task enqueue and cross-silo calls fail. make_signed_seer_api_request catches the error as viewer_context_jwt.no_signing_key and sends the request with no header. The rollout order in the description guards against this, but if that order slips, the first symptom will be crashes plus a log line that points at the wrong cause. A dedicated log/metric for "V1 cannot represent actor" in the Seer path would make a violation easy to spot. Making the other two paths consistent would also help.

The rest (actor-backed user_id, V2 rejecting V1 identity keys, ValueError added to the task-hook catch, and the JWT-wins branch for viewer-context-authenticated requests) looks good to me.

Comment thread src/sentry/middleware/viewer_context.py Outdated
jwt_ctx = _viewer_context_from_jwt_header(request)

if jwt_ctx is not None and request_ctx.user_id is not None:
if jwt_ctx is not None and getattr(request, "user_from_viewer_context", False):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"user_from_viewer_context" might be better as a const somewhere?

Agent contexts already exist in prod; a legacy SeerViewerContext mismatch
now logs, merges and strips elevation as on master, keeping the agent
actor. The middleware reuses is_user_from_viewer_context.

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

Reviewed by Cursor Bugbot for commit 9e02fb8. Configure here.

if actor.type == ActorType.USER:
actor = replace(actor, actor_id=explicit_user_id)
elif actor.type == ActorType.AGENT:
actor = replace(actor, delegated_user_id=explicit_user_id)

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.

Service-account identity rewritten to user

Medium Severity

Removing the typed-actor guard lets a legacy SeerViewerContext user_id replace a SERVICE_ACCOUNT with a USER actor. SERVICE_ACCOUNT has no delegated_user_id, so no mismatch is recorded and the service-account actor_id is discarded. Outbound Seer calls would then carry the wrong caller identity.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9e02fb8. Configure here.

@gricha
gricha merged commit e2a44c5 into master Oct 22, 2026
89 checks passed
@gricha
gricha deleted the feat/viewer-context-actor-poc branch October 22, 2026 23:17
marius-mbravio added a commit to marius-mbravio/sentry that referenced this pull request Oct 23, 2026
…r-local-caches

Resolves the import conflict in tests/sentry/taskworker/test_adapters.py
with getsentry#128080.
skaasten added a commit that referenced this pull request Oct 23, 2026
…128250)

Step 5 of the service accounts database design. `POST
.../service-accounts/:id/viewer-context/` now reads the account from the
cell's `ServiceAccountReplica` instead of returning canned scopes.

- It returns 404 when the account doesn't exist, is in another org, or
is inactive. The replica query filters on org and `is_active`.
- It returns the account's own scopes.
- Changes reach the next request once the account's outbox replicates to
the cell. A deleted account fails once the tombstone cascade removes its
replica. The tests check narrowing scopes, disabling the account and
deleting it.

The returned ViewerContext is still Seer's org-only one. Issuing a
`SERVICE_ACCOUNT` actor (the type landed in #128080) comes in the next
step.

Builds on #128239.

Refs ENG-8829

<!-- junior-request-attribution:start -->
via **shaun.kaasten**.
<!-- junior-request-attribution:end -->

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

--

[View Junior
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CA89MBDQB%3A1792779329.027059)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CA89MBDQB%3A1792779329.027059/?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]>
gricha added a commit that referenced this pull request Oct 23, 2026
**4/7 — WRITER: Sentry. Deploy after [GetSentry migration
#22613](getsentry/getsentry#22613

Change the serialization default to the V2 actor envelope. Keep
dual-format reads, explicit V1 serialization, and the temporary Sentry
compatibility bridge. The production diff changes one default.

Stacked on [Sentry reader
#128080](#128080). Both that
reader and [Seer reader
#8941](getsentry/seer#8941) must already be
fully deployed across web, workers, consumers, and all silos.

Fully deploy, then deploy [5/7: Seer writer
#8988](getsentry/seer#8988). Enable newly
identified actors after both writers deploy. Roll back writers while
retaining dual readers; queued or persisted V2 work still needs those
readers.

Co-authored-by: Codex <[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