Skip to content

The agent that grows with you

test: trim the ambient-chain refusal tests to two two-home invariants

teknium1297ebd506e9b
diff --git a/tests/agent/test_azure_identity_adapter.py b/tests/agent/test_azure_identity_adapter.py
index 7d5cfed0892..4426e8fc90c 100644
--- a/tests/agent/test_azure_identity_adapter.py
+++ b/tests/agent/test_azure_identity_adapter.py
@@ -337,159 +337,52 @@ class TestBuildCredential:
class TestScopedCredential:
- """Multiplex isolation: a routed profile must never mint the launch
- profile's ambient credential chain (vertex_adapter refuses the same
- pattern for GOOGLE_APPLICATION_CREDENTIALS)."""
+ """A served multiplex profile never mints the launch profile's ambient chain (#116313)."""
- @pytest.fixture
- def routed_home(self, tmp_path):
- """Install a HERMES_HOME override so build_credential takes the scoped path."""
- from hermes_constants import reset_hermes_home_override, set_hermes_home_override
- token = set_hermes_home_override(str(tmp_path / "profile_home"))
- yield
- reset_hermes_home_override(token)
-
- @pytest.fixture
- def multiplex(self):
- from agent import secret_scope
- secret_scope.set_multiplex_active(True)
- yield secret_scope
- secret_scope.set_multiplex_active(False)
-
- @pytest.fixture
- def scoped(self, multiplex, request):
- """Install a secret scope for the duration of a test."""
- def _set(secrets):
- token = multiplex.set_secret_scope(secrets)
- request.addfinalizer(lambda: multiplex.reset_secret_scope(token))
- return _set
-
- def test_multiplex_refuses_ambient_chain(
- self, fake_azure_identity, routed_home, multiplex, monkeypatch, scoped,
- ):
- """The finding: scope has no AZURE_* of its own but the process env
- carries the launch profile's service principal. The served profile
- must not mint it."""
- from agent.azure_identity_adapter import EntraIdentityConfig, build_credential
- monkeypatch.setenv("AZURE_TENANT_ID", "launch-tenant")
- monkeypatch.setenv("AZURE_CLIENT_ID", "launch-client")
- monkeypatch.setenv("AZURE_CLIENT_SECRET", "launch-secret")
- scoped({})
- with pytest.raises(RuntimeError, match="ambient DefaultAzureCredential"):
- build_credential(EntraIdentityConfig())
- assert fake_azure_identity.credential_count == 0
- assert fake_azure_identity.scoped_calls == []
-
- def test_multiplex_unscoped_refuses_too(
- self, fake_azure_identity, routed_home, multiplex,
- ):
- """No secret scope installed at all still refuses (fail closed)."""
- from agent.azure_identity_adapter import EntraIdentityConfig, build_credential
- with pytest.raises(RuntimeError, match="ambient DefaultAzureCredential"):
- build_credential(EntraIdentityConfig())
-
- def test_multiplex_scope_builds_client_secret(
- self, fake_azure_identity, routed_home, multiplex, scoped,
- ):
- from agent.azure_identity_adapter import EntraIdentityConfig, build_credential
- scoped({
- "AZURE_TENANT_ID": "t", "AZURE_CLIENT_ID": "c", "AZURE_CLIENT_SECRET": "s",
- })
- cred = build_credential(EntraIdentityConfig())
- assert cred.kind == "client_secret"
- assert fake_azure_identity.scoped_calls == [("client_secret", "t", "c", "s")]
- assert fake_azure_identity.credential_count == 0
-
- def test_multiplex_scope_builds_workload_identity(
- self, fake_azure_identity, routed_home, multiplex, scoped,
- ):
- from agent.azure_identity_adapter import EntraIdentityConfig, build_credential
- scoped({
- "AZURE_TENANT_ID": "t", "AZURE_CLIENT_ID": "c",
- "AZURE_FEDERATED_TOKEN_FILE": "/var/run/secrets/token",
- })
- cred = build_credential(EntraIdentityConfig())
- assert cred.kind == "workload_identity"
- assert fake_azure_identity.scoped_calls == [
- ("workload_identity", {"tenant_id": "t", "client_id": "c",
- "token_file_path": "/var/run/secrets/token"})]
-
- def test_client_id_alone_routes_to_managed_identity(
- self, fake_azure_identity, routed_home, multiplex, scoped,
- ):
- """Explicit per-profile opt-in to the host's user-assigned managed identity."""
- from agent.azure_identity_adapter import EntraIdentityConfig, build_credential
- scoped({"AZURE_CLIENT_ID": "user-assigned-id"})
- cred = build_credential(EntraIdentityConfig())
- assert cred.kind == "managed_identity"
- assert fake_azure_identity.scoped_calls == [
- ("managed_identity", {"client_id": "user-assigned-id"})]
- assert fake_azure_identity.credential_count == 0
-
- def test_non_multiplex_override_keeps_default_chain(
- self, fake_azure_identity, routed_home, monkeypatch,
+ def test_two_homes_multiplex_refuses_ambient_chain_and_keeps_standalone(
+ self, fake_azure_identity, tmp_path, monkeypatch,
):
- """A single-profile override run (hermes -p beta) legitimately inherits
- the process env: the refusal only applies while multiplexing."""
- from agent.azure_identity_adapter import EntraIdentityConfig, build_credential
- cred = build_credential(EntraIdentityConfig())
- assert cred is not None
- assert fake_azure_identity.credential_count == 1
-
- def test_e2e_token_provider_refuses_under_multiplex(
- self, fake_azure_identity, routed_home, multiplex, scoped,
- ):
- """The real consumer chain: build_token_provider -> build_credential ->
- _scoped_credential. A cred-less served profile fails at construction."""
- from agent.azure_identity_adapter import build_token_provider
- scoped({})
- with pytest.raises(RuntimeError, match="ambient DefaultAzureCredential"):
- build_token_provider()
-
- def test_e2e_token_provider_mints_scoped_identity(
- self, fake_azure_identity, routed_home, multiplex, scoped,
- ):
- """A profile with its own SP gets a working provider minting via its
- scope, never the process env."""
- from agent.azure_identity_adapter import build_token_provider, materialize_bearer_for_http
- scoped({"AZURE_TENANT_ID": "t", "AZURE_CLIENT_ID": "c", "AZURE_CLIENT_SECRET": "s"})
- provider = build_token_provider()
- assert materialize_bearer_for_http(provider).startswith("jwt-for-")
- assert fake_azure_identity.scoped_calls == [("client_secret", "t", "c", "s")]
-
- def test_e2e_per_home_cache_isolation(
- self, fake_azure_identity, multiplex, scoped, tmp_path,
- ):
- """Two routed homes, one process: A's scoped credential is cached under
- A's home key and never served to cred-less B."""
+ """A -> B -> A over two real homes: A (own AZURE_* in .env) builds its ClientSecretCredential,
+ cred-less B is refused instead of getting DefaultAzureCredential (which reads A's AZURE_* from
+ the process env), A again is unaffected; the probe thread runs under the caller's scope so the
+ doctor path surfaces the same refusal. Control: a standalone run keeps the ambient chain."""
+ from agent import secret_scope
+ from agent.azure_identity_adapter import EntraIdentityConfig, _probe_token, build_credential
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
- from agent.azure_identity_adapter import EntraIdentityConfig, build_credential
- tok_a = set_hermes_home_override(str(tmp_path / "home_a"))
- try:
- scoped({"AZURE_TENANT_ID": "t", "AZURE_CLIENT_ID": "c", "AZURE_CLIENT_SECRET": "s"})
- cred_a = build_credential(EntraIdentityConfig())
- assert cred_a.kind == "client_secret"
- finally:
- reset_hermes_home_override(tok_a)
- tok_b = set_hermes_home_override(str(tmp_path / "home_b"))
- try:
- scoped({})
- with pytest.raises(RuntimeError, match="ambient DefaultAzureCredential"):
- build_credential(EntraIdentityConfig())
- finally:
- reset_hermes_home_override(tok_b)
- def test_probe_surfaces_refusal_as_error(
- self, fake_azure_identity, routed_home, multiplex, scoped,
- ):
- """Doctor/probe path must not raise: the refusal lands in the error field."""
- from agent.azure_identity_adapter import describe_active_credential, has_azure_identity_credentials
- scoped({})
- assert has_azure_identity_credentials() is False
- info = describe_active_credential()
- assert info["ok"] is False
- assert "ambient DefaultAzureCredential" in info["error"]
+ home_a, home_b = tmp_path / "home-A", tmp_path / "home-B"
+ for home in (home_a, home_b):
+ home.mkdir()
+ (home_a / ".env").write_text("AZURE_TENANT_ID=tenant-A\nAZURE_CLIENT_ID=client-A\nAZURE_CLIENT_SECRET=secret-A\n")
+ (home_b / ".env").write_text("")
+ # The launch profile's .env is in the process env, exactly what DefaultAzureCredential reads.
+ monkeypatch.setenv("AZURE_TENANT_ID", "tenant-A")
+ monkeypatch.setenv("AZURE_CLIENT_ID", "client-A")
+ monkeypatch.setenv("AZURE_CLIENT_SECRET", "secret-A")
+ config = EntraIdentityConfig()
+
+ def in_scope(home, fn):
+ h_tok = set_hermes_home_override(str(home))
+ s_tok = secret_scope.set_secret_scope(secret_scope.build_profile_secret_scope(home))
+ try:
+ return fn()
+ finally:
+ secret_scope.reset_secret_scope(s_tok)
+ reset_hermes_home_override(h_tok)
+
+ # Control: standalone (no multiplex, no override) keeps today's ambient chain.
+ assert build_credential(config).kwargs is not None
+ assert fake_azure_identity.credential_count == 1
+ monkeypatch.setattr(secret_scope, "_MULTIPLEX_ACTIVE", True)
+ assert in_scope(home_a, lambda: build_credential(config)).client_id == "client-A"
+ with pytest.raises(RuntimeError, match="refused for this profile"):
+ in_scope(home_b, lambda: build_credential(config))
+ probe = in_scope(home_b, lambda: _probe_token(config, 5.0))
+ assert "refused for this profile" in probe["error"]
+ assert in_scope(home_a, lambda: build_credential(config)).client_id == "client-A"
+ # Absence on the wrong side: B never reached the ambient chain.
+ assert fake_azure_identity.credential_count == 1
class TestBuildTokenProvider:
diff --git a/tests/agent/test_bedrock_adapter.py b/tests/agent/test_bedrock_adapter.py
index f34341a7ad2..ebc0416beee 100644
--- a/tests/agent/test_bedrock_adapter.py
+++ b/tests/agent/test_bedrock_adapter.py
@@ -124,117 +124,53 @@ class TestHasAwsCredentials:
class TestScopedAwsSessionKwargs:
- """Multiplex isolation: a routed profile must not mint the launch context's
- ambient AWS chain (same refusal the Entra adapter applies to
- DefaultAzureCredential)."""
-
- @pytest.fixture
- def routed_home(self, tmp_path):
+ """A served multiplex profile never signs with the launch profile's ambient AWS chain (#116313)."""
+
+ def test_two_homes_multiplex_refuses_ambient_chain_and_keeps_standalone(self, tmp_path, monkeypatch):
+ """A -> B -> A over two real homes: A (own AWS_* in .env) gets its key pair, cred-less B is
+ refused at the production client seam BEFORE boto3 is touched (``{}`` would let
+ ``boto3.Session()`` sign as A) and its bearer read is scoped, A again is unaffected.
+ Control: a standalone run keeps the ambient chain (``{}`` + process-env bearer)."""
+ from agent import bedrock_adapter, secret_scope
+ from agent.bedrock_adapter import _cached_client, resolve_bedrock_bearer_token, scoped_aws_session_kwargs
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
- token = set_hermes_home_override(str(tmp_path / "profile_home"))
- yield
- reset_hermes_home_override(token)
-
- @pytest.fixture
- def multiplex(self):
- from agent import secret_scope
- secret_scope.set_multiplex_active(True)
- yield secret_scope
- secret_scope.set_multiplex_active(False)
-
- @pytest.fixture
- def scoped(self, multiplex, request):
- def _set(secrets):
- token = multiplex.set_secret_scope(secrets)
- request.addfinalizer(lambda: multiplex.reset_secret_scope(token))
- return _set
-
- def test_multiplex_refuses_ambient_chain(
- self, routed_home, multiplex, scoped, monkeypatch,
- ):
- """Scope has no AWS_* of its own but the process env carries the launch
- profile's keys: Session({}) would silently sign with them."""
- from agent.bedrock_adapter import scoped_aws_session_kwargs
- monkeypatch.setenv("AWS_ACCESS_KEY_ID", "launch-key")
- monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "launch-secret")
- scoped({})
- with pytest.raises(RuntimeError, match="ambient default chain"):
- scoped_aws_session_kwargs()
-
- def test_multiplex_unscoped_refuses_too(self, routed_home, multiplex):
- from agent.bedrock_adapter import scoped_aws_session_kwargs
- with pytest.raises(RuntimeError, match="ambient default chain"):
- scoped_aws_session_kwargs()
-
- def test_multiplex_partial_set_refused(self, routed_home, multiplex, scoped):
- """A lone session token still leaves boto3 to fill key/secret from the
- ambient chain, so an incomplete set refuses too."""
- from agent.bedrock_adapter import scoped_aws_session_kwargs
- scoped({"AWS_SESSION_TOKEN": "only-token"})
- with pytest.raises(RuntimeError, match="ambient default chain"):
- scoped_aws_session_kwargs()
-
- def test_multiplex_complete_key_pair_passes(self, routed_home, multiplex, scoped):
- from agent.bedrock_adapter import scoped_aws_session_kwargs
- scoped({"AWS_ACCESS_KEY_ID": "k", "AWS_SECRET_ACCESS_KEY": "s"})
- assert scoped_aws_session_kwargs() == {
- "aws_access_key_id": "k", "aws_secret_access_key": "s"}
-
- def test_multiplex_aws_profile_is_explicit_optin(self, routed_home, multiplex, scoped):
- from agent.bedrock_adapter import scoped_aws_session_kwargs
- scoped({"AWS_PROFILE": "named"})
- assert scoped_aws_session_kwargs() == {"profile_name": "named"}
-
- def test_non_multiplex_override_keeps_ambient(self, routed_home):
- """Single-profile override run: the process env is the operator's own."""
- from agent.bedrock_adapter import scoped_aws_session_kwargs
- assert scoped_aws_session_kwargs() == {}
- def test_bearer_token_reads_scope_under_multiplex(
- self, routed_home, multiplex, scoped, monkeypatch,
- ):
- """The launch profile's AWS_BEARER_TOKEN_BEDROCK must not leak to a served
- profile whose scope does not define it."""
- from agent.bedrock_adapter import resolve_bedrock_bearer_token
- monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "launch-token")
- scoped({})
- assert resolve_bedrock_bearer_token() == ""
- scoped({"AWS_BEARER_TOKEN_BEDROCK": "own-token"})
- assert resolve_bedrock_bearer_token() == "own-token"
-
- def test_e2e_cached_client_refuses_before_session(
- self, routed_home, multiplex, scoped, monkeypatch,
- ):
- """The real consumer path: _cached_client must refuse BEFORE constructing
- boto3.Session, so no ambient chain is ever consulted."""
- import agent.bedrock_adapter as ba
- session = MagicMock()
- fake_boto3 = MagicMock()
- fake_boto3.Session = session
- monkeypatch.setattr(ba, "_require_boto3", lambda: fake_boto3)
- ba.reset_client_cache()
- scoped({})
- with pytest.raises(RuntimeError, match="ambient default chain"):
- ba._cached_client({}, "bedrock-runtime", "us-east-1")
- session.assert_not_called()
- ba.reset_client_cache()
-
- def test_e2e_cached_client_builds_scoped_session(
- self, routed_home, multiplex, scoped, monkeypatch,
- ):
- """A profile with its own key pair gets a Session built from ITS scope."""
- import agent.bedrock_adapter as ba
- session = MagicMock()
- fake_boto3 = MagicMock()
- fake_boto3.Session = session
- monkeypatch.setattr(ba, "_require_boto3", lambda: fake_boto3)
- ba.reset_client_cache()
- scoped({"AWS_ACCESS_KEY_ID": "k", "AWS_SECRET_ACCESS_KEY": "s"})
- cache = {}
- ba._cached_client(cache, "bedrock-runtime", "us-east-1")
- session.assert_called_once_with(
- aws_access_key_id="k", aws_secret_access_key="s")
- ba.reset_client_cache()
+ home_a, home_b = tmp_path / "home-A", tmp_path / "home-B"
+ for home in (home_a, home_b):
+ home.mkdir()
+ (home_a / ".env").write_text(
+ "AWS_ACCESS_KEY_ID=AKIA-A\nAWS_SECRET_ACCESS_KEY=secret-A\nAWS_BEARER_TOKEN_BEDROCK=bearer-A\n"
+ )
+ (home_b / ".env").write_text("")
+ monkeypatch.setenv("AWS_ACCESS_KEY_ID", "AKIA-A")
+ monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "secret-A")
+ monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "bearer-A")
+
+ def boom():
+ raise AssertionError("boto3 must not be imported for a refused profile")
+ monkeypatch.setattr(bedrock_adapter, "_require_boto3", boom)
+
+ def in_scope(home, fn):
+ h_tok = set_hermes_home_override(str(home))
+ s_tok = secret_scope.set_secret_scope(secret_scope.build_profile_secret_scope(home))
+ try:
+ return fn()
+ finally:
+ secret_scope.reset_secret_scope(s_tok)
+ reset_hermes_home_override(h_tok)
+
+ # Control: standalone keeps the ambient chain.
+ assert scoped_aws_session_kwargs() == {}
+ assert resolve_bedrock_bearer_token() == "bearer-A"
+
+ monkeypatch.setattr(secret_scope, "_MULTIPLEX_ACTIVE", True)
+ a_kwargs = {"aws_access_key_id": "AKIA-A", "aws_secret_access_key": "secret-A"}
+ assert in_scope(home_a, scoped_aws_session_kwargs) == a_kwargs
+ with pytest.raises(RuntimeError, match="refused for this profile"):
+ in_scope(home_b, lambda: _cached_client({}, "bedrock-runtime", "us-east-1"))
+ assert in_scope(home_b, resolve_bedrock_bearer_token) == ""
+ assert in_scope(home_a, scoped_aws_session_kwargs) == a_kwargs
+ assert in_scope(home_a, resolve_bedrock_bearer_token) == "bearer-A"
class TestResolveBedrocRegion:
diff --git a/website/docs/developer-guide/multiplexing-gateway.md b/website/docs/developer-guide/multiplexing-gateway.md
index b5cad80ef35..512e43893a3 100644
--- a/website/docs/developer-guide/multiplexing-gateway.md
+++ b/website/docs/developer-guide/multiplexing-gateway.md
@@ -110,6 +110,13 @@ B's turns and into every subprocess spawned with `env=dict(os.environ)`.
- A small allowlist (`HERMES_HOME`, `HERMES_PROFILE`, proxy settings,
`API_SERVER_*` listener settings — but deliberately not `API_SERVER_KEY`)
stays global because those describe the process, not a profile.
+- Cloud SDK *default credential chains* are ambient by construction
+ (`google.auth.default()`, `DefaultAzureCredential`, `boto3.Session()` with
+ no keys): every source they walk — process env, CLI caches, instance
+ metadata — is the launch context's identity. Under multiplexing a served
+ profile without a complete credential of its own is **refused** by the
+ Vertex, Entra ID and Bedrock adapters rather than minting that identity
+ against its own `base_url`; standalone runs keep the chain.
Because the per-turn `.env` reload is a no-op under multiplexing, rotated
credentials are picked up through the profile scope on the next turn — never
diff --git a/website/docs/guides/azure-foundry.md b/website/docs/guides/azure-foundry.md
index a137c79b5db..78996fcad9c 100644
--- a/website/docs/guides/azure-foundry.md
+++ b/website/docs/guides/azure-foundry.md
@@ -141,6 +141,8 @@ No secrets land in `~/.hermes/.env` for Entra mode — `azure-identity` caches t
Interactive browser credential is excluded by default for unattended Hermes runs; use Azure CLI, Azure Developer CLI, managed identity, workload identity, or service principal credentials instead.
+**Multiplexed profiles (`gateway.multiplex_profiles: true`):** every source in that chain resolves from the *process* — the launch profile's `AZURE_*`, its `az login` session, the host's managed identity. A served profile that sets no `AZURE_*` of its own is therefore refused instead of borrowing the launch identity (the same rule the Vertex adapter applies to Application Default Credentials). Give each profile its own `AZURE_TENANT_ID` + `AZURE_CLIENT_ID` + `AZURE_CLIENT_SECRET` (or `AZURE_FEDERATED_TOKEN_FILE`) in its `.env`; `AZURE_CLIENT_ID` alone opts that profile into the host's user-assigned managed identity. Single-profile runs (`hermes`, `hermes -p beta`) keep the full chain.
+
### Deployment patterns
**Local development:**