Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
76c1f49
fix(secrets): check dependents and delete in one atomic step on every…
mrveiss Nov 8, 2026
9edb765
Merge origin/main into issue-21039
mrveiss Nov 8, 2026
6016e1d
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 8, 2026
fee4986
fix(secrets): authorize before locking, bound the lock wait, and bind…
mrveiss Nov 8, 2026
2908341
Merge remote-tracking branch 'origin/main' into issue-21039
mrveiss Nov 8, 2026
80bfd1b
Merge remote-tracking branch 'origin/issue-21039' into issue-21039
mrveiss Nov 8, 2026
894b968
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 8, 2026
c899cfe
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 8, 2026
fad30a2
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 9, 2026
f96f247
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 9, 2026
c1c761b
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 9, 2026
30037e2
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 9, 2026
3b61a0d
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 10, 2026
87f533c
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 10, 2026
4a71e5f
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 10, 2026
319e99b
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 10, 2026
05190d8
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 11, 2026
f72f3d8
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 11, 2026
465cba5
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 11, 2026
81984d4
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 12, 2026
544b6b2
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 12, 2026
f63a175
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 12, 2026
79827ea
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 12, 2026
232ed37
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 12, 2026
dac1a0a
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 12, 2026
2d4952f
fix(secrets): the dependents digest is compared through the shared co…
mrveiss Nov 12, 2026
c0b9b3b
Merge main into issue-21039 (auto-sync) [skip ci]
github-actions[bot] Nov 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions autobot-backend/api/envelope_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,14 @@ async def rotate_secret(
async def delete_secret(
secret_id: uuid.UUID,
request: Request,
confirm_dependents: bool = False,
dependents_digest: str | None = None,
who: tuple[uuid.UUID, set[str]] = Depends(principal),
session: AsyncSession = Depends(get_session),
coordinator: SecretsCoordinator = Depends(get_coordinator),
) -> None:
"""Delete a secret; 409 with the dependents listed unless ``confirm_dependents`` (#20640)."""
"""Delete a secret; 409 with the dependents listed unless ``dependents_digest`` confirms that set (#20640)."""
user_id, perms = who
await delete_as_user(request, session, coordinator, secret_id, user_id, perms, confirm_dependents, _mapped)
await delete_as_user(request, session, coordinator, secret_id, user_id, perms, dependents_digest, _mapped)


@router.post("/{secret_id}/share", status_code=status.HTTP_201_CREATED)
Expand Down
44 changes: 25 additions & 19 deletions autobot-backend/api/envelope_secrets_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
and records the intent before the write and the outcome after it (#20557). The two
envelope deletes (``DELETE /api/v2/secrets/{id}`` and the service-identity
``DELETE /api/v2/secrets/system/{id}``) reached the same store with neither. Both now
go through :func:`audited_delete`, which reuses the main route's 409
(``secrets_system_vault.refuse_dependents``) and audit adapter (``secrets_audit``),
so one operation behaves the same on every route.
go through :func:`audited_delete`, which calls the one guarded delete
(``secrets_guarded_delete.guarded_delete``: lock, check, delete, #21039) and reuses the main
route's audit adapter (``secrets_audit``), so one operation behaves the same on every route.
"""

from __future__ import annotations
Expand All @@ -22,7 +22,7 @@
from sqlalchemy.ext.asyncio import AsyncSession

from api.secrets_audit import audit_secret_op
from api.secrets_system_vault import refuse_dependents
from api.secrets_guarded_delete import guarded_delete
from autobot_shared.logging_manager import get_logger
from autobot_shared.secrets_vault import VaultRef
from services.envelope_secrets_service import SecretAccessError, SecretNotFoundError
Expand All @@ -33,34 +33,34 @@

async def audited_delete(
request: Request,
session: AsyncSession,
secret_id: uuid.UUID,
*,
dependents: Callable[[], Awaitable[list]] | None,
dependents: Callable[[], Awaitable[list]],
delete: Callable[[], Awaitable[None]],
confirmed_digest: str | None = None,
actor: str | None = None,
**who: str,
) -> None:
"""Record the intent, refuse while ``dependents()`` lists any, delete, record the outcome.
"""Record the intent, guarded-delete (:func:`guarded_delete`), record the outcome.

``dependents=None`` skips the check (a human has seen the list and confirmed). The
``confirmed_digest`` is the digest of the dependents a human was shown and confirmed. The
first record says ``attempted`` and, when it cannot be written, nothing is deleted
(503): the credential is never destroyed without its paper trail (#17038). A refusal
or failure is recorded as ``denied`` / ``failed`` and re-raised for the route to map.
``actor`` is the caller's resolved user id (recorded as is, never looked up again);
``who`` are identifiers only (e.g. ``service``), never a value.

The dependents listing and the delete are separate statements, exactly as on
``DELETE /api/secrets/{id}``: a dependent registered between them is deleted with the
secret (``ON DELETE CASCADE``), the same outcome as one registered a moment after the
delete. The check guards against a consumer a human or service already depends on, not
against a registration racing the delete, so no lock is taken (the store has none).
The dependents listing and the delete run under one row lock on the secret (#21039), so a
dependent registered while the delete is in flight is either listed (409) or refused by the
foreign key once the secret is gone; it is never deleted by the cascade unconfirmed.
"""
ref = str(secret_id)
await audit_secret_op("DELETE", ref, request, "attempted", required=True, actor=actor, phase="before_write", **who)
try:
if dependents is not None:
refuse_dependents(await dependents())
await delete()
await guarded_delete(
session, secret_id, dependents=dependents, delete=delete, confirmed_digest=confirmed_digest
)
except HTTPException as exc:
reason = "has_dependents" if exc.status_code == 409 else "http_%d" % exc.status_code
await audit_secret_op("DELETE", ref, request, "denied", reason=reason, actor=actor, **who)
Expand All @@ -84,10 +84,10 @@ async def delete_as_user(
secret_id: uuid.UUID,
user_id: uuid.UUID,
permissions: set[str],
confirm: bool,
confirmed_digest: str | None,
mapped: Callable[[Exception], HTTPException],
) -> None:
"""The user route: dependents are refused (409) unless a human ``confirm``-ed the listing.
"""The user route: dependents are refused (409) unless ``confirmed_digest`` names the listing a human saw.

``mapped`` turns a coordinator exception into the route's HTTP error (``_mapped``).
"""
Expand All @@ -103,7 +103,13 @@ async def dependents() -> list:

try:
await audited_delete(
request, secret_id, dependents=None if confirm else dependents, delete=delete, actor=str(user_id)
request,
session,
secret_id,
dependents=dependents,
delete=delete,
confirmed_digest=confirmed_digest,
actor=str(user_id),
)
except (SecretAccessError, SecretNotFoundError) as exc:
raise mapped(exc)
Expand All @@ -129,6 +135,6 @@ async def dependents() -> list:
return await coordinator.service_describe_dependencies(session, secret_id=secret_id, vault=vault)

try:
await audited_delete(request, secret_id, dependents=dependents, delete=delete, service=service_id)
await audited_delete(request, session, secret_id, dependents=dependents, delete=delete, service=service_id)
except (SecretAccessError, SecretNotFoundError) as exc:
raise mapped(exc)
5 changes: 4 additions & 1 deletion autobot-backend/api/envelope_secrets_delete_20640_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from api import envelope_secrets
from api import secrets_audit_store_18648_test as base
from api.secrets_guarded_delete import dependents_digest
from services.envelope_secrets_service import SecretAccessError, SecretNotFoundError
from testkit.fake_async_session import FakeAsyncSession

Expand All @@ -34,6 +35,7 @@
_SECRET = "11111111-1111-4111-8111-111111111111"
_DEPENDENTS = [SimpleNamespace(dependent_kind="service", dependent_id="scheduler")]
_LISTED = [{"dependent_kind": "service", "dependent_id": "scheduler"}]
_DIGEST = dependents_digest(_DEPENDENTS)


class _Coordinator:
Expand Down Expand Up @@ -108,6 +110,7 @@ def test_a_delete_with_dependents_is_refused_with_the_main_routes_409(route, sto
"code": "secret_has_dependents",
"message": "Dependents use this secret",
"dependents": _LISTED,
"dependents_digest": _DIGEST,
}
assert coordinator.vault == {_SECRET}, "the secret must still be stored"
assert client.session.commits == 0
Expand All @@ -125,7 +128,7 @@ def test_a_user_can_confirm_past_dependents_but_a_service_cannot(route, store) -
kind, url = route
coordinator = _Coordinator(_DEPENDENTS)

response, _ = _run(coordinator, url, store, "?confirm_dependents=true")
response, _ = _run(coordinator, url, store, f"?dependents_digest={_DIGEST}")

if kind == "user":
assert response.status_code == 204
Expand Down
4 changes: 2 additions & 2 deletions autobot-backend/api/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ async def delete_secret(
secret_id: str,
http_request: Request,
chat_id: str | None = Query(None),
confirm_dependents: bool = Query(False),
dependents_digest: str | None = Query(None),
admin_check: bool = Depends(check_admin_permission),
):
"""Delete a secret (Issue #744: requires admin authentication)"""
Expand All @@ -1007,7 +1007,7 @@ async def delete_secret(
success = await asyncio.to_thread(secrets_manager.delete_secret, secret_id, chat_id=chat_id)
# #17099/#17731: a SYSTEM secret lives in the vault; delete it there too (409 while dependents exist).
success = success or await delete_system_vault_secret(
secret_id, owner_id, http_request.headers, confirm_dependents
secret_id, owner_id, http_request.headers, dependents_digest
)
if not success:
# #16428: revoke() is idempotent, so check existence first to 404 an unknown id.
Expand Down
12 changes: 10 additions & 2 deletions autobot-backend/api/secrets_defects_20557_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ async def describe_dependencies(self, session, *, user_id, permissions, secret_i
return [SimpleNamespace(dependent_kind=k, dependent_id=i) for k, i in self.dependents]


def _digest(dependents) -> str:
"""The digest a human echoes to confirm the listed (kind, id) dependents."""
from api.secrets_guarded_delete import dependents_digest

return dependents_digest([SimpleNamespace(dependent_kind=k, dependent_id=i) for k, i in dependents])


def _with_dependents(client_and_vault, monkeypatch, dependents): # noqa: F811
client, _ = client_and_vault()
vault = _VaultWithDependents(dependents)
Expand All @@ -92,7 +99,7 @@ def test_delete_with_dependents_is_refused_and_lists_them(client_and_vault, monk
def test_a_confirmed_delete_removes_the_secret(client_and_vault, monkeypatch) -> None: # noqa: F811
client, vault = _with_dependents(client_and_vault, monkeypatch, [("service", "scheduler")])

response = client.delete(f"/api/secrets/{_SECRET_ID}?confirm_dependents=true")
response = client.delete(f"/api/secrets/{_SECRET_ID}?dependents_digest={_digest([('service', 'scheduler')])}")

assert response.status_code == 200
assert _VAULT_KEY not in vault.store
Expand All @@ -103,7 +110,8 @@ def test_the_internal_key_cannot_confirm_past_dependents(client_and_vault, monke
monkeypatch.setattr(secrets_system_vault, "verify_internal_api_key", lambda k: k == "svc")

response = client.delete(
f"/api/secrets/{_SECRET_ID}?confirm_dependents=true", headers={"X-Internal-API-Key": "svc"}
f"/api/secrets/{_SECRET_ID}?dependents_digest={_digest([('service', 'scheduler')])}",
headers={"X-Internal-API-Key": "svc"},
)

assert response.status_code == 403
Expand Down
Loading