Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/google/adk/tools/bigquery/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class BigQueryToolConfig(BaseModel):
their application/agent for tracking or support purpose, they can set this field.
"""

compute_project_id: Optional[str] = None
"""GCP project ID to use for the BigQuery compute operations.

This can be set as a guardrail to ensure that the tools perform the compute
operations (such as query execution) in a specific project.
"""

@field_validator('application_name')
@classmethod
def validate_application_name(cls, v):
Expand Down
14 changes: 14 additions & 0 deletions src/google/adk/tools/bigquery/query_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ def execute_sql(
}
"""
try:
# Validate compute project if applicable
if (
settings.compute_project_id
and project_id != settings.compute_project_id
):
return {
"status": "ERROR",
"error_details": (
f"Cannot execute query in the project {project_id}, as the tool"
" is restricted to execute queries only in the project"
f" {settings.compute_project_id}."
),
}

# Get BigQuery client
bq_client = client.get_bigquery_client(
project=project_id,
Expand Down
22 changes: 22 additions & 0 deletions tests/unittests/tools/bigquery/test_bigquery_query_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,3 +1006,25 @@ def test_execute_sql_bq_client_creation(mock_get_bigquery_client):
mock_get_bigquery_client.call_args.kwargs["user_agent"]
== application_name
)


def test_execute_sql_unexpected_project_id():
"""Test execute_sql tool invocation with unexpected project id."""
compute_project_id = "compute_project_id"
tool_call_project_id = "project_id"
query = "SELECT 1"
credentials = mock.create_autospec(Credentials, instance=True)
tool_settings = BigQueryToolConfig(compute_project_id=compute_project_id)
tool_context = mock.create_autospec(ToolContext, instance=True)

result = execute_sql(
tool_call_project_id, query, credentials, tool_settings, tool_context
)
assert result == {
"status": "ERROR",
"error_details": (
f"Cannot execute query in the project {tool_call_project_id}, as the"
" tool is restricted to execute queries only in the project"
f" {compute_project_id}."
),
}