Skip to content

Commit 3fa993b

Browse files
wukathcopybara-github
authored andcommitted
fix: Add mtls support for gcp_utils
Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 944121631
1 parent 80f0277 commit 3fa993b

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

src/google/adk/cli/utils/gcp_utils.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
from typing import Optional
2424
from typing import Tuple
2525

26+
from google.adk.utils import _mtls_utils
2627
import google.auth
2728
import google.auth.exceptions
2829
from google.auth.transport.requests import AuthorizedSession
2930
from google.auth.transport.requests import Request
3031
import requests
3132

3233
_VERTEX_AI_ENDPOINT = "https://{location}-aiplatform.googleapis.com/v1beta1"
34+
_VERTEX_AI_MTLS_ENDPOINT = (
35+
"https://{location}-aiplatform.mtls.googleapis.com/v1beta1"
36+
)
3337

3438

3539
def check_adc() -> bool:
@@ -75,7 +79,18 @@ def _call_vertex_express_api(
7579
"""Calls a Vertex AI Express API."""
7680
credentials, _ = google.auth.default()
7781
session = AuthorizedSession(credentials)
78-
url = f"{_VERTEX_AI_ENDPOINT.format(location=location)}/vertexExpress{action}"
82+
83+
if _mtls_utils.use_client_cert_effective():
84+
session.configure_mtls_channel()
85+
endpoint = _mtls_utils.get_api_endpoint(
86+
location=location,
87+
default_template=_VERTEX_AI_ENDPOINT,
88+
mtls_template=_VERTEX_AI_MTLS_ENDPOINT,
89+
)
90+
else:
91+
endpoint = _VERTEX_AI_ENDPOINT.format(location=location)
92+
93+
url = f"{endpoint}/vertexExpress{action}"
7994
headers = {
8095
"Content-Type": "application/json",
8196
}

tests/unittests/cli/utils/test_gcp_utils.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525

2626
class TestGcpUtils(unittest.TestCase):
2727

28+
def setUp(self):
29+
super().setUp()
30+
patcher = mock.patch(
31+
"google.adk.cli.utils.gcp_utils._mtls_utils.use_client_cert_effective",
32+
return_value=False,
33+
)
34+
self.mock_use_client_cert_effective = patcher.start()
35+
self.addCleanup(patcher.stop)
36+
2837
@mock.patch("google.auth.default")
2938
def test_check_adc_success(self, mock_auth_default):
3039
mock_auth_default.return_value = (mock.Mock(), "test-project")
@@ -168,6 +177,52 @@ def test_list_gcp_projects_import_error(self):
168177
):
169178
gcp_utils.list_gcp_projects()
170179

180+
@mock.patch("google.adk.cli.utils.gcp_utils.AuthorizedSession")
181+
@mock.patch("google.auth.default")
182+
def test_retrieve_express_project_mtls_enabled(
183+
self, mock_auth_default, mock_session_cls
184+
):
185+
# Enable mtls
186+
self.mock_use_client_cert_effective.return_value = True
187+
188+
mock_auth_default.return_value = (mock.Mock(), "test-project-id")
189+
190+
mock_session = mock.Mock()
191+
mock_session_cls.return_value = mock_session
192+
mock_response = mock.Mock()
193+
mock_response.json.return_value = {
194+
"expressProject": {
195+
"projectId": "test-project",
196+
"defaultApiKey": "test-api-key",
197+
"region": "us-central1",
198+
}
199+
}
200+
mock_session.get.return_value = mock_response
201+
202+
with mock.patch(
203+
"google.adk.cli.utils.gcp_utils._mtls_utils.get_api_endpoint",
204+
return_value=(
205+
"https://us-central1-aiplatform.mtls.googleapis.com/v1beta1"
206+
),
207+
) as mock_get_api_endpoint:
208+
result = gcp_utils.retrieve_express_project()
209+
210+
self.assertEqual(result["project_id"], "test-project")
211+
mock_session.configure_mtls_channel.assert_called_once()
212+
mock_get_api_endpoint.assert_called_once_with(
213+
location="us-central1",
214+
default_template="https://{location}-aiplatform.googleapis.com/v1beta1",
215+
mtls_template=(
216+
"https://{location}-aiplatform.mtls.googleapis.com/v1beta1"
217+
),
218+
)
219+
mock_session.get.assert_called_once()
220+
args, _ = mock_session.get.call_args
221+
self.assertEqual(
222+
args[0],
223+
"https://us-central1-aiplatform.mtls.googleapis.com/v1beta1/vertexExpress:retrieveExpressProject",
224+
)
225+
171226

172227
if __name__ == "__main__":
173228
unittest.main()

0 commit comments

Comments
 (0)