Skip to content

feat(service-accounts): Replicate service account tokens through their outbox - #130265

Merged
skaasten merged 1 commit into
masterfrom
feat/service-account-token-replication-caller
Nov 9, 2026
Merged

skaasten merged 1 commit into
masterfrom
feat/service-account-token-replication-caller

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Turns on replication for service account tokens. ServiceAccountToken.handle_async_replication now calls cell_replica_service.upsert_replicated_service_account_token, so creating, updating or revoking a token writes its ServiceAccountTokenReplica in the org's cell.

This is split from #130108 because #130108 adds that RPC method. If control calls a cell RPC method in the same deploy that adds it, control can call a cell that doesn't serve the method yet. Merge and deploy this only after #130108 is deployed everywhere.

Until this lands, token outboxes are still written but drain without doing anything, so any token saved in that window has no cell replica. After this deploys, raise outbox_replication.service_accounts_serviceaccounttoken.replication_version in the options automator. That backfills the replicas for those tokens.

Tests: new ServiceAccountTokenOutboxReplicationTest, which saves tokens through the outbox:

  • creating a token writes its replica
  • revoking it updates the replica's date_deactivated
  • a missing account replica is skipped without leaving the outbox stuck

The service account, hybridcloud, backup and silo test suites pass (760 tests), and prek and mypy pass.

via shaun.kaasten.

--

View Session [Sentry]

@sentry-junior
sentry-junior Bot added this pull request to stack #130109 November 6, 2026 17:41
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 6, 2026
@sentry-junior
sentry-junior Bot force-pushed the feat/service-account-token-replication-caller branch from 828f703 to 7b360ff Compare November 6, 2026 18:12
@skaasten
skaasten force-pushed the feat/service-account-token-replication-caller branch from 7b360ff to ae12b7f Compare November 9, 2026 14:48
@skaasten
skaasten requested a review from markstory November 9, 2026 14:52
serialize_service_account_token,
)

cell_replica_service.upsert_replicated_service_account_token(

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.

Won't merge this until base branch has completely deployed to control.

Base automatically changed from feat/service-account-token-replica to master November 9, 2026 14:55
@sentry-junior
sentry-junior Bot requested review from a team as code owners November 9, 2026 14:55
skaasten added a commit that referenced this pull request Nov 9, 2026
…130108)

Part of the service account access tokens plan. This PR adds the
cell-side copy of `ServiceAccountToken` (#130103, merged) and the RPC
method that writes it. **Nothing calls the RPC method yet.**

The caller, `ServiceAccountToken.handle_async_replication`, is in
#130265, which is stacked on this PR. Per the hybrid cloud deploy rule,
the RPC method has to be deployed in every cell before anything calls
it, so **#130265 must merge only after this PR is deployed everywhere.**

Nothing creates or reads tokens yet. The authenticator comes in a later
PR and will read the replica this PR adds.

**What changes**

- `ServiceAccountToken` becomes a `ReplicatedControlModel` with a new
`SERVICE_ACCOUNT_TOKEN_UPDATE` outbox category (53), registered in
`ORGANIZATION_SCOPE` and sharded on the org like the account's outbox.
This needs no schema change. Because the model has no
`handle_async_replication` yet, its outboxes drain without doing
anything in the cell. Deletes still write tombstones.
-
`outbox_replication.service_accounts_serviceaccounttoken.replication_version`
is registered (default 0) for the backfill.
- `ServiceAccountTokenReplica` (cell) is created by migration
`service_accounts/0007`. It has:
  - an FK to `ServiceAccountReplica` and an FK to the org
  - a unique `token_id` hybrid cloud FK
  - `token_hashed`, indexed but not unique, as on `OrgAuthTokenReplica`
  - `scope_list`, `date_expires` and `date_deactivated`

The table is new and empty, so the migration is safe to run during
deploy.
- `cell_replica_service.upsert_replicated_service_account_token` is
declared on `CellReplicaService`
(`@cell_rpc_method(resolve=ByCellName())`) and fully implemented in
`DatabaseBackedCellReplicaService`:
- It looks up the account's replica and takes `organization_id` from the
token. There's no separate org lookup: the account replica's org FK
cascades, so finding it proves the org exists.
- If the account replica is missing, it logs
`service_account_token_replica.missing_service_account`, records a
`skipped` replica write and returns. It doesn't raise, so an
out-of-order message can't block the org's outbox shard.
- Otherwise it upserts the replica through the shared
`handle_replication`, unchanged. On the update path (a revoke or scope
change), that still loads the org row once to build the update.
- `RpcServiceAccountToken` and `serialize_service_account_token` are
added. `token_hashed` is `repr=False`, and the replica's `__repr__`
leaves the hash out.
- Deleting a token row in control (on org deletion) removes the replica
through the tombstone cascade on `token_id`.

**Rollout**

1. Deploy this PR. Token outboxes are written and drained as no-ops.
2. Merge and deploy #130265, which starts calling the RPC method.
3. Raise
`outbox_replication.service_accounts_serviceaccounttoken.replication_version`
in the options automator to backfill replicas for any tokens saved
between steps 1 and 2.

**Expected check warnings**

- The optional prod/dev RPC schema diff will list
`region_replica/upsert_replicated_service_account_token` as a new
method.

**Tests**

`ServiceAccountTokenReplicaServiceTest` (all silos) calls the RPC method
directly and covers:
- the outbox's scope, shard and cell
- an upsert creating the replica with every field and the account
replica link
- a revoke keeping the replica with `date_deactivated` set
- a token of a deleted (inactive) account still replicating
- a missing account replica being skipped without raising
- delete removing the replica through the tombstone cascade

Outbox-driven replication tests are in #130265.

These also pass locally (754 tests on a fresh test DB):
- `tests/sentry/service_accounts` and `tests/sentry/hybridcloud`
- `tests/sentry/backup/test_exhaustive.py`
- `tests/sentry/db/test_silo_models.py`

`makemigrations --check` finds no changes. prek and mypy pass.

**Known failure from master**

- `migration drift` fails on the `sentry_grouprelease.new_release_id`
indexes. None of the reported drift touches `service_accounts`.

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

<!-- junior-session-footer:start -->
<!--
junior-conversation-id:agent-dispatch%3Adispatch_f7e53e47800bce9272ae0b23084ecc16
-->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent-dispatch%3Adispatch_f7e53e47800bce9272ae0b23084ecc16)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent-dispatch%3Adispatch_f7e53e47800bce9272ae0b23084ecc16/?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]>
…r outbox

Call upsert_replicated_service_account_token from ServiceAccountToken's
handle_async_replication. This must deploy after the PR that added the RPC
method, so every cell serves it before control starts calling it.

Co-Authored-By: Shaun Kaasten <[email protected]>
@skaasten
skaasten force-pushed the feat/service-account-token-replication-caller branch from ae12b7f to d068ac2 Compare November 9, 2026 14:56
@skaasten
skaasten merged commit adba888 into master Nov 9, 2026
79 checks passed
@skaasten
skaasten deleted the feat/service-account-token-replication-caller branch November 9, 2026 18:29
sentry-junior Bot added a commit that referenced this pull request Nov 12, 2026
#130265 replicates service account tokens through their outbox, so the
revoke test can check the cell replica's date_deactivated instead of
asserting that the replication handler ran.

Co-Authored-By: Shaun Kaasten <[email protected]>
sentry-junior Bot added a commit that referenced this pull request Nov 12, 2026
#130265 replicates service account tokens through their outbox, so the
revoke test can check the cell replica's date_deactivated instead of
asserting that the replication handler ran.

Co-Authored-By: Shaun Kaasten <[email protected]>
sentry-junior Bot added a commit that referenced this pull request Nov 12, 2026
#130265 replicates service account tokens through their outbox, so the
revoke test can check the cell replica's date_deactivated instead of
asserting that the replication handler ran.

Co-Authored-By: Shaun Kaasten <[email protected]>
sentry-junior Bot added a commit that referenced this pull request Nov 12, 2026
#130265 replicates service account tokens through their outbox, so the
revoke test can check the cell replica's date_deactivated instead of
asserting that the replication handler ran.

Co-Authored-By: Shaun Kaasten <[email protected]>
skaasten added a commit that referenced this pull request Nov 13, 2026
#130265 replicates service account tokens through their outbox, so the
revoke test can check the cell replica's date_deactivated instead of
asserting that the replication handler ran.

Co-Authored-By: Shaun Kaasten <[email protected]>
skaasten added a commit that referenced this pull request Nov 13, 2026
#130265 replicates service account tokens through their outbox, so the
revoke test can check the cell replica's date_deactivated instead of
asserting that the replication handler ran.

Co-Authored-By: Shaun Kaasten <[email protected]>
skaasten added a commit that referenced this pull request Nov 13, 2026
#130265 replicates service account tokens through their outbox, so the
revoke test can check the cell replica's date_deactivated instead of
asserting that the replication handler ran.

Co-Authored-By: Shaun Kaasten <[email protected]>
skaasten added a commit that referenced this pull request Nov 13, 2026
…ens (#130508)

Adds `DELETE
/organizations/{org}/service-accounts/{id}/tokens/{token_id}/`. This is
the last of three small token endpoint PRs, one per endpoint:

1. List tokens: #130483
2. Create a token: #130507
3. **Revoke a token (this PR)**

The endpoint needs `service_account:write`. It locks the token row, sets
`date_deactivated` and saves it, so the token's outbox updates the
replica in the org's cell. It returns 204. The token must belong to the
account in the URL, and the account must be active and in the URL's org.
Otherwise, or if the token is already revoked, it returns 404.

It uses the same guards as create: only a person signed in as themselves
can call it, and Seer agent tokens and impersonation sessions are
refused.

Each revoke writes a `SERVICE_ACCOUNT_TOKEN_REMOVE` (227) audit log
entry. Its data holds the token name, scopes and account, never the
token or its hash.

The settings UI's Revoke button in #130485 calls this endpoint.

**Tests:** the revoke test drains the token's outbox and checks that the
cell's `ServiceAccountTokenReplica` gets the token's new
`date_deactivated`. This works because #130265, the token replication
handler, is merged.

<!-- junior-request-attribution:start -->
via @skaasten.
<!-- junior-request-attribution:end -->

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

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CA89MBDQB%3A1794494200.498819)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CA89MBDQB%3A1794494200.498819/?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]>
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