Skip to content

feat(auth): Replicate service accounts to the cell - #128524

Merged
skaasten merged 8 commits into
masterfrom
shaun/feat/service-account-cell-replica
Oct 23, 2026
Merged

skaasten merged 8 commits into
masterfrom
shaun/feat/service-account-cell-replica

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Switches service accounts from a cached control RPC (#128182) to a cell replica.

Cell code like Seer workflow configs can then read service accounts locally and use a real foreign key instead of a cross-silo HybridCloudForeignKey.

We're doing it now because there's nothing to backfill: the service account endpoints on master are still mocks, so no ServiceAccount rows exist yet. This only holds while this PR lands before the real endpoints (#128239).

What changes

  • New cell models ServiceAccountReplica and ServiceAccountProjectReplica (migration service_accounts/0002).
  • ServiceAccount is now a ReplicatedControlModel on the existing SERVICE_ACCOUNT_UPDATE outbox. Project link changes already write the account's outbox, so one handler (cell_replica_service.upsert_replicated_service_account) upserts the account and replaces its whole set of project links.
  • Links are only replicated for projects that are currently in the account's org. The handler computes that set once and uses it for both deleting and inserting links, so a link whose project was deleted (until the tombstone cascade reaches control) or transferred to another org is dropped.
  • Deleting an account removes the replica through the existing tombstone cascade: service_account_id is a HybridCloudForeignKey with CASCADE, and project links cascade from the replica.
  • Replaces the service account RPC service from feat(auth): Add cached service account RPC for cells #128182. The control method get_service_account, the cell cache wrapper (service_account_service.get), and its cache-clearing receiver are removed. Nothing has ever called them, so there's no cross-silo deploy skew to protect and no deprecation cycle is needed.

Project transfers

  • Project.transfer_to deletes the old org's replica links for the project in the same transaction, so access ends with the transfer.
  • It also writes a PROJECT_UPDATE cell outbox (Project.outbox_for_transfer), sharded on the org the project left, with the transfer time in its payload. Its receiver calls a new control RPC method, service_account_service.remove_project_links_after_transfer, which deletes ServiceAccountProject rows for the project from accounts outside its current org, and from the old org's accounts linked before the transfer. Without this, transferring the project back to the original org would let the next account update restore the old access. The second case covers a project that moves away and back before the outbox runs; repeat departures from one org coalesce, and the latest transfer time covers the earlier ones.
  • The outbox isn't flushed on commit (flush=False), so a failing drain can't abort the rest of transfer_to (releases, environments, monitors, workflows) after the org change commits. The cleanup runs on the async outbox drain.
  • Each control delete writes the account's outbox, so the replica stays in sync.
  • A replication that races a transfer can read the project in its old org and insert a link after the transfer's cleanup deletes the replica links. That link lasts until the transfer's outbox cleanup deletes the control link and the account replicates again. ServiceAccountProjectReplica documents that readers must check the project is in the account's org.

Expected check warnings

  • The optional "Shows the difference between the prod and dev schema" job flags service_account/get_service_account as removed without deprecation, and reports service_account/remove_project_links_after_transfer as added. Both are expected: the removed method had no callers on either silo.
  • The same job currently fails earlier on an OpenAPI diff in the replays endpoints, which this PR doesn't touch.

Affects open PRs

Follow-ups

  • Replication is eventually consistent. An account used right after it's created may not be in the cell yet.
  • The transfer cleanup compares a cell timestamp (transferred_at) with a control one (ServiceAccountProject.date_added). Clock skew between silos only matters for a link added to the old org within that skew of the project returning to it.

Tests

  • tests/sentry/service_accounts: ServiceAccountReplicationTest covers create, update, link add/remove, links to projects outside the org, idempotency, and delete through the tombstone cascade. For transfers it covers: the replica link is gone before the outbox runs and the control link after; a project transferred away and back doesn't regain the link; links from accounts in the new org are kept; a project transferred away and back before the outbox runs loses the old link but keeps one added after it returned; the transfer finishes when the control cleanup fails; and the handler drops an existing link whose project left the org. The transfer tests fail without the receiver change.
  • Also passing: tests/sentry/models/test_project.py, tests/sentry/core/endpoints/test_project_transfer.py, tests/sentry/receivers/outbox/test_cell.py, tests/sentry/hybridcloud/test_rpc.py, tests/sentry/hybridcloud/rpc, tests/sentry/hybridcloud/test_replica.py. Earlier runs of the backup, outbox, user model, silo, and deletions suites passed except tests that need Snuba.
  • sentry rpcschema output has service_account/remove_project_links_after_transfer and region_replica/upsert_replicated_service_account, and no get_service_account.
  • prek and mypy pass.

via shaun.kaasten.

--

View Junior Session [Sentry]

Replace the cached control RPC with a cell replica, written by the
SERVICE_ACCOUNT_UPDATE outbox, so cell code can read service accounts
locally and point real foreign keys at them. No service accounts exist
yet, so there is nothing to backfill.

Co-Authored-By: Shaun Kaasten <[email protected]>
@sentry-junior
sentry-junior Bot requested review from a team as code owners October 22, 2026 15:32
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 22, 2026
@linear-code

linear-code Bot commented Oct 22, 2026

Copy link
Copy Markdown

ENG-8829

Removing get_service_account in the same PR fails the RPC schema check.
Keep the control RPC method, marked deprecated, and drop only the cell
cache wrapper and its cache-clearing receiver.
@github-actions

Copy link
Copy Markdown
Contributor

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

for 0002_add_cell_replicas in service_accounts

--
-- Create model ServiceAccountReplica
--
CREATE TABLE "service_accounts_serviceaccountreplica" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "service_account_id" bigint NOT NULL UNIQUE, "name" varchar(64) NOT NULL, "scope_list" text[] NOT NULL, "is_active" boolean DEFAULT true NOT NULL, "organization_id" bigint NOT NULL);
--
-- Create model ServiceAccountProjectReplica
--
CREATE TABLE "service_accounts_serviceaccountprojectreplica" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "project_id" bigint NOT NULL, "service_account_id" bigint NOT NULL, CONSTRAINT "service_accounts_serviceaccountprojectreplica_unique" UNIQUE ("service_account_id", "project_id"));
ALTER TABLE "service_accounts_serviceaccountreplica" ADD CONSTRAINT "service_accounts_ser_organization_id_10f92ccf_fk_sentry_or" FOREIGN KEY ("organization_id") REFERENCES "sentry_organization" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "service_accounts_serviceaccountreplica" VALIDATE CONSTRAINT "service_accounts_ser_organization_id_10f92ccf_fk_sentry_or";
CREATE INDEX CONCURRENTLY "service_accounts_serviceaccountreplica_organization_id_10f92ccf" ON "service_accounts_serviceaccountreplica" ("organization_id");
ALTER TABLE "service_accounts_serviceaccountprojectreplica" ADD CONSTRAINT "service_accounts_ser_project_id_d7971e90_fk_sentry_pr" FOREIGN KEY ("project_id") REFERENCES "sentry_project" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "service_accounts_serviceaccountprojectreplica" VALIDATE CONSTRAINT "service_accounts_ser_project_id_d7971e90_fk_sentry_pr";
ALTER TABLE "service_accounts_serviceaccountprojectreplica" ADD CONSTRAINT "service_accounts_ser_service_account_id_7f375b3e_fk_service_a" FOREIGN KEY ("service_account_id") REFERENCES "service_accounts_serviceaccountreplica" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "service_accounts_serviceaccountprojectreplica" VALIDATE CONSTRAINT "service_accounts_ser_service_account_id_7f375b3e_fk_service_a";
CREATE INDEX CONCURRENTLY "service_accounts_serviceac_project_id_d7971e90" ON "service_accounts_serviceaccountprojectreplica" ("project_id");
CREATE INDEX CONCURRENTLY "service_accounts_serviceac_service_account_id_7f375b3e" ON "service_accounts_serviceaccountprojectreplica" ("service_account_id");

@skaasten
skaasten marked this pull request as draft October 22, 2026 16:06
sentry-junior Bot and others added 3 commits October 22, 2026 16:27
Reverts 8e3466b. get_service_account has had no callers since it
landed, so there is no cross-silo deploy skew to protect against and
it does not need a deprecation cycle. The RPC schema diff check will
report the removed path; that is expected for this change.

Co-Authored-By: Shaun Kaasten <[email protected]>
The replica handler only filtered by organization when inserting links,
so a link to a project that moved to another org stayed in the replica.
Filter the account's project IDs by org once and use that set for both
the delete and the insert.

Project.transfer_to also deletes the old org's replica links right away,
since the control link survives the transfer and nothing updates the
account until its next change.

Co-Authored-By: Shaun Kaasten <[email protected]>
A control ServiceAccountProject row survived a transfer of its project
to another org. The replica ignored it while the project was away, but
transferring the project back made the next account update restore the
old access.

Project.transfer_to now writes the project's PROJECT_UPDATE outbox, and
its receiver calls a new control RPC method that deletes links from
service accounts outside the project's current org. Each delete writes
the account's outbox, so its replica updates as well.

Co-Authored-By: Shaun Kaasten <[email protected]>
@skaasten
skaasten marked this pull request as ready for review October 22, 2026 17:16
@skaasten
skaasten requested review from a team as code owners October 22, 2026 17:16

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

Stale Bugbot comment from a previous run.

Comment thread src/sentry/models/project.py Resolved
@skaasten
skaasten requested review from gricha and markstory October 22, 2026 17:23

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

Looks good to me.

The cell replicates an account's projects along with the account, so adding or
removing a link writes the account's outbox, not one of its own. That includes the
tombstone cascade, which deletes rows one instance at a time. The manager's bulk
methods write outboxes too; queryset `update()` and `delete()` don't, so avoid them.

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.

👍 There is some CI safeguards to help catch and prevent queryset deletes on models that are using outboxes.

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 - 👍

Comment on lines +166 to +168
service_account_id = HybridCloudForeignKey(
"service_accounts.ServiceAccount", null=False, on_delete="CASCADE", unique=True
)

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.

Tombstone processing can have a bit more lag than regular outboxes do. If you want consistency faster, you can implement handle_async_deletion and do an RPC call.

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.

Okay - Something for us to watch if it becomes an issue.

@skaasten skaasten closed this Oct 22, 2026
@skaasten skaasten reopened this Oct 22, 2026

@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 2 potential issues.

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 8f932f3. Configure here.

Comment thread src/sentry/models/project.py Resolved
Comment thread src/sentry/receivers/outbox/cell.py Resolved
Don't flush the transfer's PROJECT_UPDATE outbox on commit. A failed
synchronous drain would abort the rest of transfer_to after the org
change had already committed.

Shard the outbox on the org the project left and record when it left.
The control cleanup then also deletes that org's links created before
the transfer, so a project transferred away and back before the outbox
runs doesn't keep its old service account links.

Co-Authored-By: Shaun Kaasten <[email protected]>
@skaasten
skaasten requested a review from markstory October 22, 2026 19:38
@skaasten

Copy link
Copy Markdown
Contributor

@markstory Added some new commits to deal with projects that transfer to another org 🙏

Replication read a project's org without a lock, so a concurrent
transfer could delete the replica links and commit before replication
inserted a link to the transferred project. Lock the project rows with
FOR NO KEY UPDATE so either the transfer waits and deletes the new
links, or replication waits and sees the project in its new org.

Co-Authored-By: shaun.kaasten <[email protected]>
@skaasten
skaasten marked this pull request as draft October 22, 2026 19:56
…ation

Locking project rows during replication serialized it with transfers,
but it blocked project updates for every replication. A stale link
from a replication that races a transfer is removed when the
transfer's outbox cleanup replicates, so readers check the project's
org instead.

Co-Authored-By: shaun.kaasten <[email protected]>
@skaasten
skaasten marked this pull request as ready for review October 22, 2026 20:04
~Q(service_account__organization_id=organization_id)
| Q(
service_account__organization_id=previous_organization_id,
date_added__lte=transferred_at,

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.

this was needed to ensure it hasn't been transferred back later.

@skaasten
skaasten merged commit 4991eed into master Oct 23, 2026
91 of 92 checks passed
@skaasten
skaasten deleted the shaun/feat/service-account-cell-replica branch October 23, 2026 16:31
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