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

Allow placeholders in Cloud SQL config #495

Merged
merged 2 commits into from
Jun 8, 2021
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 @@ -31,9 +31,12 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.bind.PlaceholdersResolver;
import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -67,8 +70,10 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
LOGGER.info("post-processing Cloud SQL properties for + " + databaseType.name());
}

// Bind properties without resolving placeholders
Binder binder = new Binder(ConfigurationPropertySources.get(environment), null, null, null, null);
// Bind properties without resolving Secret Manager placeholders
Binder binder = new Binder(ConfigurationPropertySources.get(environment),
new NonSecretsManagerPropertiesPlaceholdersResolver(environment),
null, null, null);

String cloudSqlPropertiesPrefix = GcpCloudSqlProperties.class.getAnnotation(ConfigurationProperties.class).value();
GcpCloudSqlProperties sqlProperties = binder
Expand Down Expand Up @@ -189,4 +194,21 @@ private void setCredentialsFileProperty(Resource credentialsLocation) {
}
}

private static class NonSecretsManagerPropertiesPlaceholdersResolver implements PlaceholdersResolver {
private PlaceholdersResolver resolver;

NonSecretsManagerPropertiesPlaceholdersResolver(Environment environment) {
this.resolver = new PropertySourcesPlaceholdersResolver(environment);
}

@Override
public Object resolvePlaceholders(Object value) {
if (value.toString().contains("sm://")) {
return value;
}
else {
return resolver.resolvePlaceholders(value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void testIamAuth() {
}

@Test
public void testPlaceholdersNotResolved() {
public void testSecretManagerPlaceholdersNotResolved() {
this.contextRunner.withPropertyValues(
"spring.cloud.gcp.sql.instance-connection-name=world:asia:japan",
"spring.cloud.gcp.sql.database-name=${sm://my-db}")
Expand All @@ -278,6 +278,22 @@ public void testPlaceholdersNotResolved() {
});
}

@Test
public void testEnvPlaceholdersResolved() {
this.contextRunner.withPropertyValues(
"DB_NAME=mydb",
"spring.cloud.gcp.sql.instance-connection-name=world:asia:japan",
"spring.cloud.gcp.sql.database-name=${DB_NAME:not_available}")
.run(context -> {
assertThat(context.getEnvironment().getPropertySources()
.get("CLOUD_SQL_DATA_SOURCE_URL")
.getProperty("spring.datasource.url"))
.isEqualTo("jdbc:mysql://google/mydb?"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory"
+ "&cloudSqlInstance=world:asia:japan");
});
}

@Test
public void testSkipOnBootstrap() {
new ApplicationContextRunner()
Expand Down