Skip to content

Commit 4f5cedb

Browse files
committed
fix(toolbox-adk): prevent swallowed exceptions in auth flows
Adds warnings with stack traces when ADC or 3LO auth fails unpredictably. Enforces strict exception checking in integration tests.
1 parent fe7ca86 commit 4f5cedb

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

packages/toolbox-adk/src/toolbox_adk/client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
from contextvars import ContextVar
1617
from typing import Any, Awaitable, Callable, Dict, Optional, Union
1718

@@ -118,8 +119,12 @@ def get_token() -> str:
118119
try:
119120
token = id_token.fetch_id_token(request, audience)
120121
return f"Bearer {token}"
121-
except Exception:
122+
except Exception as e:
122123
# Fallback to default credentials
124+
logging.warning(
125+
f"Failed to fetch ID token for audience {audience} using ADC: {e}. "
126+
"Falling back to google.auth.default()."
127+
)
123128
creds, _ = google.auth.default()
124129
if not creds.valid:
125130
creds.refresh(request)

packages/toolbox-adk/src/toolbox_adk/tool.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
from typing import Any, Awaitable, Callable, Dict, Optional, cast
1617

1718
import toolbox_core
@@ -146,6 +147,12 @@ async def run_async(
146147
ctx.error = e
147148
if "credential" in str(e).lower() or isinstance(e, ValueError):
148149
raise e
150+
151+
logging.warning(
152+
f"Unexpected error in get_auth_response during 3LO retrieval: {e}. "
153+
"Falling back to request_credential.",
154+
exc_info=True
155+
)
149156
# Fallback to request logic
150157
ctx_any = cast(Any, tool_context)
151158
ctx_any.request_credential(auth_config_adk)

packages/toolbox-adk/tests/integration/test_integration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ async def test_3lo_flow_simulation(self):
159159
# If it fails, strictly it's likely a 401 or similar from the backend interactions.
160160
# This confirms the wrapper proceeded to call the backend and did NOT request credentials again.
161161
mock_ctx_second.request_credential.assert_not_called()
162+
err_msg = str(e).lower()
163+
assert any(x in err_msg for x in ["401", "403", "unauthorized", "forbidden"]), f"Caught UNEXPECTED exception: {type(e).__name__}: {e}"
162164
print(f"Caught expected server exception with fake token: {e}")
163165

164166
finally:

0 commit comments

Comments
 (0)