-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathclient.py
More file actions
533 lines (478 loc) · 20.6 KB
/
Copy pathclient.py
File metadata and controls
533 lines (478 loc) · 20.6 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
"""Base HTTP client for Agent Control server communication."""
import hashlib
import logging
import os
from collections.abc import Generator
from types import TracebackType
from typing import Any, cast
import httpx
from . import __version__ as sdk_version
from .runtime_auth import (
RuntimeAuthMode,
RuntimeTokenCache,
normalize_runtime_auth_mode,
parse_runtime_token_exchange_response,
)
_logger = logging.getLogger(__name__)
_RUNTIME_AUTH_MODE_ENV_VAR = "AGENT_CONTROL_RUNTIME_AUTH_MODE"
_RUNTIME_TOKEN_HEADER_ENV_VAR = "AGENT_CONTROL_RUNTIME_TOKEN_HEADER"
_DEFAULT_RUNTIME_TOKEN_HEADER = "Authorization"
_DEFAULT_RUNTIME_TOKEN_REFRESH_MARGIN_SECONDS = 30
_AUTO_RUNTIME_TOKEN_FALLBACK_STATUSES = {404, 500, 502, 503, 504}
_GLOBAL_RUNTIME_TOKEN_FALLBACK_STATUSES = {404}
def _runtime_cache_identity(api_key: str | None, api_key_header: str) -> str:
"""Return a stable cache identity without storing the raw credential."""
normalized_header = api_key_header.lower()
if not api_key:
return f"api_key:{normalized_header}:anonymous"
digest = hashlib.sha256(api_key.encode("utf-8")).hexdigest()
return f"api_key:{normalized_header}:{digest}"
class _AgentControlAuth(httpx.Auth):
"""Attach local API-key credentials unless a request already carries a token.
The API key is suppressed when the request already presents a bearer
credential on ``Authorization`` or a runtime token on its dedicated
header, so a runtime-authenticated evaluation carries a single
credential regardless of which header the runtime token rides.
"""
def __init__(
self,
api_key: str | None,
header_name: str = "X-API-Key",
runtime_token_header: str | None = None,
) -> None:
self._api_key = api_key
self._header_name = header_name
self._runtime_token_header = runtime_token_header
def auth_flow(
self,
request: httpx.Request,
) -> Generator[httpx.Request, httpx.Response, None]:
runtime_token_on_dedicated_header = (
self._runtime_token_header is not None
and self._runtime_token_header in request.headers
)
if (
self._api_key
and "Authorization" not in request.headers
and not runtime_token_on_dedicated_header
and self._header_name not in request.headers
):
request.headers[self._header_name] = self._api_key
yield request
class AgentControlClient:
"""
Async HTTP client for Agent Control server.
This is the base client that provides the HTTP connection management.
Specific operations are organized into separate modules:
agents, policies, controls, evaluation.
Authentication:
The client supports API key authentication. By default the key is
sent on the ``X-API-Key`` header; set ``api_key_header`` (or the
``AGENT_CONTROL_API_KEY_HEADER`` environment variable) to override.
API key can be provided:
1. Directly via the `api_key` parameter
2. Via the AGENT_CONTROL_API_KEY environment variable
Usage:
# Explicit API key
async with AgentControlClient(api_key="my-secret-key") as client:
await client.health_check()
# From environment variable
os.environ["AGENT_CONTROL_API_KEY"] = "my-secret-key"
async with AgentControlClient() as client:
await client.health_check()
# Custom header name (e.g., when the upstream auth expects something
# other than X-API-Key). The header name applies to every request
# this client sends.
async with AgentControlClient(
api_key="my-secret-key", api_key_header="X-Custom-API-Key"
) as client:
await client.health_check()
"""
# Environment variable name for API key
API_KEY_ENV_VAR = "AGENT_CONTROL_API_KEY"
API_KEY_HEADER_ENV_VAR = "AGENT_CONTROL_API_KEY_HEADER"
DEFAULT_API_KEY_HEADER = "X-API-Key"
BASE_URL_ENV_VAR = "AGENT_CONTROL_URL"
def __init__(
self,
base_url: str | None = None,
timeout: float = 30.0,
api_key: str | None = None,
api_key_header: str | None = None,
runtime_auth_mode: RuntimeAuthMode | str | None = None,
runtime_token_header: str | None = None,
runtime_token_cache: RuntimeTokenCache | None = None,
runtime_token_refresh_margin_seconds: int = (_DEFAULT_RUNTIME_TOKEN_REFRESH_MARGIN_SECONDS),
transport: httpx.AsyncBaseTransport | None = None,
):
"""
Initialize the client.
Args:
base_url: Base URL of the Agent Control server. If not provided,
AGENT_CONTROL_URL is used, falling back to http://localhost:8000.
timeout: Request timeout in seconds
api_key: API key for authentication. If not provided, will attempt
to read from AGENT_CONTROL_API_KEY environment variable.
api_key_header: HTTP header name to send the API key on. Defaults
to ``X-API-Key``; the AGENT_CONTROL_API_KEY_HEADER
environment variable overrides the default. Useful when
the configured upstream auth expects a different header.
runtime_auth_mode: Runtime auth mode for evaluation requests. ``auto``
attempts target-bound JWT exchange and falls back to normal
request auth when the exchange endpoint is unavailable. ``jwt``
requires a successful exchange. ``api_key`` and ``none`` keep
evaluation requests on the normal request-auth path.
runtime_token_header: HTTP header name to send the runtime token
on. Defaults to ``Authorization``; the
AGENT_CONTROL_RUNTIME_TOKEN_HEADER environment variable
overrides the default. Point this at a dedicated header (e.g.
``X-Agent-Control-Runtime-Token``) when the server runs behind
a gateway that reserves ``Authorization`` for its own identity
JWT. The server must be configured to read the same header.
runtime_token_cache: Optional cache shared across client instances.
runtime_token_refresh_margin_seconds: Refresh cached runtime tokens
before this many seconds of validity remain.
transport: Optional httpx transport, primarily for tests.
"""
resolved_base_url = base_url or os.environ.get(
self.BASE_URL_ENV_VAR, "http://localhost:8000"
)
self.base_url = resolved_base_url.rstrip("/")
self.timeout = timeout
self._api_key = api_key or os.environ.get(self.API_KEY_ENV_VAR)
self._api_key_header = (
api_key_header
or os.environ.get(self.API_KEY_HEADER_ENV_VAR)
or self.DEFAULT_API_KEY_HEADER
)
self._runtime_cache_identity = _runtime_cache_identity(self._api_key, self._api_key_header)
configured_runtime_mode = runtime_auth_mode or os.environ.get(_RUNTIME_AUTH_MODE_ENV_VAR)
self._runtime_auth_mode = normalize_runtime_auth_mode(configured_runtime_mode)
if runtime_token_header is not None and not runtime_token_header.strip():
raise ValueError("runtime_token_header must not be blank.")
# A whitespace-only env var falls back to the default header, matching
# the server's _resolve_runtime_token_header(); an explicit blank
# *param* above is a hard error.
env_runtime_token_header = os.environ.get(_RUNTIME_TOKEN_HEADER_ENV_VAR)
if env_runtime_token_header is not None and not env_runtime_token_header.strip():
env_runtime_token_header = None
self._runtime_token_header = (
runtime_token_header
or env_runtime_token_header
or _DEFAULT_RUNTIME_TOKEN_HEADER
).strip()
# On Authorization the server keeps the ``Bearer`` scheme prefix
# (existing contract); a dedicated header carries the raw token so the
# value no longer collides with a gateway's Authorization JWT.
# Mirror of LocalJwtVerifyProvider._require_bearer on the server — the
# send format and the verifier's expectation must stay in sync.
self._runtime_token_use_bearer = (
self._runtime_token_header.lower() == _DEFAULT_RUNTIME_TOKEN_HEADER.lower()
)
if runtime_token_refresh_margin_seconds < 0:
raise ValueError("runtime_token_refresh_margin_seconds must be >= 0.")
self._runtime_token_refresh_margin_seconds = runtime_token_refresh_margin_seconds
self._runtime_token_cache = runtime_token_cache or RuntimeTokenCache()
self._transport = transport
self._client: httpx.AsyncClient | None = None
self._server_version_warning_emitted = False
@property
def api_key(self) -> str | None:
"""Get the configured API key (read-only)."""
return self._api_key
@property
def api_key_header(self) -> str:
"""Get the header name the API key is sent on (read-only)."""
return self._api_key_header
@property
def runtime_auth_mode(self) -> RuntimeAuthMode:
"""Get the configured runtime auth mode (read-only)."""
return self._runtime_auth_mode
def _get_headers(self) -> dict[str, str]:
"""Build base SDK metadata headers."""
return {
"X-Agent-Control-SDK": "python",
"X-Agent-Control-SDK-Version": sdk_version,
}
async def _check_server_version(self, response: httpx.Response) -> None:
"""Warn once when the server major version differs from the SDK major."""
if self._server_version_warning_emitted:
return
server_version = response.headers.get("X-Agent-Control-Server-Version")
if not server_version:
return
sdk_major = sdk_version.split(".", 1)[0]
server_major = server_version.split(".", 1)[0]
if sdk_major == server_major:
return
_logger.warning(
"Agent Control SDK major version %s is talking to server major version %s. "
"Upgrade the SDK and server together to avoid control-schema mismatches.",
sdk_version,
server_version,
)
self._server_version_warning_emitted = True
async def __aenter__(self) -> "AgentControlClient":
"""Async context manager entry."""
self._client = httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
headers=self._get_headers(),
auth=_AgentControlAuth(
self._api_key,
self._api_key_header,
runtime_token_header=(
None if self._runtime_token_use_bearer else self._runtime_token_header
),
),
transport=self._transport,
event_hooks={"response": [self._check_server_version]},
)
return self
async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Async context manager exit."""
if self._client:
await self._client.aclose()
async def health_check(self) -> dict[str, str]:
"""
Check server health.
Returns:
Dictionary with health status
Raises:
httpx.HTTPError: If request fails
"""
if self._client is None:
raise RuntimeError("Client not initialized. Use 'async with' context manager.")
response = await self._client.get("/health")
response.raise_for_status()
from typing import cast
return cast(dict[str, str], response.json())
@property
def http_client(self) -> httpx.AsyncClient:
"""Get the underlying HTTP client."""
if self._client is None:
raise RuntimeError("Client not initialized. Use 'async with' context manager.")
return self._client
async def post_runtime_evaluation(
self,
*,
json: dict[str, Any],
headers: dict[str, str] | None = None,
target_type: str | None = None,
target_id: str | None = None,
) -> httpx.Response:
"""POST an evaluation request with runtime auth when configured."""
runtime_authorization = await self._runtime_authorization(
target_type=target_type,
target_id=target_id,
)
request_headers = self._merge_runtime_headers(headers, runtime_authorization)
response = await self.http_client.post(
"/api/v1/evaluation",
json=json,
headers=request_headers,
)
if _should_refresh_runtime_token(response) and runtime_authorization is not None:
await response.aread()
if target_type is not None and target_id is not None:
self._runtime_token_cache.remove(
self.base_url,
target_type,
target_id,
cache_identity=self._runtime_cache_identity,
)
runtime_authorization = await self._runtime_authorization(
target_type=target_type,
target_id=target_id,
force_refresh=True,
allow_auto_fallback=False,
)
request_headers = self._merge_runtime_headers(headers, runtime_authorization)
response = await self.http_client.post(
"/api/v1/evaluation",
json=json,
headers=request_headers,
)
if (
_should_refresh_runtime_token(response)
and target_type is not None
and target_id is not None
):
await response.aread()
self._runtime_token_cache.remove(
self.base_url,
target_type,
target_id,
cache_identity=self._runtime_cache_identity,
)
return response
def _format_runtime_token(self, token: str) -> str:
"""Format a runtime token for the configured header.
Prefixes ``Bearer `` only on the ``Authorization`` header (existing
server contract); a dedicated runtime header carries the raw token.
"""
if self._runtime_token_use_bearer:
return f"Bearer {token}"
return token
def _merge_runtime_headers(
self,
headers: dict[str, str] | None,
runtime_authorization: str | None,
) -> dict[str, str] | None:
"""Merge caller headers with an optional runtime token header."""
if headers is None and runtime_authorization is None:
return None
merged = dict(headers or {})
if runtime_authorization is not None:
merged[self._runtime_token_header] = runtime_authorization
return merged
async def _runtime_authorization(
self,
*,
target_type: str | None,
target_id: str | None,
force_refresh: bool = False,
allow_auto_fallback: bool = True,
) -> str | None:
"""Return an Authorization header value for runtime evaluation."""
if self._runtime_auth_mode in {"none", "api_key"}:
return None
if target_type is None or target_id is None:
if self._runtime_auth_mode == "jwt":
raise RuntimeError(
"runtime_auth_mode='jwt' requires target_type and target_id "
"for evaluation requests."
)
return None
if (
self._runtime_auth_mode == "auto"
and not force_refresh
and self._runtime_token_cache.is_jwt_unavailable(
self.base_url,
target_type,
target_id,
cache_identity=self._runtime_cache_identity,
)
):
return None
if not force_refresh:
cached = self._runtime_token_cache.get(
self.base_url,
target_type,
target_id,
cache_identity=self._runtime_cache_identity,
refresh_margin_seconds=self._runtime_token_refresh_margin_seconds,
)
if cached is not None:
return self._format_runtime_token(cached.token)
exchange_lock = self._runtime_token_cache.exchange_lock(
self.base_url,
target_type,
target_id,
cache_identity=self._runtime_cache_identity,
)
async with exchange_lock:
if (
self._runtime_auth_mode == "auto"
and not force_refresh
and self._runtime_token_cache.is_jwt_unavailable(
self.base_url,
target_type,
target_id,
cache_identity=self._runtime_cache_identity,
)
):
return None
if not force_refresh:
cached = self._runtime_token_cache.get(
self.base_url,
target_type,
target_id,
cache_identity=self._runtime_cache_identity,
refresh_margin_seconds=self._runtime_token_refresh_margin_seconds,
)
if cached is not None:
return self._format_runtime_token(cached.token)
token = await self._exchange_runtime_token(
target_type=target_type,
target_id=target_id,
allow_auto_fallback=allow_auto_fallback,
)
if token is None:
return None
return self._format_runtime_token(token)
async def _exchange_runtime_token(
self,
*,
target_type: str,
target_id: str,
allow_auto_fallback: bool = True,
) -> str | None:
"""Exchange the configured credential for a target-bound runtime token."""
try:
response = await self.http_client.post(
"/api/v1/auth/runtime-token-exchange",
json={"target_type": target_type, "target_id": target_id},
)
except httpx.RequestError:
if self._runtime_auth_mode == "auto" and allow_auto_fallback:
_logger.debug(
"Runtime token exchange request failed; falling back to normal request auth "
"for %s/%s.",
target_type,
target_id,
)
self._runtime_token_cache.mark_jwt_unavailable(
server_url=self.base_url,
target_type=target_type,
target_id=target_id,
cache_identity=self._runtime_cache_identity,
)
return None
raise
if (
self._runtime_auth_mode == "auto"
and allow_auto_fallback
and response.status_code in _AUTO_RUNTIME_TOKEN_FALLBACK_STATUSES
):
_logger.debug(
"Runtime token exchange returned HTTP %s; falling back to normal request auth "
"for %s/%s.",
response.status_code,
target_type,
target_id,
)
self._runtime_token_cache.mark_jwt_unavailable(
server_url=self.base_url,
target_type=target_type,
target_id=target_id,
cache_identity=self._runtime_cache_identity,
globally=response.status_code in _GLOBAL_RUNTIME_TOKEN_FALLBACK_STATUSES,
)
return None
response.raise_for_status()
payload = response.json()
if not isinstance(payload, dict):
raise RuntimeError("Runtime token exchange response was not an object.")
token = parse_runtime_token_exchange_response(
cast(dict[str, object], payload),
server_url=self.base_url,
)
if token.target_type != target_type or token.target_id != target_id:
raise RuntimeError(
"Runtime token exchange response target did not match the requested target."
)
self._runtime_token_cache.set(token, cache_identity=self._runtime_cache_identity)
return token.token
def _should_refresh_runtime_token(response: httpx.Response) -> bool:
if response.status_code == 401:
return True
if response.status_code != 403:
return False
authenticate = response.headers.get("WWW-Authenticate", "")
return "invalid_token" in authenticate.lower()