diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/core/GcpProperties.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/core/GcpProperties.java index c29a98db59..a95fac5fff 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/core/GcpProperties.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/core/GcpProperties.java @@ -16,6 +16,8 @@ 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; @@ -23,10 +25,13 @@ 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; diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/secretmanager/SecretManagerConfigDataLocationResolver.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/secretmanager/SecretManagerConfigDataLocationResolver.java index f75c9a1d1f..f9f97e3ed6 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/secretmanager/SecretManagerConfigDataLocationResolver.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/secretmanager/SecretManagerConfigDataLocationResolver.java @@ -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; @@ -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; @@ -64,6 +66,9 @@ public List 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)); @@ -74,10 +79,10 @@ 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, @@ -85,6 +90,11 @@ private static void registerSecretManagerBeans(ConfigDataLocationResolverContext 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() @@ -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