feat: ServiceAccount with ID token option#4474
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
/gemini review |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
…edential potentially being None.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds the ability to use ID tokens with Service Accounts, which is a great feature for service-to-service authentication. The implementation looks solid, with changes to the ServiceAccount model, the credential exchanger logic, and corresponding unit tests.
I've left a few comments on potential improvements:
- In
service_account_exchanger.py, there are a couple of redundant checks forservice_account_credentialthat can be removed to simplify the code, as this is already handled at the beginning of the function. I also noticed an opportunity to reuse aRequestobject for better performance and consistency. - In the new tests, there's a redundant
monkeypatchcall that can be removed.
Overall, the changes are well-implemented and tested. Addressing these minor points will improve code clarity and maintainability.
| if config.service_account_credential is None: | ||
| raise ValueError("service_account_credential is required when use_default_credential is False") | ||
|
|
There was a problem hiding this comment.
This check for service_account_credential is redundant. A check at the beginning of the exchange_credential method (lines 62-69) already ensures service_account_credential is provided when use_default_credential is false. This check also raises a more specific AuthCredentialMissingError. Please remove this redundant check and the extra newline.
| if config.service_account_credential is None: | ||
| raise ValueError("service_account_credential is required when use_default_credential is False") |
| ) | ||
| quota_project_id = None | ||
|
|
||
| credentials.refresh(Request()) |
There was a problem hiding this comment.
For consistency and potential performance benefits, it's better to reuse the request object that was instantiated on line 79. The google-auth library is designed to allow reusing transport sessions. Please change this to credentials.refresh(request).
| credentials.refresh(Request()) | |
| credentials.refresh(request) |
| monkeypatch.setattr( | ||
| google.oauth2.id_token, | ||
| "fetch_id_token", | ||
| mock_fetch_id_token, | ||
| ) |
There was a problem hiding this comment.
|
Hi @lpezet , Thank you for your contribution! We appreciate you taking the time to submit this pull request.
|
I've been burnt before contributing to other Google projects, and this really is an Auth concern that, as other comments in the code mentioned, might need more thoughts than my little PR. READ: I'm not going to do much more unless you can tell me this has high chances to be merged. |
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
ServiceAccountCredentialExchangerimplementation is restricted to OAuth2-type tokens (access token).For Service-to-Service authentication, ID token+audience is needed.
Solution:
Provide attributes in
ServiceAccountauth config to specify ID token-type auth.Testing Plan
Unit Tests:
NB: Some of
test_cli_deploy.pyfail for me but I have not changed anything there:Manual End-to-End (E2E) Tests:
In my setup I have a custom MCP Server deployed to Cloud Run running under a SA "producer", NOT public, and with a SA "consumer" granted "role/run.invoker" on that service.
Paste the following:
Checklist
Additional context