Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
sai-sunder-s marked this conversation as resolved.
Show resolved Hide resolved
}

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);
sai-sunder-s marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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