Skip to content

Commit 66cf08d

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Update agent_registry to handle mTLS endpoints internally
Transforms connection URIs conditionally based on mtls.should_use_mtls_endpoint() and client_cert checks, addressing the mTLS implementation guidelines safely. PiperOrigin-RevId: 954825139
1 parent 1478e1a commit 66cf08d

2 files changed

Lines changed: 28 additions & 18 deletions

File tree

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
4040
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams
4141
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
42+
from google.adk.utils import _mtls_utils
4243
import google.auth
4344
from google.auth.transport import mtls
4445
from google.auth.transport import requests as requests_auth
@@ -222,7 +223,12 @@ def __init__(
222223
else None
223224
)
224225
self._session.configure_mtls_channel(client_cert_source)
225-
self._base_url = _get_agent_registry_base_url(client_cert_source)
226+
self._use_mtls = _should_use_mtls_endpoint(client_cert_source)
227+
self._base_url = (
228+
AGENT_REGISTRY_MTLS_BASE_URL
229+
if self._use_mtls
230+
else AGENT_REGISTRY_BASE_URL
231+
)
226232

227233
def _get_auth_headers(self) -> Dict[str, str]:
228234
"""Refreshes credentials and returns authorization headers."""
@@ -325,6 +331,8 @@ def _get_connection_uri(
325331
if protocol_binding and mapped_binding != protocol_binding:
326332
continue
327333
if url := i.get("url"):
334+
if self._use_mtls:
335+
url = _mtls_utils.effective_googleapis_endpoint(url)
328336
return url, protocol_version, mapped_binding
329337

330338
return None, None, None
@@ -637,17 +645,19 @@ def _use_client_cert_effective() -> bool:
637645
return use_client_cert_str == "true"
638646

639647

640-
def _get_agent_registry_base_url(client_cert_source: Any | None = None) -> str:
641-
"""Returns the base URL based on mTLS configuration and cert availability."""
648+
def _should_use_mtls_endpoint(client_cert_source: Any | None = None) -> bool:
649+
"""Returns whether the mTLS endpoint should be used."""
650+
try:
651+
return bool(mtls.should_use_mtls_endpoint())
652+
except (ImportError, AttributeError):
653+
pass
642654
use_mtls_endpoint_str = os.getenv(
643655
"GOOGLE_API_USE_MTLS_ENDPOINT", _MtlsEndpoint.AUTO.value
644656
).lower()
645657
try:
646658
use_mtls_endpoint = _MtlsEndpoint(use_mtls_endpoint_str)
647659
except ValueError:
648660
use_mtls_endpoint = _MtlsEndpoint.AUTO
649-
if (use_mtls_endpoint is _MtlsEndpoint.ALWAYS) or (
661+
return (use_mtls_endpoint is _MtlsEndpoint.ALWAYS) or (
650662
use_mtls_endpoint is _MtlsEndpoint.AUTO and client_cert_source is not None
651-
):
652-
return AGENT_REGISTRY_MTLS_BASE_URL
653-
return AGENT_REGISTRY_BASE_URL
663+
)

tests/unittests/integrations/agent_registry/test_agent_registry.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -888,23 +888,23 @@ def test_use_client_cert_effective(
888888
assert _use_client_cert_effective() == expected
889889

890890
@pytest.mark.parametrize(
891-
"use_mtls_env, client_cert_source, expected_domain",
891+
"use_mtls_env, client_cert_source, expected",
892892
[
893893
# Auto mode (default)
894-
(None, None, "agentregistry.googleapis.com"),
895-
(None, lambda: True, "agentregistry.mtls.googleapis.com"),
894+
(None, None, False),
895+
(None, lambda: True, True),
896896
# Always mode
897-
("always", None, "agentregistry.mtls.googleapis.com"),
898-
("always", lambda: True, "agentregistry.mtls.googleapis.com"),
897+
("always", None, True),
898+
("always", lambda: True, True),
899899
# Never mode
900-
("never", None, "agentregistry.googleapis.com"),
901-
("never", lambda: True, "agentregistry.googleapis.com"),
900+
("never", None, False),
901+
("never", lambda: True, False),
902902
],
903903
)
904-
def test_get_agent_registry_base_url(
905-
self, use_mtls_env, client_cert_source, expected_domain, registry
904+
def test_should_use_mtls_endpoint(
905+
self, use_mtls_env, client_cert_source, expected, registry
906906
):
907-
from google.adk.integrations.agent_registry.agent_registry import _get_agent_registry_base_url
907+
from google.adk.integrations.agent_registry.agent_registry import _should_use_mtls_endpoint
908908

909909
env_patch = {}
910910
if use_mtls_env is not None:
@@ -914,7 +914,7 @@ def test_get_agent_registry_base_url(
914914
env_patch = {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}
915915

916916
with patch.dict(os.environ, env_patch):
917-
assert expected_domain in _get_agent_registry_base_url(client_cert_source)
917+
assert expected == _should_use_mtls_endpoint(client_cert_source)
918918

919919
def test_make_request_error_handling(self, registry):
920920
mock_session = registry._session

0 commit comments

Comments
 (0)