Repository navigation
feat(auth): Replicate service accounts to the cell - #128524
Conversation
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]>
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.
|
This PR has a migration; here is the generated SQL for for --
-- 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"); |
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]>
| 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. |
There was a problem hiding this comment.
👍 There is some CI safeguards to help catch and prevent queryset deletes on models that are using outboxes.
| service_account_id = HybridCloudForeignKey( | ||
| "service_accounts.ServiceAccount", null=False, on_delete="CASCADE", unique=True | ||
| ) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Okay - Something for us to watch if it becomes an issue.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ 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.
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]>
|
@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]>
…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]>
| ~Q(service_account__organization_id=organization_id) | ||
| | Q( | ||
| service_account__organization_id=previous_organization_id, | ||
| date_added__lte=transferred_at, |
There was a problem hiding this comment.
this was needed to ensure it hasn't been transferred back later.

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
ServiceAccountrows exist yet. This only holds while this PR lands before the real endpoints (#128239).What changes
ServiceAccountReplicaandServiceAccountProjectReplica(migrationservice_accounts/0002).ServiceAccountis now aReplicatedControlModelon the existingSERVICE_ACCOUNT_UPDATEoutbox. 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.service_account_idis aHybridCloudForeignKeywithCASCADE, and project links cascade from the replica.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_todeletes the old org's replica links for the project in the same transaction, so access ends with the transfer.PROJECT_UPDATEcell 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 deletesServiceAccountProjectrows 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.flush=False), so a failing drain can't abort the rest oftransfer_to(releases, environments, monitors, workflows) after the org change commits. The cleanup runs on the async outbox drain.ServiceAccountProjectReplicadocuments that readers must check the project is in the account's org.Expected check warnings
service_account/get_service_accountas removed without deprecation, and reportsservice_account/remove_project_links_after_transferas added. Both are expected: the removed method had no callers on either silo.Affects open PRs
service_account_service.get. It needs to readServiceAccountReplicainstead, and only count project links whose project is in the account's org.FlexibleForeignKey(on_delete=SET_NULL) atServiceAccountReplicaand drop its tombstone handling and RPC check.Follow-ups
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:ServiceAccountReplicationTestcovers 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.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 rpcschemaoutput hasservice_account/remove_project_links_after_transferandregion_replica/upsert_replicated_service_account, and noget_service_account.prekand mypy pass.via shaun.kaasten.
--
View Junior Session [Sentry]