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: pass enable-iam-auth=true to spring.r2dbc.properties #1715

Merged
merged 2 commits into from
Apr 11, 2023
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 @@ -67,6 +67,14 @@ public void postProcessEnvironment(

CredentialsPropertiesSetter.setCredentials(sqlProperties,
propertiesRetriever.getGcpProperties());

if (sqlProperties.isEnableIamAuth()) {
environment
.getPropertySources()
.addFirst(
new MapPropertySource("CLOUD_SQL_R2DBC_ENABLE_IAM_AUTH",
Map.of("spring.r2dbc.properties", Map.of("ENABLE_IAM_AUTH", "true"))));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
Expand Down Expand Up @@ -51,43 +52,34 @@ void testCreateUrl_postgres() {

@Test
void testSetR2dbcProperty_postgres() {
verifyThatCorrectUrlAndUsernameSet(
new String[] {"io.r2dbc.postgresql"},
"postgres",
"r2dbc:gcp:postgres://my-project:region:my-instance/my-database");
}

/**
* Verifies that correct database properties got injected into context, given a passed-in list of
* packages to retain on the classpath.
*
* @param driverPackagesToInclude a list of driver packages to keep on the classpath
* @param username expected {@code spring.r2dbc.username} value to verify
* @param url expected {@code spring.r2dbc.username} value to verify
*/
private void verifyThatCorrectUrlAndUsernameSet(
String[] driverPackagesToInclude,
String username,
String url) {
// Because `FilteredClassLoader` accepts a list of packages to remove from classpath,
// `driverPackagesToInclude` is used to calculate the inverse list of packages to _exclude_.
Set<String> driverPackagesToExclude = new HashSet<>(List.of("io.r2dbc.postgresql"));
Arrays.asList(driverPackagesToInclude).forEach(driverPackagesToExclude::remove);

this.contextRunner
.withPropertyValues(
"spring.cloud.gcp.sql.databaseName=my-database",
"spring.cloud.gcp.sql.instanceConnectionName=my-project:region:my-instance")
.withClassLoader(new FilteredClassLoader(driverPackagesToExclude.toArray(new String[0])))
.withClassLoader(excludeDriverPackages(new String[] {"io.r2dbc.postgresql"}))
.run(
context -> {
assertThat(context.getEnvironment().getProperty("spring.r2dbc.url"))
.isEqualTo(url);
.isEqualTo("r2dbc:gcp:postgres://my-project:region:my-instance/my-database");
assertThat(context.getEnvironment().getProperty("spring.r2dbc.username"))
.isEqualTo(username);
.isEqualTo("postgres");
});
}

@Test
@SuppressWarnings("unchecked")
void testEnableIamAuthPresent() {
this.contextRunner
.withPropertyValues(
"spring.cloud.gcp.sql.databaseName=my-database",
"spring.cloud.gcp.sql.instanceConnectionName=my-project:region:my-instance",
"spring.cloud.gcp.sql.enable-iam-auth=true")
.withClassLoader(excludeDriverPackages(new String[] {"io.r2dbc.postgresql"}))
.run(
context -> assertThat(context.getEnvironment().getProperty("spring.r2dbc.properties", Map.class, Map.of()))
.isEqualTo(Map.of("ENABLE_IAM_AUTH", "true")));
}

@Test
void testGetEnabledDatatype_noR2dbcConnectorsPresent() {
this.contextRunner
Expand All @@ -96,31 +88,42 @@ void testGetEnabledDatatype_noR2dbcConnectorsPresent() {
"com.google.cloud.sql.core.GcpConnectionFactoryProviderMysql",
"com.google.cloud.sql.core.GcpConnectionFactoryProviderPostgres"))
.run(
context -> {
assertThat(r2dbcPostProcessor.getEnabledDatabaseType(context.getEnvironment()))
.isNull();
});
context -> assertThat(r2dbcPostProcessor.getEnabledDatabaseType(context.getEnvironment()))
.isNull());
}

@Test
void testGetEnabledDatatype_noConnectionFactoryPresent() {
this.contextRunner
.withClassLoader(new FilteredClassLoader("io.r2dbc.spi.ConnectionFactory"))
.run(
context -> {
assertThat(r2dbcPostProcessor.getEnabledDatabaseType(context.getEnvironment()))
.isNull();
});
context -> assertThat(r2dbcPostProcessor.getEnabledDatabaseType(context.getEnvironment()))
.isNull());
}

@Test
void testGetEnabledDatatype_r2dbcDisabled() {
this.contextRunner
.withPropertyValues("spring.cloud.gcp.sql.r2dbc.enabled=false")
.run(
context -> {
assertThat(r2dbcPostProcessor.getEnabledDatabaseType(context.getEnvironment()))
.isNull();
});
context -> assertThat(r2dbcPostProcessor.getEnabledDatabaseType(context.getEnvironment()))
.isNull());
}

/**
* Returns a class loader which excludes certain driver packages from the classpath
* unless they are in {@code driverPackagesToInclude}.
*
* @param driverPackagesToInclude A list of driver packages to keep on the classpath
* @return A class loader with a set of driver packages that are excluded from the classpath
*/
private FilteredClassLoader excludeDriverPackages(String[] driverPackagesToInclude) {
// Because `FilteredClassLoader` accepts a list of packages to remove from classpath,
// `driverPackagesToInclude` is used to calculate the inverse list of packages to _exclude_.
Set<String> driverPackagesToExclude = new HashSet<>(List.of("io.r2dbc.postgresql"));
Arrays.stream(driverPackagesToInclude)
.forEach(driverPackagesToExclude::remove);

return new FilteredClassLoader(driverPackagesToExclude.toArray(new String[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
properties = {
"spring.cloud.gcp.sql.databaseName=code_samples_test_r2dbc_db",
"spring.cloud.gcp.sql.instanceConnectionName=spring-cloud-gcp-ci:us-central1:testpostgres",
"spring.r2dbc.password=test"
"spring.cloud.gcp.sql.enable-iam.auth=true"
})
@EnabledIfSystemProperty(named = "it.cloudsql", matches = "true")
class SqlR2dbcPostgresSampleApplicationIntegrationTests {
Expand Down