Polish "Add support for Postgres trust host auth method with Docker Compose"
See gh-41511
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
services:
|
||||
database:
|
||||
image: '{imageName}'
|
||||
ports:
|
||||
- '5432'
|
||||
environment:
|
||||
- 'POSTGRES_USER=myuser'
|
||||
- 'POSTGRES_DB=mydatabase'
|
||||
- 'POSTGRES_HOST_AUTH_METHOD=trust'
|
||||
Reference in New Issue
Block a user