Skip to content

feat(auth): Add ServiceAccount and ServiceAccountProject models - #128049

Merged
skaasten merged 5 commits into
masterfrom
feat/add-service-account-models
Oct 21, 2026
Merged

skaasten merged 5 commits into
masterfrom
feat/add-service-account-models

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

This adds a new sentry.service_accounts Django app with the two control-silo tables that will back the mocked service account APIs from #127588. Nothing reads or writes them yet. The cell lookup and real endpoints come in follow-up PRs, and #128257 moves the mocked endpoints into the same app.

  • ServiceAccount holds the org, the name (unique per org, at most 64 characters), scope_list, is_active and created_by. Scopes are validated against SENTRY_SCOPES minus the OIDC scopes.
  • ServiceAccountProject holds the projectIds the API accepts. project_id is a HybridCloudForeignKey, so the tombstone cascade removes a link after its project is deleted.

The app gets its own migration history (service_accounts/0001_initial) and app-prefixed tables (service_accounts_serviceaccount, service_accounts_serviceaccountproject), so it no longer competes for sentry migration numbers.

Cells won't keep a replica table. They'll read accounts through a cached RPC to control, the same way they read users, and a SERVICE_ACCOUNT_UPDATE outbox (organization scope) will clear that cache. Both models are ControlOutboxProducingModels and write that outbox for the account: ServiceAccount on every change, and ServiceAccountProject when links are added or removed. That includes the manager's bulk methods and the per-instance tombstone cascade. Nothing consumes the outbox yet; cache clearing lands with the RPC.

Review notes:

  • Both models use RelocationScope.Organization. They're in the exhaustive backup fixtures, whose round-trip tests also check that project_id is remapped to the imported project instead of being copied.
  • User.merge_to now moves created_by. The user-merge coverage test requires this for every org-scoped model that references User.
  • The migration only creates new tables.

Refs ENG-8829

via shaun.kaasten.

--

View Junior Session [Sentry]

@linear-code

linear-code Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

ENG-8829

AIML-3754

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 19, 2026
@sentry-junior
sentry-junior Bot force-pushed the feat/add-service-account-models branch from 02a0362 to 744982c Compare October 19, 2026 17:18
@sentry

sentry Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Sentry Snapshot Testing

Name Added Removed Changed Renamed Unchanged Skipped Status
sentry-frontend
sentry-frontend
- - - - - - ⏳ Processing

⚙️ sentry-frontend Snapshot Settings

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL for src/sentry/service_accounts/migrations/0001_initial.py

for 0001_initial in service_accounts

--
-- Create model ServiceAccount
--
CREATE TABLE "service_accounts_serviceaccount" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "organization_id" bigint NOT NULL, "name" varchar(64) NOT NULL, "scope_list" text[] NOT NULL, "is_active" boolean DEFAULT true NOT NULL, "date_added" timestamp with time zone NOT NULL, "date_updated" timestamp with time zone NOT NULL, "created_by_id" bigint NULL);
--
-- Create model ServiceAccountProject
--
CREATE TABLE "service_accounts_serviceaccountproject" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "date_updated" timestamp with time zone NOT NULL, "date_added" timestamp with time zone NOT NULL, "project_id" bigint NOT NULL, "service_account_id" bigint NOT NULL);
--
-- Create constraint service_accounts_serviceaccount_org_name_unique on model serviceaccount
--
CREATE UNIQUE INDEX CONCURRENTLY "service_accounts_serviceaccount_org_name_unique" ON "service_accounts_serviceaccount" ("organization_id", "name");
ALTER TABLE "service_accounts_serviceaccount" ADD CONSTRAINT "service_accounts_serviceaccount_org_name_unique" UNIQUE USING INDEX "service_accounts_serviceaccount_org_name_unique";
--
-- Create constraint service_accounts_serviceaccountproject_unique on model serviceaccountproject
--
CREATE UNIQUE INDEX CONCURRENTLY "service_accounts_serviceaccountproject_unique" ON "service_accounts_serviceaccountproject" ("service_account_id", "project_id");
ALTER TABLE "service_accounts_serviceaccountproject" ADD CONSTRAINT "service_accounts_serviceaccountproject_unique" UNIQUE USING INDEX "service_accounts_serviceaccountproject_unique";
ALTER TABLE "service_accounts_serviceaccount" ADD CONSTRAINT "service_accounts_ser_created_by_id_07f80396_fk_auth_user" FOREIGN KEY ("created_by_id") REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "service_accounts_serviceaccount" VALIDATE CONSTRAINT "service_accounts_ser_created_by_id_07f80396_fk_auth_user";
CREATE INDEX CONCURRENTLY "service_accounts_serviceaccount_organization_id_c630fd27" ON "service_accounts_serviceaccount" ("organization_id");
CREATE INDEX CONCURRENTLY "service_accounts_serviceaccount_created_by_id_07f80396" ON "service_accounts_serviceaccount" ("created_by_id");
ALTER TABLE "service_accounts_serviceaccountproject" ADD CONSTRAINT "service_accounts_ser_service_account_id_d06c4b81_fk_service_a" FOREIGN KEY ("service_account_id") REFERENCES "service_accounts_serviceaccount" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "service_accounts_serviceaccountproject" VALIDATE CONSTRAINT "service_accounts_ser_service_account_id_d06c4b81_fk_service_a";
CREATE INDEX CONCURRENTLY "service_accounts_serviceaccountproject_project_id_db371335" ON "service_accounts_serviceaccountproject" ("project_id");
CREATE INDEX CONCURRENTLY "service_accounts_serviceac_service_account_id_d06c4b81" ON "service_accounts_serviceaccountproject" ("service_account_id");

@sentry-junior
sentry-junior Bot force-pushed the feat/add-service-account-models branch from 7dfb391 to 1ca261d Compare October 19, 2026 17:37
@skaasten
skaasten marked this pull request as ready for review October 19, 2026 17:48
@skaasten
skaasten requested review from a team as code owners October 19, 2026 17:48

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

Migration lgtm, I'll leave it to your team for final approval

@skaasten
skaasten requested a review from gricha October 20, 2026 14:47
@sentry-junior
sentry-junior Bot force-pushed the feat/add-service-account-models branch from 1ca261d to d736278 Compare October 20, 2026 14:57
@skaasten
skaasten added this pull request to stack #128203 October 20, 2026 15:31
@skaasten
skaasten force-pushed the feat/add-service-account-models branch from d736278 to d79e54d Compare October 20, 2026 15:41
Comment thread src/sentry/models/serviceaccount.py Resolved
Comment thread src/sentry/models/serviceaccount.py Resolved
Comment thread src/sentry/service_accounts/models.py Resolved
Comment thread src/sentry/service_accounts/models.py Resolved
sentry-junior Bot and others added 5 commits October 20, 2026 17:31
Add the control-silo tables that will back the mocked service account APIs.
ServiceAccount stores the org, name, scopes, active flag and creator.
ServiceAccountProject stores the project IDs the API accepts, with a hybrid
cloud foreign key so links are removed when a project is deleted.

Cells won't keep a replica. They'll read accounts through a cached RPC, the
same way they read users, so ServiceAccount is a ReplicatedControlModel with
a new SERVICE_ACCOUNT_UPDATE outbox category that will clear that cache. Both
models are relocatable with the organization, and user merges move
created_by.

Refs ENG-8829

Co-Authored-By: shaun.kaasten <[email protected]>

Co-Authored-By: shaun.kaasten <[email protected]>

Co-Authored-By: shaun.kaasten <[email protected]>
The outbox backfill reads outbox_replication.<table>.replication_version for every
ReplicatedControlModel, so ServiceAccount needs its option registered like the
other replicated models.

Refs ENG-8829

Co-Authored-By: shaun.kaasten <[email protected]>
The exhaustive backup round-trip already checks that ServiceAccountProject.project_id
is remapped on import, since hybrid cloud foreign keys get a ForeignKeyComparator.

Refs ENG-8829

Co-Authored-By: shaun.kaasten <[email protected]>
ServiceAccount doesn't replicate state to cells, so it now uses
ControlOutboxProducingModel with an explicit outbox instead of
ReplicatedControlModel. ServiceAccountProject writes the same account
outbox when links are added or removed, including the per-instance
tombstone cascade after a project is deleted, so callers no longer have
to remember to save the account to clear the cell's cached copy.

Refs ENG-8829

Co-Authored-By: shaun.kaasten <[email protected]>
… app

Following review, service accounts get their own Django app so models,
endpoints and serializers can live together with their own migration
history. The tables haven't shipped, so this replaces sentry migration
1231 with service_accounts 0001_initial and uses app-prefixed table and
constraint names. The outbox replication options follow the new table
names.

Refs ENG-8829

Co-Authored-By: shaun.kaasten <[email protected]>
@skaasten
skaasten requested a review from markstory October 21, 2026 14:14
@skaasten
skaasten merged commit 9d9d571 into master Oct 21, 2026
91 checks passed
@skaasten
skaasten deleted the feat/add-service-account-models branch October 21, 2026 16:50
skaasten added a commit that referenced this pull request Oct 21, 2026
Step 3 of the service accounts database design: the cell reads service
accounts through a cached RPC to control, the same way
`ViewerContextAuthentication` reads users through
`user_service.get_user()`. There is no cell replica table.

- `service_account_service.get_service_account` (control) returns
`RpcServiceAccount` with `scope_list`, `is_active` and the account's
`ServiceAccountProject` IDs.
- `service_account_service.get(service_account_id=...)` reads through
the cell cache (`@back_with_silo_cache`) and is what callers should use.
It doesn't check the org or `is_active`; callers (viewer-context and
auth, in later PRs) check both against the request.
- A `SERVICE_ACCOUNT_UPDATE` receiver in `receivers/outbox/control.py`
clears the cached entry in the org's cell. `ServiceAccount` and
`ServiceAccountProject` both write that outbox on save and delete, so
changes to scopes, `is_active` and project links reach the next cell
request without saving the account.
- The service lives in `sentry.service_accounts.services`, next to the
models, and that package is registered for RPC discovery.

Stacked on #128049. Nothing calls the new service yet.

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

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

--

[View Junior
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CA89MBDQB%3A1792502266.308789)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CA89MBDQB%3A1792502266.308789/?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]>
sentry-junior Bot added a commit that referenced this pull request Oct 23, 2026
Service accounts are getting their own Django app (#128049), so the
mocked management and viewer-context endpoints and their tests move
there too. Only module paths change: routes, URL names, silo routing
and responses are untouched.

Refs ENG-8829

Co-Authored-By: shaun.kaasten <[email protected]>
sentry-junior Bot added a commit that referenced this pull request Oct 23, 2026
Service accounts are getting their own Django app (#128049), so the
mocked management and viewer-context endpoints and their tests move
there too. Only module paths change: routes, URL names, silo routing
and responses are untouched.

Refs ENG-8829

Co-Authored-By: shaun.kaasten <[email protected]>

Co-Authored-By: Shaun Kaasten <[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.

4 participants