Skip to content

feat(service-accounts): Soft-delete service accounts - #129950

Merged
skaasten merged 3 commits into
masterfrom
feat/service-accounts-soft-delete
Nov 5, 2026
Merged

skaasten merged 3 commits into
masterfrom
feat/service-accounts-soft-delete

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

PR 2 of the service account soft-delete plan. PR 1 (#129914) made names unique only among active accounts, so a deleted account's name can be reused.

Deleting a service account now keeps the row and sets is_active=False, like User, instead of hard-deleting it. That keeps references such as created_by_service_account_id on dashboards pointing at a real account, and it makes the token work later in the plan simpler. The cell replica stays too, with is_active=False.

ServiceAccount.deactivate() does the delete. It locks the active row, removes its project links, clears all_projects and turns the account off. Everything that reads an account through the API or RPC now treats a deleted account as missing (404 / None). The cell-side readers already reject inactive accounts.

Deleted is now the only way to be inactive, so isActive is gone from the request and the response. In a response it was always true, and in a request only true was useful. Like other unknown fields, isActive in a request is now ignored. Seer's generated client still lists the field, but no Seer code reads or sends it, so it can drop out the next time the client is regenerated. Audit entries no longer record is_active.

If two DELETE requests race, the second one gets a 404 and writes no audit entry.

Left as is:

  • Seer's "Service account is inactive" error message. Seer tests match on it, and the plan marks the change as optional.
  • No new org-deletion test. Rows still go with the org through the existing cascade, and a test would only exercise that.

via shaun.kaasten.

--

View Session [Sentry]

Deleting a service account now keeps the row with is_active=False, the way deleting a user works, instead of hard-deleting it. No tombstone is written, so cell tables that point at the account (dashboards, saved queries, investigations, Seer workflows) keep their reference. Project links are removed and all_projects is cleared. The cell replica stays and replicates is_active=False, which every replica reader already checks.

Control-side lookups (list, details, PUT, and the RPC get/update/delete) hide deleted accounts. POST and PUT now reject isActive=false with a 400, since an account can no longer be paused; isActive=true is still accepted so generated clients keep working.

Co-Authored-By: Shaun Kaasten <[email protected]>

Co-Authored-By: Shaun Kaasten <[email protected]>
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 4, 2026
Deleting is the only way to turn a service account off, and deleted
accounts return 404, so isActive was always true in responses and
could only be set to true in requests. Seer's generated client has
the field, but no Seer code reads or sends it.

Co-Authored-By: Shaun Kaasten <[email protected]>

@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 1 potential issue.

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 4a9aa71. Configure here.

@skaasten
skaasten requested a review from markstory November 5, 2026 15:24
@skaasten
skaasten merged commit 9457bb2 into master Nov 5, 2026
83 of 85 checks passed
@skaasten
skaasten deleted the feat/service-accounts-soft-delete branch November 5, 2026 18:28
skaasten pushed a commit that referenced this pull request Nov 6, 2026
This is part 1 of the service account access tokens plan. It's the first
PR in a stack:

1. **This PR**: the control `ServiceAccountToken` model and its table.
2. #130108: replicating tokens to the cell
(`ServiceAccountTokenReplica`, the outbox and the RPC method).

Nothing creates, reads or authenticates tokens yet.

**What changes**

`ServiceAccountToken` is a plain control silo model in this PR. The next
PR makes it a `ReplicatedControlModel`, which needs no schema change.

- Only `token_hashed` (unique) and `token_last_characters` are stored.
The plaintext is never stored.
- Each token has its own `scope_list`, validated with
`validate_grantable_scopes`. The authenticator will intersect it with
the account's current scopes on every request, so narrowing the account
narrows the token, but widening the account doesn't widen it.
- `date_deactivated` is the token's status: `NULL` means active.
Revoking keeps the row, like org auth tokens. There's no separate
`is_active` boolean that could disagree with it.
- `service_account` is a normal `CASCADE` FK. Accounts are soft-deleted
(#129950), so it only fires when the org is deleted.
- `organization_id` is copied from the account, so the outbox and the
cell replica in the next PR don't need a join.
- `created_by` is always a human user. Per the plan, token management is
human-only.
- `RelocationScope.Excluded`: a relocated token would need a new secret
anyway.
- `__repr__` leaves out the hash.

Migration `service_accounts/0005` creates the table. It's new and empty,
so the migration is safe to run during deploy.

**Tests**

- `ServiceAccountTokenTest`: `scope_list` rejects non-grantable scopes,
and the repr omits the hash.
- `tests/sentry/service_accounts`,
`hybridcloud/tasks/test_backfill_outboxes.py`, the backup coverage,
invariants and snapshots tests, and `db/test_silo_models.py` pass
locally.
- The migration applies, reverses and reapplies cleanly, and
`makemigrations --check` finds no changes. prek and mypy pass.

**Known failures from master**

- `migration drift` fails on the `sentry_grouprelease.new_release_id`
indexes.
- 3 tests in `seer/endpoints/test_organization_seer_workflow_configs.py`
fail.

Both fail the same way on master today.

<!-- junior-request-attribution:start -->
via [automation
`evt_96f99ce311a0097e57d5a80fb82b61c7`](https://junior-prod.sentry.dev/automations/evt_96f99ce311a0097e57d5a80fb82b61c7)
· Run ID `dispatch_f7e53e47800bce9272ae0b23084ecc16`, **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>

This branch was successfully deployed

1 active deployment
Preview — 425b75aa Deployed Nov 5, 2026 by vercel[bot]
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