Skip to content

Commit 19ac679

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Add regional endpoint support to SecretManagerClient
The `SecretManagerClient` now accepts a `location` parameter to connect to a regional Secret Manager endpoint. PiperOrigin-RevId: 894356924
1 parent 1b05842 commit 19ac679

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ def __init__(
4242
self,
4343
service_account_json: Optional[str] = None,
4444
auth_token: Optional[str] = None,
45+
location: Optional[str] = None,
4546
):
4647
"""Initializes the SecretManagerClient.
4748
4849
Args:
4950
service_account_json: The content of a service account JSON keyfile (as
5051
a string), not the file path. Must be valid JSON.
5152
auth_token: An existing Google Cloud authorization token.
53+
location: The Google Cloud location (region) to use for the Secret
54+
Manager service. If not provided, the global endpoint is used.
5255
5356
Raises:
5457
ValueError: If neither `service_account_json` nor `auth_token` is
@@ -92,8 +95,15 @@ def __init__(
9295
)
9396

9497
self._credentials = credentials
98+
99+
client_options = None
100+
if location:
101+
client_options = {
102+
"api_endpoint": f"secretmanager.{location}.rep.googleapis.com"
103+
}
104+
95105
self._client = secretmanager.SecretManagerServiceClient(
96-
credentials=self._credentials
106+
credentials=self._credentials, client_options=client_options
97107
)
98108

99109
def get_secret(self, resource_name: str) -> str:

tests/unittests/integrations/secret_manager/test_secret_client.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_init_with_default_credentials(
5050
scopes=["https://www.googleapis.com/auth/cloud-platform"]
5151
)
5252
mock_secret_manager_client.assert_called_once_with(
53-
credentials=mock_credentials
53+
credentials=mock_credentials, client_options=None
5454
)
5555
assert client._credentials == mock_credentials
5656
assert client._client == mock_secret_manager_client.return_value
@@ -80,7 +80,7 @@ def test_init_with_service_account_json(
8080
json.loads(service_account_json)
8181
)
8282
mock_secret_manager_client.assert_called_once_with(
83-
credentials=mock_credentials
83+
credentials=mock_credentials, client_options=None
8484
)
8585
assert client._credentials == mock_credentials
8686
assert client._client == mock_secret_manager_client.return_value
@@ -106,11 +106,38 @@ def test_init_with_auth_token(self, mock_secret_manager_client):
106106
# Verify
107107
mock_credentials.refresh.assert_called_once()
108108
mock_secret_manager_client.assert_called_once_with(
109-
credentials=mock_credentials
109+
credentials=mock_credentials, client_options=None
110110
)
111111
assert client._credentials == mock_credentials
112112
assert client._client == mock_secret_manager_client.return_value
113113

114+
@patch("google.cloud.secretmanager.SecretManagerServiceClient")
115+
@patch(
116+
"google.adk.integrations.secret_manager.secret_client.default_service_credential"
117+
)
118+
def test_init_with_location(
119+
self, mock_default_service_credential, mock_secret_manager_client
120+
):
121+
"""Test initialization with a specific location."""
122+
# Setup
123+
mock_credentials = MagicMock()
124+
mock_default_service_credential.return_value = (
125+
mock_credentials,
126+
"test-project",
127+
)
128+
location = "us-central1"
129+
130+
# Execute
131+
SecretManagerClient(location=location)
132+
133+
# Verify
134+
mock_secret_manager_client.assert_called_once_with(
135+
credentials=mock_credentials,
136+
client_options={
137+
"api_endpoint": f"secretmanager.{location}.rep.googleapis.com"
138+
},
139+
)
140+
114141
@patch(
115142
"google.adk.integrations.secret_manager.secret_client.default_service_credential"
116143
)

0 commit comments

Comments
 (0)