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
agentic_auth/redis.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Async Redis client — used only as the immutable compiled-DAG cache."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from redis.asyncio import Redis, from_url
|
|
6
|
+
|
|
7
|
+
from agentic_auth import runtime
|
|
8
|
+
from agentic_auth.config import get_settings
|
|
9
|
+
|
|
10
|
+
_client: Redis | None = None
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_redis() -> Redis:
|
|
14
|
+
current = runtime.override()
|
|
15
|
+
if current is not None:
|
|
16
|
+
return current.redis
|
|
17
|
+
global _client
|
|
18
|
+
if _client is None:
|
|
19
|
+
_client = from_url(get_settings().redis_url, decode_responses=True)
|
|
20
|
+
return _client
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def close_redis() -> None:
|
|
24
|
+
global _client
|
|
25
|
+
if _client is not None:
|
|
26
|
+
await _client.aclose()
|
|
27
|
+
_client = None
|
|
File without changes
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Persistence for ``agents`` (+ latest-manifest projections)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import uuid
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from sqlalchemy import func, select
|
|
9
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
10
|
+
|
|
11
|
+
from agentic_auth.models import Agent, Manifest
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def get_by_name(session: AsyncSession, name: str) -> Agent | None:
|
|
15
|
+
return (await session.execute(select(Agent).where(Agent.name == name))).scalar_one_or_none()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def get_by_id(session: AsyncSession, agent_id: uuid.UUID) -> Agent | None:
|
|
19
|
+
return await session.get(Agent, agent_id)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async def create(session: AsyncSession, name: str) -> Agent:
|
|
23
|
+
"""Idempotent create-by-name: returns the existing row if present."""
|
|
24
|
+
existing = await get_by_name(session, name)
|
|
25
|
+
if existing is not None:
|
|
26
|
+
return existing
|
|
27
|
+
agent = Agent(name=name)
|
|
28
|
+
session.add(agent)
|
|
29
|
+
await session.flush()
|
|
30
|
+
return agent
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
async def list_page(
|
|
34
|
+
session: AsyncSession, limit: int, cursor: str | None, *, status: str | None = None
|
|
35
|
+
) -> tuple[list[Agent], str | None]:
|
|
36
|
+
stmt = select(Agent).order_by(Agent.name).limit(limit + 1)
|
|
37
|
+
if cursor:
|
|
38
|
+
stmt = stmt.where(Agent.name > cursor)
|
|
39
|
+
if status is not None:
|
|
40
|
+
stmt = stmt.where(Agent.status == status)
|
|
41
|
+
rows = list((await session.execute(stmt)).scalars().all())
|
|
42
|
+
next_cursor = rows[limit - 1].name if len(rows) > limit else None
|
|
43
|
+
return rows[:limit], next_cursor
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def latest_manifest(session: AsyncSession, agent_id: uuid.UUID) -> Manifest | None:
|
|
47
|
+
stmt = (
|
|
48
|
+
select(Manifest)
|
|
49
|
+
.where(Manifest.agent_id == agent_id)
|
|
50
|
+
.order_by(Manifest.version.desc())
|
|
51
|
+
.limit(1)
|
|
52
|
+
)
|
|
53
|
+
return (await session.execute(stmt)).scalar_one_or_none()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
async def latest_versions(
|
|
57
|
+
session: AsyncSession, agent_ids: list[uuid.UUID]
|
|
58
|
+
) -> dict[uuid.UUID, tuple[int, str]]:
|
|
59
|
+
if not agent_ids:
|
|
60
|
+
return {}
|
|
61
|
+
sub = (
|
|
62
|
+
select(
|
|
63
|
+
Manifest.agent_id,
|
|
64
|
+
func.max(Manifest.version).label("v"),
|
|
65
|
+
)
|
|
66
|
+
.where(Manifest.agent_id.in_(agent_ids))
|
|
67
|
+
.group_by(Manifest.agent_id)
|
|
68
|
+
.subquery()
|
|
69
|
+
)
|
|
70
|
+
stmt = select(Manifest.agent_id, Manifest.version, Manifest.status).join(
|
|
71
|
+
sub, (Manifest.agent_id == sub.c.agent_id) & (Manifest.version == sub.c.v)
|
|
72
|
+
)
|
|
73
|
+
return {row.agent_id: (row.version, row.status) for row in (await session.execute(stmt)).all()}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
async def set_status(
|
|
77
|
+
session: AsyncSession,
|
|
78
|
+
agent: Agent,
|
|
79
|
+
*,
|
|
80
|
+
status: str,
|
|
81
|
+
principal_ref: str | None = None,
|
|
82
|
+
trust_anchor_version: int | None = None,
|
|
83
|
+
pending_identity: dict[str, Any] | None = None,
|
|
84
|
+
) -> Agent:
|
|
85
|
+
"""The one place agent identity state changes. Callers commit."""
|
|
86
|
+
agent.status = status
|
|
87
|
+
agent.principal_ref = principal_ref
|
|
88
|
+
agent.trust_anchor_version = trust_anchor_version
|
|
89
|
+
agent.pending_identity = pending_identity
|
|
90
|
+
await session.flush()
|
|
91
|
+
return agent
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def revoked_names(session: AsyncSession) -> set[str]:
|
|
95
|
+
rows = (await session.execute(select(Agent.name).where(Agent.status == "revoked"))).scalars()
|
|
96
|
+
return set(rows)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""Append-only persistence for ``audit_decisions``.
|
|
2
|
+
|
|
3
|
+
Append and read only. There is deliberately no update or delete function in this module —
|
|
4
|
+
and if one were added, the database would refuse to run it
|
|
5
|
+
(``agentic_auth/models/audit_decision.py``).
|
|
6
|
+
|
|
7
|
+
``append`` takes every chain field explicitly, with no defaults: a row that silently claimed
|
|
8
|
+
a chain state nobody computed would be exactly the kind of record this table exists to rule
|
|
9
|
+
out. :class:`agentic_auth.services.audit.AuditService` is its only caller.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import uuid
|
|
15
|
+
from datetime import datetime
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from sqlalchemy import func, select
|
|
19
|
+
from sqlalchemy.ext.asyncio import AsyncSession as DbSession
|
|
20
|
+
|
|
21
|
+
from agentic_auth.models import AuditDecision
|
|
22
|
+
from agentic_auth.models.audit_decision import target_kind_of
|
|
23
|
+
from agentic_auth.schemas.audit import AuditDecisionFilter
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def append(
|
|
27
|
+
db: DbSession,
|
|
28
|
+
*,
|
|
29
|
+
action_id: uuid.UUID,
|
|
30
|
+
ts: datetime,
|
|
31
|
+
session_id: uuid.UUID,
|
|
32
|
+
caller: str,
|
|
33
|
+
target: str,
|
|
34
|
+
data_class: str,
|
|
35
|
+
hop: int,
|
|
36
|
+
outcome: str,
|
|
37
|
+
deny_reason: str | None,
|
|
38
|
+
recoverable: bool,
|
|
39
|
+
enforced: bool,
|
|
40
|
+
taint: str | None,
|
|
41
|
+
taint_source: str | None,
|
|
42
|
+
privileged: bool,
|
|
43
|
+
policy_version: int,
|
|
44
|
+
act_chain: list[Any],
|
|
45
|
+
acting_agent: str | None,
|
|
46
|
+
subject_jti: str | None,
|
|
47
|
+
parent_action_id: uuid.UUID | None,
|
|
48
|
+
chain: list[dict[str, Any]] | None,
|
|
49
|
+
chain_complete: bool,
|
|
50
|
+
chain_break: str | None,
|
|
51
|
+
guarantee_level: str | None = None,
|
|
52
|
+
deployment: str = "server",
|
|
53
|
+
step_up_ref: uuid.UUID | None = None,
|
|
54
|
+
) -> AuditDecision:
|
|
55
|
+
row = AuditDecision(
|
|
56
|
+
id=action_id,
|
|
57
|
+
ts=ts,
|
|
58
|
+
session_id=session_id,
|
|
59
|
+
caller=caller,
|
|
60
|
+
acting_agent=acting_agent,
|
|
61
|
+
target=target,
|
|
62
|
+
target_kind=target_kind_of(target),
|
|
63
|
+
data_class=data_class,
|
|
64
|
+
hop=hop,
|
|
65
|
+
outcome=outcome,
|
|
66
|
+
deny_reason=deny_reason,
|
|
67
|
+
recoverable=recoverable,
|
|
68
|
+
enforced=enforced,
|
|
69
|
+
taint=taint,
|
|
70
|
+
taint_source=taint_source,
|
|
71
|
+
privileged=privileged,
|
|
72
|
+
policy_version=policy_version,
|
|
73
|
+
act_chain=act_chain,
|
|
74
|
+
subject_jti=subject_jti,
|
|
75
|
+
guarantee_level=guarantee_level,
|
|
76
|
+
deployment=deployment,
|
|
77
|
+
step_up_ref=step_up_ref,
|
|
78
|
+
parent_action_id=parent_action_id,
|
|
79
|
+
chain=chain,
|
|
80
|
+
chain_complete=chain_complete,
|
|
81
|
+
chain_break=chain_break,
|
|
82
|
+
)
|
|
83
|
+
db.add(row)
|
|
84
|
+
await db.flush()
|
|
85
|
+
return row
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
async def get(db: DbSession, action_id: uuid.UUID) -> AuditDecision | None:
|
|
89
|
+
return await db.get(AuditDecision, action_id)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
async def get_committed_allow(db: DbSession, action_id: uuid.UUID) -> AuditDecision | None:
|
|
93
|
+
"""The row a token exchange spends.
|
|
94
|
+
|
|
95
|
+
Called from the exchange's own request, whose session can only see committed rows — so
|
|
96
|
+
a decision whose commit never landed is simply not found here. The caller distinguishes
|
|
97
|
+
"not found" from "found, but a deny".
|
|
98
|
+
"""
|
|
99
|
+
return await db.get(AuditDecision, action_id)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
async def list_page(
|
|
103
|
+
db: DbSession,
|
|
104
|
+
*,
|
|
105
|
+
filters: AuditDecisionFilter,
|
|
106
|
+
limit: int,
|
|
107
|
+
position: tuple[int, int] | None,
|
|
108
|
+
) -> tuple[list[AuditDecision], bool, int | None]:
|
|
109
|
+
"""One page, newest first, by insertion sequence. Returns ``(rows, has_more, hwm)``.
|
|
110
|
+
|
|
111
|
+
``position`` is ``(before, hwm)`` from a cursor, or ``None`` for the first page, which
|
|
112
|
+
fixes the high-water mark. Ordering by ``seq`` rather than ``ts`` matters: ``ts`` is
|
|
113
|
+
taken before commit, so commit order and ``ts`` order can disagree, and a keyset on
|
|
114
|
+
``ts`` can skip a row that commits late.
|
|
115
|
+
"""
|
|
116
|
+
if position is None:
|
|
117
|
+
hwm = await db.scalar(select(func.max(AuditDecision.seq)))
|
|
118
|
+
if hwm is None:
|
|
119
|
+
return [], False, None
|
|
120
|
+
before = hwm + 1
|
|
121
|
+
else:
|
|
122
|
+
before, hwm = position
|
|
123
|
+
|
|
124
|
+
stmt = (
|
|
125
|
+
select(AuditDecision)
|
|
126
|
+
.where(AuditDecision.seq <= hwm, AuditDecision.seq < before)
|
|
127
|
+
.order_by(AuditDecision.seq.desc())
|
|
128
|
+
.limit(limit + 1)
|
|
129
|
+
)
|
|
130
|
+
if filters.session_id is not None:
|
|
131
|
+
stmt = stmt.where(AuditDecision.session_id == filters.session_id)
|
|
132
|
+
if filters.agent is not None:
|
|
133
|
+
stmt = stmt.where(AuditDecision.acting_agent == filters.agent)
|
|
134
|
+
if filters.outcome is not None:
|
|
135
|
+
stmt = stmt.where(AuditDecision.outcome == filters.outcome)
|
|
136
|
+
if filters.deny_reason is not None:
|
|
137
|
+
stmt = stmt.where(AuditDecision.deny_reason == filters.deny_reason.value)
|
|
138
|
+
if filters.enforced is not None:
|
|
139
|
+
stmt = stmt.where(AuditDecision.enforced == filters.enforced)
|
|
140
|
+
if filters.target_kind is not None:
|
|
141
|
+
stmt = stmt.where(AuditDecision.target_kind == filters.target_kind)
|
|
142
|
+
if filters.since is not None:
|
|
143
|
+
stmt = stmt.where(AuditDecision.ts >= filters.since)
|
|
144
|
+
if filters.until is not None:
|
|
145
|
+
stmt = stmt.where(AuditDecision.ts <= filters.until)
|
|
146
|
+
if filters.taint is not None:
|
|
147
|
+
stmt = stmt.where(AuditDecision.taint == filters.taint)
|
|
148
|
+
if filters.privileged is not None:
|
|
149
|
+
stmt = stmt.where(AuditDecision.privileged == filters.privileged)
|
|
150
|
+
|
|
151
|
+
rows = list((await db.execute(stmt)).scalars().all())
|
|
152
|
+
return rows[:limit], len(rows) > limit, hwm
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Append-only persistence for ``compiled_policies`` (versioned per scope)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import uuid
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from sqlalchemy import func, select
|
|
9
|
+
from sqlalchemy.exc import IntegrityError
|
|
10
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
11
|
+
|
|
12
|
+
from agentic_auth.models import CompiledPolicy
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async def append_version(
|
|
16
|
+
session: AsyncSession,
|
|
17
|
+
*,
|
|
18
|
+
scope: str,
|
|
19
|
+
dag: dict[str, Any],
|
|
20
|
+
source_manifest_ids: list[uuid.UUID],
|
|
21
|
+
max_retries: int = 5,
|
|
22
|
+
) -> CompiledPolicy:
|
|
23
|
+
for _ in range(max_retries):
|
|
24
|
+
current = (
|
|
25
|
+
await session.execute(
|
|
26
|
+
select(func.max(CompiledPolicy.version)).where(CompiledPolicy.scope == scope)
|
|
27
|
+
)
|
|
28
|
+
).scalar_one()
|
|
29
|
+
version = (current or 0) + 1
|
|
30
|
+
row = CompiledPolicy(
|
|
31
|
+
scope=scope,
|
|
32
|
+
version=version,
|
|
33
|
+
dag={**dag, "version": version},
|
|
34
|
+
source_manifest_ids=source_manifest_ids,
|
|
35
|
+
)
|
|
36
|
+
try:
|
|
37
|
+
async with session.begin_nested():
|
|
38
|
+
session.add(row)
|
|
39
|
+
await session.flush()
|
|
40
|
+
return row
|
|
41
|
+
except IntegrityError:
|
|
42
|
+
continue
|
|
43
|
+
raise RuntimeError("could not assign a compiled-policy version after retries")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def get_version(session: AsyncSession, scope: str, version: int) -> CompiledPolicy | None:
|
|
47
|
+
return (
|
|
48
|
+
await session.execute(
|
|
49
|
+
select(CompiledPolicy).where(
|
|
50
|
+
CompiledPolicy.scope == scope, CompiledPolicy.version == version
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
).scalar_one_or_none()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
async def get_latest(session: AsyncSession, scope: str) -> CompiledPolicy | None:
|
|
57
|
+
return (
|
|
58
|
+
await session.execute(
|
|
59
|
+
select(CompiledPolicy)
|
|
60
|
+
.where(CompiledPolicy.scope == scope)
|
|
61
|
+
.order_by(CompiledPolicy.version.desc())
|
|
62
|
+
.limit(1)
|
|
63
|
+
)
|
|
64
|
+
).scalar_one_or_none()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def latest_version_number(session: AsyncSession, scope: str) -> int | None:
|
|
68
|
+
return (
|
|
69
|
+
await session.execute(
|
|
70
|
+
select(func.max(CompiledPolicy.version)).where(CompiledPolicy.scope == scope)
|
|
71
|
+
)
|
|
72
|
+
).scalar_one()
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Append-only persistence for ``manifests``.
|
|
2
|
+
|
|
3
|
+
Version numbers are server-assigned, monotonic per agent, and gap-free. The repository
|
|
4
|
+
exposes no update or delete.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import uuid
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from sqlalchemy import func, select
|
|
13
|
+
from sqlalchemy.exc import IntegrityError
|
|
14
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
15
|
+
|
|
16
|
+
from agentic_auth.models import Manifest
|
|
17
|
+
from agentic_auth.models.manifest import STATUS_PROPOSED
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def _next_version(session: AsyncSession, agent_id: uuid.UUID) -> int:
|
|
21
|
+
current = (
|
|
22
|
+
await session.execute(
|
|
23
|
+
select(func.max(Manifest.version)).where(Manifest.agent_id == agent_id)
|
|
24
|
+
)
|
|
25
|
+
).scalar_one()
|
|
26
|
+
return (current or 0) + 1
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
async def append_version(
|
|
30
|
+
session: AsyncSession,
|
|
31
|
+
*,
|
|
32
|
+
agent_id: uuid.UUID,
|
|
33
|
+
doc: dict[str, Any],
|
|
34
|
+
status: str,
|
|
35
|
+
attested_by: str,
|
|
36
|
+
override_reason: str | None,
|
|
37
|
+
max_retries: int = 5,
|
|
38
|
+
) -> Manifest:
|
|
39
|
+
"""Insert the next version for an agent, retrying on a lost race for the number."""
|
|
40
|
+
if not attested_by or not attested_by.strip():
|
|
41
|
+
raise ValueError("attested_by must not be empty")
|
|
42
|
+
|
|
43
|
+
for _ in range(max_retries):
|
|
44
|
+
version = await _next_version(session, agent_id)
|
|
45
|
+
manifest = Manifest(
|
|
46
|
+
agent_id=agent_id,
|
|
47
|
+
version=version,
|
|
48
|
+
doc=doc,
|
|
49
|
+
status=status,
|
|
50
|
+
attested_by=attested_by,
|
|
51
|
+
override_reason=override_reason,
|
|
52
|
+
)
|
|
53
|
+
try:
|
|
54
|
+
async with session.begin_nested():
|
|
55
|
+
session.add(manifest)
|
|
56
|
+
await session.flush()
|
|
57
|
+
return manifest
|
|
58
|
+
except IntegrityError:
|
|
59
|
+
# Lost the race for this version number; recompute and retry.
|
|
60
|
+
continue
|
|
61
|
+
raise RuntimeError("could not assign a manifest version after retries")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
async def get_version(session: AsyncSession, agent_id: uuid.UUID, version: int) -> Manifest | None:
|
|
65
|
+
return (
|
|
66
|
+
await session.execute(
|
|
67
|
+
select(Manifest).where(Manifest.agent_id == agent_id, Manifest.version == version)
|
|
68
|
+
)
|
|
69
|
+
).scalar_one_or_none()
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
async def get_latest(session: AsyncSession, agent_id: uuid.UUID) -> Manifest | None:
|
|
73
|
+
return (
|
|
74
|
+
await session.execute(
|
|
75
|
+
select(Manifest)
|
|
76
|
+
.where(Manifest.agent_id == agent_id)
|
|
77
|
+
.order_by(Manifest.version.desc())
|
|
78
|
+
.limit(1)
|
|
79
|
+
)
|
|
80
|
+
).scalar_one_or_none()
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
async def list_versions(session: AsyncSession, agent_id: uuid.UUID) -> list[Manifest]:
|
|
84
|
+
return list(
|
|
85
|
+
(
|
|
86
|
+
await session.execute(
|
|
87
|
+
select(Manifest).where(Manifest.agent_id == agent_id).order_by(Manifest.version)
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
.scalars()
|
|
91
|
+
.all()
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
async def latest_resolvable(session: AsyncSession, agent_id: uuid.UUID) -> Manifest | None:
|
|
96
|
+
"""The newest manifest that may take part in a decision — never a proposal.
|
|
97
|
+
|
|
98
|
+
``get_latest`` deliberately still returns a ``proposed`` row, because the proposal
|
|
99
|
+
endpoints need to read it. Anything on a decision path calls this instead.
|
|
100
|
+
"""
|
|
101
|
+
return (
|
|
102
|
+
await session.execute(
|
|
103
|
+
select(Manifest)
|
|
104
|
+
.where(Manifest.agent_id == agent_id, Manifest.status != STATUS_PROPOSED)
|
|
105
|
+
.order_by(Manifest.version.desc())
|
|
106
|
+
.limit(1)
|
|
107
|
+
)
|
|
108
|
+
).scalar_one_or_none()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
async def proposed_for(session: AsyncSession, agent_id: uuid.UUID) -> Manifest | None:
|
|
112
|
+
"""The agent's outstanding proposal, if any. At most one is ever open, because a
|
|
113
|
+
ratification flips the row out of ``proposed`` and only one run may be in flight."""
|
|
114
|
+
return (
|
|
115
|
+
await session.execute(
|
|
116
|
+
select(Manifest)
|
|
117
|
+
.where(Manifest.agent_id == agent_id, Manifest.status == STATUS_PROPOSED)
|
|
118
|
+
.order_by(Manifest.version.desc())
|
|
119
|
+
.limit(1)
|
|
120
|
+
)
|
|
121
|
+
).scalar_one_or_none()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
async def proposed_from_run(session: AsyncSession, run_id: uuid.UUID) -> Manifest | None:
|
|
125
|
+
return (
|
|
126
|
+
await session.execute(select(Manifest).where(Manifest.proposed_from_run_id == run_id))
|
|
127
|
+
).scalar_one_or_none()
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
async def all_reconciled(session: AsyncSession) -> list[Manifest]:
|
|
131
|
+
"""Latest reconciled manifest for every agent (input to DAG compilation).
|
|
132
|
+
|
|
133
|
+
**This is where a proposal stops.** The filter below already excludes
|
|
134
|
+
``status = 'proposed'``, so an observation-derived manifest never reaches the
|
|
135
|
+
compiler, no ``compiled_policies`` row exists for it, and therefore
|
|
136
|
+
``SessionService.consent()`` raises before its insert and no session can pin it.
|
|
137
|
+
Inertness is a property of this one predicate, so ``tests/unit/test_proposed_is_inert.py``
|
|
138
|
+
asserts it explicitly rather than trusting that it stays true by accident.
|
|
139
|
+
"""
|
|
140
|
+
sub = (
|
|
141
|
+
select(Manifest.agent_id, func.max(Manifest.version).label("v"))
|
|
142
|
+
.where(Manifest.status == "reconciled")
|
|
143
|
+
.group_by(Manifest.agent_id)
|
|
144
|
+
.subquery()
|
|
145
|
+
)
|
|
146
|
+
stmt = select(Manifest).join(
|
|
147
|
+
sub, (Manifest.agent_id == sub.c.agent_id) & (Manifest.version == sub.c.v)
|
|
148
|
+
)
|
|
149
|
+
return list((await session.execute(stmt)).scalars().all())
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Persistence for ``reconciliation_reports`` (one per manifest submission)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import uuid
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from sqlalchemy import select
|
|
9
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
10
|
+
|
|
11
|
+
from agentic_auth.models import Manifest, ReconciliationReport
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def create(
|
|
15
|
+
session: AsyncSession,
|
|
16
|
+
*,
|
|
17
|
+
manifest_id: uuid.UUID,
|
|
18
|
+
diff: list[dict[str, Any]],
|
|
19
|
+
discovery_ok: bool,
|
|
20
|
+
) -> ReconciliationReport:
|
|
21
|
+
report = ReconciliationReport(manifest_id=manifest_id, diff=diff, discovery_ok=discovery_ok)
|
|
22
|
+
session.add(report)
|
|
23
|
+
await session.flush()
|
|
24
|
+
return report
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
async def get_latest_for_agent(
|
|
28
|
+
session: AsyncSession, agent_id: uuid.UUID
|
|
29
|
+
) -> tuple[ReconciliationReport, Manifest] | None:
|
|
30
|
+
stmt = (
|
|
31
|
+
select(ReconciliationReport, Manifest)
|
|
32
|
+
.join(Manifest, ReconciliationReport.manifest_id == Manifest.id)
|
|
33
|
+
.where(Manifest.agent_id == agent_id)
|
|
34
|
+
.order_by(Manifest.version.desc())
|
|
35
|
+
.limit(1)
|
|
36
|
+
)
|
|
37
|
+
row = (await session.execute(stmt)).first()
|
|
38
|
+
if row is None:
|
|
39
|
+
return None
|
|
40
|
+
return row[0], row[1]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Persistence for ``sessions``.
|
|
2
|
+
|
|
3
|
+
Create and read only — a session is immutable after insert, so there is deliberately no
|
|
4
|
+
update path here for a future edit to reach for.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import uuid
|
|
10
|
+
from datetime import datetime
|
|
11
|
+
|
|
12
|
+
from sqlalchemy.ext.asyncio import AsyncSession as DbSession
|
|
13
|
+
|
|
14
|
+
from agentic_auth.models import Session
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def create(
|
|
18
|
+
db: DbSession,
|
|
19
|
+
*,
|
|
20
|
+
scope: str,
|
|
21
|
+
user_sub: str,
|
|
22
|
+
policy_version: int,
|
|
23
|
+
hop_budget: int,
|
|
24
|
+
expires_at: datetime,
|
|
25
|
+
consented_data_classes: list[str] | None = None,
|
|
26
|
+
) -> Session:
|
|
27
|
+
row = Session(
|
|
28
|
+
scope=scope,
|
|
29
|
+
user_sub=user_sub,
|
|
30
|
+
policy_version=policy_version,
|
|
31
|
+
hop_budget=hop_budget,
|
|
32
|
+
expires_at=expires_at,
|
|
33
|
+
consented_data_classes=consented_data_classes,
|
|
34
|
+
)
|
|
35
|
+
db.add(row)
|
|
36
|
+
await db.flush()
|
|
37
|
+
return row
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
async def get(db: DbSession, session_id: uuid.UUID) -> Session | None:
|
|
41
|
+
return await db.get(Session, session_id)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Append-only persistence for ``transaction_tokens``.
|
|
2
|
+
|
|
3
|
+
Append and read only. ``UNIQUE (action_id)`` turns a second mint for one decision into an
|
|
4
|
+
``IntegrityError`` at ``flush()``; the service maps that to ``409``.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import uuid
|
|
10
|
+
from datetime import datetime
|
|
11
|
+
|
|
12
|
+
from sqlalchemy import select
|
|
13
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
14
|
+
|
|
15
|
+
from agentic_auth.models import TransactionToken
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def append(
|
|
19
|
+
db: AsyncSession,
|
|
20
|
+
*,
|
|
21
|
+
jti: str,
|
|
22
|
+
action_id: uuid.UUID,
|
|
23
|
+
session_id: uuid.UUID,
|
|
24
|
+
target: str,
|
|
25
|
+
params_hash: str,
|
|
26
|
+
binding: list[str] | None,
|
|
27
|
+
issued_at: datetime,
|
|
28
|
+
expires_at: datetime,
|
|
29
|
+
) -> TransactionToken:
|
|
30
|
+
row = TransactionToken(
|
|
31
|
+
jti=jti,
|
|
32
|
+
action_id=action_id,
|
|
33
|
+
session_id=session_id,
|
|
34
|
+
target=target,
|
|
35
|
+
params_hash=params_hash,
|
|
36
|
+
binding=binding,
|
|
37
|
+
issued_at=issued_at,
|
|
38
|
+
expires_at=expires_at,
|
|
39
|
+
)
|
|
40
|
+
db.add(row)
|
|
41
|
+
await db.flush()
|
|
42
|
+
return row
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
async def get_by_action(db: AsyncSession, action_id: uuid.UUID) -> TransactionToken | None:
|
|
46
|
+
return (
|
|
47
|
+
await db.execute(select(TransactionToken).where(TransactionToken.action_id == action_id))
|
|
48
|
+
).scalar_one_or_none()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
async def get_by_actions(
|
|
52
|
+
db: AsyncSession, action_ids: list[uuid.UUID]
|
|
53
|
+
) -> dict[uuid.UUID, TransactionToken]:
|
|
54
|
+
"""The fingerprints for a page of decisions, in one query."""
|
|
55
|
+
if not action_ids:
|
|
56
|
+
return {}
|
|
57
|
+
rows = (
|
|
58
|
+
await db.execute(select(TransactionToken).where(TransactionToken.action_id.in_(action_ids)))
|
|
59
|
+
).scalars()
|
|
60
|
+
return {row.action_id: row for row in rows}
|