authzai 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentic_auth/__init__.py +8 -0
- agentic_auth/_extras.py +24 -0
- agentic_auth/api/__init__.py +0 -0
- agentic_auth/api/deps.py +112 -0
- agentic_auth/api/errors.py +51 -0
- agentic_auth/api/responses.py +38 -0
- agentic_auth/api/service_deps.py +113 -0
- agentic_auth/api/v1/__init__.py +0 -0
- agentic_auth/api/v1/agents.py +88 -0
- agentic_auth/api/v1/audit.py +67 -0
- agentic_auth/api/v1/authorize.py +439 -0
- agentic_auth/api/v1/consent.py +21 -0
- agentic_auth/api/v1/decisions.py +43 -0
- agentic_auth/api/v1/enrollment.py +48 -0
- agentic_auth/api/v1/identity.py +97 -0
- agentic_auth/api/v1/jwks.py +21 -0
- agentic_auth/api/v1/manifests.py +68 -0
- agentic_auth/api/v1/scopes.py +22 -0
- agentic_auth/api/v1/token.py +203 -0
- agentic_auth/api/v1/transaction.py +94 -0
- agentic_auth/boundary/__init__.py +26 -0
- agentic_auth/boundary/guarantee.py +96 -0
- agentic_auth/cli/__init__.py +27 -0
- agentic_auth/cli/audit.py +130 -0
- agentic_auth/cli/drift.py +164 -0
- agentic_auth/cli/drift_render.py +148 -0
- agentic_auth/cli/identity.py +63 -0
- agentic_auth/cli/manifest.py +275 -0
- agentic_auth/config.py +182 -0
- agentic_auth/db.py +49 -0
- agentic_auth/embedded/__init__.py +427 -0
- agentic_auth/embedded/_schema.py +8 -0
- agentic_auth/embedded/_transport.py +39 -0
- agentic_auth/embedded/capabilities.py +34 -0
- agentic_auth/embedded/export.py +86 -0
- agentic_auth/embedded/idp.py +78 -0
- agentic_auth/embedded/store.py +240 -0
- agentic_auth/errors.py +145 -0
- agentic_auth/main.py +141 -0
- agentic_auth/models/__init__.py +70 -0
- agentic_auth/models/agent.py +58 -0
- agentic_auth/models/audit_decision.py +199 -0
- agentic_auth/models/compiled_policy.py +28 -0
- agentic_auth/models/manifest.py +96 -0
- agentic_auth/models/reconciliation_report.py +27 -0
- agentic_auth/models/sandbox_run.py +93 -0
- agentic_auth/models/session.py +63 -0
- agentic_auth/models/step_up_grant.py +106 -0
- agentic_auth/models/transaction_token.py +73 -0
- agentic_auth/models/trust_anchor.py +59 -0
- agentic_auth/models/types.py +95 -0
- agentic_auth/observability/__init__.py +370 -0
- agentic_auth/observability/config.py +137 -0
- agentic_auth/observability/context.py +128 -0
- agentic_auth/observability/ids.py +122 -0
- agentic_auth/observability/logs.py +191 -0
- agentic_auth/observability/pep.py +169 -0
- agentic_auth/observability/spans.py +225 -0
- agentic_auth/observability/surface.py +286 -0
- agentic_auth/observability/tracing.py +167 -0
- agentic_auth/redis.py +27 -0
- agentic_auth/repositories/__init__.py +0 -0
- agentic_auth/repositories/agents.py +96 -0
- agentic_auth/repositories/audit_decisions.py +152 -0
- agentic_auth/repositories/compiled_policies.py +72 -0
- agentic_auth/repositories/manifests.py +149 -0
- agentic_auth/repositories/reconciliation_reports.py +40 -0
- agentic_auth/repositories/sessions.py +41 -0
- agentic_auth/repositories/transaction_tokens.py +60 -0
- agentic_auth/repositories/trust_anchors.py +150 -0
- agentic_auth/runtime.py +81 -0
- agentic_auth/schemas/__init__.py +0 -0
- agentic_auth/schemas/agent.py +58 -0
- agentic_auth/schemas/audit.py +207 -0
- agentic_auth/schemas/authorize.py +205 -0
- agentic_auth/schemas/common.py +40 -0
- agentic_auth/schemas/consent.py +74 -0
- agentic_auth/schemas/dag.py +39 -0
- agentic_auth/schemas/edge_trace.py +166 -0
- agentic_auth/schemas/identity.py +152 -0
- agentic_auth/schemas/manifest.py +117 -0
- agentic_auth/schemas/manifest_api.py +38 -0
- agentic_auth/schemas/reconciliation.py +49 -0
- agentic_auth/schemas/token.py +122 -0
- agentic_auth/schemas/transaction.py +28 -0
- agentic_auth/sdk/__init__.py +114 -0
- agentic_auth/sdk/client.py +677 -0
- agentic_auth/sdk/context.py +95 -0
- agentic_auth/sdk/drift.py +118 -0
- agentic_auth/sdk/errors.py +147 -0
- agentic_auth/sdk/identity.py +162 -0
- agentic_auth/sdk/planes.py +362 -0
- agentic_auth/sdk/provenance.py +63 -0
- agentic_auth/services/__init__.py +0 -0
- agentic_auth/services/anchors.py +81 -0
- agentic_auth/services/audit.py +222 -0
- agentic_auth/services/audit_chain.py +144 -0
- agentic_auth/services/audit_cursor.py +55 -0
- agentic_auth/services/catalog_service.py +58 -0
- agentic_auth/services/chain_query.py +152 -0
- agentic_auth/services/dag_compiler.py +199 -0
- agentic_auth/services/decision_cache.py +123 -0
- agentic_auth/services/decision_history.py +71 -0
- agentic_auth/services/discovery.py +95 -0
- agentic_auth/services/drift.py +330 -0
- agentic_auth/services/edge_rules.py +62 -0
- agentic_auth/services/edge_trace.py +198 -0
- agentic_auth/services/enrollment.py +310 -0
- agentic_auth/services/enrollment_rules.py +80 -0
- agentic_auth/services/manifest_service.py +166 -0
- agentic_auth/services/metrics.py +419 -0
- agentic_auth/services/pdp.py +312 -0
- agentic_auth/services/policy_loader.py +255 -0
- agentic_auth/services/principal.py +143 -0
- agentic_auth/services/reconciler.py +119 -0
- agentic_auth/services/revocation.py +149 -0
- agentic_auth/services/sessions.py +231 -0
- agentic_auth/services/taint.py +204 -0
- agentic_auth/services/tokens/__init__.py +11 -0
- agentic_auth/services/tokens/act_chain.py +76 -0
- agentic_auth/services/tokens/exchange.py +319 -0
- agentic_auth/services/tokens/jwt_profile.py +362 -0
- agentic_auth/services/tokens/keys.py +117 -0
- agentic_auth/services/tokens/profile.py +167 -0
- agentic_auth/services/tokens/replay.py +53 -0
- agentic_auth/services/transactions.py +172 -0
- agentic_auth/sidecar/__init__.py +7 -0
- agentic_auth/sidecar/__main__.py +66 -0
- agentic_auth/sidecar/app.py +349 -0
- agentic_auth/sidecar/config.py +253 -0
- agentic_auth/sidecar/deny.py +108 -0
- agentic_auth/sidecar/enforce.py +640 -0
- agentic_auth/sidecar/identity.py +37 -0
- agentic_auth/verify/__init__.py +208 -0
- agentic_auth/verify/admit.py +131 -0
- agentic_auth/verify/anchors.py +250 -0
- agentic_auth/verify/canonical.py +228 -0
- agentic_auth/verify/grants.py +158 -0
- agentic_auth/verify/identity.py +111 -0
- agentic_auth/verify/replay.py +67 -0
- agentic_auth/verify/transaction.py +164 -0
- authzai-0.2.0.dist-info/METADATA +276 -0
- authzai-0.2.0.dist-info/RECORD +148 -0
- authzai-0.2.0.dist-info/WHEEL +5 -0
- authzai-0.2.0.dist-info/entry_points.txt +5 -0
- authzai-0.2.0.dist-info/licenses/LICENSE +202 -0
- authzai-0.2.0.dist-info/licenses/NOTICE +8 -0
- authzai-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Persistence for ``trust_anchors``.
|
|
2
|
+
|
|
3
|
+
The only update this module performs is setting ``revoked_at`` — once. There is no delete.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import base64
|
|
9
|
+
import uuid
|
|
10
|
+
from datetime import datetime
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from sqlalchemy import func, select, update
|
|
14
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
15
|
+
|
|
16
|
+
from agentic_auth.models import TrustAnchor
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def current(session: AsyncSession, agent_id: uuid.UUID) -> TrustAnchor | None:
|
|
20
|
+
"""The live anchor, or — when the agent is revoked — the most recently revoked one, so
|
|
21
|
+
identity material stays readable after revocation. ``None`` when never enrolled."""
|
|
22
|
+
live = (
|
|
23
|
+
await session.execute(
|
|
24
|
+
select(TrustAnchor).where(
|
|
25
|
+
TrustAnchor.agent_id == agent_id, TrustAnchor.revoked_at.is_(None)
|
|
26
|
+
)
|
|
27
|
+
)
|
|
28
|
+
).scalar_one_or_none()
|
|
29
|
+
if live is not None:
|
|
30
|
+
return live
|
|
31
|
+
return (
|
|
32
|
+
await session.execute(
|
|
33
|
+
select(TrustAnchor)
|
|
34
|
+
.where(TrustAnchor.agent_id == agent_id)
|
|
35
|
+
.order_by(TrustAnchor.version.desc())
|
|
36
|
+
.limit(1)
|
|
37
|
+
)
|
|
38
|
+
).scalar_one_or_none()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def next_version(session: AsyncSession, agent_id: uuid.UUID) -> int:
|
|
42
|
+
highest = (
|
|
43
|
+
await session.execute(
|
|
44
|
+
select(func.max(TrustAnchor.version)).where(TrustAnchor.agent_id == agent_id)
|
|
45
|
+
)
|
|
46
|
+
).scalar_one()
|
|
47
|
+
return (highest or 0) + 1
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
async def append(
|
|
51
|
+
session: AsyncSession,
|
|
52
|
+
*,
|
|
53
|
+
agent_id: uuid.UUID,
|
|
54
|
+
version: int,
|
|
55
|
+
jwks: dict[str, Any],
|
|
56
|
+
key_thumbprints: list[str],
|
|
57
|
+
principal_ref: str,
|
|
58
|
+
manifest_version: int,
|
|
59
|
+
) -> TrustAnchor:
|
|
60
|
+
row = TrustAnchor(
|
|
61
|
+
agent_id=agent_id,
|
|
62
|
+
version=version,
|
|
63
|
+
jwks=jwks,
|
|
64
|
+
key_thumbprints=key_thumbprints,
|
|
65
|
+
principal_ref=principal_ref,
|
|
66
|
+
manifest_version=manifest_version,
|
|
67
|
+
)
|
|
68
|
+
session.add(row)
|
|
69
|
+
await session.flush()
|
|
70
|
+
return row
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
async def revoke_current(
|
|
74
|
+
session: AsyncSession, agent_id: uuid.UUID, *, at: datetime, reason: str | None
|
|
75
|
+
) -> TrustAnchor | None:
|
|
76
|
+
"""Set ``revoked_at`` on the live anchor. Returns the row, or ``None`` if none was live."""
|
|
77
|
+
live = (
|
|
78
|
+
await session.execute(
|
|
79
|
+
select(TrustAnchor).where(
|
|
80
|
+
TrustAnchor.agent_id == agent_id, TrustAnchor.revoked_at.is_(None)
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
).scalar_one_or_none()
|
|
84
|
+
if live is None:
|
|
85
|
+
return None
|
|
86
|
+
await session.execute(
|
|
87
|
+
update(TrustAnchor)
|
|
88
|
+
.where(TrustAnchor.id == live.id)
|
|
89
|
+
.values(revoked_at=at, revoke_reason=reason)
|
|
90
|
+
)
|
|
91
|
+
live.revoked_at = at
|
|
92
|
+
live.revoke_reason = reason
|
|
93
|
+
return live
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
async def revoked_thumbprints(session: AsyncSession, agent_id: uuid.UUID) -> set[str]:
|
|
97
|
+
rows = (
|
|
98
|
+
await session.execute(
|
|
99
|
+
select(TrustAnchor.key_thumbprints).where(
|
|
100
|
+
TrustAnchor.agent_id == agent_id, TrustAnchor.revoked_at.is_not(None)
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
).scalars()
|
|
104
|
+
out: set[str] = set()
|
|
105
|
+
for thumbprints in rows:
|
|
106
|
+
out.update(thumbprints)
|
|
107
|
+
return out
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def encode_cursor(revoked_at: datetime, agent_id: uuid.UUID) -> str:
|
|
111
|
+
raw = f"{revoked_at.isoformat()}|{agent_id}".encode()
|
|
112
|
+
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def decode_cursor(cursor: str) -> tuple[datetime, uuid.UUID]:
|
|
116
|
+
padded = cursor + "=" * (-len(cursor) % 4)
|
|
117
|
+
raw = base64.urlsafe_b64decode(padded).decode()
|
|
118
|
+
ts, agent = raw.split("|", 1)
|
|
119
|
+
return datetime.fromisoformat(ts), uuid.UUID(agent)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
async def list_revocations(
|
|
123
|
+
session: AsyncSession,
|
|
124
|
+
*,
|
|
125
|
+
since: datetime | None,
|
|
126
|
+
limit: int,
|
|
127
|
+
cursor: str | None,
|
|
128
|
+
) -> tuple[list[TrustAnchor], str | None]:
|
|
129
|
+
"""Revoked anchors, oldest first, keyset on ``(revoked_at, agent_id)``."""
|
|
130
|
+
stmt = (
|
|
131
|
+
select(TrustAnchor)
|
|
132
|
+
.where(TrustAnchor.revoked_at.is_not(None))
|
|
133
|
+
.order_by(TrustAnchor.revoked_at, TrustAnchor.agent_id)
|
|
134
|
+
.limit(limit + 1)
|
|
135
|
+
)
|
|
136
|
+
if since is not None:
|
|
137
|
+
stmt = stmt.where(TrustAnchor.revoked_at >= since)
|
|
138
|
+
if cursor:
|
|
139
|
+
after_ts, after_agent = decode_cursor(cursor)
|
|
140
|
+
stmt = stmt.where(
|
|
141
|
+
(TrustAnchor.revoked_at > after_ts)
|
|
142
|
+
| ((TrustAnchor.revoked_at == after_ts) & (TrustAnchor.agent_id > after_agent))
|
|
143
|
+
)
|
|
144
|
+
rows = list((await session.execute(stmt)).scalars().all())
|
|
145
|
+
next_cursor = None
|
|
146
|
+
if len(rows) > limit:
|
|
147
|
+
last = rows[limit - 1]
|
|
148
|
+
assert last.revoked_at is not None
|
|
149
|
+
next_cursor = encode_cursor(last.revoked_at, last.agent_id)
|
|
150
|
+
return rows[:limit], next_cursor
|
agentic_auth/runtime.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Which control plane this code is running for.
|
|
2
|
+
|
|
3
|
+
The control plane keeps its collaborators in process-wide accessors: ``get_settings()``,
|
|
4
|
+
``get_engine()``, ``get_redis()``, ``get_issuer_key()``, ``get_metrics()``. A deployed control
|
|
5
|
+
plane is one per process, and those accessors are exactly right for it.
|
|
6
|
+
|
|
7
|
+
The embedded control plane (``agentic_auth.embedded``) breaks that assumption on purpose:
|
|
8
|
+
two instances in one test process must not share a signing key, a database or an audit log
|
|
9
|
+
(FR-016). So each accessor first asks ``override()`` whether the current task is running on
|
|
10
|
+
behalf of an embedded instance, and only otherwise falls back to the process-wide value.
|
|
11
|
+
|
|
12
|
+
The override travels in a ``ContextVar``, so it follows ``await``, ``asyncio.create_task`` and
|
|
13
|
+
Starlette background tasks, and is set per request by the embedded app's middleware. Outside
|
|
14
|
+
that, it is ``None`` and every accessor behaves exactly as it always has.
|
|
15
|
+
|
|
16
|
+
Stdlib only at import time: ``agentic_auth.config`` imports this module.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import contextlib
|
|
22
|
+
from collections.abc import Iterator
|
|
23
|
+
from contextvars import ContextVar
|
|
24
|
+
from dataclasses import dataclass, field
|
|
25
|
+
from datetime import datetime
|
|
26
|
+
from typing import TYPE_CHECKING, Any, Literal
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
import httpx
|
|
30
|
+
from redis.asyncio import Redis
|
|
31
|
+
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker
|
|
32
|
+
|
|
33
|
+
from agentic_auth.config import Settings
|
|
34
|
+
from agentic_auth.services.metrics import ControlPlaneMetrics
|
|
35
|
+
from agentic_auth.services.tokens.keys import IssuerKey
|
|
36
|
+
|
|
37
|
+
Deployment = Literal["server", "embedded"]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class Runtime:
|
|
42
|
+
"""Everything the process-wide accessors would otherwise hand out, for one instance."""
|
|
43
|
+
|
|
44
|
+
settings: Settings
|
|
45
|
+
engine: AsyncEngine
|
|
46
|
+
sessionmaker: async_sessionmaker[AsyncSession]
|
|
47
|
+
redis: Redis
|
|
48
|
+
issuer_key: IssuerKey
|
|
49
|
+
metrics: ControlPlaneMetrics
|
|
50
|
+
# The one HTTP client this instance uses for outbound calls it makes on its own behalf:
|
|
51
|
+
# capability discovery and the identity provider's JWKS. Routed in-process, never a socket.
|
|
52
|
+
http: httpx.AsyncClient
|
|
53
|
+
deployment: Deployment = "embedded"
|
|
54
|
+
# Sessions created before this moment are refused (SESSION_SUPERSEDED). Set when a
|
|
55
|
+
# durable store is reopened: the taint marks and replay records that protected those
|
|
56
|
+
# sessions lived in memory and are gone, so honouring them would fail open.
|
|
57
|
+
superseded_before: datetime | None = None
|
|
58
|
+
# Built lazily inside the runtime, because constructing one reads get_issuer_key().
|
|
59
|
+
_token_profile: Any = field(default=None, repr=False)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
_current: ContextVar[Runtime | None] = ContextVar("agentic_auth_runtime", default=None)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def override() -> Runtime | None:
|
|
66
|
+
"""The embedded instance the current task runs for, or ``None`` for the process default."""
|
|
67
|
+
return _current.get()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def deployment() -> Deployment:
|
|
71
|
+
current = _current.get()
|
|
72
|
+
return current.deployment if current is not None else "server"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@contextlib.contextmanager
|
|
76
|
+
def use_runtime(runtime: Runtime) -> Iterator[Runtime]:
|
|
77
|
+
token = _current.set(runtime)
|
|
78
|
+
try:
|
|
79
|
+
yield runtime
|
|
80
|
+
finally:
|
|
81
|
+
_current.reset(token)
|
|
File without changes
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Request/response models for agent registration and reads."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import uuid
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
from typing import Literal
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel, ConfigDict
|
|
10
|
+
|
|
11
|
+
from agentic_auth.schemas.manifest import AgentManifest, AgentName
|
|
12
|
+
|
|
13
|
+
ManifestStatus = Literal["reconciled", "needs_review", "rejected"]
|
|
14
|
+
# Phase 4 identity state.
|
|
15
|
+
AgentStatus = Literal["registered", "enrolled", "quarantined", "revoked"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class OverrideDecl(BaseModel):
|
|
19
|
+
model_config = ConfigDict(extra="forbid")
|
|
20
|
+
reason: str
|
|
21
|
+
|
|
22
|
+
def validated_reason(self) -> str:
|
|
23
|
+
if not self.reason.strip():
|
|
24
|
+
raise ValueError("override.reason must not be empty")
|
|
25
|
+
return self.reason
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class AgentCreate(BaseModel):
|
|
29
|
+
model_config = ConfigDict(extra="forbid")
|
|
30
|
+
name: AgentName
|
|
31
|
+
manifest: AgentManifest
|
|
32
|
+
override: OverrideDecl | None = None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class AgentRegistered(BaseModel):
|
|
36
|
+
agent_id: uuid.UUID
|
|
37
|
+
manifest_version: int
|
|
38
|
+
status: ManifestStatus
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AgentRead(BaseModel):
|
|
42
|
+
id: uuid.UUID
|
|
43
|
+
name: str
|
|
44
|
+
status: AgentStatus
|
|
45
|
+
principal_ref: str | None
|
|
46
|
+
trust_anchor_version: int | None
|
|
47
|
+
latest_manifest_version: int | None
|
|
48
|
+
latest_status: ManifestStatus | None
|
|
49
|
+
created_at: datetime
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class AgentListItem(BaseModel):
|
|
53
|
+
id: uuid.UUID
|
|
54
|
+
name: str
|
|
55
|
+
status: AgentStatus
|
|
56
|
+
trust_anchor_version: int | None
|
|
57
|
+
latest_manifest_version: int | None
|
|
58
|
+
latest_status: ManifestStatus | None
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"""The audit plane's read models — see contracts/audit-plane.openapi.yaml.
|
|
2
|
+
|
|
3
|
+
Separate read shapes for the single-action chain and (later) the history list, per
|
|
4
|
+
CLAUDE.md. Nothing here is ever written back: audit data has no create or update schema,
|
|
5
|
+
because nothing creates or updates it through the API.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import uuid
|
|
11
|
+
from datetime import datetime
|
|
12
|
+
from typing import Any, Literal
|
|
13
|
+
|
|
14
|
+
from pydantic import (
|
|
15
|
+
BaseModel,
|
|
16
|
+
ConfigDict,
|
|
17
|
+
Field,
|
|
18
|
+
SerializationInfo,
|
|
19
|
+
SerializerFunctionWrapHandler,
|
|
20
|
+
field_validator,
|
|
21
|
+
model_serializer,
|
|
22
|
+
model_validator,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
from agentic_auth.boundary.guarantee import Guarantee
|
|
26
|
+
from agentic_auth.schemas.authorize import DenyReason
|
|
27
|
+
from agentic_auth.services.audit_chain import ChainBreak
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class RootConsent(BaseModel):
|
|
31
|
+
"""The human at the start of every chain, and what they approved."""
|
|
32
|
+
|
|
33
|
+
model_config = ConfigDict(extra="forbid")
|
|
34
|
+
|
|
35
|
+
user_sub: str
|
|
36
|
+
scope: str
|
|
37
|
+
consented_at: datetime
|
|
38
|
+
session_id: uuid.UUID
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class ChainBreakRead(BaseModel):
|
|
42
|
+
model_config = ConfigDict(extra="forbid")
|
|
43
|
+
|
|
44
|
+
at_hop: int = Field(ge=1, description="The first hop whose link could not be established")
|
|
45
|
+
reason: ChainBreak
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class TransactionFingerprint(BaseModel):
|
|
49
|
+
"""The fingerprint of a privileged call's transaction token: the binding digest
|
|
50
|
+
and lifetime, never the token. Joined from ``transaction_tokens`` on ``action_id``."""
|
|
51
|
+
|
|
52
|
+
model_config = ConfigDict(extra="forbid", from_attributes=True)
|
|
53
|
+
|
|
54
|
+
params_hash: str
|
|
55
|
+
binding: list[str] | None
|
|
56
|
+
issued_at: datetime
|
|
57
|
+
expires_at: datetime
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class ChainHop(BaseModel):
|
|
61
|
+
model_config = ConfigDict(extra="forbid")
|
|
62
|
+
|
|
63
|
+
hop: int = Field(ge=1)
|
|
64
|
+
action_id: uuid.UUID | None
|
|
65
|
+
acting_agent: str | None
|
|
66
|
+
target: str | None
|
|
67
|
+
target_kind: Literal["tool", "agent"] | None
|
|
68
|
+
data_class: str | None
|
|
69
|
+
decision: Literal["allow", "deny"] | None
|
|
70
|
+
deny_reason: DenyReason | None
|
|
71
|
+
enforced: bool | None
|
|
72
|
+
ts: datetime | None
|
|
73
|
+
# Phase 4, additive. Absent on hops written before it (reported as null / false).
|
|
74
|
+
subject_jti: str | None = None
|
|
75
|
+
taint: Literal["trusted_internal", "untrusted_external"] | None = None
|
|
76
|
+
taint_source: str | None = None
|
|
77
|
+
privileged: bool = False
|
|
78
|
+
# Only the queried action's own hop can carry this (the join is on ITS row); null
|
|
79
|
+
# elsewhere and for non-privileged or never-minted calls.
|
|
80
|
+
transaction: TransactionFingerprint | None = None
|
|
81
|
+
# Phase 5, additive. How strong the enforcement actually was for this hop. A row
|
|
82
|
+
# written before the field exists reads as `full` — accurate rather than a guess,
|
|
83
|
+
# because every enforcement point that produced one was per-hop.
|
|
84
|
+
guarantee_level: Guarantee = Guarantee.FULL
|
|
85
|
+
step_up_ref: uuid.UUID | None = None
|
|
86
|
+
# True only on hops of a legacy record whose detail was never captured. Omitted
|
|
87
|
+
# otherwise, so a complete chain carries no field suggesting anything is missing.
|
|
88
|
+
unavailable: bool = False
|
|
89
|
+
|
|
90
|
+
@model_serializer(mode="wrap")
|
|
91
|
+
def _omit_available(
|
|
92
|
+
self, handler: SerializerFunctionWrapHandler, _info: SerializationInfo
|
|
93
|
+
) -> dict[str, Any]:
|
|
94
|
+
data: dict[str, Any] = handler(self)
|
|
95
|
+
if not self.unavailable:
|
|
96
|
+
data.pop("unavailable", None)
|
|
97
|
+
return data
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class ChainRead(BaseModel):
|
|
101
|
+
"""One recorded action's full delegation path, from one row plus its session."""
|
|
102
|
+
|
|
103
|
+
model_config = ConfigDict(extra="forbid", populate_by_name=True)
|
|
104
|
+
|
|
105
|
+
action_id: uuid.UUID
|
|
106
|
+
complete: bool
|
|
107
|
+
# `break` is a Python keyword; the wire name is still `break`.
|
|
108
|
+
chain_break: ChainBreakRead | None = Field(
|
|
109
|
+
default=None, serialization_alias="break", validation_alias="break"
|
|
110
|
+
)
|
|
111
|
+
root_consent: RootConsent
|
|
112
|
+
policy_version: int
|
|
113
|
+
chain: list[ChainHop] = Field(min_length=1)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# -- decision history ---------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class AuditDecisionFilter(BaseModel):
|
|
120
|
+
"""Every filter ``GET /v1/audit/decisions`` accepts. Unknown parameters are refused."""
|
|
121
|
+
|
|
122
|
+
model_config = ConfigDict(extra="forbid")
|
|
123
|
+
|
|
124
|
+
session_id: uuid.UUID | None = None
|
|
125
|
+
agent: str | None = Field(default=None, max_length=255, description="Verified acting agent")
|
|
126
|
+
outcome: Literal["allow", "deny"] | None = None
|
|
127
|
+
deny_reason: DenyReason | None = None
|
|
128
|
+
enforced: bool | None = None
|
|
129
|
+
target_kind: Literal["tool", "agent"] | None = None
|
|
130
|
+
since: datetime | None = None
|
|
131
|
+
until: datetime | None = None
|
|
132
|
+
# Phase 4
|
|
133
|
+
taint: Literal["trusted_internal", "untrusted_external"] | None = None
|
|
134
|
+
privileged: bool | None = None
|
|
135
|
+
# Phase 5. "Show me everything that ran below full guarantee" is the operator question
|
|
136
|
+
# the guarantee level exists to answer, and it needs to be answerable over history, not
|
|
137
|
+
# only as a live counter.
|
|
138
|
+
guarantee_level: Guarantee | None = None
|
|
139
|
+
step_up_ref: uuid.UUID | None = None
|
|
140
|
+
|
|
141
|
+
@field_validator("since", "until")
|
|
142
|
+
@classmethod
|
|
143
|
+
def _timezone_aware(cls, value: datetime | None) -> datetime | None:
|
|
144
|
+
# A naive timestamp would be compared against UTC rows under an assumed zone.
|
|
145
|
+
if value is not None and value.tzinfo is None:
|
|
146
|
+
raise ValueError("must include a timezone")
|
|
147
|
+
return value
|
|
148
|
+
|
|
149
|
+
@model_validator(mode="after")
|
|
150
|
+
def _ordered(self) -> AuditDecisionFilter:
|
|
151
|
+
if self.since is not None and self.until is not None and self.since > self.until:
|
|
152
|
+
raise ValueError("since must not be after until")
|
|
153
|
+
return self
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class AuditDecisionListItem(BaseModel):
|
|
157
|
+
model_config = ConfigDict(extra="forbid", from_attributes=True)
|
|
158
|
+
|
|
159
|
+
id: uuid.UUID
|
|
160
|
+
ts: datetime
|
|
161
|
+
session_id: uuid.UUID
|
|
162
|
+
acting_agent: str | None
|
|
163
|
+
caller: str
|
|
164
|
+
target: str
|
|
165
|
+
target_kind: Literal["tool", "agent"]
|
|
166
|
+
data_class: str
|
|
167
|
+
hop: int
|
|
168
|
+
outcome: Literal["allow", "deny"]
|
|
169
|
+
deny_reason: DenyReason | None
|
|
170
|
+
enforced: bool
|
|
171
|
+
policy_version: int
|
|
172
|
+
chain_complete: bool
|
|
173
|
+
# Phase 4, additive; null / false on rows written before it.
|
|
174
|
+
taint: Literal["trusted_internal", "untrusted_external"] | None = None
|
|
175
|
+
taint_source: str | None = None
|
|
176
|
+
privileged: bool = False
|
|
177
|
+
transaction: TransactionFingerprint | None = None
|
|
178
|
+
# Phase 5, additive. NULL in the column reads as `full` (see ChainHop).
|
|
179
|
+
guarantee_level: Guarantee = Guarantee.FULL
|
|
180
|
+
step_up_ref: uuid.UUID | None = None
|
|
181
|
+
# Additive. `embedded` marks a decision made by an in-process control plane, which is
|
|
182
|
+
# for development and testing only (agentic_auth.embedded).
|
|
183
|
+
deployment: Literal["server", "embedded"] = "server"
|
|
184
|
+
|
|
185
|
+
@field_validator("guarantee_level", mode="before")
|
|
186
|
+
@classmethod
|
|
187
|
+
def _legacy_rows_are_full(cls, value: object) -> object:
|
|
188
|
+
"""A NULL column is a pre-Phase-5 row, and every one of those came from the
|
|
189
|
+
sidecar or the control plane's own path — both per-hop. Reading it as `full` is
|
|
190
|
+
accurate; leaving it null would make honest old rows look suspicious."""
|
|
191
|
+
return Guarantee.FULL if value is None else value
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class AuditPageInfo(BaseModel):
|
|
195
|
+
model_config = ConfigDict(extra="forbid")
|
|
196
|
+
|
|
197
|
+
next_cursor: str | None
|
|
198
|
+
limit: int
|
|
199
|
+
# The high-water mark this iteration is fixed to. Null only when nothing is recorded.
|
|
200
|
+
as_of_seq: int | None
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class AuditDecisionPage(BaseModel):
|
|
204
|
+
model_config = ConfigDict(extra="forbid")
|
|
205
|
+
|
|
206
|
+
data: list[AuditDecisionListItem]
|
|
207
|
+
page: AuditPageInfo
|