Merge pull request #41511 from theodorosidmar

* gh-41511:
  Polish "Add support for Postgres trust host auth method with Docker Compose"
  Add support for Postgres trust host auth method with Docker Compose

Closes gh-41511
This commit is contained in:
Andy Wilkinson
2024-07-16 19:43:01 +01:00
6 changed files with 80 additions and 0 deletions

View File

@@ -21,6 +21,8 @@ dependencies {
dockerTestRuntimeOnly("com.microsoft.sqlserver:mssql-jdbc")
dockerTestRuntimeOnly("com.oracle.database.r2dbc:oracle-r2dbc")
dockerTestRuntimeOnly("io.r2dbc:r2dbc-mssql")
dockerTestRuntimeOnly("org.postgresql:postgresql")
dockerTestRuntimeOnly("org.postgresql:r2dbc-postgresql")
implementation("com.fasterxml.jackson.core:jackson-databind")
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names")

View File

@@ -16,11 +16,17 @@
package org.springframework.boot.docker.compose.service.connection.postgres;
import java.sql.Driver;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,6 +45,15 @@ class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {
assertConnectionDetails(connectionDetails);
}
@DockerComposeTest(composeFile = "postgres-with-trust-host-auth-method-compose.yaml", image = TestImage.POSTGRESQL)
void runCreatesConnectionDetailsThatCanAccessDatabaseWhenHostAuthMethodIsTrust(
JdbcConnectionDetails connectionDetails) throws ClassNotFoundException {
assertThat(connectionDetails.getUsername()).isEqualTo("myuser");
assertThat(connectionDetails.getPassword()).isNull();
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:postgresql://").endsWith("/mydatabase");
checkDatabaseAccess(connectionDetails);
}
@Test
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) {
@@ -51,4 +66,16 @@ class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:postgresql://").endsWith("/mydatabase");
}
@SuppressWarnings("unchecked")
private void checkDatabaseAccess(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setUrl(connectionDetails.getJdbcUrl());
dataSource.setUsername(connectionDetails.getUsername());
dataSource.setPassword(connectionDetails.getPassword());
dataSource.setDriverClass((Class<? extends Driver>) ClassUtils.forName(connectionDetails.getDriverClassName(),
getClass().getClassLoader()));
JdbcTemplate template = new JdbcTemplate(dataSource);
assertThat(template.queryForObject(DatabaseDriver.POSTGRESQL.getValidationQuery(), Integer.class)).isEqualTo(1);
}
}

View File

@@ -16,11 +16,16 @@
package org.springframework.boot.docker.compose.service.connection.postgres;
import java.time.Duration;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.r2dbc.core.DatabaseClient;
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,6 +44,17 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {
assertConnectionDetails(connectionDetails);
}
@DockerComposeTest(composeFile = "postgres-with-trust-host-auth-method-compose.yaml", image = TestImage.POSTGRESQL)
void runCreatesConnectionDetailsThatCanAccessDatabaseWhenHostAuthMethodIsTrust(
R2dbcConnectionDetails connectionDetails) {
ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.USER)).isEqualTo("myuser");
assertThat(connectionFactoryOptions.getValue(ConnectionFactoryOptions.PASSWORD)).isNull();
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.DATABASE))
.isEqualTo("mydatabase");
checkDatabaseAccess(connectionDetails);
}
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) {
assertConnectionDetails(connectionDetails);
@@ -51,4 +67,14 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret");
}
private void checkDatabaseAccess(R2dbcConnectionDetails connectionDetails) {
ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
Object result = DatabaseClient.create(ConnectionFactories.get(connectionFactoryOptions))
.sql(DatabaseDriver.POSTGRESQL.getValidationQuery())
.map((row, metadata) -> row.get(0))
.first()
.block(Duration.ofSeconds(30));
assertThat(result).isEqualTo(1);
}
}

View File

@@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '5432'
environment:
- 'POSTGRES_USER=myuser'
- 'POSTGRES_DB=mydatabase'
- 'POSTGRES_HOST_AUTH_METHOD=trust'

View File

@@ -28,6 +28,7 @@ import org.springframework.util.StringUtils;
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Sidmar Theodoro
*/
class PostgresEnvironment {
@@ -44,11 +45,19 @@ class PostgresEnvironment {
}
private String extractPassword(Map<String, String> env) {
if (hasTrustHostAuthMethod(env)) {
return null;
}
String password = env.getOrDefault("POSTGRES_PASSWORD", env.get("POSTGRESQL_PASSWORD"));
Assert.state(StringUtils.hasLength(password), "PostgreSQL password must be provided");
return password;
}
private Boolean hasTrustHostAuthMethod(Map<String, String> env) {
String hostAuthMethod = env.get("POSTGRES_HOST_AUTH_METHOD");
return "trust".equals(hostAuthMethod);
}
String getUsername() {
return this.username;
}

View File

@@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Sidmar Theodoro
*/
class PostgresEnvironmentTests {
@@ -78,6 +79,12 @@ class PostgresEnvironmentTests {
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasTrustHostAuthMethod() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_HOST_AUTH_METHOD", "trust"));
assertThat(environment.getPassword()).isNull();
}
@Test
void getDatabaseWhenNoPostgresDbOrPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));