Skip to content

Commit

Permalink
fix: Secret Manager module configures wrong GcpProjectIdProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickGotthard committed Nov 18, 2024
1 parent a12638c commit 1bb45e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@

package com.google.cloud.spring.autoconfigure.core;

import static com.google.cloud.spring.autoconfigure.core.GcpProperties.PREFIX;

import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.CredentialsSupplier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.ImportRuntimeHints;

/** Top-level auto-config settings. */
@ConfigurationProperties("spring.cloud.gcp")
@ConfigurationProperties(PREFIX)
@ImportRuntimeHints(CredentialsRuntimeHints.class)
public class GcpProperties implements CredentialsSupplier {

/** Configuration prefix. */
public static final String PREFIX = "spring.cloud.gcp";

/** GCP project ID where services are running. */
private String projectId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
import com.google.cloud.spring.autoconfigure.core.GcpProperties;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.core.DefaultGcpProjectIdProvider;
import com.google.cloud.spring.core.GcpProjectIdProvider;
Expand All @@ -29,6 +30,7 @@
import org.apache.arrow.util.VisibleForTesting;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolver;
Expand Down Expand Up @@ -64,6 +66,9 @@ public List<SecretManagerConfigDataResource> resolve(ConfigDataLocationResolverC
}

private static void registerSecretManagerBeans(ConfigDataLocationResolverContext context) {
// Register the Core properties.
registerBean(
context, GcpProperties.class, getGcpProperties(context));
// Register the Secret Manager properties.
registerBean(
context, GcpSecretManagerProperties.class, getSecretManagerProperties(context));
Expand All @@ -74,17 +79,22 @@ private static void registerSecretManagerBeans(ConfigDataLocationResolverContext
// lazy register the client solely for unit test.
BootstrapRegistry.InstanceSupplier.from(() -> createSecretManagerClient(context)));
// Register the GCP Project ID provider.
registerAndPromoteBean(
registerBean(
context,
GcpProjectIdProvider.class,
BootstrapRegistry.InstanceSupplier.of(createProjectIdProvider(context)));
createProjectIdProvider(context));
// Register the Secret Manager template.
registerAndPromoteBean(
context,
SecretManagerTemplate.class,
BootstrapRegistry.InstanceSupplier.of(createSecretManagerTemplate(context)));
}

private static GcpProperties getGcpProperties(
ConfigDataLocationResolverContext context) {
return context.getBinder().bind(GcpProperties.PREFIX, GcpProperties.class).orElse(new GcpProperties());
}

private static GcpSecretManagerProperties getSecretManagerProperties(
ConfigDataLocationResolverContext context) {
return context.getBinder()
Expand All @@ -94,10 +104,21 @@ private static GcpSecretManagerProperties getSecretManagerProperties(

private static GcpProjectIdProvider createProjectIdProvider(
ConfigDataLocationResolverContext context) {
GcpSecretManagerProperties properties = context.getBootstrapContext()
.get(GcpSecretManagerProperties.class);
return properties.getProjectId() != null
? properties::getProjectId : new DefaultGcpProjectIdProvider();

ConfigurableBootstrapContext bootstrapContext = context.getBootstrapContext();

GcpSecretManagerProperties secretManagerProperties = bootstrapContext.get(GcpSecretManagerProperties.class);
if(secretManagerProperties.getProjectId() != null) {
return secretManagerProperties::getProjectId;
}

GcpProperties gcpProperties = bootstrapContext.get(GcpProperties.class);
if(gcpProperties.getProjectId() != null) {
return gcpProperties::getProjectId;
}

return new DefaultGcpProjectIdProvider();

}

@VisibleForTesting
Expand Down

0 comments on commit 1bb45e4

Please sign in to comment.