Skip to content

Commit b315b00

Browse files
wukathcopybara-github
authored andcommitted
fix: Call mtls.should_use_mtls_endpoint with client_cert_available argument
Fixes failing unit test - there was a false negative in "auto" mode when a certificate was present Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 954884006
1 parent 95feafa commit b315b00

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/google/adk/integrations/agent_registry/agent_registry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,11 @@ def _use_client_cert_effective() -> bool:
648648
def _should_use_mtls_endpoint(client_cert_source: Any | None = None) -> bool:
649649
"""Returns whether the mTLS endpoint should be used."""
650650
try:
651-
return bool(mtls.should_use_mtls_endpoint())
651+
return bool(
652+
mtls.should_use_mtls_endpoint(
653+
client_cert_available=client_cert_source is not None
654+
)
655+
)
652656
except (ImportError, AttributeError):
653657
pass
654658
use_mtls_endpoint_str = os.getenv(

tests/unittests/integrations/agent_registry/test_agent_registry.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from google.adk.auth.auth_credential import OAuth2Auth
2626
from google.adk.integrations.agent_registry import AgentRegistry
2727
from google.adk.integrations.agent_registry.agent_registry import _ProtocolType
28+
from google.adk.integrations.agent_registry.agent_registry import _should_use_mtls_endpoint
2829
from google.adk.telemetry.tracing import GCP_MCP_SERVER_DESTINATION_ID
2930
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
3031
import httpx
@@ -904,17 +905,36 @@ def test_use_client_cert_effective(
904905
def test_should_use_mtls_endpoint(
905906
self, use_mtls_env, client_cert_source, expected, registry
906907
):
907-
from google.adk.integrations.agent_registry.agent_registry import _should_use_mtls_endpoint
908-
909908
env_patch = {}
910909
if use_mtls_env is not None:
911910
env_patch["GOOGLE_API_USE_MTLS_ENDPOINT"] = use_mtls_env
912911
else:
913912
# Ensure any ambient env var doesn't leak into the test
914913
env_patch = {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}
915914

915+
# Scenario 1: Library function does not exist (fallback path)
916916
with patch.dict(os.environ, env_patch):
917-
assert expected == _should_use_mtls_endpoint(client_cert_source)
917+
with patch(
918+
"google.auth.transport.mtls.should_use_mtls_endpoint", create=True
919+
) as mock_func:
920+
mock_func.side_effect = AttributeError("Mocked missing attribute")
921+
assert expected == _should_use_mtls_endpoint(client_cert_source)
922+
923+
# Scenario 2: Library function exists (standard path)
924+
def mock_impl(client_cert_available=None):
925+
use_mtls = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
926+
if use_mtls == "always":
927+
return True
928+
if use_mtls == "never":
929+
return False
930+
return bool(client_cert_available)
931+
932+
with patch.dict(os.environ, env_patch):
933+
with patch(
934+
"google.auth.transport.mtls.should_use_mtls_endpoint", create=True
935+
) as mock_func:
936+
mock_func.side_effect = mock_impl
937+
assert expected == _should_use_mtls_endpoint(client_cert_source)
918938

919939
def test_make_request_error_handling(self, registry):
920940
mock_session = registry._session

0 commit comments

Comments
 (0)