8000 feat: use bigquery scope by default in bigquery credentials. · NSFWExpert/adk-python@ba5b80d · GitHub
[go: up one dir, main page]

Skip to content

Commit ba5b80d

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: use bigquery scope by default in bigquery credentials.
Right now the agent builder has to specify the bigquery scope explicitly for bigquery tools, which is somewhat unnecessary. With this change the user does not have to specify scopes, although they would still have the ability to overrides scopes if necessary. PiperOrigin-RevId: 765354010
1 parent 8759a25 commit ba5b80d

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

contributing/samples/bigquery/agent.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
credentials_config = BigQueryCredentialsConfig(
3737
client_id=os.getenv("OAUTH_CLIENT_ID"),
3838
client_secret=os.getenv("OAUTH_CLIENT_SECRET"),
39-
scopes=["https://www.googleapis.com/auth/bigquery"],
4039
)
4140

4241
bigquery_toolset = BigQueryToolset(credentials_config=credentials_config)

src/google/adk/tools/bigquery/bigquery_credentials.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from ..tool_context import ToolContext
3535

3636
BIGQUERY_TOKEN_CACHE_KEY = "bigquery_token_cache"
37+
BIGQUERY_DEFAULT_SCOPE = ["https://www.googleapis.com/auth/bigquery"]
3738

3839

3940
class BigQueryCredentialsConfig(BaseModel):
@@ -66,15 +67,14 @@ class BigQueryCredentialsConfig(BaseModel):
6667
client_secret: Optional[str] = None
6768
"""the oauth client secret to use."""
6869
scopes: Optional[List[str]] = None
69-
"""the scopes to use.
70-
"""
70+
"""the scopes to use."""
7171

7272
@model_validator(mode="after")
7373
def __post_init__(self) -> BigQueryCredentialsConfig:
7474
"""Validate that either credentials or client ID/secret are provided."""
7575
if not self.credentials and (not self.client_id or not self.client_secret):
7676
raise ValueError(
77-
"Must provide either credentials or client_id abd client_secret pair."
77+
"Must provide either credentials or client_id and client_secret pair."
7878
)
7979
if self.credentials and (
8080
self.client_id or self.client_secret or self.scopes
@@ -88,6 +88,10 @@ def __post_init__(self) -> BigQueryCredentialsConfig:
8888
self.client_id = self.credentials.client_id
8989
self.client_secret = self.credentials.client_secret
9090
self.scopes = self.credentials.scopes
91+
92+
if not self.scopes:
93+
self.scopes = BIGQUERY_DEFAULT_SCOPE
94+
9195
return self
9296

9397

tests/unittests/tools/bigquery/test_bigquery_credentials.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,58 @@ def test_valid_credentials_object(self):
4747
assert config.client_secret == "test_client_secret"
4848
assert config.scopes == ["https://www.googleapis.com/auth/calendar"]
4949

50-
def test_valid_client_id_secret_pair(self):
51-
"""Test that providing client ID and secret without credentials works.
50+
def test_valid_client_id_secret_pair_default_scope(self):
51+
"""Test that providing client ID and secret with default scope works.
5252
5353
This tests the scenario where users want to create new OAuth credentials
54-
from scratch using their application's client ID and secret.
54+
from scratch using their application's client ID and secret and does not
55+
specify the scopes explicitly.
5556
"""
5657
config = BigQueryCredentialsConfig(
5758
client_id="test_client_id",
5859
client_secret="test_client_secret",
59-
scopes=["https://www.googleapis.com/auth/bigquery"],
60+
)
61+
62+
assert config.credentials is None
63+
assert config.client_id == "test_client_id"
64+
assert config.client_secret == "test_client_secret"
65+
assert config.scopes == ["https://www.googleapis.com/auth/bigquery"]
66+
67+
def test_valid_client_id_secret_pair_w_scope(self):
68+
"""Test that providing client ID and secret with explicit scopes works.
69+
70+
This tests the scenario where users want to create new OAuth credentials
71+
from scratch using their application's client ID and secret and does specify
72+
the scopes explicitly.
73+
"""
74+
config = BigQueryCredentialsConfig(
75+
client_id="test_client_id",
76+
client_secret="test_client_secret",
77+
scopes=[
78+
"https://www.googleapis.com/auth/bigquery",
79+
"https://www.googleapis.com/auth/drive",
80+
],
81+
)
82+
83+
assert config.credentials is None
84+
assert config.client_id == "test_client_id"
85+
assert config.client_secret == "test_client_secret"
86+
assert config.scopes == [
87+
"https://www.googleapis.com/auth/bigquery",
88+
"https://www.googleapis.com/auth/drive",
89+
]
90+
91+
def test_valid_client_id_secret_pair_w_empty_scope(self):
92+
"""Test that providing client ID and secret with empty scope works.
93+
94+
This tests the corner case scenario where users want to create new OAuth
95+
credentials from scratch using their application's client ID and secret but
96+
specifies empty scope, in which case the default BQ scope is used.
97+
"""
98+
config = BigQueryCredentialsConfig(
99+
client_id="test_client_id",
100+
client_secret="test_client_secret",
101+
scopes=[],
60102
)
61103

62104
assert config.credentials is None
@@ -73,7 +115,7 @@ def test_missing_client_secret_raises_error(self):
73115
with pytest.raises(
74116
ValueError,
75117
match=(
76-
"Must provide either credentials or client_id abd client_secret"
118+
"Must provide either credentials or client_id and client_secret"
77119
" pair"
78120
),
79121
):
@@ -84,7 +126,7 @@ def test_missing_client_id_raises_error(self):
84126
with pytest.raises(
85127
ValueError,
86128
match=(
87-
"Must provide either credentials or client_id abd client_secret"
129+
"Must provide either credentials or client_id and client_secret"
88130
" pair"
89131
),
90132
):
@@ -99,7 +141,7 @@ def test_empty_configuration_raises_error(self):
99141
with pytest.raises(
100142
ValueError,
101143
match=(
102-
"Must provide either credentials or client_id abd client_secret"
144+
"Must provide either credentials or client_id and client_secret"
103145
" pair"
104146
),
105147
):

0 commit comments

Comments
 (0)
0