File size: 16,264 Bytes
e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 ede4c1f e727dd5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | """HTTP surface for the authorization admission store.
Three endpoints, and deliberately no judgement of its own:
* ``POST /api/v0/authorizations/admit`` — the gate turns an approved decision
and a sealed envelope into one spendable record.
* ``GET /api/v0/authorizations/{id}`` — status, for anyone who may read.
* ``POST /api/v0/authorizations/{id}/consume`` — the addressed agent spends it,
exactly once, for one stated action.
WHY THIS MODULE DECIDES ALMOST NOTHING
``authorization_store`` already answers who may admit, who may spend, and what
a refusal is called. The temptation in an adapter is to answer those a second
time -- a scope check at the door, an expiry check before the call -- and a
second answer is a second authority that can drift from the first. So the two
mutating endpoints authenticate the caller and hand the principal to the store
unexamined. A 403 from ``/admit`` is the store's ``not_gate_principal``, not a
decision made here.
That is not fastidiousness. ``main.has_scope`` expands wildcards, so a key
holding ``gate:*`` satisfies it; ``_check_gate_authority`` requires
``gate:admit_authorization`` by exact membership, specifically so that a
wildcard on an agent key cannot become the power to authorize that agent's own
work. Pre-checking with the looser rule and then calling the store would leave
the endpoint's *answer* correct while making the adapter say yes to something
the store exists to refuse. The next person to add a fast path would inherit
that as precedent.
THE ONE CHECK THIS MODULE DOES OWN
``read_authorization`` is a bare SELECT with no authority check at all, by
design -- it grants nothing and spends nothing. That makes this module the only
thing standing between an authorization id and the sealed envelope, which
carries allowed paths, budgets, and the nonce. So the read endpoint owns its
scope check, and returns an allowlisted projection rather than the row.
THE CLOCK IS THE SERVER'S
``now`` is taken from the injected clock and never from the request. An expiry
the caller can move is not an expiry. Callers cannot smuggle one either: the
envelope contract refuses unknown fields, so a payload carrying ``now`` is
malformed rather than helpful.
"""
from __future__ import annotations
import time
import uuid
from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Optional
from fastapi import APIRouter, Body, Depends, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from authorization_envelope import AuthorizationError, Expectation
from authorization_store import (
AuditRecorder,
Principal,
admit_authorization,
consume_authorization,
read_authorization,
)
from temporal_contract import system_temporal_profile
from temporal_ledger import record_temporal_mutation
# Refusal code -> HTTP status. Everything absent is 422: the request was
# understood and refused on its merits, which is the common case here.
#
# The 503s are the exception worth naming. `audit_unavailable` and
# `audit_not_independent` are not the caller's fault -- they mean this server
# cannot record attempts durably, so it refuses to act at all. Reporting those
# as 4xx would tell a caller to fix a request that was never the problem.
REFUSAL_STATUS: Dict[str, int] = {
"not_gate_principal": 403,
"recipient_mismatch": 403,
"unknown_gate_decision": 404,
"unknown_authorization": 404,
"already_admitted": 409,
"authorization_id_conflict": 409,
"nonce_reused": 409,
"replayed_authorization": 409,
"audit_unavailable": 503,
"audit_not_independent": 503,
}
DEFAULT_REFUSAL_STATUS = 422
READ_SCOPE = "reader:authorizations"
# An allowlist, not a denylist. A column added to authorization_admissions
# later must not start appearing here because nobody remembered to exclude it;
# `envelope`, `nonce` and `consumed_expectation` are exactly the fields that
# would be worst to leak by omission.
READABLE_FIELDS = (
"authorization_id",
"task_id",
"spec_sha256",
"envelope_sha256",
"gate_decision_id",
"addressed_agent",
"lane",
"branch",
"base_sha",
"issued_at",
"expires_at",
"admitted_by",
"admitted_at",
"consumed_at",
"consumed_by",
)
# `consume_authorization` returns the verified Envelope object, because holding
# it is the permission. Over HTTP that has to become a chosen projection: the
# consumer already has the envelope it just spent, so what it needs back is
# confirmation plus the operative grant it is now acting under -- paths,
# budgets, and the window. The nonce is never echoed, spent or not; it is
# single-use material, not a receipt.
CONSUMED_FIELDS = ("authorization_id", "status", "consumed_by", "consumed_at")
GRANT_FIELDS = (
"requested_action",
"lane",
"branch",
"base_sha",
"allowed_paths",
"risk_tier",
"max_tokens",
"max_seconds",
"tests",
"stop_conditions",
"depends_on",
)
def _consumed_body(result: Dict[str, Any]) -> Dict[str, Any]:
body = {name: result[name] for name in CONSUMED_FIELDS if name in result}
envelope = result.get("envelope")
if envelope is None:
return body
body["envelope_sha256"] = getattr(envelope, "envelope_sha256", None)
body["expires_at"] = getattr(envelope, "expires_at", None)
grant: Dict[str, Any] = {}
for name in GRANT_FIELDS:
value = getattr(envelope, name, None)
if value is None:
continue
grant[name] = list(value) if isinstance(value, tuple) else value
body["grant"] = grant
return body
class ExpectationIn(BaseModel):
agent: str
lane: str
action: str
branch: str
base_sha: str
intended_paths: List[str] = []
class ConsumeIn(BaseModel):
expected_envelope_sha256: str
expected_task_id: str
expected_spec_sha256: str
expectation: ExpectationIn
def _refusal(error: AuthorizationError) -> JSONResponse:
"""One shape for every refusal, with the store's code preserved verbatim.
The code is the stable part of this API. Callers branch on
``refusal_code``; the message is for humans and may be reworded.
"""
body: Dict[str, Any] = {
"refusal_code": error.code,
"detail": str(error),
}
field = getattr(error, "field", None)
if field:
body["field"] = field
return JSONResponse(
status_code=REFUSAL_STATUS.get(error.code, DEFAULT_REFUSAL_STATUS),
content=body,
)
def _store_principal(caller: Any) -> Principal:
"""The caller as the store expects it. No scopes are added or expanded."""
return Principal(agent_id=caller.agent_id, scopes=tuple(caller.scopes or ()))
def _may_read(caller: Any) -> bool:
"""Deliberately narrower than ``main.has_scope``.
Only the exact read scope or a full reader wildcard. `writer:*` grants
nothing here: being able to write threads is not a reason to be handed the
shape of somebody's authorization.
"""
scopes = set(caller.scopes or ())
return READ_SCOPE in scopes or "reader:*" in scopes
def _recorded_at(now: Any) -> datetime:
if isinstance(now, (int, float)) and not isinstance(now, bool):
try:
return datetime.fromtimestamp(float(now), timezone.utc)
except (OverflowError, OSError, ValueError):
pass
return datetime.now(timezone.utc)
class _TemporalAuditRecorder(AuditRecorder):
"""Commit each authorization attempt and its temporal row together."""
def __init__(
self,
cursor: Any,
commit: Callable,
rollback: Callable,
temporal_recorder: Callable,
):
super().__init__(cursor, lambda: None)
self._atomic_commit = commit
self._atomic_rollback = rollback
self._temporal_recorder = temporal_recorder
def record(
self,
kind: str,
authorization_id: Optional[str],
principal: Principal,
outcome: str,
refusal_code: Optional[str],
detail: Optional[str],
now: Any,
) -> None:
operation_id = f"auth_attempt_{uuid.uuid4().hex}"
occurred_at = _recorded_at(now)
super().record(
kind, authorization_id, principal, outcome, refusal_code, detail, now,
)
try:
self._temporal_recorder(
self.cursor,
profile=system_temporal_profile(
now=occurred_at,
action_basis=f"authorization_attempt:{kind}:{outcome}",
discovery_basis="authorization_request_received_at",
),
actor_id=principal.agent_id,
operation_id=operation_id,
entity_type="authorization_attempt",
entity_id=operation_id,
mutation_kind="create",
source="authorization_api.audit",
recorded_at=occurred_at,
)
except Exception:
self._atomic_rollback()
raise
self._atomic_commit()
def mount_authorization_api(
app: Any,
db_factory: Callable[[], Any],
principal_dependency: Callable[..., Any],
clock: Callable[[], float] = time.time,
temporal_recorder: Callable[..., Dict[str, Any]] = record_temporal_mutation,
) -> APIRouter:
"""Mount the three endpoints on ``app``.
``db_factory`` must return a *new* connection per call. The store refuses an
audit recorder that shares the caller's connection, so the mutating paths
open two and would fail closed rather than silently log into the same
transaction they might roll back.
"""
router = APIRouter(prefix="/api/v0/authorizations", tags=["authorization"])
def _mutate(handler: Callable[[Any, AuditRecorder, float], Dict[str, Any]],
success_status: int,
project: Callable[[Dict[str, Any]], Dict[str, Any]] = dict,
*,
actor_id: str,
entity_type: str,
entity_id: str,
mutation_kind: str,
action_basis: str,
) -> JSONResponse:
"""Run one store call on its own connection, audited on a second.
The audit connection is committed row by row inside the recorder, so it
is not rolled back here even when the operation is: that is the point of
it. The operation connection is rolled back on refusal so a partial
statement cannot outlive the answer.
"""
now = clock()
occurred_at = _recorded_at(now)
operation_id = f"auth_mutation_{uuid.uuid4().hex}"
with db_factory() as conn:
with db_factory() as audit_conn:
with conn.cursor() as cur:
with audit_conn.cursor() as audit_cur:
audit = _TemporalAuditRecorder(
audit_cur, audit_conn.commit, audit_conn.rollback,
temporal_recorder,
)
try:
result = handler(cur, audit, now)
except AuthorizationError as error:
conn.rollback()
return _refusal(error)
try:
temporal_recorder(
cur,
profile=system_temporal_profile(
now=occurred_at,
action_basis=action_basis,
discovery_basis="authorization_request_received_at",
),
actor_id=actor_id,
operation_id=operation_id,
entity_type=entity_type,
entity_id=entity_id,
mutation_kind=mutation_kind,
source="authorization_api",
recorded_at=occurred_at,
)
except Exception:
conn.rollback()
raise
conn.commit()
return JSONResponse(status_code=success_status, content=project(result))
@router.post("/admit")
def admit(request: Request,
payload: Dict[str, Any] = Body(...),
caller: Any = Depends(principal_dependency)) -> JSONResponse:
"""Admit one envelope. Only the human gate can succeed here.
No scope check precedes the store call, and that is not an omission --
see the module docstring. The payload is passed through exactly as
received so the envelope contract can refuse unknown fields; parsing it
into a model here would silently drop them.
"""
principal = _store_principal(caller)
return _mutate(
lambda cur, audit, now: admit_authorization(
cur, payload, principal=principal, now=now, audit=audit),
success_status=201,
actor_id=principal.agent_id,
entity_type="authorization_admission",
entity_id=str(payload.get("authorization_id") or "Q?"),
mutation_kind="admit",
action_basis="authorization_admitted",
)
@router.get("/{authorization_id}")
def read(authorization_id: str,
caller: Any = Depends(principal_dependency)) -> JSONResponse:
"""Status only. Spends nothing, writes no ledger row, grants nothing."""
if not _may_read(caller):
return JSONResponse(
status_code=403,
content={"refusal_code": "missing_scope",
"detail": f"Missing scope: {READ_SCOPE}"},
)
with db_factory() as conn:
with conn.cursor() as cur:
record = read_authorization(cur, authorization_id)
if record is None:
return JSONResponse(
status_code=404,
content={"refusal_code": "unknown_authorization",
"detail": f"no admitted authorization "
f"'{authorization_id}'"},
)
body = {name: record[name] for name in READABLE_FIELDS
if name in record}
body["status"] = ("admitted" if record.get("consumed_at") is None
else "consumed")
return JSONResponse(status_code=200, content=body)
@router.post("/{authorization_id}/consume")
def consume(authorization_id: str,
body: ConsumeIn,
caller: Any = Depends(principal_dependency)) -> JSONResponse:
"""Spend one authorization for one stated action, exactly once.
Single use is decided by the store's guarded UPDATE, not by this
endpoint reading first: two concurrent callers both reach it and the
database picks the winner.
"""
principal = _store_principal(caller)
expectation = Expectation(
agent=body.expectation.agent,
lane=body.expectation.lane,
action=body.expectation.action,
branch=body.expectation.branch,
base_sha=body.expectation.base_sha,
intended_paths=tuple(body.expectation.intended_paths),
)
return _mutate(
lambda cur, audit, now: consume_authorization(
cur, authorization_id,
principal=principal,
expectation=expectation,
now=now,
expected_envelope_sha256=body.expected_envelope_sha256,
expected_task_id=body.expected_task_id,
expected_spec_sha256=body.expected_spec_sha256,
audit=audit),
success_status=200,
project=_consumed_body,
actor_id=principal.agent_id,
entity_type="authorization_admission",
entity_id=authorization_id,
mutation_kind="consume",
action_basis="authorization_consumed",
)
app.include_router(router)
return router
|