Skip to content

feat(auth): Add cached service account RPC for cells - #128182

Merged
skaasten merged 2 commits into
masterfrom
feat/service-account-rpc-cache
Oct 21, 2026
Merged

skaasten merged 2 commits into
masterfrom
feat/service-account-rpc-cache

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

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.

via shaun.kaasten.

--

View Junior Session [Sentry]

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 20, 2026
@sentry-junior
sentry-junior Bot force-pushed the feat/service-account-rpc-cache branch from 33618aa to da14ea0 Compare October 20, 2026 15:20
@skaasten
skaasten added this pull request to stack #128203 October 20, 2026 15:31
@skaasten
skaasten force-pushed the feat/service-account-rpc-cache branch from da14ea0 to 1137eb8 Compare October 20, 2026 15:41
@skaasten
skaasten marked this pull request as ready for review October 20, 2026 15:41
@skaasten
skaasten requested review from a team as code owners October 20, 2026 15:41
@sentry-junior
sentry-junior Bot requested review from a team as code owners October 20, 2026 17:38
@sentry-junior
sentry-junior Bot force-pushed the feat/service-account-rpc-cache branch from 55950ef to e3b1f0a Compare October 20, 2026 18:02
@sentry-junior
sentry-junior Bot removed the request for review from a team October 20, 2026 18:04
@skaasten
skaasten requested a review from gricha October 21, 2026 14:23
Base automatically changed from feat/add-service-account-models to master October 21, 2026 16:50
sentry-junior Bot and others added 2 commits October 21, 2026 12:50
Cells look up service accounts through a cached RPC to control, the same
way they look up users. The service lives in the sentry.service_accounts
app next to the models. A SERVICE_ACCOUNT_UPDATE control outbox receiver
clears the cached entry in the org's cell, so account updates, deletes and
project link changes reach the next cell request.

Refs ENG-8829

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

Callers use the service object for both reads, so they don't need to
know the cached read is a module-level function.

Co-Authored-By: shaun.kaasten <[email protected]>
@skaasten
skaasten force-pushed the feat/service-account-rpc-cache branch from e3b1f0a to 46e3712 Compare October 21, 2026 16:50
@skaasten
skaasten merged commit b5dde63 into master Oct 21, 2026
86 of 87 checks passed
@skaasten
skaasten deleted the feat/service-account-rpc-cache branch October 21, 2026 17:04
skaasten added a commit that referenced this pull request Oct 23, 2026
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]>
skaasten added a commit that referenced this pull request Oct 23, 2026
…se (#128239)

Step 4 of the service accounts database design: the list, create,
retrieve, update and delete endpoints from #127588 now read and write
`ServiceAccount` and `ServiceAccountProject` instead of returning canned
data. The request and response contract doesn't change, because Seer
builds against it.

- Create stores the account with `created_by` and its projects in one
transaction. A duplicate name in the org returns 400, and the unique
constraint covers the race between the check and the insert.
- Update is partial. Sending `projectIds` replaces the set, and `null`
or `[]` clears it. Leaving it out keeps the set. The account is saved
even when only its projects change, because the account's outbox is what
clears the cell's cached copy.
- Delete is a hard delete. The project rows cascade, and the outbox
clears the cell cache.
- An account in another org returns 404.
- The existing rules stay as they were: callers can only grant scopes
they hold and projects they can access.
- New audit log events: `SERVICE_ACCOUNT_ADD` (223),
`SERVICE_ACCOUNT_EDIT` (224) and `SERVICE_ACCOUNT_REMOVE` (225).

Review notes:
- `Factories.create_service_account` lands here instead of in step 7,
because the endpoint tests need it.
- The viewer-context endpoint is still mocked. Its canned scopes moved
into that module and are replaced in step 5.

Stacked on #128182.

Refs ENG-8829

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

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

--

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