Skip to content

Commit 150b721

Browse files
committed
fix:refactor compute and cloudshell credentials to pass quota project to base class
1 parent aa04739 commit 150b721

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ public static CloudShellCredentials create(int authPort) {
6666
return CloudShellCredentials.newBuilder().setAuthPort(authPort).build();
6767
}
6868

69-
private CloudShellCredentials(int authPort) {
70-
this.authPort = authPort;
69+
private CloudShellCredentials(Builder builder) {
70+
super(builder);
71+
this.authPort = builder.getAuthPort();
7172
}
7273

7374
protected int getAuthPort() {
@@ -141,7 +142,7 @@ public int getAuthPort() {
141142
}
142143

143144
public CloudShellCredentials build() {
144-
return new CloudShellCredentials(authPort);
145+
return new CloudShellCredentials(this);
145146
}
146147
}
147148
}

oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,23 @@ public class ComputeEngineCredentials extends GoogleCredentials
120120
private transient String serviceAccountEmail;
121121

122122
/**
123-
* Constructor with overridden transport.
123+
* Constructor with builder
124124
*
125-
* @param transportFactory HTTP transport factory, creates the transport used to get access
126-
* tokens.
127-
* @param scopes scope strings for the APIs to be called. May be null or an empty collection.
128-
* @param defaultScopes default scope strings for the APIs to be called. May be null or an empty
129-
* collection. Default scopes are ignored if scopes are provided.
125+
* @param builder builder to use for constructing the credential
130126
*/
131-
private ComputeEngineCredentials(
132-
HttpTransportFactory transportFactory,
133-
Collection<String> scopes,
134-
Collection<String> defaultScopes) {
135-
super(/* accessToken= */ null, COMPUTE_REFRESH_MARGIN, COMPUTE_EXPIRATION_MARGIN);
127+
private ComputeEngineCredentials(Builder builder) {
128+
super(builder);
136129

137130
this.transportFactory =
138131
firstNonNull(
139-
transportFactory,
132+
builder.getHttpTransportFactory(),
140133
getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY));
141134
this.transportFactoryClassName = this.transportFactory.getClass().getName();
135+
136+
Collection<String> scopes = builder.getScopes();
142137
// Use defaultScopes only when scopes don't exist.
143138
if (scopes == null || scopes.isEmpty()) {
144-
scopes = defaultScopes;
139+
scopes = builder.getDefaultScopes();
145140
}
146141
if (scopes == null) {
147142
this.scopes = ImmutableSet.<String>of();
@@ -155,14 +150,14 @@ private ComputeEngineCredentials(
155150
/** Clones the compute engine account with the specified scopes. */
156151
@Override
157152
public GoogleCredentials createScoped(Collection<String> newScopes) {
158-
return new ComputeEngineCredentials(this.transportFactory, newScopes, null);
153+
return toBuilder().setScopes(newScopes).build();
159154
}
160155

161156
/** Clones the compute engine account with the specified scopes. */
162157
@Override
163158
public GoogleCredentials createScoped(
164159
Collection<String> newScopes, Collection<String> newDefaultScopes) {
165-
return new ComputeEngineCredentials(this.transportFactory, newScopes, newDefaultScopes);
160+
return toBuilder().setScopes(newScopes).setDefaultScopes(newDefaultScopes).build();
166161
}
167162

168163
/**
@@ -171,7 +166,7 @@ public GoogleCredentials createScoped(
171166
* @return new ComputeEngineCredentials
172167
*/
173168
public static ComputeEngineCredentials create() {
174-
return new ComputeEngineCredentials(null, null, null);
169+
return newBuilder().build();
175170
}
176171

177172
public final Collection<String> getScopes() {
@@ -541,10 +536,16 @@ private String getDefaultServiceAccount() throws IOException {
541536
public static class Builder extends GoogleCredentials.Builder {
542537
private HttpTransportFactory transportFactory;
543538
private Collection<String> scopes;
539+
private Collection<String> defaultScopes;
544540

545-
protected Builder() {}
541+
protected Builder() {
542+
// Override the default refresh and expiration margin set by OAuth2Credentials.Builder
543+
this.refreshMargin = COMPUTE_REFRESH_MARGIN;
544+
this.expirationMargin = COMPUTE_EXPIRATION_MARGIN;
545+
}
546546

547547
protected Builder(ComputeEngineCredentials credentials) {
548+
this();
548549
this.transportFactory = credentials.transportFactory;
549550
this.scopes = credentials.scopes;
550551
}
@@ -559,6 +560,11 @@ public Builder setScopes(Collection<String> scopes) {
559560
return this;
560561
}
561562

563+
public Builder setDefaultScopes(Collection<String> defaultScopes) {
564+
this.defaultScopes = defaultScopes;
565+
return this;
566+
}
567+
562568
public HttpTransportFactory getHttpTransportFactory() {
563569
return transportFactory;
564570
}
@@ -567,8 +573,12 @@ public Collection<String> getScopes() {
567573
return scopes;
568574
}
569575

576+
public Collection<String> getDefaultScopes() {
577+
return defaultScopes;
578+
}
579+
570580
public ComputeEngineCredentials build() {
571-
return new ComputeEngineCredentials(transportFactory, scopes, null);
581+
return new ComputeEngineCredentials(this);
572582
}
573583
}
574584
}

oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,8 @@ public void run() {
637637
public static class Builder {
638638

639639
private AccessToken accessToken;
640-
private Duration refreshMargin = DEFAULT_REFRESH_MARGIN;
641-
private Duration expirationMargin = DEFAULT_EXPIRATION_MARGIN;
640+
protected Duration refreshMargin = DEFAULT_REFRESH_MARGIN;
641+
protected Duration expirationMargin = DEFAULT_EXPIRATION_MARGIN;
642642

643643
protected Builder() {}
644644

oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,34 @@ public void getDefaultCredentials_quota_project() throws IOException {
470470
testUserProvidesToken(testProvider, USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN);
471471
}
472472

473+
@Test
474+
public void getDefaultCredentials_compute_quotaProject() throws IOException {
475+
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
476+
transportFactory.transport.setAccessToken(ACCESS_TOKEN);
477+
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
478+
testProvider.setEnv(
479+
DefaultCredentialsProvider.QUOTA_PROJECT_ENV_VAR, QUOTA_PROJECT_FROM_ENVIRONMENT);
480+
481+
GoogleCredentials defaultCredentials = testProvider.getDefaultCredentials(transportFactory);
482+
483+
assertTrue(defaultCredentials instanceof ComputeEngineCredentials);
484+
assertEquals(QUOTA_PROJECT_FROM_ENVIRONMENT, defaultCredentials.getQuotaProjectId());
485+
}
486+
487+
@Test
488+
public void getDefaultCredentials_cloudshell_quotaProject() throws IOException {
489+
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory();
490+
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
491+
testProvider.setEnv(DefaultCredentialsProvider.CLOUD_SHELL_ENV_VAR, "4");
492+
testProvider.setEnv(
493+
DefaultCredentialsProvider.QUOTA_PROJECT_ENV_VAR, QUOTA_PROJECT_FROM_ENVIRONMENT);
494+
495+
GoogleCredentials defaultCredentials = testProvider.getDefaultCredentials(transportFactory);
496+
497+
assertTrue(defaultCredentials instanceof CloudShellCredentials);
498+
assertEquals(QUOTA_PROJECT_FROM_ENVIRONMENT, defaultCredentials.getQuotaProjectId());
499+
}
500+
473501
@Test
474502
public void getDefaultCredentials_envNoGceCheck_noGceRequest() throws IOException {
475503
MockRequestCountingTransportFactory transportFactory =

0 commit comments

Comments
 (0)