Skip to content

ref(auth): Use shared viewercontext library - #128628

Merged
gricha merged 3 commits into
masterfrom
ref/use-viewercontext-lib
Oct 23, 2026
Merged

gricha merged 3 commits into
masterfrom
ref/use-viewercontext-lib

Conversation

@gricha

@gricha gricha commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Moves Sentry's ViewerContext model, contextvar and JWT encode/verify onto the shared sentry-viewercontext package (getsentry/viewercontext), so Sentry, Seer and other services share one implementation of the wire format.

sentry.viewer_context re-exports the library types, so call sites don't change. Sentry keeps signing-key lookup from settings, the early-adopter lookup, propagation metrics and its set_viewer_context_* updaters. The library's SuperuserAccess is a plain dataclass, so the Pydantic sentry.types.superuser.SuperuserAccess stays for RPC and agent tokens and is converted where it enters or leaves the ViewerContext (middleware, ViewerContextAuthentication, the agent token endpoint).

Stacked on the cleanup PR (#128112). Locks the published sentry-viewercontext 0.1.0 from the internal index (getsentry/pypi#2660).

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 22, 2026
@gricha
gricha force-pushed the ref/use-viewercontext-lib branch from 101736b to e8670f5 Compare October 23, 2026 03:02
@gricha
gricha marked this pull request as ready for review October 23, 2026 03:43
@gricha
gricha requested review from a team as code owners October 23, 2026 03:43
@gricha
gricha force-pushed the ref/viewer-context-v2-only branch from 9ad527e to 4bab5c3 Compare October 23, 2026 17:05
@gricha
gricha requested review from a team as code owners October 23, 2026 17:05
@gricha
gricha force-pushed the ref/use-viewercontext-lib branch from 2cc8472 to 9a37736 Compare October 23, 2026 17:05
@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

@gricha
gricha force-pushed the ref/viewer-context-v2-only branch from 4bab5c3 to 2809cd1 Compare October 23, 2026 17:29
@gricha
gricha force-pushed the ref/use-viewercontext-lib branch from 9a37736 to b80f052 Compare October 23, 2026 17:29
@gricha
gricha force-pushed the ref/viewer-context-v2-only branch from 2809cd1 to 73d741c Compare October 23, 2026 19:45
@gricha
gricha force-pushed the ref/use-viewercontext-lib branch from b80f052 to 911d876 Compare October 23, 2026 19:45

@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 911d876. Configure here.

return pyjwt.encode(
payload, secret, algorithm="HS256", headers={_JWT_KEY_ID_HEADER: _key_id(secret)}
)
viewer_context = dataclasses.replace(viewer_context, organization_is_early_adopter=True)

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.

Stale early-adopter flag on re-encode

Low Severity

encode_viewer_context now copies organization_is_early_adopter onto the ViewerContext before signing, and only sets that field when the org lookup is true. A decoded context that already carries True therefore keeps the claim on the next encode even if the org is no longer an early adopter. Previously the claim lived only on the JWT payload and was dropped on deserialize, so every encode rechecked the database.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 911d876. Configure here.

@gricha
gricha force-pushed the ref/viewer-context-v2-only branch from 73d741c to bf0be78 Compare October 23, 2026 20:06
@gricha
gricha force-pushed the ref/use-viewercontext-lib branch from 911d876 to 86549ba Compare October 23, 2026 20:06
Comment thread src/sentry/api/authentication.py Outdated
user_id = actor.actor_id if actor.type == ActorType.USER else actor.delegated_user_id
superuser = actor.superuser
superuser = (
SuperuserAccess(expires_at=actor.superuser.expires_at)

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.

apply DRY principles, this pattern is repeated in multiple places and likely best as a helper function of some sort

@gricha
gricha force-pushed the ref/viewer-context-v2-only branch from ac5581d to de207a3 Compare October 23, 2026 22:21
@gricha
gricha force-pushed the ref/use-viewercontext-lib branch from 12f4308 to fee7d0f Compare October 23, 2026 22:24
Base automatically changed from ref/viewer-context-v2-only to master October 23, 2026 22:55
gricha and others added 3 commits October 23, 2026 15:55
Models, contextvar and JWT encode/verify now come from the shared
sentry-viewercontext package. Sentry keeps key lookup, early-adopter
lookup, metrics and its set_viewer_context_* updaters.
Move the repeated conversion between the shared library's SuperuserAccess and
Sentry's Pydantic SuperuserAccess into from_/to_viewer_context_superuser.

Co-Authored-By: Jeremy Stanley <jeremy.stanley@sentry.io>
@gricha
gricha force-pushed the ref/use-viewercontext-lib branch from fee7d0f to 0a10c37 Compare October 23, 2026 23:06
@gricha
gricha merged commit 0cdea46 into master Oct 23, 2026
91 checks passed
@gricha
gricha deleted the ref/use-viewercontext-lib branch October 23, 2026 23:26
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