GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: ComputeEngineCredential supports specifying scopes by ahmedtd · Pull Request #457 · googleapis/google-auth-library-java

This repository was archived by the owner on May 12, 2026. It is now read-only.

Conversation

@ahmedtd

@ahmedtd ahmedtd commented Aug 6, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

The existing behavior of ComputeEngineCredentials is to not provide the ?scopes parameter in its access token request to the metadata server. This is workable on GCE, which has the concept of a scope lock / default scopes applied to the VM. However, neither new AppEngine nor GKE Workload Identity have this concept; they just return a hardcoded set of default scopes if no
specific scopes are requested. This makes usage of some Google APIs impossible using the Java client libraries.

This change adds scope support to ComputeEngineCredentials. It's optional, so the current behavior will stay the default.
However, if the user specifies scopes on the builder or using createScoped, then those scopes will be passed to the metadata server.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahmedtd
ahmedtd requested a review from a team August 6, 2020 00:43
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Aug 6, 2020
@ahmedtd

ahmedtd commented Aug 6, 2020

Copy link
Copy Markdown
Author

@chingor13 This is the patch we discussed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

elharo
elharo reviewed Aug 6, 2020
View reviewed changes

@elharo elharo left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It really helps if changes like this are associated with a detailed issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pom.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

chingor13
chingor13 suggested changes Aug 6, 2020
View reviewed changes
Comment thread oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java Outdated
*/
public static ComputeEngineCredentials create() {
return new ComputeEngineCredentials(null);
return new ComputeEngineCredentials(ImmutableSet.<String>of(), null);

@chingor13 chingor13 Aug 6, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Should this default to null instead?

An empty collection of scopes might make the url something like https://some-url/?scopes= which may be correct if you ask for no scopes, but if you pass null, it should exclude the scopes param.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahmedtd ahmedtd Aug 7, 2020

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes, you're right. I originally wrote this so that an empty collection would result in not sending the ?scopes param, but then changed it to null.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahmedtd ahmedtd Aug 7, 2020

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Done

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chingor13

chingor13 commented Aug 6, 2020

Copy link
Copy Markdown
Contributor

cc @salrashid123

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chingor13 chingor13 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 6, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 6, 2020
@ahmedtd
ahmedtd force-pushed the gce-scopes branch from 72a3b06 to 828fadb Compare August 7, 2020 01:47
@ahmedtd
feat: ComputeEngineCredential supports specifying scopes
fe7a190
The existing behavior of ComputeEngineCredentials is to not provide the
`?scopes` parameter in its access token request to the metadata
server.  This is workable on GCE, which has the concept of a scope
lock / default scopes applied to the VM.  However, neither new
AppEngine nor GKE Workload Identity have this concept; they just
return a hardcoded set of default scopes if no specific scopes are
requested.  This makes usage of some Google APIs impossible using the
Java client libraries.

This change adds scope support to ComputeEngineCredentials.  It's
optional, so the current behavior will stay the default.  However, if
the user specifies scopes on the builder or using `createScoped`, then
those scopes will be passed to the metadata server.
@ahmedtd
ahmedtd force-pushed the gce-scopes branch from 828fadb to fe7a190 Compare August 7, 2020 01:52
@chingor13 chingor13 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 7, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 7, 2020
chingor13
chingor13 suggested changes Aug 7, 2020
View reviewed changes

@chingor13 chingor13 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Also please run mvn com.coveo:fmt-maven-plugin:format to appease the linter.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java
HttpResponse response = getMetadataResponse(getTokenServerEncodedUrl());
GenericUrl url = new GenericUrl(getTokenServerEncodedUrl());
if (scopes != null) {
url.set("scopes", String.join(",", scopes));

@chingor13 chingor13 Aug 7, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks like String.join was added in Java 8 and this library still currently support Java 7.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@elharo elharo Aug 10, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You can use Guava's Joiner in this class.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahmedtd

ahmedtd commented Aug 8, 2020

Copy link
Copy Markdown
Author

@elharo I opened #458 . I have some follow up work to confirm that this works the same way in the AppEngine flex environment.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

elharo
elharo reviewed Aug 10, 2020
View reviewed changes
Comment thread oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java
HttpResponse response = getMetadataResponse(getTokenServerEncodedUrl());
GenericUrl url = new GenericUrl(getTokenServerEncodedUrl());
if (scopes != null) {
url.set("scopes", String.join(",", scopes));

@elharo elharo Aug 10, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You can use Guava's Joiner in this class.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java
}

public Builder setScopes(Collection<String> scopes) {
this.scopes = scopes;

@elharo elharo Aug 10, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You probably want a defensive copy to avoid exposing mutable internal state.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java
return transportFactory;
}

public Collection<String> getScopes() {

@elharo elharo Aug 10, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You probably want a defensive copy to avoid exposing mutable internal state.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread oauth2_http/javatests/com/google/auth/oauth2/MockMetadataServerTransport.java
GenericUrl parsedURL = new GenericUrl(url);

if ("/computeMetadata/v1/instance/service-accounts/default/token".equals(parsedURL.getRawPath())) {
String scopes = (String)parsedURL.getFirst("scopes");

@elharo elharo Aug 10, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

space after cast

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chingor13

chingor13 commented Dec 16, 2020

Copy link
Copy Markdown
Contributor

Addressed in #514

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chingor13 chingor13 closed this Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

2 more reviewers
@elharo elharo elharo left review comments
@chingor13 chingor13 chingor13 requested changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@ahmedtd @chingor13 @elharo @kokoro-team
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.