Skip to content

Commit

Permalink
Allow placeholders in Cloud SQL config (#495)
Browse files Browse the repository at this point in the history
Secret Manager placehoders are still skipped as before.

Fixes: #491.
  • Loading branch information
meltsufin authored Jun 8, 2021
1 parent fe0a2cb commit 157e38c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
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

0 comments on commit 157e38c

Please sign in to comment.