Skip to content

Commit 3139855

Browse files
jazhang00copybara-github
authored andcommitted
fix(integrations): use the mTLS Model Armor endpoint when mTLS is enabled
ModelArmorPlugin passed the plain regional host as an explicit api_endpoint, so it never switched to the regional mTLS host. Certificate-bound credentials, such as Agent Identity on Agent Runtime and Cloud Run, are rejected by the plain host, so every screening call failed with UNAUTHENTICATED and, with the default block_on_screening_failure, every prompt was blocked. The endpoint is now resolved with _mtls_utils.get_api_endpoint, as other regional Google API clients in ADK do. Fixes #7553 Co-authored-by: Jason Zhang <jasoncz@google.com> PiperOrigin-RevId: 1000269814
1 parent 0491606 commit 3139855

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎src/google/adk/integrations/model_armor/_plugin.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from ...plugins.base_plugin import BasePlugin
4545
from ...tools.base_tool import BaseTool
4646
from ...tools.tool_context import ToolContext
47+
from ...utils import _mtls_utils
4748
from ._config import ModelArmorConfig
4849

4950
try:
@@ -474,8 +475,12 @@ def _extract_tool_result_text(result: Any) -> Optional[str]:
474475

475476

476477
def _regional_endpoint(location: str) -> str:
477-
"""Builds the Model Armor regional endpoint for a location."""
478-
return f'modelarmor.{location}.rep.googleapis.com'
478+
"""Returns the regional Model Armor endpoint, the mTLS one if enabled."""
479+
return _mtls_utils.get_api_endpoint(
480+
location=location,
481+
default_template='modelarmor.{location}.rep.googleapis.com',
482+
mtls_template='modelarmor.{location}.rep.mtls.googleapis.com',
483+
)
479484

480485

481486
def _shared_template_location(*template_names: Optional[str]) -> str:

‎tests/unittests/integrations/model_armor/test_model_armor_plugin.py‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from google.adk.integrations.model_armor._plugin import _TOOL_OUTPUT_CHUNK_OVERLAP_CHARS
3131
from google.adk.models.llm_request import LlmRequest
3232
from google.adk.models.llm_response import LlmResponse
33+
from google.adk.utils import _mtls_utils
3334
from google.auth.credentials import AnonymousCredentials
3435
from google.cloud import modelarmor_v1
3536
from google.genai import types
@@ -550,12 +551,31 @@ async def test_fully_screened_clean_content_passes_through():
550551
# --- Regional endpoint ------------------------------------------------------
551552

552553

553-
def test_regional_endpoint_format():
554-
"""The host pattern Model Armor serves regional traffic on."""
555-
assert (
556-
_regional_endpoint('us-central1')
557-
== 'modelarmor.us-central1.rep.googleapis.com'
558-
)
554+
@pytest.mark.parametrize(
555+
('mtls_setting', 'cert_available', 'expected'),
556+
[
557+
('never', True, 'modelarmor.us-central1.rep.googleapis.com'),
558+
('auto', False, 'modelarmor.us-central1.rep.googleapis.com'),
559+
('auto', True, 'modelarmor.us-central1.rep.mtls.googleapis.com'),
560+
('always', False, 'modelarmor.us-central1.rep.mtls.googleapis.com'),
561+
],
562+
)
563+
def test_regional_endpoint_follows_mtls_setting(
564+
monkeypatch, mtls_setting, cert_available, expected
565+
):
566+
"""The regional endpoint is the mTLS one if mTLS is enabled."""
567+
monkeypatch.setenv('GOOGLE_API_USE_MTLS_ENDPOINT', mtls_setting)
568+
with (
569+
mock.patch.object(
570+
_mtls_utils, 'use_client_cert_effective', return_value=cert_available
571+
),
572+
mock.patch.object(
573+
_mtls_utils.mtls,
574+
'has_default_client_cert_source',
575+
return_value=cert_available,
576+
),
577+
):
578+
assert _regional_endpoint('us-central1') == expected
559579

560580

561581
@pytest.mark.asyncio

0 commit comments

Comments
 (0)