text stringlengths 0 1.47k |
|---|
# cache of the graph key record. The graph stays the source of truth (list/durable); dual-writes on |
# mint/revoke keep this in sync (instant revocation, no TTL lag), and a lazy backfill on first |
# resolve migrates pre-existing keys so Gremlin is never hit on the hot path afterward. |
def _apikey_id(sha: str) -> str: |
return f"apikey::{sha}" |
# Per-item TTL on key docs: NOT the revocation mechanism (dual-write on revoke is, giving instant |
# revocation) — it's a STALENESS BOUND. If a revoke's dual-write ever fails, the stale-valid cache |
# doc expires within this window and the next resolve falls back to the authoritative graph |
# (revoked). An active key is re-backfilled at most once per TTL (one Gremlin read/hour), negligible. |
_APIKEY_TTL_S = int(os.environ.get("HR_APIKEY_TTL_S", "3600")) |
async def apikey_get(sha: str) -> dict | None: |
"""Point-read a key doc by hash. Returns None on miss OR on ANY error (incl. a missing SDK / |
misconfig) — the caller falls back to the graph (the source of truth), so nothing here can |
ever fail-close public auth.""" |
try: |
cex = _exceptions() |
cont = await _container() |
try: |
return await _retryable(lambda: cont.read_item(_apikey_id(sha), partition_key=_apikey_id(sha))) |
except cex.CosmosResourceNotFoundError: |
return None |
except Exception: # noqa: BLE001 — store down / SDK missing → graph fallback |
return None |
async def apikey_put(sha: str, org: str, member: str, workspace: str, |
workspace_default: bool, revoked: bool = False, |
create_only: bool = False) -> None: |
"""Write the key doc, TTL-bounded. mint uses upsert (authoritative fresh key). The lazy |
BACKFILL uses create_only=True: a create_item that 409s (a doc already exists — possibly a |
revoke tombstone written in the race window) leaves it untouched. A plain upsert here would |
resurrect a just-revoked key for up to a TTL (HR-INF-010 review #1).""" |
cont = await _container() |
item = {"id": _apikey_id(sha), "pk": _apikey_id(sha), "kind": "apikey", |
"org": org, "member": member, "workspace": workspace, |
"workspace_default": bool(workspace_default), "revoked": bool(revoked), |
"ts": time.time(), "ttl": _APIKEY_TTL_S} |
if create_only: |
cex = _exceptions() |
try: |
await _retryable(lambda: cont.create_item(item)) |
except cex.CosmosResourceExistsError: |
return # a doc (maybe a tombstone) already exists — never clobber |
else: |
await _retryable(lambda: cont.upsert_item(item)) |
async def apikey_revoke(sha: str) -> None: |
"""Mark a key revoked (dual-write on revoke). Upserts a revoked marker even for a key never |
backfilled, so the hot read returns None immediately without consulting the graph. The revoked |
marker gets a LONGER TTL than a live doc so it can't expire back to a graph re-read before the |
graph revocation is certain to be visible.""" |
cont = await _container() |
cex = _exceptions() |
ttl = max(_APIKEY_TTL_S * 24, 86400) # keep the tombstone well past a live doc's lifetime |
try: |
cur = await _retryable(lambda: cont.read_item(_apikey_id(sha), partition_key=_apikey_id(sha))) |
cur["revoked"] = True |
cur["ttl"] = ttl |
await _retryable(lambda: cont.upsert_item(cur)) |
except cex.CosmosResourceNotFoundError: |
await _retryable(lambda: cont.upsert_item({ |
"id": _apikey_id(sha), "pk": _apikey_id(sha), "kind": "apikey", "revoked": True, |
"ts": time.time(), "ttl": ttl})) |
async def healthcheck() -> bool: |
"""Readiness probe: can we actually reach the control store? Cheap point read |
of a sentinel id (404 is a healthy answer — the store responded).""" |
cont = await _container() |
cex = _exceptions() |
try: |
await cont.read_item("health::probe", partition_key="org::_health") |
except cex.CosmosResourceNotFoundError: |
return True |
return True |
# azure-cosmos uses azure.core.MatchConditions for optimistic concurrency. |
try: # imported lazily-safe so module import never fails when the SDK is absent |
from azure.core import MatchConditions as _MC |
_IF_MATCH = _MC.IfNotModified |
except Exception: # pragma: no cover |
_IF_MATCH = None |
# ===== file: gateway/media_plane.py ===== |
"""Making a picture, a clip, a line of speech — and assembling them into one film. |
Two callers, one engine, one place the provider key is resolved: |
* the `media` MCP server this gateway hosts, so an agent can ask for a CAPABILITY |
(text_to_video, text_to_image, …) and be told which model actually ran; |
* the routes a media app calls to read its canvas, poll its jobs and export a film. |
WHAT NEVER LEAVES THIS PROCESS. The provider key is resolved from the integrations document |
inside the gateway, at the moment of use. The runner never receives it (an agent gets a per- |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.