Skip to content

Commit 5d4a13f

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: implement dynamic mtls endpoint resolution for secret manager
Replace hardcoded regional URLs with a helper function that prioritizes `.mtls.` endpoints when client certificates are present and enabled, fulfilling mTLS/CAA requirements. PiperOrigin-RevId: 937477755
1 parent a562a31 commit 5d4a13f

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/google/adk/integrations/secret_manager/secret_client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@
2626
from google.oauth2 import service_account
2727

2828
from ... import version
29+
from ...utils import mtls_utils
2930

3031
USER_AGENT = f"google-adk/{version.__version__}"
3132

33+
_DEFAULT_REGIONAL_ENDPOINT_TEMPLATE = (
34+
"secretmanager.{location}.rep.googleapis.com"
35+
)
36+
_DEFAULT_MTLS_REGIONAL_ENDPOINT_TEMPLATE = (
37+
"secretmanager.{location}.rep.mtls.googleapis.com"
38+
)
39+
3240

3341
class SecretManagerClient:
3442
"""A client for interacting with Google Cloud Secret Manager.
@@ -104,7 +112,11 @@ def __init__(
104112
client_options = None
105113
if location:
106114
client_options = {
107-
"api_endpoint": f"secretmanager.{location}.rep.googleapis.com"
115+
"api_endpoint": mtls_utils.get_api_endpoint(
116+
location,
117+
_DEFAULT_REGIONAL_ENDPOINT_TEMPLATE,
118+
_DEFAULT_MTLS_REGIONAL_ENDPOINT_TEMPLATE,
119+
)
108120
}
109121

110122
self._client = secretmanager.SecretManagerServiceClient(

tests/unittests/integrations/secret_manager/test_secret_client.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,14 @@ def test_init_with_auth_token(self, mock_secret_manager_client):
123123
@patch(
124124
"google.adk.integrations.secret_manager.secret_client.default_service_credential"
125125
)
126+
@patch(
127+
"google.adk.integrations.secret_manager.secret_client.mtls_utils.get_api_endpoint"
128+
)
126129
def test_init_with_location(
127-
self, mock_default_service_credential, mock_secret_manager_client
130+
self,
131+
mock_get_api_endpoint,
132+
mock_default_service_credential,
133+
mock_secret_manager_client,
128134
):
129135
"""Test initialization with a specific location."""
130136
# Setup
@@ -134,6 +140,7 @@ def test_init_with_location(
134140
"test-project",
135141
)
136142
location = "us-central1"
143+
mock_get_api_endpoint.return_value = "resolved-endpoint"
137144

138145
# Execute
139146
SecretManagerClient(location=location)
@@ -143,9 +150,14 @@ def test_init_with_location(
143150
call_kwargs = mock_secret_manager_client.call_args.kwargs
144151
assert call_kwargs["credentials"] == mock_credentials
145152
assert call_kwargs["client_options"] == {
146-
"api_endpoint": f"secretmanager.{location}.rep.googleapis.com"
153+
"api_endpoint": "resolved-endpoint"
147154
}
148155
assert call_kwargs["client_info"].user_agent == USER_AGENT
156+
mock_get_api_endpoint.assert_called_once_with(
157+
location,
158+
"secretmanager.{location}.rep.googleapis.com",
159+
"secretmanager.{location}.rep.mtls.googleapis.com",
160+
)
149161

150162
@patch(
151163
"google.adk.integrations.secret_manager.secret_client.default_service_credential"

0 commit comments

Comments
 (0)