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,253 @@
|
|
|
1
|
+
"""Sidecar configuration.
|
|
2
|
+
|
|
3
|
+
Startup is deliberately strict. A sidecar that comes up but cannot enforce is worse than
|
|
4
|
+
one that refuses to start, because the first looks healthy while forwarding everything.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from agentic_auth.boundary.guarantee import Guarantee
|
|
16
|
+
|
|
17
|
+
ENV_PREFIX = "AGENTIC_AUTH_SIDECAR_"
|
|
18
|
+
|
|
19
|
+
# Reserved for the enforcement point's own routes (GET /__agentic/metrics, /__agentic/status).
|
|
20
|
+
# `upstream_map` prefixes are integrator-chosen and could legitimately be `/metrics`, so the
|
|
21
|
+
# telemetry routes live under a namespace no sensible upstream mapping would claim — and
|
|
22
|
+
# `validate()` refuses a mapping that would shadow it rather than silently losing either the
|
|
23
|
+
# proxy route or the metrics route.
|
|
24
|
+
RESERVED_PATH_PREFIX = "/__agentic"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class SidecarConfigError(ValueError):
|
|
28
|
+
"""The sidecar cannot be configured to enforce anything."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class Upstream:
|
|
33
|
+
"""One mapped route.
|
|
34
|
+
|
|
35
|
+
``target`` and ``data_class`` are what the policy is expressed in; a URL path alone
|
|
36
|
+
cannot supply them. Either pin them here, or let an SDK-aware agent send them per
|
|
37
|
+
request as ``X-Agentic-Target`` / ``X-Agentic-Data-Class``. If neither produces a
|
|
38
|
+
value the call is blocked — guessing a data class would be inventing consent.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
url: str
|
|
42
|
+
target: str | None = None
|
|
43
|
+
data_class: str | None = None
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def parse(cls, prefix: str, raw: Any) -> Upstream:
|
|
47
|
+
if isinstance(raw, str):
|
|
48
|
+
return cls(url=raw.rstrip("/"))
|
|
49
|
+
if not isinstance(raw, dict) or "url" not in raw:
|
|
50
|
+
raise SidecarConfigError(
|
|
51
|
+
f"upstream_map[{prefix!r}] must be a URL string or an object with a 'url'"
|
|
52
|
+
)
|
|
53
|
+
unknown = set(raw) - {"url", "target", "data_class"}
|
|
54
|
+
if unknown:
|
|
55
|
+
raise SidecarConfigError(
|
|
56
|
+
f"upstream_map[{prefix!r}] has unknown keys: {sorted(unknown)}"
|
|
57
|
+
)
|
|
58
|
+
return cls(
|
|
59
|
+
url=str(raw["url"]).rstrip("/"),
|
|
60
|
+
target=raw.get("target"),
|
|
61
|
+
data_class=raw.get("data_class"),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass(frozen=True)
|
|
66
|
+
class SidecarConfig:
|
|
67
|
+
pdp_url: str
|
|
68
|
+
listen_addr: str = "127.0.0.1:8081"
|
|
69
|
+
upstream_map: dict[str, Upstream] = field(default_factory=dict)
|
|
70
|
+
# Decisions are made and recorded, but a deny does not block. A rollout aid.
|
|
71
|
+
observe_mode: bool = False
|
|
72
|
+
request_timeout_seconds: float = 30.0
|
|
73
|
+
control_plane_timeout_seconds: float = 2.0
|
|
74
|
+
# When set, append one edge event per call (contracts/edge-trace.schema.json). Passive:
|
|
75
|
+
# it never changes what is forwarded or blocked.
|
|
76
|
+
trace_path: str | None = None
|
|
77
|
+
# Phase 4. Largest body the sidecar buffers to bind a privileged call; over the cap is
|
|
78
|
+
# blocked, never forwarded unbound.
|
|
79
|
+
max_privileged_body_bytes: int = 1_048_576
|
|
80
|
+
# Phase 4 identity: both or neither. With a key the sidecar signs an actor_token at every
|
|
81
|
+
# exchange and adds Agent-Identity on every forwarded call.
|
|
82
|
+
agent_name: str | None = None
|
|
83
|
+
identity_key_path: str | None = None
|
|
84
|
+
# Phase 5. What this enforcement point reports on every decision. A sidecar is in the
|
|
85
|
+
# call path and decides every hop, so `full` is accurate. It is configurable only so a
|
|
86
|
+
# deployment that knows it is weaker can say so.
|
|
87
|
+
guarantee_level: str = "full"
|
|
88
|
+
# Phase 6. Per enforcement point, mirroring the control plane's settings —
|
|
89
|
+
# there is deliberately no fleet-wide telemetry switch, for the same reason there is no
|
|
90
|
+
# fleet-wide observe mode.
|
|
91
|
+
telemetry_enabled: bool = False
|
|
92
|
+
telemetry_key: str = ""
|
|
93
|
+
telemetry_identity_disclosure: bool = False
|
|
94
|
+
telemetry_allow_sample_ratio: float = 1.0
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def host(self) -> str:
|
|
98
|
+
return self.listen_addr.rsplit(":", 1)[0]
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def port(self) -> int:
|
|
102
|
+
return int(self.listen_addr.rsplit(":", 1)[1])
|
|
103
|
+
|
|
104
|
+
def resolve(self, path: str) -> tuple[str, Upstream] | None:
|
|
105
|
+
"""Longest-prefix match, so ``/kb/deep`` wins over ``/kb`` when both are mapped."""
|
|
106
|
+
best: tuple[str, Upstream] | None = None
|
|
107
|
+
for prefix, upstream in self.upstream_map.items():
|
|
108
|
+
matches = path == prefix or path.startswith(prefix.rstrip("/") + "/")
|
|
109
|
+
if matches and (best is None or len(prefix) > len(best[0])):
|
|
110
|
+
best = (prefix, upstream)
|
|
111
|
+
return best
|
|
112
|
+
|
|
113
|
+
def validate(self) -> SidecarConfig:
|
|
114
|
+
if not self.pdp_url:
|
|
115
|
+
raise SidecarConfigError("pdp_url is required — a sidecar with no PDP cannot enforce")
|
|
116
|
+
if not self.upstream_map:
|
|
117
|
+
raise SidecarConfigError(
|
|
118
|
+
"upstream_map is required and must be non-empty — an empty map forwards nothing "
|
|
119
|
+
"and silently blocks everything"
|
|
120
|
+
)
|
|
121
|
+
if ":" not in self.listen_addr:
|
|
122
|
+
raise SidecarConfigError(f"listen_addr must be host:port, got {self.listen_addr!r}")
|
|
123
|
+
try:
|
|
124
|
+
_ = self.port
|
|
125
|
+
except ValueError as exc:
|
|
126
|
+
raise SidecarConfigError(
|
|
127
|
+
f"listen_addr port is not a number: {self.listen_addr!r}"
|
|
128
|
+
) from exc
|
|
129
|
+
if bool(self.agent_name) != bool(self.identity_key_path):
|
|
130
|
+
raise SidecarConfigError(
|
|
131
|
+
"agent_name and identity_key_path must be set together — an identity without "
|
|
132
|
+
"a name, or a name without a key, cannot sign anything"
|
|
133
|
+
)
|
|
134
|
+
if self.identity_key_path and not Path(self.identity_key_path).is_file():
|
|
135
|
+
raise SidecarConfigError(f"identity_key_path {self.identity_key_path!r} is not a file")
|
|
136
|
+
if self.max_privileged_body_bytes < 1:
|
|
137
|
+
raise SidecarConfigError("max_privileged_body_bytes must be positive")
|
|
138
|
+
try:
|
|
139
|
+
Guarantee(self.guarantee_level)
|
|
140
|
+
except ValueError as exc:
|
|
141
|
+
raise SidecarConfigError(
|
|
142
|
+
f"guarantee_level must be one of {[g.value for g in Guarantee]}, "
|
|
143
|
+
f"got {self.guarantee_level!r}"
|
|
144
|
+
) from exc
|
|
145
|
+
shadowing = sorted(
|
|
146
|
+
prefix
|
|
147
|
+
for prefix in self.upstream_map
|
|
148
|
+
if prefix == RESERVED_PATH_PREFIX
|
|
149
|
+
or prefix.startswith(RESERVED_PATH_PREFIX + "/")
|
|
150
|
+
or RESERVED_PATH_PREFIX.startswith(prefix.rstrip("/") + "/")
|
|
151
|
+
)
|
|
152
|
+
if shadowing:
|
|
153
|
+
raise SidecarConfigError(
|
|
154
|
+
f"upstream_map prefixes {shadowing} would shadow the reserved "
|
|
155
|
+
f"{RESERVED_PATH_PREFIX!r} namespace, which serves this enforcement point's own "
|
|
156
|
+
"metrics and status; a sidecar whose telemetry is unreachable cannot be "
|
|
157
|
+
"monitored, and one whose proxy route is unreachable cannot enforce"
|
|
158
|
+
)
|
|
159
|
+
if self.telemetry_enabled and not self.telemetry_key:
|
|
160
|
+
raise SidecarConfigError(
|
|
161
|
+
"telemetry_enabled requires telemetry_key — without it, identity pseudonyms "
|
|
162
|
+
"would be either plaintext or an unkeyed digest that is trivially reversible"
|
|
163
|
+
)
|
|
164
|
+
return self
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _from_mapping(raw: dict[str, Any]) -> SidecarConfig:
|
|
168
|
+
unknown = set(raw) - {
|
|
169
|
+
"pdp_url",
|
|
170
|
+
"listen_addr",
|
|
171
|
+
"upstream_map",
|
|
172
|
+
"observe_mode",
|
|
173
|
+
"request_timeout_seconds",
|
|
174
|
+
"control_plane_timeout_seconds",
|
|
175
|
+
"trace_path",
|
|
176
|
+
"max_privileged_body_bytes",
|
|
177
|
+
"agent_name",
|
|
178
|
+
"identity_key_path",
|
|
179
|
+
"guarantee_level",
|
|
180
|
+
"telemetry_enabled",
|
|
181
|
+
"telemetry_key",
|
|
182
|
+
"telemetry_identity_disclosure",
|
|
183
|
+
"telemetry_allow_sample_ratio",
|
|
184
|
+
}
|
|
185
|
+
if unknown:
|
|
186
|
+
raise SidecarConfigError(f"unknown configuration keys: {sorted(unknown)}")
|
|
187
|
+
# `agentic-auth drift record` exports AGENTIC_AUTH_TRACE_PATH to the test run it wraps,
|
|
188
|
+
# so a sidecar started inside that run records even when its config file says nothing.
|
|
189
|
+
trace_path = raw.get("trace_path") or os.environ.get("AGENTIC_AUTH_TRACE_PATH") or None
|
|
190
|
+
return SidecarConfig(
|
|
191
|
+
pdp_url=str(raw.get("pdp_url", "")).rstrip("/"),
|
|
192
|
+
listen_addr=str(raw.get("listen_addr", "127.0.0.1:8081")),
|
|
193
|
+
upstream_map={
|
|
194
|
+
prefix: Upstream.parse(prefix, value)
|
|
195
|
+
for prefix, value in (raw.get("upstream_map") or {}).items()
|
|
196
|
+
},
|
|
197
|
+
observe_mode=bool(raw.get("observe_mode", False)),
|
|
198
|
+
request_timeout_seconds=float(raw.get("request_timeout_seconds", 30.0)),
|
|
199
|
+
control_plane_timeout_seconds=float(raw.get("control_plane_timeout_seconds", 2.0)),
|
|
200
|
+
trace_path=str(trace_path) if trace_path else None,
|
|
201
|
+
max_privileged_body_bytes=int(raw.get("max_privileged_body_bytes", 1_048_576)),
|
|
202
|
+
agent_name=raw.get("agent_name") or None,
|
|
203
|
+
identity_key_path=raw.get("identity_key_path") or None,
|
|
204
|
+
guarantee_level=str(raw.get("guarantee_level") or "full"),
|
|
205
|
+
telemetry_enabled=bool(raw.get("telemetry_enabled", False)),
|
|
206
|
+
telemetry_key=str(raw.get("telemetry_key") or ""),
|
|
207
|
+
telemetry_identity_disclosure=bool(raw.get("telemetry_identity_disclosure", False)),
|
|
208
|
+
telemetry_allow_sample_ratio=float(raw.get("telemetry_allow_sample_ratio", 1.0)),
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def load(path: str | Path | None = None) -> SidecarConfig:
|
|
213
|
+
"""Load from a YAML/JSON file, or from ``AGENTIC_AUTH_SIDECAR_*`` environment vars."""
|
|
214
|
+
if path is not None:
|
|
215
|
+
import yaml
|
|
216
|
+
|
|
217
|
+
raw = yaml.safe_load(Path(path).read_text()) or {}
|
|
218
|
+
if not isinstance(raw, dict):
|
|
219
|
+
raise SidecarConfigError(f"{path}: expected a mapping at the top level")
|
|
220
|
+
return _from_mapping(raw).validate()
|
|
221
|
+
|
|
222
|
+
env_map = os.environ.get(f"{ENV_PREFIX}UPSTREAM_MAP", "")
|
|
223
|
+
try:
|
|
224
|
+
upstream_map = json.loads(env_map) if env_map else {}
|
|
225
|
+
except json.JSONDecodeError as exc:
|
|
226
|
+
raise SidecarConfigError(f"{ENV_PREFIX}UPSTREAM_MAP is not valid JSON: {exc}") from exc
|
|
227
|
+
|
|
228
|
+
return _from_mapping(
|
|
229
|
+
{
|
|
230
|
+
"pdp_url": os.environ.get(f"{ENV_PREFIX}PDP_URL", ""),
|
|
231
|
+
"listen_addr": os.environ.get(f"{ENV_PREFIX}LISTEN_ADDR", "127.0.0.1:8081"),
|
|
232
|
+
"upstream_map": upstream_map,
|
|
233
|
+
"observe_mode": os.environ.get(f"{ENV_PREFIX}OBSERVE_MODE", "").lower()
|
|
234
|
+
in ("1", "true", "yes"),
|
|
235
|
+
"trace_path": os.environ.get(f"{ENV_PREFIX}TRACE_PATH", ""),
|
|
236
|
+
"max_privileged_body_bytes": os.environ.get(
|
|
237
|
+
f"{ENV_PREFIX}MAX_PRIVILEGED_BODY_BYTES", "1048576"
|
|
238
|
+
),
|
|
239
|
+
"agent_name": os.environ.get(f"{ENV_PREFIX}AGENT_NAME", ""),
|
|
240
|
+
"identity_key_path": os.environ.get(f"{ENV_PREFIX}IDENTITY_KEY_PATH", ""),
|
|
241
|
+
"guarantee_level": os.environ.get(f"{ENV_PREFIX}GUARANTEE_LEVEL", "full"),
|
|
242
|
+
"telemetry_enabled": os.environ.get(f"{ENV_PREFIX}TELEMETRY_ENABLED", "").lower()
|
|
243
|
+
in ("1", "true", "yes"),
|
|
244
|
+
"telemetry_key": os.environ.get(f"{ENV_PREFIX}TELEMETRY_KEY", ""),
|
|
245
|
+
"telemetry_identity_disclosure": os.environ.get(
|
|
246
|
+
f"{ENV_PREFIX}TELEMETRY_IDENTITY_DISCLOSURE", ""
|
|
247
|
+
).lower()
|
|
248
|
+
in ("1", "true", "yes"),
|
|
249
|
+
"telemetry_allow_sample_ratio": os.environ.get(
|
|
250
|
+
f"{ENV_PREFIX}TELEMETRY_ALLOW_SAMPLE_RATIO", "1.0"
|
|
251
|
+
),
|
|
252
|
+
}
|
|
253
|
+
).validate()
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""The structured refusal the agent receives.
|
|
2
|
+
|
|
3
|
+
``reason`` and ``recoverable`` are the machine contract — an agent may branch on them.
|
|
4
|
+
``message`` is for humans and may change between versions.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from starlette.responses import JSONResponse
|
|
12
|
+
|
|
13
|
+
from agentic_auth.schemas.authorize import INFRASTRUCTURE_REASONS, DenyReason, is_recoverable
|
|
14
|
+
|
|
15
|
+
# Policy said no (403) vs. we could not ask or could not record (503). Collapsing the two
|
|
16
|
+
# would make "you may not" indistinguishable from "we do not know", and only one of those
|
|
17
|
+
# is worth retrying. The set lives in agentic_auth.schemas.authorize so observe mode, this renderer,
|
|
18
|
+
# and the SDK can never classify a reason differently.
|
|
19
|
+
__all__ = ["INFRASTRUCTURE_REASONS"]
|
|
20
|
+
|
|
21
|
+
UPSTREAM_NOT_MAPPED = "UPSTREAM_NOT_MAPPED"
|
|
22
|
+
TARGET_NOT_RESOLVED = "TARGET_NOT_RESOLVED"
|
|
23
|
+
# Phase 4, local to the sidecar: a privileged call whose body could not be bound.
|
|
24
|
+
BODY_TOO_LARGE_TO_BIND = "BODY_TOO_LARGE_TO_BIND"
|
|
25
|
+
PARAMS_NOT_AN_OBJECT = "PARAMS_NOT_AN_OBJECT"
|
|
26
|
+
# Phase 4, infrastructure: the mint could not be reached or answered unusably (503).
|
|
27
|
+
TRANSACTION_SERVICE_UNAVAILABLE = "TRANSACTION_SERVICE_UNAVAILABLE"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def deny_body(
|
|
31
|
+
reason: str,
|
|
32
|
+
message: str,
|
|
33
|
+
*,
|
|
34
|
+
recoverable: bool = False,
|
|
35
|
+
target: str | None = None,
|
|
36
|
+
hop: int | None = None,
|
|
37
|
+
session_id: str | None = None,
|
|
38
|
+
policy_version: int | None = None,
|
|
39
|
+
) -> dict[str, Any]:
|
|
40
|
+
details: dict[str, Any] = {"reason": reason, "recoverable": recoverable}
|
|
41
|
+
for key, value in (
|
|
42
|
+
("target", target),
|
|
43
|
+
("hop", hop),
|
|
44
|
+
("session_id", session_id),
|
|
45
|
+
("policy_version", policy_version),
|
|
46
|
+
):
|
|
47
|
+
if value is not None:
|
|
48
|
+
details[key] = value
|
|
49
|
+
return {"error": {"code": "policy_denied", "message": message, "details": details}}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def policy_denied(
|
|
53
|
+
reason: DenyReason | str,
|
|
54
|
+
message: str,
|
|
55
|
+
**details: Any,
|
|
56
|
+
) -> JSONResponse:
|
|
57
|
+
reason_value = reason.value if isinstance(reason, DenyReason) else reason
|
|
58
|
+
status = 503 if reason in INFRASTRUCTURE_REASONS else 403
|
|
59
|
+
recoverable = is_recoverable(reason) if isinstance(reason, DenyReason) else False
|
|
60
|
+
return JSONResponse(
|
|
61
|
+
status_code=status,
|
|
62
|
+
content=deny_body(reason_value, message, recoverable=recoverable, **details),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def not_mapped(path: str) -> JSONResponse:
|
|
67
|
+
"""Blocked, not forwarded. An unmapped upstream is a hole in the policy, not a pass."""
|
|
68
|
+
return JSONResponse(
|
|
69
|
+
status_code=403,
|
|
70
|
+
content=deny_body(
|
|
71
|
+
UPSTREAM_NOT_MAPPED,
|
|
72
|
+
f"no upstream mapped for {path!r}; the sidecar forwards only what it is configured to",
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def target_unresolved(path: str) -> JSONResponse:
|
|
78
|
+
return JSONResponse(
|
|
79
|
+
status_code=403,
|
|
80
|
+
content=deny_body(
|
|
81
|
+
TARGET_NOT_RESOLVED,
|
|
82
|
+
f"no target/data_class configured for {path!r} and none supplied by the caller; "
|
|
83
|
+
"refusing rather than guessing a data class",
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def unauthorized(message: str) -> JSONResponse:
|
|
89
|
+
return JSONResponse(
|
|
90
|
+
status_code=401,
|
|
91
|
+
content={"error": {"code": "unauthorized", "message": message}},
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def unbindable(reason: str, message: str, *, target: str) -> JSONResponse:
|
|
96
|
+
"""A privileged call the sidecar cannot bind. Blocked, never forwarded unbound."""
|
|
97
|
+
return JSONResponse(
|
|
98
|
+
status_code=403, content=deny_body(reason, message, recoverable=False, target=target)
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def transaction_unavailable(message: str, *, target: str) -> JSONResponse:
|
|
103
|
+
return JSONResponse(
|
|
104
|
+
status_code=503,
|
|
105
|
+
content=deny_body(
|
|
106
|
+
TRANSACTION_SERVICE_UNAVAILABLE, message, recoverable=False, target=target
|
|
107
|
+
),
|
|
108
|
+
)
|