diff --git a/agents/gecko-triage/README.md b/agents/gecko-triage/README.md index 17f1a29c2b5..3e054c743c9 100644 --- a/agents/gecko-triage/README.md +++ b/agents/gecko-triage/README.md @@ -82,11 +82,11 @@ flowchart TD The agent never holds a Bugzilla key and never writes. It records actions; hackbot-api applies them as `hackbot@mozilla.tld` and tags every comment `gecko-triage`. -| Mode | Model auth | Bugzilla key | -| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | -| Cloud Run job | Workload Identity Federation, with no Anthropic API key. The deploy sets the `ANTHROPIC_FEDERATION_RULE_ID` and the other ids, and the runtime writes the token file (`libs/hackbot-runtime/hackbot_runtime/anthropic_wif.py`). | A shared broker secret from Secret Manager. The Hackbot deploy in mozilla/webservices-infra sets it. Only the broker holds it. | -| Local, Docker | `ANTHROPIC_API_KEY` in the root `.env` | `BUGZILLA_API_KEY` in the root `.env`, given to the broker only | -| Local, host | `ANTHROPIC_API_KEY` when the shell sets it, else the Claude Code login | `BUGZILLA_API_KEY` in the broker's terminal | +| Mode | Model auth | Bugzilla key | +| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | +| Cloud Run job | Workload Identity Federation, with no Anthropic API key. The deploy sets the `ANTHROPIC_FEDERATION_RULE_ID` and the other ids, and the runtime writes the token file (`libs/hackbot-runtime/hackbot_runtime/wif.py`). | A shared broker secret from Secret Manager. The Hackbot deploy in mozilla/webservices-infra sets it. Only the broker holds it. | +| Local, Docker | `ANTHROPIC_API_KEY` in the root `.env` | `BUGZILLA_API_KEY` in the root `.env`, given to the broker only | +| Local, host | `ANTHROPIC_API_KEY` when the shell sets it, else the Claude Code login | `BUGZILLA_API_KEY` in the broker's terminal | In every mode, the broker serves only read tools. The bot account reads only public bugs, and later the Hackbot Bugzilla proxy controls which bugs a key can read. For a local run, diff --git a/docs/hackbot/security.md b/docs/hackbot/security.md index 6fe2175e998..c8d9625de2a 100644 --- a/docs/hackbot/security.md +++ b/docs/hackbot/security.md @@ -48,17 +48,16 @@ short-lived access token, so none needs a static key in the container. The runtime fetches that token from the GCP metadata server, writes it to a private file the SDK reads, and keeps refreshing it in the background — Google tokens outlive neither a long run nor a single exchange, so the file has to stay fresh for the whole run -([hackbot_runtime/anthropic_wif.py](../../libs/hackbot-runtime/hackbot_runtime/anthropic_wif.py)). +([hackbot_runtime/wif.py](../../libs/hackbot-runtime/hackbot_runtime/wif.py)). Federation is enabled by the presence of `ANTHROPIC_FEDERATION_RULE_ID`. An `ANTHROPIC_API_KEY` set _alongside_ it is refused with an error, because the key would take precedence and silently shadow federation. -OpenAI federation is enabled by the presence of `OPENAI_FEDERATION_RULE_ID` -([hackbot_runtime/openai_wif.py](../../libs/hackbot-runtime/hackbot_runtime/openai_wif.py)), -which exposes the token through `OPENAI_IDENTITY_TOKEN_FILE`. The token's audience defaults -to `https://api.openai.com/v1` and can be overridden with `OPENAI_WIF_AUDIENCE`. Here -WIF has precedence over an `OPENAI_API_KEY`, so a leftover key only logs a warning. +OpenAI federation is enabled by the presence of `OPENAI_FEDERATION_RULE_ID`, which exposes +the token through `OPENAI_IDENTITY_TOKEN_FILE`. The token's audience defaults to +`https://api.openai.com/v1` and can be overridden with `OPENAI_WIF_AUDIENCE`. Here WIF has +precedence over an `OPENAI_API_KEY`, so a leftover key only logs a warning. ## The signed upload policy diff --git a/libs/hackbot-runtime/hackbot_runtime/anthropic_wif.py b/libs/hackbot-runtime/hackbot_runtime/anthropic_wif.py deleted file mode 100644 index 72c77a39c2c..00000000000 --- a/libs/hackbot-runtime/hackbot_runtime/anthropic_wif.py +++ /dev/null @@ -1,217 +0,0 @@ -"""GCP Workload Identity Federation for Anthropic API auth. - -On GCP (Cloud Run, Cloud Functions, GCE, GKE) a workload can fetch a -Google-signed OIDC identity token from the instance metadata server (via the -``google-auth`` library) and exchange it for a short-lived Anthropic access -token, instead of shipping a static ``ANTHROPIC_API_KEY``. The Anthropic SDK and -the Claude Code CLI (which the -claude-agent-sdk spawns) do the exchange and refresh themselves, driven entirely -by environment variables: - - ``ANTHROPIC_FEDERATION_RULE_ID`` federation rule (``fdrl_…``) ── set by deploy - ``ANTHROPIC_ORGANIZATION_ID`` Anthropic org id ── set by deploy - ``ANTHROPIC_SERVICE_ACCOUNT_ID`` target service account ── set by deploy - ``ANTHROPIC_WORKSPACE_ID`` workspace (optional) ── set by deploy - ``ANTHROPIC_IDENTITY_TOKEN_FILE`` path to the Google JWT ── managed here - -The deploy provisions the four federation ids; this module owns the fifth. It -fetches the Google identity token, writes it to a private file, points -``ANTHROPIC_IDENTITY_TOKEN_FILE`` at it, and keeps it fresh in a background -thread — Google identity tokens live ~1h, and the SDK re-reads the file on every -exchange, so a periodically-rewritten file transparently keeps auth alive for -runs longer than a token's lifetime. - -When ``ANTHROPIC_FEDERATION_RULE_ID`` is absent the runtime is in API-key mode -and this module is inert, so local/compose runs keep using ``ANTHROPIC_API_KEY``. - -See https://platform.claude.com/docs/en/manage-claude/wif-providers/gcp -""" - -import contextlib -import logging -import os -import tempfile -import threading -from pathlib import Path - -import google.auth.transport.requests -import google.oauth2.id_token -from google.auth.exceptions import GoogleAuthError - -from hackbot_runtime.providers import ProviderError - -log = logging.getLogger("hackbot_runtime.anthropic_wif") - -# Audience the metadata token is minted for; must match the federation rule's -# ``audience`` matcher on the Anthropic side. -ANTHROPIC_AUDIENCE = "https://api.anthropic.com" - -# Presence of the federation rule id is what flips the runtime from API-key mode -# to WIF mode — the deploy sets it only on federation-enabled workloads. -RULE_ID_ENV = "ANTHROPIC_FEDERATION_RULE_ID" -TOKEN_FILE_ENV = "ANTHROPIC_IDENTITY_TOKEN_FILE" -REFRESH_INTERVAL_ENV = "ANTHROPIC_IDENTITY_TOKEN_REFRESH_SECONDS" - -# Google identity tokens expire after ~1h; refresh well inside that window so -# the file always carries plenty of remaining lifetime for the SDK to exchange. -DEFAULT_REFRESH_INTERVAL = 1800 - -# Set once configure() succeeds; keeps the daemon refresher alive for the -# process lifetime and makes a second configure() call a no-op. -_refresher: _TokenFileRefresher | None = None - - -def is_enabled() -> bool: - """True when the deploy provisioned a federation rule (WIF mode).""" - return bool(os.environ.get(RULE_ID_ENV)) - - -def fetch_gcp_identity_token(audience: str = ANTHROPIC_AUDIENCE) -> str: - """Fetch a Google-signed OIDC identity token for ``audience``. - - Delegates to ``google.oauth2.id_token``, which sources the token from the - GCE/Cloud Run/GKE metadata server (or a ``GOOGLE_APPLICATION_CREDENTIALS`` - service-account file for local testing). On the metadata path it requests - ``format=full``, so the token carries the ``email`` claim the federation rule - matches on — without it the exchange fails with ``invalid_grant``. - """ - request = google.auth.transport.requests.Request() - try: - token = google.oauth2.id_token.fetch_id_token(request, audience) - except GoogleAuthError as exc: - # Surface provider-credential failures as a single ProviderError type so - # callers (and the refresh loop) handle them uniformly. - raise ProviderError( - f"Failed to fetch a Google identity token for Anthropic federation: {exc}" - ) from exc - if not token: - raise ProviderError( - "Google returned an empty identity token; the workload has no usable " - "service account." - ) - return token - - -def _refresh_interval() -> float: - raw = os.environ.get(REFRESH_INTERVAL_ENV) - if not raw: - return DEFAULT_REFRESH_INTERVAL - try: - value = float(raw) - except ValueError: - log.warning( - "%s=%r is not a number; falling back to %ss", - REFRESH_INTERVAL_ENV, - raw, - DEFAULT_REFRESH_INTERVAL, - ) - return DEFAULT_REFRESH_INTERVAL - if value <= 0: - log.warning( - "%s=%s is not positive; falling back to %ss", - REFRESH_INTERVAL_ENV, - raw, - DEFAULT_REFRESH_INTERVAL, - ) - return DEFAULT_REFRESH_INTERVAL - return value - - -def _default_token_path() -> Path: - """A private, process-owned path for the identity token file. - - ``mkdtemp`` gives a 0700 directory so the bearer token isn't world-readable. - """ - return Path(tempfile.mkdtemp(prefix="anthropic-wif-")) / "identity-token" - - -class _TokenFileRefresher: - """Keeps ``token_file`` populated with a fresh Google identity token. - - The first write happens synchronously in :meth:`start` so a metadata-server - failure surfaces immediately (fail fast) rather than as an opaque auth error - once the agent is mid-run. Subsequent writes run on a daemon thread. - """ - - def __init__( - self, token_file: Path, interval: float, audience: str = ANTHROPIC_AUDIENCE - ): - self._token_file = token_file - self._interval = interval - self._audience = audience - self._stop = threading.Event() - self._thread = threading.Thread( - target=self._loop, name="anthropic-wif-refresh", daemon=True - ) - - def _write_token(self) -> None: - """Atomically replace the token file so the SDK never reads a partial write.""" - token = fetch_gcp_identity_token(self._audience) - self._token_file.parent.mkdir(parents=True, exist_ok=True) - fd, tmp = tempfile.mkstemp(dir=self._token_file.parent, prefix=".identity-") - try: - with os.fdopen(fd, "w", encoding="utf-8") as handle: - handle.write(token) - os.replace(tmp, self._token_file) - except BaseException: - with contextlib.suppress(OSError): - os.unlink(tmp) - raise - - def _loop(self) -> None: - # wait() returns True only when stop() is set, so the loop exits cleanly - # on shutdown and otherwise re-writes the token every interval. - while not self._stop.wait(self._interval): - try: - self._write_token() - except (ProviderError, OSError) as exc: - # A transient metadata blip is survivable: the previous token is - # still on disk and valid for a while, so log and keep the loop - # alive to retry on the next tick rather than crashing the thread. - log.warning( - "Failed to refresh Anthropic WIF identity token; " - "serving the previously written token: %s", - exc, - ) - - def start(self) -> None: - self._write_token() - self._thread.start() - - def stop(self) -> None: - self._stop.set() - - -def configure() -> bool: - """Wire up Anthropic WIF auth for the Claude SDK/CLI when the deploy enables it. - - Returns ``True`` when WIF was configured, ``False`` in API-key mode. Idempotent: - a second call while the refresher is already running is a no-op. - """ - global _refresher - if not is_enabled(): - return False - if _refresher is not None: - return True - - if os.environ.get("ANTHROPIC_API_KEY"): - log.error( - "ANTHROPIC_API_KEY is set while Workload Identity Federation is " - "configured; the API key takes precedence and shadows WIF. Unset it " - "if you intend to authenticate via federation." - ) - return False - - token_file = Path(os.environ.get(TOKEN_FILE_ENV) or _default_token_path()) - interval = _refresh_interval() - refresher = _TokenFileRefresher(token_file, interval) - refresher.start() - os.environ[TOKEN_FILE_ENV] = str(token_file) - _refresher = refresher - log.info( - "Anthropic auth: GCP Workload Identity Federation " - "(identity token file %s, refresh every %ss)", - token_file, - interval, - ) - return True diff --git a/libs/hackbot-runtime/hackbot_runtime/openai_wif.py b/libs/hackbot-runtime/hackbot_runtime/openai_wif.py deleted file mode 100644 index 23726e982be..00000000000 --- a/libs/hackbot-runtime/hackbot_runtime/openai_wif.py +++ /dev/null @@ -1,223 +0,0 @@ -"""GCP Workload Identity Federation for OpenAI API auth. - -On GCP (Cloud Run, Cloud Functions, GCE, GKE) a workload can fetch a -Google-signed OIDC identity token from the instance metadata server (via the -``google-auth`` library) and exchange it for a short-lived OpenAI access -token, instead of shipping a static ``OPENAI_API_KEY``. The Codex CLI does the -exchange and refresh itself, driven entirely by environment variables: - - ``OPENAI_FEDERATION_RULE_ID`` federation rule (``idpm_…``) ── set by deploy - ``OPENAI_WIF_AUDIENCE`` audience override (optional) ── set by deploy - ``OPENAI_IDENTITY_TOKEN_REFRESH_SECONDS`` refresh cadence (optional) ── set by deploy - ``OPENAI_IDENTITY_TOKEN_FILE`` path to the Google JWT ── managed here - -The deploy provisions the federation rule id; this module owns the token file. It -fetches the Google identity token, writes it to a private file, points -``OPENAI_IDENTITY_TOKEN_FILE`` at it, and keeps it fresh in a background thread — -Google identity tokens live ~1h, and Codex re-reads the file whenever it needs -another OpenAI access token, so a periodically-rewritten file transparently keeps -auth alive for runs longer than a token's lifetime. - -When ``OPENAI_FEDERATION_RULE_ID`` is absent the runtime is in API-key mode and -this module is inert, so local/compose runs keep using ``OPENAI_API_KEY``. - -See https://developers.openai.com/api/docs/guides/workload-identity-federation -""" - -from __future__ import annotations - -import contextlib -import logging -import os -import tempfile -import threading -from pathlib import Path - -import google.auth.transport.requests -import google.oauth2.id_token -from google.auth.exceptions import GoogleAuthError - -from hackbot_runtime.providers import ProviderError - -log = logging.getLogger("hackbot_runtime.openai_wif") - -# Default audience the metadata token is minted for; must match the federation -# rule's audience on the OpenAI side. Overridable via ``AUDIENCE_ENV``. -OPENAI_AUDIENCE = "https://api.openai.com/v1" - -# Presence of the federation rule id is what flips the runtime from API-key mode -# to WIF mode — the deploy sets it only on federation-enabled workloads. -RULE_ID_ENV = "OPENAI_FEDERATION_RULE_ID" -AUDIENCE_ENV = "OPENAI_WIF_AUDIENCE" -TOKEN_FILE_ENV = "OPENAI_IDENTITY_TOKEN_FILE" -REFRESH_INTERVAL_ENV = "OPENAI_IDENTITY_TOKEN_REFRESH_SECONDS" - -# Google identity tokens expire after ~1h; refresh well inside that window so -# the file always carries plenty of remaining lifetime for Codex to exchange. -DEFAULT_REFRESH_INTERVAL = 1800 - -# Set once configure() succeeds; keeps the daemon refresher alive for the -# process lifetime and makes a second configure() call a no-op. -_refresher: _TokenFileRefresher | None = None - - -def is_enabled() -> bool: - """True when the deploy provisioned a federation rule (WIF mode).""" - return bool(os.environ.get(RULE_ID_ENV)) - - -def _audience() -> str: - return os.environ.get(AUDIENCE_ENV) or OPENAI_AUDIENCE - - -def fetch_gcp_identity_token(audience: str = OPENAI_AUDIENCE) -> str: - """Fetch a Google-signed OIDC identity token for ``audience``. - - Delegates to ``google.oauth2.id_token``, which sources the token from the - GCE/Cloud Run/GKE metadata server (or a ``GOOGLE_APPLICATION_CREDENTIALS`` - service-account file for local testing). On the metadata path it requests - ``format=full``, so the token carries the ``email`` claim a federation rule - can match on. - """ - request = google.auth.transport.requests.Request() - try: - token = google.oauth2.id_token.fetch_id_token(request, audience) - except GoogleAuthError as exc: - # Surface provider-credential failures as a single ProviderError type so - # callers (and the refresh loop) handle them uniformly. - raise ProviderError( - f"Failed to fetch a Google identity token for OpenAI federation: {exc}" - ) from exc - if not token: - raise ProviderError( - "Google returned an empty identity token; the workload has no usable " - "service account." - ) - return token - - -def _refresh_interval() -> float: - raw = os.environ.get(REFRESH_INTERVAL_ENV) - if not raw: - return DEFAULT_REFRESH_INTERVAL - try: - value = float(raw) - except ValueError: - log.warning( - "%s=%r is not a number; falling back to %ss", - REFRESH_INTERVAL_ENV, - raw, - DEFAULT_REFRESH_INTERVAL, - ) - return DEFAULT_REFRESH_INTERVAL - if value <= 0: - log.warning( - "%s=%s is not positive; falling back to %ss", - REFRESH_INTERVAL_ENV, - raw, - DEFAULT_REFRESH_INTERVAL, - ) - return DEFAULT_REFRESH_INTERVAL - return value - - -def _default_token_path() -> Path: - """A private, process-owned path for the identity token file. - - ``mkdtemp`` gives a 0700 directory so the bearer token isn't world-readable. - The path is absolute, as Codex requires. - """ - return Path(tempfile.mkdtemp(prefix="openai-wif-")) / "identity-token" - - -class _TokenFileRefresher: - """Keeps ``token_file`` populated with a fresh Google identity token. - - The first write happens synchronously in :meth:`start` so a metadata-server - failure surfaces immediately (fail fast) rather than as an opaque auth error - once the agent is mid-run. Subsequent writes run on a daemon thread. - """ - - def __init__( - self, token_file: Path, interval: float, audience: str = OPENAI_AUDIENCE - ): - self._token_file = token_file - self._interval = interval - self._audience = audience - self._stop = threading.Event() - self._thread = threading.Thread( - target=self._loop, name="openai-wif-refresh", daemon=True - ) - - def _write_token(self) -> None: - """Atomically replace the token file so Codex never reads a partial write.""" - token = fetch_gcp_identity_token(self._audience) - self._token_file.parent.mkdir(parents=True, exist_ok=True) - fd, tmp = tempfile.mkstemp(dir=self._token_file.parent, prefix=".identity-") - try: - with os.fdopen(fd, "w", encoding="utf-8") as handle: - handle.write(token) - os.replace(tmp, self._token_file) - except BaseException: - with contextlib.suppress(OSError): - os.unlink(tmp) - raise - - def _loop(self) -> None: - # wait() returns True only when stop() is set, so the loop exits cleanly - # on shutdown and otherwise re-writes the token every interval. - while not self._stop.wait(self._interval): - try: - self._write_token() - except (ProviderError, OSError) as exc: - # A transient metadata blip is survivable: the previous token is - # still on disk and valid for a while, so log and keep the loop - # alive to retry on the next tick rather than crashing the thread. - log.warning( - "Failed to refresh OpenAI WIF identity token; " - "serving the previously written token: %s", - exc, - ) - - def start(self) -> None: - self._write_token() - self._thread.start() - - def stop(self) -> None: - self._stop.set() - - -def configure() -> bool: - """Wire up OpenAI WIF auth for Codex when the deploy enables it. - - Returns ``True`` when WIF was configured, ``False`` in API-key mode. Idempotent: - a second call while the refresher is already running is a no-op. - """ - global _refresher - if not is_enabled(): - return False - if _refresher is not None: - return True - - if os.environ.get("OPENAI_API_KEY"): - log.warning( - "OPENAI_API_KEY is set while Workload Identity Federation is " - "configured; Codex will use federation instead. Unset " - "OPENAI_API_KEY to keep a long-lived key out of the container." - ) - - token_file = Path(os.environ.get(TOKEN_FILE_ENV) or _default_token_path()) - interval = _refresh_interval() - audience = _audience() - refresher = _TokenFileRefresher(token_file, interval, audience) - refresher.start() - os.environ[TOKEN_FILE_ENV] = str(token_file) - _refresher = refresher - log.info( - "OpenAI auth: GCP Workload Identity Federation " - "(audience %s, identity token file %s, refresh every %ss)", - audience, - token_file, - interval, - ) - return True diff --git a/libs/hackbot-runtime/hackbot_runtime/runtime.py b/libs/hackbot-runtime/hackbot_runtime/runtime.py index 4cecbb629cd..268b7f579c7 100644 --- a/libs/hackbot-runtime/hackbot_runtime/runtime.py +++ b/libs/hackbot-runtime/hackbot_runtime/runtime.py @@ -11,7 +11,7 @@ import sentry_sdk from pydantic import ValidationError -from hackbot_runtime import anthropic_wif, openai_wif, wandb_wif +from hackbot_runtime import wif from hackbot_runtime.config import HackbotConfig, load_config from hackbot_runtime.context import HackbotContext from hackbot_runtime.results import HackbotAgentResult @@ -43,12 +43,8 @@ def _configure_auth() -> None: Workload Identity Federation, so in deployment none needs a long-lived key in the agent container; each falls back to its API-key env var locally. """ - if anthropic_wif.configure(): - log.info("Configured Anthropic WIF authentication") - if openai_wif.configure(): - log.info("Configured OpenAI WIF authentication") - if wandb_wif.configure(): - log.info("Configured W&B WIF authentication") + for settings in wif.PROVIDERS: + wif.configure(settings.from_env()) def _configure_sentry(entrypoint: Callable) -> None: diff --git a/libs/hackbot-runtime/hackbot_runtime/tracing.py b/libs/hackbot-runtime/hackbot_runtime/tracing.py index a5c101b2aab..790b09a699d 100644 --- a/libs/hackbot-runtime/hackbot_runtime/tracing.py +++ b/libs/hackbot-runtime/hackbot_runtime/tracing.py @@ -4,7 +4,7 @@ runtime before an agent's ``main()`` runs captures every query, model response, and tool call as a trace with no per-agent instrumentation. It authenticates either from ``WANDB_API_KEY`` (local/dev) or, in deployment, from the short-lived -identity token that :mod:`hackbot_runtime.wandb_wif` writes -- so the agent +identity token that :mod:`hackbot_runtime.wif` writes -- so the agent container needs no long-lived W&B credential. Tracing is opt-in: it activates only when one of those is present, and never fails the run if Weave can't start. @@ -26,7 +26,7 @@ from opentelemetry import trace as otel_trace from opentelemetry.sdk.trace import SpanProcessor, TracerProvider -from hackbot_runtime import wandb_wif +from hackbot_runtime import wif if TYPE_CHECKING: from opentelemetry.context import Context @@ -68,12 +68,11 @@ def resolve_agent_name(entrypoint: Callable) -> str: def _init_weave() -> bool: """Initialize Weave when credentials are available; return whether enabled. - Credentials come from ``WANDB_API_KEY`` (local) or the identity token file set - by :mod:`wandb_wif` (deployment via federation). No-op when neither is present. + Credentials come from ``WANDB_API_KEY`` (local) or the identity token file + :mod:`wif` keeps fresh (deployment via federation). No-op when neither is present. """ - if not ( - os.environ.get("WANDB_API_KEY") or os.environ.get(wandb_wif.TOKEN_FILE_ENV) - ): + wandb = wif.WandbWifSettings.from_env() + if not (wandb.api_key or wandb.identity_token_file): return False project = os.environ.get("WEAVE_PROJECT", DEFAULT_WEAVE_PROJECT) diff --git a/libs/hackbot-runtime/hackbot_runtime/wandb_wif.py b/libs/hackbot-runtime/hackbot_runtime/wandb_wif.py deleted file mode 100644 index 379b9e2f228..00000000000 --- a/libs/hackbot-runtime/hackbot_runtime/wandb_wif.py +++ /dev/null @@ -1,202 +0,0 @@ -"""GCP Workload Identity Federation for Weights & Biases (Weave) auth. - -W&B identity federation lets the wandb/weave SDK authenticate from a short-lived -JWT instead of a long-lived ``WANDB_API_KEY``: the SDK reads a JWT from the file -named by ``WANDB_IDENTITY_TOKEN_FILE`` and exchanges it for a W&B access token. On -GCP we mint that JWT as a Google-signed OIDC identity token (from the metadata -server, via ``google-auth``), so the agent container holds no W&B credential at -all -- the same shape as :mod:`hackbot_runtime.anthropic_wif`. - - ``WANDB_FEDERATION_AUDIENCE`` audience for the Google JWT ── set by deploy - ``WANDB_IDENTITY_TOKEN_FILE`` path to the Google JWT ── managed here - ``WANDB_IDENTITY_TOKEN_REFRESH_SECONDS`` refresh cadence (optional) ── set by deploy - -The deploy provisions the audience (which must match the W&B org's federation -trust config); this module owns the token file. It fetches the identity token, -writes it to a private file, points ``WANDB_IDENTITY_TOKEN_FILE`` at it, and keeps -it fresh in a background thread — Google identity tokens live ~1h and the SDK -re-reads the file when it re-exchanges, so a periodically-rewritten file keeps -auth alive for runs longer than a token's lifetime. - -When ``WANDB_FEDERATION_AUDIENCE`` is absent the runtime is in API-key mode and -this module is inert, so local/compose runs keep using ``WANDB_API_KEY``. - -See https://docs.wandb.ai/platform/hosting/iam/identity_federation -""" - -import contextlib -import logging -import os -import tempfile -import threading -from pathlib import Path - -import google.auth.transport.requests -import google.oauth2.id_token -from google.auth.exceptions import GoogleAuthError - -from hackbot_runtime.providers import ProviderError - -log = logging.getLogger("hackbot_runtime.wandb_wif") - -# Presence of the audience is what flips the runtime from API-key mode to WIF -# mode — the deploy sets it only on federation-enabled workloads. Its value is -# the audience the Google identity token is minted for, which must match the -# W&B org's federation trust configuration. -AUDIENCE_ENV = "WANDB_FEDERATION_AUDIENCE" -TOKEN_FILE_ENV = "WANDB_IDENTITY_TOKEN_FILE" -REFRESH_INTERVAL_ENV = "WANDB_IDENTITY_TOKEN_REFRESH_SECONDS" - -# Google identity tokens expire after ~1h; refresh well inside that window so -# the file always carries plenty of remaining lifetime for the SDK to exchange. -DEFAULT_REFRESH_INTERVAL = 1800 - -# Set once configure() succeeds; keeps the daemon refresher alive for the -# process lifetime and makes a second configure() call a no-op. -_refresher: _TokenFileRefresher | None = None - - -def is_enabled() -> bool: - """True when the deploy provisioned a federation audience (WIF mode).""" - return bool(os.environ.get(AUDIENCE_ENV)) - - -def fetch_gcp_identity_token(audience: str) -> str: - """Fetch a Google-signed OIDC identity token for ``audience``. - - Delegates to ``google.oauth2.id_token``, which sources the token from the - GCE/Cloud Run/GKE metadata server (or a ``GOOGLE_APPLICATION_CREDENTIALS`` - service-account file for local testing). - """ - request = google.auth.transport.requests.Request() - try: - token = google.oauth2.id_token.fetch_id_token(request, audience) - except GoogleAuthError as exc: - raise ProviderError( - f"Failed to fetch a Google identity token for W&B federation: {exc}" - ) from exc - if not token: - raise ProviderError( - "Google returned an empty identity token; the workload has no usable " - "service account." - ) - return token - - -def _refresh_interval() -> float: - raw = os.environ.get(REFRESH_INTERVAL_ENV) - if not raw: - return DEFAULT_REFRESH_INTERVAL - try: - value = float(raw) - except ValueError: - log.warning( - "%s=%r is not a number; falling back to %ss", - REFRESH_INTERVAL_ENV, - raw, - DEFAULT_REFRESH_INTERVAL, - ) - return DEFAULT_REFRESH_INTERVAL - if value <= 0: - log.warning( - "%s=%s is not positive; falling back to %ss", - REFRESH_INTERVAL_ENV, - raw, - DEFAULT_REFRESH_INTERVAL, - ) - return DEFAULT_REFRESH_INTERVAL - return value - - -def _default_token_path() -> Path: - """A private, process-owned path for the identity token file. - - ``mkdtemp`` gives a 0700 directory so the bearer token isn't world-readable. - """ - return Path(tempfile.mkdtemp(prefix="wandb-wif-")) / "identity-token" - - -class _TokenFileRefresher: - """Keeps ``token_file`` populated with a fresh Google identity token. - - The first write happens synchronously in :meth:`start` so a metadata-server - failure surfaces immediately (fail fast) rather than as an opaque auth error - once the agent is mid-run. Subsequent writes run on a daemon thread. - """ - - def __init__(self, token_file: Path, interval: float, audience: str): - self._token_file = token_file - self._interval = interval - self._audience = audience - self._stop = threading.Event() - self._thread = threading.Thread( - target=self._loop, name="wandb-wif-refresh", daemon=True - ) - - def _write_token(self) -> None: - """Atomically replace the token file so the SDK never reads a partial write.""" - token = fetch_gcp_identity_token(self._audience) - self._token_file.parent.mkdir(parents=True, exist_ok=True) - fd, tmp = tempfile.mkstemp(dir=self._token_file.parent, prefix=".identity-") - try: - with os.fdopen(fd, "w", encoding="utf-8") as handle: - handle.write(token) - os.replace(tmp, self._token_file) - except BaseException: - with contextlib.suppress(OSError): - os.unlink(tmp) - raise - - def _loop(self) -> None: - while not self._stop.wait(self._interval): - try: - self._write_token() - except (ProviderError, OSError) as exc: - log.warning( - "Failed to refresh W&B WIF identity token; " - "serving the previously written token: %s", - exc, - ) - - def start(self) -> None: - self._write_token() - self._thread.start() - - def stop(self) -> None: - self._stop.set() - - -def configure() -> bool: - """Wire up W&B WIF auth for the weave SDK when the deploy enables it. - - Returns ``True`` when WIF was configured, ``False`` in API-key mode. Idempotent: - a second call while the refresher is already running is a no-op. - """ - global _refresher - if not is_enabled(): - return False - if _refresher is not None: - return True - - if os.environ.get("WANDB_API_KEY"): - log.error( - "WANDB_API_KEY is set while Workload Identity Federation is configured; " - "the API key takes precedence and shadows WIF. Unset it if you intend " - "to authenticate via federation." - ) - return False - - audience = os.environ[AUDIENCE_ENV] - token_file = Path(os.environ.get(TOKEN_FILE_ENV) or _default_token_path()) - interval = _refresh_interval() - refresher = _TokenFileRefresher(token_file, interval, audience) - refresher.start() - os.environ[TOKEN_FILE_ENV] = str(token_file) - _refresher = refresher - log.info( - "W&B auth: GCP Workload Identity Federation " - "(identity token file %s, refresh every %ss)", - token_file, - interval, - ) - return True diff --git a/libs/hackbot-runtime/hackbot_runtime/wif.py b/libs/hackbot-runtime/hackbot_runtime/wif.py new file mode 100644 index 00000000000..9ab781b3b84 --- /dev/null +++ b/libs/hackbot-runtime/hackbot_runtime/wif.py @@ -0,0 +1,365 @@ +"""GCP Workload Identity Federation for model-provider and observability auth. + +On GCP (Cloud Run, Cloud Functions, GCE, GKE) a workload can fetch a +Google-signed OIDC identity token from the instance metadata server (via the +``google-auth`` library) and exchange it for a short-lived provider access token, +instead of shipping a static API key. Anthropic (Claude SDK/CLI), OpenAI (Codex +CLI) and W&B (wandb/weave SDK) all support this, and each client does the +exchange and refresh itself, driven entirely by environment variables: + + Anthropic + ``ANTHROPIC_FEDERATION_RULE_ID`` federation rule (``fdrl_…``) ── set by deploy + ``ANTHROPIC_ORGANIZATION_ID`` Anthropic org id ── set by deploy + ``ANTHROPIC_SERVICE_ACCOUNT_ID`` target service account ── set by deploy + ``ANTHROPIC_WORKSPACE_ID`` workspace (optional) ── set by deploy + ``ANTHROPIC_IDENTITY_TOKEN_REFRESH_SECONDS`` refresh cadence (optional) ── set by deploy + ``ANTHROPIC_IDENTITY_TOKEN_FILE`` path to the Google JWT ── managed here + + OpenAI + ``OPENAI_FEDERATION_RULE_ID`` federation rule (``idpm_…``) ── set by deploy + ``OPENAI_WIF_AUDIENCE`` audience override (optional) ── set by deploy + ``OPENAI_IDENTITY_TOKEN_REFRESH_SECONDS`` refresh cadence (optional) ── set by deploy + ``OPENAI_IDENTITY_TOKEN_FILE`` path to the Google JWT ── managed here + + W&B + ``WANDB_FEDERATION_AUDIENCE`` audience for the Google JWT ── set by deploy + ``WANDB_IDENTITY_TOKEN_REFRESH_SECONDS`` refresh cadence (optional) ── set by deploy + ``WANDB_IDENTITY_TOKEN_FILE`` path to the Google JWT ── managed here + +The deploy provisions the federation config; this module owns the token file. It +fetches the Google identity token, writes it to the token-file env var's path +(or, when that's unset, to a private temp file it then points the env var at), +and keeps it fresh in a background thread — Google identity tokens live ~1h, +and the clients re-read the file on every exchange, so a periodically-rewritten +file transparently keeps auth alive for runs longer than a token's lifetime. + +When a provider's enabling env var is absent the runtime is in API-key mode and +:func:`configure` is inert for it, so local/compose runs keep using the API key. +""" + +import contextlib +import logging +import os +import tempfile +import threading +from pathlib import Path +from typing import Annotated, Any, ClassVar, Protocol + +import google.auth.transport.requests +import google.oauth2.id_token +from google.auth.exceptions import GoogleAuthError +from pydantic import ( + BaseModel, + Field, + PositiveFloat, + ValidationError, + ValidatorFunctionWrapHandler, + field_validator, +) +from pydantic_settings import BaseSettings, SettingsConfigDict + +from hackbot_runtime.providers import ProviderError + +log = logging.getLogger("hackbot_runtime.wif") + +# Google identity tokens expire after ~1h; refresh well inside that window so +# the file always carries plenty of remaining lifetime for the client to exchange. +DEFAULT_REFRESH_INTERVAL = 1800 + + +class WifConfig(Protocol): + """How one provider's client is wired up for federation, with values resolved.""" + + # Actual name for logs and errors, e.g. "Anthropic". + name: ClassVar[str] + # Short identifier for the token directory prefix and refresher thread name. + provider_id: ClassVar[str] + token_file_env: ClassVar[str] + # Whether a static API key takes precedence over federation in the client. + # If so, a leftover key would silently shadow WIF, so configure() refuses. + api_key_shadows_wif: ClassVar[bool] + + @property + def enabled(self) -> bool: + """True when the deploy provisioned federation for this provider.""" + ... + + @property + def audience(self) -> str | None: ... + + @property + def identity_token_file(self) -> Path | None: ... + + @property + def identity_token_refresh_seconds(self) -> float: ... + + @property + def api_key(self) -> str | None: ... + + +class _WifSettings(BaseModel): + """Base model for settings shared by all WIF providers.""" + + identity_token_file: Path | None = None + identity_token_refresh_seconds: PositiveFloat = DEFAULT_REFRESH_INTERVAL + api_key: str | None = None + + @field_validator("identity_token_refresh_seconds", mode="wrap") + @classmethod + def _fall_back_on_bad_interval( + cls, value: Any, handler: ValidatorFunctionWrapHandler + ) -> float: + try: + return handler(value) + except ValidationError: + log.warning( + "%sIDENTITY_TOKEN_REFRESH_SECONDS=%r is not a positive number; " + "falling back to %ss", + cls.model_config.get("env_prefix", ""), + value, + DEFAULT_REFRESH_INTERVAL, + ) + return DEFAULT_REFRESH_INTERVAL + + +# See https://platform.claude.com/docs/en/manage-claude/wif-providers/gcp +class AnthropicWifSettings(_WifSettings): + name: ClassVar[str] = "Anthropic" + provider_id: ClassVar[str] = "anthropic" + token_file_env: ClassVar[str] = "ANTHROPIC_IDENTITY_TOKEN_FILE" + api_key_shadows_wif: ClassVar[bool] = True + audience: ClassVar[str] = "https://api.anthropic.com" + + federation_rule_id: str | None = None + + @property + def enabled(self) -> bool: + return bool(self.federation_rule_id) + + @classmethod + def from_env(cls) -> AnthropicWifSettings: + return _AnthropicWifEnvSettings() + + +class _AnthropicWifEnvSettings(AnthropicWifSettings, BaseSettings): + model_config = SettingsConfigDict( + env_prefix="ANTHROPIC_", env_ignore_empty=True, extra="ignore" + ) + + +# See https://developers.openai.com/api/docs/guides/workload-identity-federation +class OpenAIWifSettings(_WifSettings): + name: ClassVar[str] = "OpenAI" + provider_id: ClassVar[str] = "openai" + token_file_env: ClassVar[str] = "OPENAI_IDENTITY_TOKEN_FILE" + api_key_shadows_wif: ClassVar[bool] = False + + federation_rule_id: str | None = None + audience: Annotated[str, Field(validation_alias="OPENAI_WIF_AUDIENCE")] = ( + "https://api.openai.com/v1" + ) + + @property + def enabled(self) -> bool: + return bool(self.federation_rule_id) + + @classmethod + def from_env(cls) -> OpenAIWifSettings: + return _OpenAIWifEnvSettings() + + +class _OpenAIWifEnvSettings(OpenAIWifSettings, BaseSettings): + model_config = SettingsConfigDict( + env_prefix="OPENAI_", env_ignore_empty=True, extra="ignore" + ) + + +# See https://docs.wandb.ai/platform/hosting/iam/identity_federation +class WandbWifSettings(_WifSettings): + name: ClassVar[str] = "W&B" + provider_id: ClassVar[str] = "wandb" + token_file_env: ClassVar[str] = "WANDB_IDENTITY_TOKEN_FILE" + api_key_shadows_wif: ClassVar[bool] = True + + audience: Annotated[ + str | None, Field(validation_alias="WANDB_FEDERATION_AUDIENCE") + ] = None + + @property + def enabled(self) -> bool: + return bool(self.audience) + + @classmethod + def from_env(cls) -> WandbWifSettings: + return _WandbWifEnvSettings() + + +class _WandbWifEnvSettings(WandbWifSettings, BaseSettings): + model_config = SettingsConfigDict( + env_prefix="WANDB_", env_ignore_empty=True, extra="ignore" + ) + + +PROVIDERS = ( + AnthropicWifSettings, + OpenAIWifSettings, + WandbWifSettings, +) + +# Populated once configure() succeeds for a provider (keyed by provider_id); +# keeps the daemon refresher alive for the process lifetime and makes a second +# configure() call a no-op. +_refreshers: dict[str, TokenFileRefresher] = {} + + +def fetch_gcp_identity_token(audience: str) -> str: + """Fetch a Google-signed OIDC identity token for ``audience``. + + Delegates to ``google.oauth2.id_token``, which sources the token from the + GCE/Cloud Run/GKE metadata server (or a ``GOOGLE_APPLICATION_CREDENTIALS`` + service-account file for local testing). On the metadata path it requests + ``format=full``, so the token carries the ``email`` claim federation rules + match on — without it the Anthropic exchange fails with ``invalid_grant``. + """ + request = google.auth.transport.requests.Request() + try: + token = google.oauth2.id_token.fetch_id_token(request, audience) + except GoogleAuthError as exc: + # Surface provider-credential failures as a single ProviderError type so + # callers (and the refresh loop) handle them uniformly. + raise ProviderError( + f"Failed to fetch a Google identity token for audience {audience}: {exc}" + ) from exc + if not token: + raise ProviderError( + "Google returned an empty identity token; the workload has no usable " + "service account." + ) + return token + + +def _default_token_path(provider_id: str) -> Path: + """A private, process-owned, absolute path for the identity token file. + + ``mkdtemp`` gives a 0700 directory so the bearer token isn't world-readable. + The path is absolute, as Codex requires. + """ + return Path(tempfile.mkdtemp(prefix=f"{provider_id}-wif-")) / "identity-token" + + +class TokenFileRefresher: + """Keeps ``token_file`` populated with a fresh Google identity token. + + The first write happens synchronously in :meth:`start` so a metadata-server + failure surfaces immediately (fail fast) rather than as an opaque auth error + once the agent is mid-run. Subsequent writes run on a daemon thread. + """ + + def __init__( + self, + token_file: Path, + interval: float, + audience: str, + provider_id: str, + name: str, + ): + self._token_file = token_file + self._interval = interval + self._audience = audience + self._name = name + self._stop = threading.Event() + self._thread = threading.Thread( + target=self._loop, name=f"{provider_id}-wif-refresh", daemon=True + ) + + def _write_token(self) -> None: + """Atomically replace the token file so clients never read a partial write.""" + token = fetch_gcp_identity_token(self._audience) + self._token_file.parent.mkdir(parents=True, exist_ok=True) + fd, tmp = tempfile.mkstemp(dir=self._token_file.parent, prefix=".identity-") + try: + with os.fdopen(fd, "w", encoding="utf-8") as handle: + handle.write(token) + os.replace(tmp, self._token_file) + except BaseException: + with contextlib.suppress(OSError): + os.unlink(tmp) + raise + + def _loop(self) -> None: + # wait() returns True only when stop() is set, so the loop exits cleanly + # on shutdown and otherwise re-writes the token every interval. + while not self._stop.wait(self._interval): + try: + self._write_token() + except (ProviderError, OSError) as exc: + # A transient metadata blip is survivable: the previous token is + # still on disk and valid for a while, so log and keep the loop + # alive to retry on the next tick rather than crashing the thread. + log.warning( + "Failed to refresh %s WIF identity token; " + "serving the previously written token: %s", + self._name, + exc, + ) + + def start(self) -> None: + self._write_token() + self._thread.start() + + def stop(self) -> None: + self._stop.set() + + +def configure(config: WifConfig) -> bool: + """Wire up WIF auth for ``config``'s provider client when the deploy enables it. + + Returns ``True`` when WIF was configured, ``False`` in API-key mode. Idempotent: + a second call while the provider's refresher is already running is a no-op. + """ + if not config.enabled: + return False + if config.provider_id in _refreshers: + return True + + if config.api_key: + if config.api_key_shadows_wif: + log.error( + "A %s API key is set while Workload Identity Federation is " + "configured; the API key takes precedence and shadows WIF. Unset " + "it if you intend to authenticate via federation.", + config.name, + ) + return False + log.warning( + "A %s API key is set while Workload Identity Federation is " + "configured; federation will be used instead. Unset it to keep a " + "long-lived key out of the container.", + config.name, + ) + + if not config.audience: + raise ProviderError(f"No {config.name} federation audience is configured.") + token_file = config.identity_token_file or _default_token_path(config.provider_id) + interval = config.identity_token_refresh_seconds + refresher = TokenFileRefresher( + token_file, interval, config.audience, config.provider_id, config.name + ) + refresher.start() + if config.identity_token_file is None: + os.environ[config.token_file_env] = str(token_file) + log.info( + "%s is not set. Using a temporary identity token file at %s", + config.token_file_env, + token_file, + ) + _refreshers[config.provider_id] = refresher + log.info( + "%s auth: GCP Workload Identity Federation " + "(audience %s, identity token file %s, refresh every %ss)", + config.name, + config.audience, + token_file, + interval, + ) + return True diff --git a/libs/hackbot-runtime/tests/test_anthropic_wif.py b/libs/hackbot-runtime/tests/test_anthropic_wif.py deleted file mode 100644 index bc41237609f..00000000000 --- a/libs/hackbot-runtime/tests/test_anthropic_wif.py +++ /dev/null @@ -1,207 +0,0 @@ -"""Tests for GCP Workload Identity Federation auth setup.""" - -import logging -import os - -import pytest -from google.auth.exceptions import GoogleAuthError -from hackbot_runtime import anthropic_wif -from hackbot_runtime.providers import ProviderError - - -@pytest.fixture(autouse=True) -def _reset_module_state(): - """Stop any refresher a test started and clear the module-level singleton.""" - yield - if anthropic_wif._refresher is not None: - anthropic_wif._refresher.stop() - anthropic_wif._refresher = None - - -@pytest.fixture -def _no_federation_env(monkeypatch): - for var in ( - anthropic_wif.RULE_ID_ENV, - anthropic_wif.TOKEN_FILE_ENV, - anthropic_wif.REFRESH_INTERVAL_ENV, - ): - monkeypatch.delenv(var, raising=False) - - -def test_is_enabled_follows_rule_id(monkeypatch): - monkeypatch.delenv(anthropic_wif.RULE_ID_ENV, raising=False) - assert anthropic_wif.is_enabled() is False - monkeypatch.setenv(anthropic_wif.RULE_ID_ENV, "fdrl_abc") - assert anthropic_wif.is_enabled() is True - - -def test_configure_is_inert_without_federation(_no_federation_env, monkeypatch): - called = False - - def _should_not_fetch(*_a, **_k): - nonlocal called - called = True - return "tok" - - monkeypatch.setattr(anthropic_wif, "fetch_gcp_identity_token", _should_not_fetch) - - assert anthropic_wif.configure() is False - assert called is False - assert anthropic_wif.TOKEN_FILE_ENV not in os.environ - - -def test_configure_writes_token_and_sets_env(_no_federation_env, monkeypatch, tmp_path): - token_file = tmp_path / "identity-token" - monkeypatch.setenv(anthropic_wif.RULE_ID_ENV, "fdrl_abc") - monkeypatch.setenv(anthropic_wif.TOKEN_FILE_ENV, str(token_file)) - monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) - monkeypatch.setattr( - anthropic_wif, "fetch_gcp_identity_token", lambda *a, **k: "google.jwt.token" - ) - - assert anthropic_wif.configure() is True - - assert token_file.read_text() == "google.jwt.token" - assert os.environ[anthropic_wif.TOKEN_FILE_ENV] == str(token_file) - - -def test_configure_refuses_when_api_key_set( - _no_federation_env, monkeypatch, tmp_path, caplog -): - monkeypatch.setenv(anthropic_wif.RULE_ID_ENV, "fdrl_abc") - monkeypatch.setenv(anthropic_wif.TOKEN_FILE_ENV, str(tmp_path / "identity-token")) - monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-leftover") - monkeypatch.setattr( - anthropic_wif, "fetch_gcp_identity_token", lambda *a, **k: "tok" - ) - - with caplog.at_level(logging.ERROR, logger=anthropic_wif.log.name): - assert anthropic_wif.configure() is False - - # A key set alongside federation is flagged as an error and left untouched. - assert any( - rec.levelno == logging.ERROR and "ANTHROPIC_API_KEY" in rec.message - for rec in caplog.records - ) - assert os.environ["ANTHROPIC_API_KEY"] == "sk-leftover" - assert anthropic_wif._refresher is None - - -def test_configure_is_idempotent(_no_federation_env, monkeypatch, tmp_path): - monkeypatch.setenv(anthropic_wif.RULE_ID_ENV, "fdrl_abc") - monkeypatch.setenv(anthropic_wif.TOKEN_FILE_ENV, str(tmp_path / "tok")) - monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) - - calls = 0 - - def _fetch(*_a, **_k): - nonlocal calls - calls += 1 - return "tok" - - monkeypatch.setattr(anthropic_wif, "fetch_gcp_identity_token", _fetch) - - assert anthropic_wif.configure() is True - assert anthropic_wif.configure() is True - # Second call short-circuits: no new refresher, no extra fetch. - assert calls == 1 - - -def test_configure_defaults_token_path_when_unset(_no_federation_env, monkeypatch): - monkeypatch.setenv(anthropic_wif.RULE_ID_ENV, "fdrl_abc") - monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) - monkeypatch.setattr( - anthropic_wif, "fetch_gcp_identity_token", lambda *a, **k: "tok" - ) - - assert anthropic_wif.configure() is True - - path = os.environ[anthropic_wif.TOKEN_FILE_ENV] - assert "anthropic-wif-" in path - assert os.path.exists(path) - - -def test_fetch_uses_google_auth_with_audience(monkeypatch): - captured = {} - - def _fake_fetch(request, audience): - captured["request"] = request - captured["audience"] = audience - return "signed.jwt.value" - - monkeypatch.setattr( - anthropic_wif.google.oauth2.id_token, "fetch_id_token", _fake_fetch - ) - - token = anthropic_wif.fetch_gcp_identity_token() - - assert token == "signed.jwt.value" - assert captured["audience"] == anthropic_wif.ANTHROPIC_AUDIENCE - assert captured["request"] is not None - - -def test_fetch_wraps_google_auth_error(monkeypatch): - def _raise(*_a, **_k): - raise GoogleAuthError("metadata server unreachable") - - monkeypatch.setattr(anthropic_wif.google.oauth2.id_token, "fetch_id_token", _raise) - with pytest.raises(ProviderError, match="Failed to fetch a Google identity token"): - anthropic_wif.fetch_gcp_identity_token() - - -def test_fetch_rejects_empty(monkeypatch): - monkeypatch.setattr( - anthropic_wif.google.oauth2.id_token, "fetch_id_token", lambda *a, **k: "" - ) - with pytest.raises(ProviderError, match="empty identity token"): - anthropic_wif.fetch_gcp_identity_token() - - -@pytest.mark.parametrize( - "raw, expected", - [ - (None, anthropic_wif.DEFAULT_REFRESH_INTERVAL), - ("900", 900), - ("not-a-number", anthropic_wif.DEFAULT_REFRESH_INTERVAL), - ("0", anthropic_wif.DEFAULT_REFRESH_INTERVAL), - ("-5", anthropic_wif.DEFAULT_REFRESH_INTERVAL), - ], -) -def test_refresh_interval_parsing(monkeypatch, raw, expected): - if raw is None: - monkeypatch.delenv(anthropic_wif.REFRESH_INTERVAL_ENV, raising=False) - else: - monkeypatch.setenv(anthropic_wif.REFRESH_INTERVAL_ENV, raw) - assert anthropic_wif._refresh_interval() == expected - - -def test_token_file_write_is_atomic_and_clean(monkeypatch, tmp_path): - token_file = tmp_path / "identity-token" - monkeypatch.setattr( - anthropic_wif, "fetch_gcp_identity_token", lambda *a, **k: "the.jwt" - ) - refresher = anthropic_wif._TokenFileRefresher(token_file, interval=1800) - - refresher._write_token() - - assert token_file.read_text() == "the.jwt" - # No leftover temp files from the atomic replace. - assert list(tmp_path.iterdir()) == [token_file] - - -def test_refresh_loop_survives_provider_error(monkeypatch, tmp_path): - refresher = anthropic_wif._TokenFileRefresher(tmp_path / "tok", interval=0) - calls = [] - - def _boom(*_a, **_k): - calls.append(1) - # Stop after the first failure so the loop exits on its next tick. - refresher.stop() - raise ProviderError("transient") - - monkeypatch.setattr(anthropic_wif, "fetch_gcp_identity_token", _boom) - - # A ProviderError mid-run is logged and swallowed, not raised out of _loop. - refresher._loop() - - assert calls == [1] diff --git a/libs/hackbot-runtime/tests/test_openai_wif.py b/libs/hackbot-runtime/tests/test_openai_wif.py deleted file mode 100644 index 6fece20c0a3..00000000000 --- a/libs/hackbot-runtime/tests/test_openai_wif.py +++ /dev/null @@ -1,187 +0,0 @@ -"""Tests for OpenAI GCP Workload Identity Federation auth setup.""" - -import os - -import pytest -from google.auth.exceptions import GoogleAuthError -from hackbot_runtime import openai_wif -from hackbot_runtime.providers import ProviderError - - -@pytest.fixture(autouse=True) -def _reset_module_state(): - """Stop any refresher a test started and clear the module-level singleton.""" - yield - if openai_wif._refresher is not None: - openai_wif._refresher.stop() - openai_wif._refresher = None - - -@pytest.fixture -def _no_federation_env(monkeypatch): - for var in ( - openai_wif.RULE_ID_ENV, - openai_wif.AUDIENCE_ENV, - openai_wif.TOKEN_FILE_ENV, - openai_wif.REFRESH_INTERVAL_ENV, - "OPENAI_API_KEY", - ): - monkeypatch.delenv(var, raising=False) - - -def test_is_enabled_follows_rule_id(monkeypatch): - monkeypatch.delenv(openai_wif.RULE_ID_ENV, raising=False) - assert openai_wif.is_enabled() is False - monkeypatch.setenv(openai_wif.RULE_ID_ENV, "idpm_abc") - assert openai_wif.is_enabled() is True - - -def test_configure_is_inert_without_federation(_no_federation_env, monkeypatch): - called = False - - def _should_not_fetch(*_a, **_k): - nonlocal called - called = True - return "tok" - - monkeypatch.setattr(openai_wif, "fetch_gcp_identity_token", _should_not_fetch) - - assert openai_wif.configure() is False - assert called is False - assert openai_wif.TOKEN_FILE_ENV not in os.environ - - -def test_configure_writes_token_and_sets_env(_no_federation_env, monkeypatch, tmp_path): - token_file = tmp_path / "identity-token" - monkeypatch.setenv(openai_wif.RULE_ID_ENV, "idpm_abc") - monkeypatch.setenv(openai_wif.TOKEN_FILE_ENV, str(token_file)) - captured = {} - - def _fetch(audience): - captured["audience"] = audience - return "google.jwt.token" - - monkeypatch.setattr(openai_wif, "fetch_gcp_identity_token", _fetch) - - assert openai_wif.configure() is True - - assert token_file.read_text() == "google.jwt.token" - assert os.environ[openai_wif.TOKEN_FILE_ENV] == str(token_file) - assert captured["audience"] == openai_wif.OPENAI_AUDIENCE - - -def test_configure_is_idempotent(_no_federation_env, monkeypatch, tmp_path): - monkeypatch.setenv(openai_wif.RULE_ID_ENV, "idpm_abc") - monkeypatch.setenv(openai_wif.TOKEN_FILE_ENV, str(tmp_path / "tok")) - - calls = 0 - - def _fetch(*_a, **_k): - nonlocal calls - calls += 1 - return "tok" - - monkeypatch.setattr(openai_wif, "fetch_gcp_identity_token", _fetch) - - assert openai_wif.configure() is True - assert openai_wif.configure() is True - # Second call short-circuits: no new refresher, no extra fetch. - assert calls == 1 - - -def test_configure_defaults_token_path_when_unset(_no_federation_env, monkeypatch): - monkeypatch.setenv(openai_wif.RULE_ID_ENV, "idpm_abc") - monkeypatch.setattr(openai_wif, "fetch_gcp_identity_token", lambda *a, **k: "tok") - - assert openai_wif.configure() is True - - path = os.environ[openai_wif.TOKEN_FILE_ENV] - assert "openai-wif-" in path - assert os.path.isabs(path) - assert os.path.exists(path) - - -def test_fetch_uses_google_auth_with_audience(monkeypatch): - captured = {} - - def _fake_fetch(request, audience): - captured["request"] = request - captured["audience"] = audience - return "signed.jwt.value" - - monkeypatch.setattr( - openai_wif.google.oauth2.id_token, "fetch_id_token", _fake_fetch - ) - - token = openai_wif.fetch_gcp_identity_token() - - assert token == "signed.jwt.value" - assert captured["audience"] == openai_wif.OPENAI_AUDIENCE - assert captured["request"] is not None - - -def test_fetch_wraps_google_auth_error(monkeypatch): - def _raise(*_a, **_k): - raise GoogleAuthError("metadata server unreachable") - - monkeypatch.setattr(openai_wif.google.oauth2.id_token, "fetch_id_token", _raise) - with pytest.raises(ProviderError, match="Failed to fetch a Google identity token"): - openai_wif.fetch_gcp_identity_token() - - -def test_fetch_rejects_empty(monkeypatch): - monkeypatch.setattr( - openai_wif.google.oauth2.id_token, "fetch_id_token", lambda *a, **k: "" - ) - with pytest.raises(ProviderError, match="empty identity token"): - openai_wif.fetch_gcp_identity_token() - - -@pytest.mark.parametrize( - "raw, expected", - [ - (None, openai_wif.DEFAULT_REFRESH_INTERVAL), - ("900", 900), - ("not-a-number", openai_wif.DEFAULT_REFRESH_INTERVAL), - ("0", openai_wif.DEFAULT_REFRESH_INTERVAL), - ("-5", openai_wif.DEFAULT_REFRESH_INTERVAL), - ], -) -def test_refresh_interval_parsing(monkeypatch, raw, expected): - if raw is None: - monkeypatch.delenv(openai_wif.REFRESH_INTERVAL_ENV, raising=False) - else: - monkeypatch.setenv(openai_wif.REFRESH_INTERVAL_ENV, raw) - assert openai_wif._refresh_interval() == expected - - -def test_token_file_write_is_atomic_and_clean(monkeypatch, tmp_path): - token_file = tmp_path / "identity-token" - monkeypatch.setattr( - openai_wif, "fetch_gcp_identity_token", lambda *a, **k: "the.jwt" - ) - refresher = openai_wif._TokenFileRefresher(token_file, interval=1800) - - refresher._write_token() - - assert token_file.read_text() == "the.jwt" - # No leftover temp files from the atomic replace. - assert list(tmp_path.iterdir()) == [token_file] - - -def test_refresh_loop_survives_provider_error(monkeypatch, tmp_path): - refresher = openai_wif._TokenFileRefresher(tmp_path / "tok", interval=0) - calls = [] - - def _boom(*_a, **_k): - calls.append(1) - # Stop after the first failure so the loop exits on its next tick. - refresher.stop() - raise ProviderError("transient") - - monkeypatch.setattr(openai_wif, "fetch_gcp_identity_token", _boom) - - # A ProviderError mid-run is logged and swallowed, not raised out of _loop. - refresher._loop() - - assert calls == [1] diff --git a/libs/hackbot-runtime/tests/test_tracing.py b/libs/hackbot-runtime/tests/test_tracing.py index 9a1b0713444..c0ed0b2d551 100644 --- a/libs/hackbot-runtime/tests/test_tracing.py +++ b/libs/hackbot-runtime/tests/test_tracing.py @@ -1,7 +1,7 @@ """Tests for Weave tracing setup and agent-name derivation.""" import pytest -from hackbot_runtime import tracing, wandb_wif +from hackbot_runtime import tracing from opentelemetry.sdk.trace import TracerProvider from weave.conversation.agent_context import resolve_agent_name as weave_agent_name @@ -15,7 +15,7 @@ def _entrypoint_at(path: str): @pytest.fixture(autouse=True) def _clear_env(monkeypatch): - for var in ("WANDB_API_KEY", wandb_wif.TOKEN_FILE_ENV, "WEAVE_PROJECT"): + for var in ("WANDB_API_KEY", "WANDB_IDENTITY_TOKEN_FILE", "WEAVE_PROJECT"): monkeypatch.delenv(var, raising=False) @@ -62,7 +62,7 @@ def test_init_weave_enabled_by_wif_token_file(monkeypatch): monkeypatch.setattr( weave, "init", lambda project, **kw: calls.update(project=project) ) - monkeypatch.setenv(wandb_wif.TOKEN_FILE_ENV, "/run/wandb-identity-token") + monkeypatch.setenv("WANDB_IDENTITY_TOKEN_FILE", "/run/wandb-identity-token") assert tracing._init_weave() is True assert calls == {"project": tracing.DEFAULT_WEAVE_PROJECT} diff --git a/libs/hackbot-runtime/tests/test_wandb_wif.py b/libs/hackbot-runtime/tests/test_wandb_wif.py deleted file mode 100644 index 9d9dab5b931..00000000000 --- a/libs/hackbot-runtime/tests/test_wandb_wif.py +++ /dev/null @@ -1,218 +0,0 @@ -"""Tests for W&B GCP Workload Identity Federation auth setup.""" - -import logging -import os - -import pytest -from google.auth.exceptions import GoogleAuthError -from hackbot_runtime import wandb_wif -from hackbot_runtime.providers import ProviderError - -_AUDIENCE = "https://example.wandb.example/federation" - - -@pytest.fixture(autouse=True) -def _reset_module_state(): - yield - if wandb_wif._refresher is not None: - wandb_wif._refresher.stop() - wandb_wif._refresher = None - - -@pytest.fixture -def _no_federation_env(monkeypatch): - for var in ( - wandb_wif.AUDIENCE_ENV, - wandb_wif.TOKEN_FILE_ENV, - wandb_wif.REFRESH_INTERVAL_ENV, - ): - monkeypatch.delenv(var, raising=False) - - -def test_is_enabled_follows_audience(monkeypatch): - monkeypatch.delenv(wandb_wif.AUDIENCE_ENV, raising=False) - assert wandb_wif.is_enabled() is False - monkeypatch.setenv(wandb_wif.AUDIENCE_ENV, _AUDIENCE) - assert wandb_wif.is_enabled() is True - - -def test_configure_is_inert_without_federation(_no_federation_env, monkeypatch): - called = False - - def _should_not_fetch(*_a, **_k): - nonlocal called - called = True - return "tok" - - monkeypatch.setattr(wandb_wif, "fetch_gcp_identity_token", _should_not_fetch) - - assert wandb_wif.configure() is False - assert called is False - assert wandb_wif.TOKEN_FILE_ENV not in os.environ - - -def test_configure_writes_token_and_sets_env(_no_federation_env, monkeypatch, tmp_path): - token_file = tmp_path / "identity-token" - monkeypatch.setenv(wandb_wif.AUDIENCE_ENV, _AUDIENCE) - monkeypatch.setenv(wandb_wif.TOKEN_FILE_ENV, str(token_file)) - monkeypatch.delenv("WANDB_API_KEY", raising=False) - monkeypatch.setattr( - wandb_wif, "fetch_gcp_identity_token", lambda *a, **k: "google.jwt.token" - ) - - assert wandb_wif.configure() is True - - assert token_file.read_text() == "google.jwt.token" - assert os.environ[wandb_wif.TOKEN_FILE_ENV] == str(token_file) - - -def test_configure_refuses_when_api_key_set( - _no_federation_env, monkeypatch, tmp_path, caplog -): - monkeypatch.setenv(wandb_wif.AUDIENCE_ENV, _AUDIENCE) - monkeypatch.setenv(wandb_wif.TOKEN_FILE_ENV, str(tmp_path / "identity-token")) - monkeypatch.setenv("WANDB_API_KEY", "leftover") - monkeypatch.setattr(wandb_wif, "fetch_gcp_identity_token", lambda *a, **k: "tok") - - with caplog.at_level(logging.ERROR, logger=wandb_wif.log.name): - assert wandb_wif.configure() is False - - assert any( - rec.levelno == logging.ERROR and "WANDB_API_KEY" in rec.message - for rec in caplog.records - ) - assert os.environ["WANDB_API_KEY"] == "leftover" - assert wandb_wif._refresher is None - - -def test_configure_is_idempotent(_no_federation_env, monkeypatch, tmp_path): - monkeypatch.setenv(wandb_wif.AUDIENCE_ENV, _AUDIENCE) - monkeypatch.setenv(wandb_wif.TOKEN_FILE_ENV, str(tmp_path / "tok")) - monkeypatch.delenv("WANDB_API_KEY", raising=False) - - calls = 0 - - def _fetch(*_a, **_k): - nonlocal calls - calls += 1 - return "tok" - - monkeypatch.setattr(wandb_wif, "fetch_gcp_identity_token", _fetch) - - assert wandb_wif.configure() is True - assert wandb_wif.configure() is True - assert calls == 1 - - -def test_configure_defaults_token_path_when_unset(_no_federation_env, monkeypatch): - monkeypatch.setenv(wandb_wif.AUDIENCE_ENV, _AUDIENCE) - monkeypatch.delenv("WANDB_API_KEY", raising=False) - monkeypatch.setattr(wandb_wif, "fetch_gcp_identity_token", lambda *a, **k: "tok") - - assert wandb_wif.configure() is True - - path = os.environ[wandb_wif.TOKEN_FILE_ENV] - assert "wandb-wif-" in path - assert os.path.exists(path) - - -def test_configure_passes_audience_to_fetch(_no_federation_env, monkeypatch, tmp_path): - monkeypatch.setenv(wandb_wif.AUDIENCE_ENV, _AUDIENCE) - monkeypatch.setenv(wandb_wif.TOKEN_FILE_ENV, str(tmp_path / "tok")) - monkeypatch.delenv("WANDB_API_KEY", raising=False) - - seen = {} - - def _fetch(audience): - seen["audience"] = audience - return "tok" - - monkeypatch.setattr(wandb_wif, "fetch_gcp_identity_token", _fetch) - - assert wandb_wif.configure() is True - assert seen["audience"] == _AUDIENCE - - -def test_fetch_uses_google_auth_with_audience(monkeypatch): - captured = {} - - def _fake_fetch(request, audience): - captured["request"] = request - captured["audience"] = audience - return "signed.jwt.value" - - monkeypatch.setattr(wandb_wif.google.oauth2.id_token, "fetch_id_token", _fake_fetch) - - token = wandb_wif.fetch_gcp_identity_token(_AUDIENCE) - - assert token == "signed.jwt.value" - assert captured["audience"] == _AUDIENCE - assert captured["request"] is not None - - -def test_fetch_wraps_google_auth_error(monkeypatch): - def _raise(*_a, **_k): - raise GoogleAuthError("metadata server unreachable") - - monkeypatch.setattr(wandb_wif.google.oauth2.id_token, "fetch_id_token", _raise) - with pytest.raises(ProviderError, match="Failed to fetch a Google identity token"): - wandb_wif.fetch_gcp_identity_token(_AUDIENCE) - - -def test_fetch_rejects_empty(monkeypatch): - monkeypatch.setattr( - wandb_wif.google.oauth2.id_token, "fetch_id_token", lambda *a, **k: "" - ) - with pytest.raises(ProviderError, match="empty identity token"): - wandb_wif.fetch_gcp_identity_token(_AUDIENCE) - - -@pytest.mark.parametrize( - "raw, expected", - [ - (None, wandb_wif.DEFAULT_REFRESH_INTERVAL), - ("900", 900), - ("not-a-number", wandb_wif.DEFAULT_REFRESH_INTERVAL), - ("0", wandb_wif.DEFAULT_REFRESH_INTERVAL), - ("-5", wandb_wif.DEFAULT_REFRESH_INTERVAL), - ], -) -def test_refresh_interval_parsing(monkeypatch, raw, expected): - if raw is None: - monkeypatch.delenv(wandb_wif.REFRESH_INTERVAL_ENV, raising=False) - else: - monkeypatch.setenv(wandb_wif.REFRESH_INTERVAL_ENV, raw) - assert wandb_wif._refresh_interval() == expected - - -def test_token_file_write_is_atomic_and_clean(monkeypatch, tmp_path): - token_file = tmp_path / "identity-token" - monkeypatch.setattr( - wandb_wif, "fetch_gcp_identity_token", lambda *a, **k: "the.jwt" - ) - refresher = wandb_wif._TokenFileRefresher( - token_file, interval=1800, audience=_AUDIENCE - ) - - refresher._write_token() - - assert token_file.read_text() == "the.jwt" - assert list(tmp_path.iterdir()) == [token_file] - - -def test_refresh_loop_survives_provider_error(monkeypatch, tmp_path): - refresher = wandb_wif._TokenFileRefresher( - tmp_path / "tok", interval=0, audience=_AUDIENCE - ) - calls = [] - - def _boom(*_a, **_k): - calls.append(1) - refresher.stop() - raise ProviderError("transient") - - monkeypatch.setattr(wandb_wif, "fetch_gcp_identity_token", _boom) - - refresher._loop() - - assert calls == [1] diff --git a/libs/hackbot-runtime/tests/test_wif.py b/libs/hackbot-runtime/tests/test_wif.py new file mode 100644 index 00000000000..4d3f22a71b4 --- /dev/null +++ b/libs/hackbot-runtime/tests/test_wif.py @@ -0,0 +1,310 @@ +"""Tests for GCP Workload Identity Federation auth setup.""" + +import logging +import os + +import pytest +from google.auth.exceptions import GoogleAuthError +from hackbot_runtime import wif +from hackbot_runtime.providers import ProviderError + +_WANDB_AUDIENCE = "https://example.wandb.example/federation" + +# The value that enables federation for each provider, as the deploy would set it. +_ENABLE_VALUES = { + wif.AnthropicWifSettings.provider_id: "fdrl_abc", + wif.OpenAIWifSettings.provider_id: "idpm_abc", + wif.WandbWifSettings.provider_id: _WANDB_AUDIENCE, +} + +_EXPECTED_AUDIENCES = { + wif.AnthropicWifSettings.provider_id: "https://api.anthropic.com", + wif.OpenAIWifSettings.provider_id: "https://api.openai.com/v1", + wif.WandbWifSettings.provider_id: _WANDB_AUDIENCE, +} + +all_configs = pytest.mark.parametrize( + "config", wif.PROVIDERS, ids=[c.provider_id for c in wif.PROVIDERS] +) + + +@pytest.fixture(autouse=True) +def _reset_module_state(): + """Stop any refresher a test started and clear the module-level registry.""" + yield + for refresher in wif._refreshers.values(): + refresher.stop() + wif._refreshers.clear() + + +_ENABLE_ENVS = { + wif.AnthropicWifSettings.provider_id: "ANTHROPIC_FEDERATION_RULE_ID", + wif.OpenAIWifSettings.provider_id: "OPENAI_FEDERATION_RULE_ID", + wif.WandbWifSettings.provider_id: "WANDB_FEDERATION_AUDIENCE", +} + +_API_KEY_ENVS = { + wif.AnthropicWifSettings.provider_id: "ANTHROPIC_API_KEY", + wif.OpenAIWifSettings.provider_id: "OPENAI_API_KEY", + wif.WandbWifSettings.provider_id: "WANDB_API_KEY", +} + + +@pytest.fixture(autouse=True) +def _no_federation_env(monkeypatch): + for prefix in ("ANTHROPIC_", "OPENAI_", "WANDB_"): + for var in list(os.environ): + if var.startswith(prefix): + monkeypatch.delenv(var) + + +def _enable(monkeypatch, config): + monkeypatch.setenv( + _ENABLE_ENVS[config.provider_id], _ENABLE_VALUES[config.provider_id] + ) + + +@all_configs +def test_enabled_follows_enabled_env(monkeypatch, config): + assert config.from_env().enabled is False + _enable(monkeypatch, config) + assert config.from_env().enabled is True + + +@all_configs +def test_empty_enabled_env_counts_as_unset(monkeypatch, config): + monkeypatch.setenv(_ENABLE_ENVS[config.provider_id], "") + assert config.from_env().enabled is False + + +@all_configs +def test_configure_is_inert_without_federation(monkeypatch, config): + called = False + + def _should_not_fetch(*_a, **_k): + nonlocal called + called = True + return "tok" + + monkeypatch.setattr(wif, "fetch_gcp_identity_token", _should_not_fetch) + + assert wif.configure(config.from_env()) is False + assert called is False + assert config.token_file_env not in os.environ + + +@all_configs +def test_configure_writes_token_to_configured_path(monkeypatch, tmp_path, config): + token_file = tmp_path / "identity-token" + _enable(monkeypatch, config) + monkeypatch.setenv(config.token_file_env, str(token_file)) + seen = {} + + def _fetch(audience): + seen["audience"] = audience + return "google.jwt.token" + + monkeypatch.setattr(wif, "fetch_gcp_identity_token", _fetch) + + assert wif.configure(config.from_env()) is True + + assert token_file.read_text() == "google.jwt.token" + assert seen["audience"] == _EXPECTED_AUDIENCES[config.provider_id] + + +@all_configs +def test_configure_is_idempotent(monkeypatch, tmp_path, config): + _enable(monkeypatch, config) + monkeypatch.setenv(config.token_file_env, str(tmp_path / "tok")) + + calls = 0 + + def _fetch(*_a, **_k): + nonlocal calls + calls += 1 + return "tok" + + monkeypatch.setattr(wif, "fetch_gcp_identity_token", _fetch) + + assert wif.configure(config.from_env()) is True + assert wif.configure(config.from_env()) is True + # Second call short-circuits: no new refresher, no extra fetch. + assert calls == 1 + + +def test_configure_tracks_providers_independently(monkeypatch, tmp_path): + for config in wif.PROVIDERS: + _enable(monkeypatch, config) + monkeypatch.setenv(config.token_file_env, str(tmp_path / config.provider_id)) + monkeypatch.setattr(wif, "fetch_gcp_identity_token", lambda aud: f"tok:{aud}") + + for config in wif.PROVIDERS: + assert wif.configure(config.from_env()) is True + + for config in wif.PROVIDERS: + expected = f"tok:{_EXPECTED_AUDIENCES[config.provider_id]}" + assert (tmp_path / config.provider_id).read_text() == expected + + +@all_configs +def test_configure_defaults_token_path_when_unset(monkeypatch, caplog, config): + _enable(monkeypatch, config) + monkeypatch.setattr(wif, "fetch_gcp_identity_token", lambda *a, **k: "tok") + + with caplog.at_level(logging.INFO, logger=wif.log.name): + assert wif.configure(config.from_env()) is True + + path = os.environ[config.token_file_env] + assert f"{config.provider_id}-wif-" in path + assert os.path.isabs(path) + assert os.path.exists(path) + assert any( + rec.levelno == logging.INFO + and config.token_file_env in rec.message + and "temporary" in rec.message + for rec in caplog.records + ) + + +@pytest.mark.parametrize( + "config", + [wif.AnthropicWifSettings, wif.WandbWifSettings], + ids=["anthropic", "wandb"], +) +def test_configure_refuses_when_shadowing_api_key_set( + monkeypatch, tmp_path, caplog, config +): + _enable(monkeypatch, config) + monkeypatch.setenv(config.token_file_env, str(tmp_path / "identity-token")) + monkeypatch.setenv(_API_KEY_ENVS[config.provider_id], "sk-leftover") + monkeypatch.setattr(wif, "fetch_gcp_identity_token", lambda *a, **k: "tok") + + with caplog.at_level(logging.ERROR, logger=wif.log.name): + assert wif.configure(config.from_env()) is False + + # A key set alongside federation is flagged as an error and left untouched. + assert any( + rec.levelno == logging.ERROR and config.name in rec.message + for rec in caplog.records + ) + assert os.environ[_API_KEY_ENVS[config.provider_id]] == "sk-leftover" + assert config.provider_id not in wif._refreshers + + +def test_openai_configure_warns_but_proceeds_with_api_key( + monkeypatch, tmp_path, caplog +): + _enable(monkeypatch, wif.OpenAIWifSettings) + monkeypatch.setenv( + wif.OpenAIWifSettings.token_file_env, + str(tmp_path / "identity-token"), + ) + monkeypatch.setenv(_API_KEY_ENVS[wif.OpenAIWifSettings.provider_id], "sk-leftover") + monkeypatch.setattr(wif, "fetch_gcp_identity_token", lambda *a, **k: "tok") + + with caplog.at_level(logging.WARNING, logger=wif.log.name): + assert wif.configure(wif.OpenAIWifSettings.from_env()) is True + + # Codex prefers federation over the key, so the key is only a warning. + assert any( + rec.levelno == logging.WARNING and "OpenAI API key" in rec.message + for rec in caplog.records + ) + + +def test_openai_audience_override(monkeypatch, tmp_path): + _enable(monkeypatch, wif.OpenAIWifSettings) + monkeypatch.setenv("OPENAI_WIF_AUDIENCE", "https://custom.example/aud") + monkeypatch.setenv(wif.OpenAIWifSettings.token_file_env, str(tmp_path / "tok")) + seen = {} + + def _fetch(audience): + seen["audience"] = audience + return "tok" + + monkeypatch.setattr(wif, "fetch_gcp_identity_token", _fetch) + + assert wif.configure(wif.OpenAIWifSettings.from_env()) is True + assert seen["audience"] == "https://custom.example/aud" + + +def test_fetch_uses_google_auth_with_audience(monkeypatch): + captured = {} + + def _fake_fetch(request, audience): + captured["request"] = request + captured["audience"] = audience + return "signed.jwt.value" + + monkeypatch.setattr(wif.google.oauth2.id_token, "fetch_id_token", _fake_fetch) + + token = wif.fetch_gcp_identity_token("https://aud.example") + + assert token == "signed.jwt.value" + assert captured["audience"] == "https://aud.example" + assert captured["request"] is not None + + +def test_fetch_wraps_google_auth_error(monkeypatch): + def _raise(*_a, **_k): + raise GoogleAuthError("metadata server unreachable") + + monkeypatch.setattr(wif.google.oauth2.id_token, "fetch_id_token", _raise) + with pytest.raises(ProviderError, match="Failed to fetch a Google identity token"): + wif.fetch_gcp_identity_token("https://aud.example") + + +def test_fetch_rejects_empty(monkeypatch): + monkeypatch.setattr( + wif.google.oauth2.id_token, "fetch_id_token", lambda *a, **k: "" + ) + with pytest.raises(ProviderError, match="empty identity token"): + wif.fetch_gcp_identity_token("https://aud.example") + + +@pytest.mark.parametrize( + "raw, expected", + [ + (None, wif.DEFAULT_REFRESH_INTERVAL), + ("900", 900), + ("not-a-number", wif.DEFAULT_REFRESH_INTERVAL), + ("0", wif.DEFAULT_REFRESH_INTERVAL), + ("-5", wif.DEFAULT_REFRESH_INTERVAL), + ], +) +def test_refresh_interval_parsing(monkeypatch, raw, expected): + if raw is not None: + monkeypatch.setenv("ANTHROPIC_IDENTITY_TOKEN_REFRESH_SECONDS", raw) + settings = wif.AnthropicWifSettings.from_env() + assert settings.identity_token_refresh_seconds == expected + + +def test_token_file_write_is_atomic_and_clean(monkeypatch, tmp_path): + token_file = tmp_path / "identity-token" + monkeypatch.setattr(wif, "fetch_gcp_identity_token", lambda *a, **k: "the.jwt") + refresher = wif.TokenFileRefresher( + token_file, interval=1800, audience="aud", provider_id="test", name="Test" + ) + + refresher._write_token() + + assert token_file.read_text() == "the.jwt" + assert list(tmp_path.iterdir()) == [token_file] + + +def test_refresh_loop_survives_provider_error(monkeypatch, tmp_path): + refresher = wif.TokenFileRefresher( + tmp_path / "tok", interval=0, audience="aud", provider_id="test", name="Test" + ) + calls = [] + + def _boom(*_a, **_k): + calls.append(1) + refresher.stop() + raise ProviderError("transient") + + monkeypatch.setattr(wif, "fetch_gcp_identity_token", _boom) + + refresher._loop() + + assert calls == [1]