Repository navigation
feat(seer): Associate workflow configurations with service accounts - #128462
Conversation
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 #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** - #128239 (management endpoints) asserts on the cache-clearing receiver in its tests. Those assertions need to check the replica instead. - #128250 (viewer-context, stacked on #128239) reads through `service_account_service.get`. It needs to read `ServiceAccountReplica` instead, and only count project links whose project is in the account's org. - #128462 can point a `FlexibleForeignKey` (`on_delete=SET_NULL`) at `ServiceAccountReplica` and drop its tombstone handling and RPC check. **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. <!-- junior-request-attribution:start --> via **shaun.kaasten**. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:slack%3AC0CA89MBDQB%3A1792681955.861809 --> -- [View Junior Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CA89MBDQB%3A1792681955.861809) [[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CA89MBDQB%3A1792681955.861809/?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]>
Store an optional reference to the cell replica and validate assignment and resolution. Preserve configurations on account deletion and version explicit association changes.
9331b53 to
7223a88
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 7223a88. Configure here.
Store authoritative account IDs while keeping validation and resolution local.
|
This PR has a migration; here is the generated SQL for for --
-- Add field service_account_id to seerworkflowconfig
--
ALTER TABLE "seer_workflowconfig" ADD COLUMN "service_account_id" bigint NULL;
CREATE INDEX CONCURRENTLY "seer_workflowconfig_service_account_id_8eb189a0" ON "seer_workflowconfig" ("service_account_id"); |
There was a problem hiding this comment.
The model/migration and set_service_account/resolve_service_account look right, and switching the field to a HybridCloudForeignKey on the control ServiceAccount addresses the earlier backup concern (the export test and the remap asserts in the import test confirm the ID is preserved and remapped). The blocker is the new import test, SeerWorkflowServiceAccountTests::test_account_identity_remapped_before_replication, which fails in both DB modes on the head commit:
- monolith-dbs:
assert not ServiceAccountReplica.objects.filter(service_account_id=account.id).exists()fails because the import already replicates the account when control and cell share a DB. So the "before replication" state this test relies on only exists with split DBs. - split DBs (backend test 20):
ServiceAccountReplica...update(id=account.id)puts the account's control ID onto an unrelated replica. Whenaccount.save()re-replicates,handle_replicationinserts a new replica with the next sequence PK, which hits that ID, andupsert_replicated_service_accountraises anIntegrityErroronserviceaccountreplica_pkey(OutboxFlushError, SERVICE_ACCOUNT_UPDATE).
Both failures come from how the test is set up, not from the feature code. To fix it, drop or gate the pre-replication not exists() assert so it doesn't depend on the DB topology. For the "replica PK ≠ account ID" case, either rely on the IDs naturally differing (assert replica.id != account.id after replication) or move the unrelated replica to a PK the sequence won't hand out, rather than account.id.
| def set_project_selection(self, selection: ProjectSelection) -> None: | ||
| match selection: | ||
| case AllProjects(): | ||
| all_projects = True |
There was a problem hiding this comment.
~ I wonder how we'll manage this project list on the workflow, while service accounts also have their own list of project ids - do these need to be kept in sync?
There was a problem hiding this comment.
Yes, they will need to be kept in sync. The thought is that a single service account can be used to run many seer workflows. But each workflow may target different projects.
| migrations.AddField( | ||
| model_name="seerworkflowconfig", | ||
| name="service_account_id", | ||
| field=sentry.db.models.fields.hybrid_cloud_foreign_key.HybridCloudForeignKey( |
There was a problem hiding this comment.
~ I'm curious why we don't use the replicated model id here for a real foreign key, but this seems to be the pattern with other replicated models.
There was a problem hiding this comment.
Both approaches work. I don't remember why we used hybridcloud foreign key more than a traditional foreign key though.
There was a problem hiding this comment.
Replicated model ids do not work with the backup system as the replicated model are not backed up and have a different id from the actual model, so by using a HybridCloudForeignKey to the actual model, it maintains that functionality.
There was a problem hiding this comment.
Wonder if it's worth fixing that so we have the option in the future.
skaasten
left a comment
There was a problem hiding this comment.
Just some non-blocking questions for us to consider
Explicitly simulate delayed replication after import in both database topologies. Remove the replica-PK collision setup that can collide with the database sequence; workflow unit tests cover authoritative identity resolution.

Allow Seer workflow configurations to be associated with a service account. Add a nullable, indexed hybrid-cloud foreign key and schema migration that store the account's authoritative control-silo ID. Existing configurations remain unassigned.
Assignment and resolution validate an active account in the workflow's organization against the current cell replica. Assignment locks and reloads the configuration, preserves its latest settings, and increments its version only when the association changes; it also supports replacing or clearing the account. Resolution reads current replicated account details on every call and rejects unset, missing, inactive, or cross-organization accounts. A missing replica leaves the stored account identity intact so resolution can recover after replication.
The foreign key uses the existing cross-silo deletion mechanism to clear associations when an account is deleted. Regression coverage includes stale workflow wrappers, delayed replication and deletion, repeated deletion processing, failed-save rollback, and preserving workflow settings and project selection.
Update exhaustive backup fixtures to associate workflows with service accounts through shared factories. Add coverage that organization and global exports retain the control account ID and project bindings while excluding replicas; organization imports remap account, organization, and project IDs, preserve workflow configuration, and resolve the restored account after replication. Older backups without the new field import with an unset association. Backup tests explicitly control replication delivery to keep these cases deterministic.