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 wrong GcpProjectIdProvider when using Secret Manager #3383

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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