Skip to content

Commit c441ab2

Browse files
wukathcopybara-github
authored andcommitted
fix: Support passing credentials to GCPSkillRegistry
This change allows users to pass custom credentials when initializing the GCPSkillRegistry. Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 947335970
1 parent ee7174d commit c441ab2

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/google/adk/integrations/skill_registry/gcp_skill_registry.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from google.adk.utils import _mtls_utils
2929
import google.auth
3030
import google.auth.credentials
31+
from google.auth.credentials import Credentials
3132
import google.auth.exceptions
3233
from google.auth.transport import mtls
3334
from google.auth.transport import requests as auth_requests
@@ -42,16 +43,17 @@ def __init__(
4243
*,
4344
project_id: str | None = None,
4445
location: str | None = None,
46+
credentials: Credentials | None = None,
4547
):
4648
"""Initializes the GCP Skill Registry.
4749
4850
Args:
4951
project_id: Optional GCP project ID. If omitted, loads from environment.
5052
location: Optional GCP location. If omitted, loads from environment.
53+
credentials: Optional credentials to use for the client.
5154
"""
5255
self.project_id = project_id or os.environ.get("GOOGLE_CLOUD_PROJECT")
5356
self.location = location or os.environ.get("GOOGLE_CLOUD_LOCATION")
54-
5557
# Set up SSL context for mTLS if needed
5658
self._ssl_context = None
5759
use_client_cert = _mtls_utils.use_client_cert_effective()
@@ -97,7 +99,7 @@ def __init__(
9799
"project_id and location must be specified or set via environment"
98100
" variables."
99101
)
100-
self._credentials: google.auth.credentials.Credentials | None = None
102+
self._credentials: Credentials | None = credentials
101103

102104
async def _get_headers(self) -> dict[str, str]:
103105
"""Refreshes credentials and returns authorization headers."""

tests/unittests/integrations/skill_registry/test_gcp_skill_registry.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,33 @@ async def mock_get(url, *unused_args, **kwargs):
378378

379379

380380
# pylint: enable=protected-access
381+
382+
383+
@pytest.mark.asyncio
384+
async def test_use_custom_credentials():
385+
"""Verifies that custom credentials are used when provided."""
386+
mock_creds = mock.MagicMock()
387+
mock_creds.valid = True
388+
mock_creds.token = "custom-token"
389+
mock_creds.quota_project_id = "custom-quota-project"
390+
391+
registry = gcp_skill_registry.GCPSkillRegistry(credentials=mock_creds)
392+
393+
mock_response = mock.MagicMock()
394+
mock_response.status_code = 200
395+
mock_response.json.return_value = {"skills": []}
396+
397+
with mock.patch(
398+
"httpx.AsyncClient.get", return_value=mock_response
399+
) as mock_get_called:
400+
await registry.search_skills(query="query")
401+
402+
mock_get_called.assert_called_once_with(
403+
"https://agentregistry.googleapis.com/v1alpha/projects/test-project/locations/us-central1/skills:search",
404+
headers={
405+
"Authorization": "Bearer custom-token",
406+
"Content-Type": "application/json",
407+
"x-goog-user-project": "custom-quota-project",
408+
},
409+
params={"search_string": "query"},
410+
)

0 commit comments

Comments
 (0)