Skip to content

Conversation

@zhumin8
Copy link
Contributor

@zhumin8 zhumin8 commented Jul 3, 2024

Fixes #1387 ☕️

As described in the original issue, this looks like a regression introduced in 7e26861 when migrating from deprecated constructor to use builder.
Access token is scoped and should be invalidated when scope changes.

This PR include changes:

  • ComputeEngineCredentials.createScoped() should invalidate existing AccessToken
  • ComputeEngineCredentials.createScoped(newScopes, newDefaultScopes) should respect universe domain settings.
  • For testing, update to mock transport to support returning access token when url is scoped. Should return a different access token compared to default url with no scopes.
  • Other credential types: e.g. ServiceAccountCredentials.creatScoped() should also invalidate existing AccessToken Will raise separate pr for this.

Follow up items not included in this PR:

  • Check if other GoogleCredentials subclasses that overrides createScoped() suffer from same issue (e.g. ServiceAccountCredentials.creatScoped() )

@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Jul 3, 2024
@zhumin8 zhumin8 requested a review from westarle July 3, 2024 19:45
@zhumin8 zhumin8 requested review from lqiu96 and removed request for westarle July 11, 2024 16:04
@zhumin8 zhumin8 marked this pull request as ready for review July 11, 2024 16:04
@zhumin8 zhumin8 requested review from a team as code owners July 11, 2024 16:04
Comment on lines 165 to 171
ComputeEngineCredentials.Builder builder =
ComputeEngineCredentials.newBuilder()
.setHttpTransportFactory(transportFactory)
.setScopes(newScopes)
.setDefaultScopes(newDefaultScopes);
(Builder)
this.toBuilder()
.setHttpTransportFactory(transportFactory)
.setScopes(newScopes)
.setDefaultScopes(newDefaultScopes)
.setAccessToken(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user passes in the same set of scopes (i.e. does not actually change the set of scopes for the credentials), this would invalidate the access token, right? Do you think it would make sense to check to see if the scopes changed and only invalidate the access token then? If that makes sense, perhaps that could be done in GoogleCredentials so that it could apply to every single type of Credentials?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to narrow the scope of this pr only fix this regression in behavior introduced by 7e26861. To matches the original behavior.
That said, in a follow up PR, I am considering moving the logic upstream to GoogleCredentials or OAuth2Credentials. One thought is aside from createScoped(), does the same issue exists in creating copies and setting other properties differently, e.g. set a different universe. So perhaps going to invalidate directly in toBuilder(). We can discuss further when I put up that PR.

If a user passes in the same set of scopes (i.e. does not actually change the set of scopes for the credentials), this would invalidate the access token, right?

In this case, the old access token would probably work too, but it would be safer or at least no harm to invalidate it anyway and let the refresh workflow request the token from server again.

Do you think it would make sense to check to see if the scopes changed and only invalidate the access token then? If that makes sense, perhaps that could be done in GoogleCredentials so that it could apply to every single type of Credentials?

Hmm, let's discuss more in the follow-up pr, this will be a new check, and I'd rather introduce it in GoogleCredentials than individual credential classes if possible.

Copy link
Member

@lqiu96 lqiu96 Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. We'll keep this scope for just fixing the bug and address anything else in a follow up PR.

In this case, the old access token would probably work too, but it would be safer or at least no harm to invalidate it anyway and let the refresh workflow request the token from server again.

Yep makes sense. I'm just wondering what the expected behavior is from the user POV is and wondering what the behavior is from other language auth libraries. IMO, I think it makes sense to always invalidate the access token when createScoped() is called even if the access token is technically valid. Let's aim to keep it consistent between languages if possible.

Comment on lines 167 to 168
if (url.contains("?scopes=")) {
refreshContents.put("access_token", "fake access token with scope");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, could the fake access token not be hard coded in the MockMetaDataServerTransport code?

Perhaps we could make the logic something like:

  1. MockMetaDataServerTransport takes a map of scopes -> access token pairings/ MockMetaDataServerTransport.addAccessTokenScope(scope, accessToken) will add the the pairing
  2. If url.contains("?scopes="), check the map for scopes and returns the configured access token
  3. Test passes the pairing of (scope, accesstoken) to MockMetaDataServerTransport

Comment on lines +72 to +74
if (scopesToAccessToken == null) {
scopesToAccessToken = new HashMap<>();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: initialize in the constructor

Copy link
Member

@lqiu96 lqiu96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but one last review from Wes

@sonarqubecloud
Copy link

@lqiu96 lqiu96 requested a review from westarle July 31, 2024 21:54
Copy link
Contributor

@arithmetic1728 arithmetic1728 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ComputeEngineCredentials.createScoped copies existing AccessToken

3 participants