Skip to content

Commit

Permalink
fix:refactor compute and cloudshell credentials to pass quota project…
Browse files Browse the repository at this point in the history
… to base class
  • Loading branch information
sai-sunder-s committed Jan 10, 2024
1 parent 5226e76 commit f48ba0d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public static CloudShellCredentials create(int authPort) {
return CloudShellCredentials.newBuilder().setAuthPort(authPort).build();
}

private CloudShellCredentials(int authPort) {
this.authPort = authPort;
private CloudShellCredentials(Builder builder) {
super(builder);
this.authPort = builder.getAuthPort();
}

protected int getAuthPort() {
Expand Down Expand Up @@ -145,7 +146,7 @@ public int getAuthPort() {

@Override
public CloudShellCredentials build() {
return new CloudShellCredentials(authPort);
return new CloudShellCredentials(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ private ComputeEngineCredentials(ComputeEngineCredentials.Builder builder) {
builder.getHttpTransportFactory(),
getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY));
this.transportFactoryClassName = this.transportFactory.getClass().getName();

Collection<String> scopes = builder.getScopes();
// Use defaultScopes only when scopes don't exist.
Collection<String> scopesToUse = builder.scopes;
if (scopesToUse == null || scopesToUse.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ public void run() {
public static class Builder {

private AccessToken accessToken;
private Duration refreshMargin = DEFAULT_REFRESH_MARGIN;
private Duration expirationMargin = DEFAULT_EXPIRATION_MARGIN;
protected Duration refreshMargin = DEFAULT_REFRESH_MARGIN;
protected Duration expirationMargin = DEFAULT_EXPIRATION_MARGIN;

protected Builder() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,34 @@ public void getDefaultCredentials_quota_project() throws IOException {
testUserProvidesToken(testProvider, USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN);
}

@Test
public void getDefaultCredentials_compute_quotaProject() throws IOException {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
transportFactory.transport.setAccessToken(ACCESS_TOKEN);
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
testProvider.setEnv(
DefaultCredentialsProvider.QUOTA_PROJECT_ENV_VAR, QUOTA_PROJECT_FROM_ENVIRONMENT);

GoogleCredentials defaultCredentials = testProvider.getDefaultCredentials(transportFactory);

assertTrue(defaultCredentials instanceof ComputeEngineCredentials);
assertEquals(QUOTA_PROJECT_FROM_ENVIRONMENT, defaultCredentials.getQuotaProjectId());
}

@Test
public void getDefaultCredentials_cloudshell_quotaProject() throws IOException {
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory();
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
testProvider.setEnv(DefaultCredentialsProvider.CLOUD_SHELL_ENV_VAR, "4");
testProvider.setEnv(
DefaultCredentialsProvider.QUOTA_PROJECT_ENV_VAR, QUOTA_PROJECT_FROM_ENVIRONMENT);

GoogleCredentials defaultCredentials = testProvider.getDefaultCredentials(transportFactory);

assertTrue(defaultCredentials instanceof CloudShellCredentials);
assertEquals(QUOTA_PROJECT_FROM_ENVIRONMENT, defaultCredentials.getQuotaProjectId());
}

@Test
public void getDefaultCredentials_envNoGceCheck_noGceRequest() throws IOException {
MockRequestCountingTransportFactory transportFactory =
Expand Down

0 comments on commit f48ba0d

Please sign in to comment.