Skip to content

Commit

Permalink
fix: refactor compute and cloudshell credentials to pass quota projec…
Browse files Browse the repository at this point in the history
…t to base class (#1284)

* fix:refactor compute and cloudshell credentials to pass quota project to base class

* rebase

* remove protected

* remove empty line

* Add tests

* format fix
  • Loading branch information
sai-sunder-s authored Jan 17, 2024
1 parent a7a8d7a commit fb75239
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
13 changes: 10 additions & 3 deletions oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java
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 @@ -139,13 +140,19 @@ public Builder setAuthPort(int authPort) {
return this;
}

@CanIgnoreReturnValue
public Builder setQuotaProjectId(String quotaProjectId) {
super.quotaProjectId = quotaProjectId;
return this;
}

public int getAuthPort() {
return authPort;
}

@Override
public CloudShellCredentials build() {
return new CloudShellCredentials(authPort);
return new CloudShellCredentials(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,20 @@ public void serialize() throws IOException, ClassNotFoundException {
assertEquals(credentials.toString(), deserializedCredentials.toString());
assertSame(deserializedCredentials.clock, Clock.SYSTEM);
}

@Test
public void toBuilder() {
CloudShellCredentials credentials =
CloudShellCredentials.newBuilder()
.setAuthPort(42)
.setQuotaProjectId("quota-project")
.build();

assertEquals(42, credentials.getAuthPort());
assertEquals("quota-project", credentials.getQuotaProjectId());

CloudShellCredentials secondCredentials = credentials.toBuilder().build();

assertEquals(credentials, secondCredentials);
}
}
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 fb75239

Please sign in to comment.