Skip to content

Commit

Permalink
feat: add properties to customize universeDomain and endpoint in KMS …
Browse files Browse the repository at this point in the history
…module (#3104)

* feat: add properties to customize universeDomain and endpoint in kms
  • Loading branch information
mpeddada1 authored Aug 23, 2024
1 parent a891693 commit 1de0e36
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ public class GcpKmsAutoConfiguration {
private final GcpProjectIdProvider gcpProjectIdProvider;
private final CredentialsProvider credentialsProvider;

private final String universeDomain;
private final String endpoint;

public GcpKmsAutoConfiguration(
GcpProjectIdProvider coreProjectIdProvider,
GcpKmsProperties properties,
CredentialsProvider credentialsProvider)
throws IOException {
this.universeDomain = properties.getUniverseDomain();
this.endpoint = properties.getEndpoint();
this.gcpProjectIdProvider =
properties.getProjectId() != null
? properties::getProjectId
Expand All @@ -65,13 +70,17 @@ GcpProjectIdProvider getGcpProjectIdProvider() {
@ConditionalOnMissingBean
public KeyManagementServiceClient keyManagementClient(CredentialsProvider googleCredentials)
throws IOException {
KeyManagementServiceSettings settings =
KeyManagementServiceSettings.Builder settingsBuilder =
KeyManagementServiceSettings.newBuilder()
.setCredentialsProvider(this.credentialsProvider)
.setHeaderProvider(new UserAgentHeaderProvider(GcpKmsAutoConfiguration.class))
.build();

return KeyManagementServiceClient.create(settings);
.setHeaderProvider(new UserAgentHeaderProvider(GcpKmsAutoConfiguration.class));
if (this.universeDomain != null) {
settingsBuilder.setUniverseDomain(this.universeDomain);
}
if (this.endpoint != null) {
settingsBuilder.setEndpoint(this.endpoint);
}
return KeyManagementServiceClient.create(settingsBuilder.build());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class GcpKmsProperties implements CredentialsSupplier {

/** Overrides the GCP Project ID specified in the Core module. */
private String projectId;
private String universeDomain;

private String endpoint;

@Override
public Credentials getCredentials() {
Expand All @@ -44,4 +47,20 @@ public String getProjectId() {
public void setProjectId(String projectId) {
this.projectId = projectId;
}

public String getUniverseDomain() {
return universeDomain;
}

public void setUniverseDomain(String universeDomain) {
this.universeDomain = universeDomain;
}

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,58 @@ void testShouldTakeKmsProjectIdWhenPresent() {

@Test
void testShouldTakeCoreProjectId() {
this.contextRunner
.run(ctx -> {
this.contextRunner.run(
ctx -> {
GcpKmsAutoConfiguration autoConfiguration = ctx.getBean(GcpKmsAutoConfiguration.class);
assertThat(autoConfiguration.getGcpProjectIdProvider().getProjectId()).isEqualTo(
CORE_PROJECT_NAME);
assertThat(autoConfiguration.getGcpProjectIdProvider().getProjectId())
.isEqualTo(CORE_PROJECT_NAME);
});
}

@Test
void testUniverseDomain() {
this.contextRunner
.withPropertyValues("spring.cloud.gcp.kms.universe-domain=myUniverseDomain")
.run(
ctx -> {
KeyManagementServiceClient client = ctx.getBean(KeyManagementServiceClient.class);
assertThat(client.getSettings().getUniverseDomain()).isEqualTo("myUniverseDomain");
assertThat(client.getSettings().getEndpoint())
.isEqualTo("cloudkms.myUniverseDomain:443");
});
}

@Test
void testEndpoint() {
this.contextRunner
.withPropertyValues("spring.cloud.gcp.kms.endpoint=kms.example.com:123")
.run(
ctx -> {
KeyManagementServiceClient client = ctx.getBean(KeyManagementServiceClient.class);
assertThat(client.getSettings().getEndpoint()).isEqualTo("kms.example.com:123");
});
}

@Test
void testBothUniverseDomainAndEndpointSet() {
this.contextRunner
.withPropertyValues("spring.cloud.gcp.kms.universe-domain=myUniverseDomain")
.withPropertyValues("spring.cloud.gcp.kms.endpoint=kms.example.com:123")
.run(
ctx -> {
KeyManagementServiceClient client = ctx.getBean(KeyManagementServiceClient.class);
assertThat(client.getSettings().getUniverseDomain()).isEqualTo("myUniverseDomain");
assertThat(client.getSettings().getEndpoint()).isEqualTo("kms.example.com:123");
});
}

@Test
void testNoUniverseDomainOrEndpointSet_useClientDefault() {
this.contextRunner.run(
ctx -> {
KeyManagementServiceClient client = ctx.getBean(KeyManagementServiceClient.class);
assertThat(client.getSettings().getUniverseDomain()).isEqualTo("googleapis.com");
assertThat(client.getSettings().getEndpoint()).isEqualTo("cloudkms.googleapis.com:443");
});
}

Expand Down

0 comments on commit 1de0e36

Please sign in to comment.