Skip to content

Add support for ALLOW_EMPTY_PASSWORD when using bitnami/postgresql with Docker Compose #43771

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -35,6 +35,7 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author He Zean
*/
class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {

Expand All @@ -60,6 +61,16 @@ void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectio
checkDatabaseAccess(connectionDetails);
}

@DockerComposeTest(composeFile = "postgres-bitnami-empty-password-compose.yaml",
image = TestImage.BITNAMI_POSTGRESQL)
void runWithBitnamiImageCreatesConnectionDetailsWithAllowEmptyPassword(JdbcConnectionDetails connectionDetails)
throws ClassNotFoundException {
assertThat(connectionDetails.getUsername()).isEqualTo("myuser");
assertThat(connectionDetails.getPassword()).isEmpty();
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:postgresql://").endsWith("/mydatabase");
checkDatabaseAccess(connectionDetails);
}

@DockerComposeTest(composeFile = "postgres-application-name-compose.yaml", image = TestImage.POSTGRESQL)
void runCreatesConnectionDetailsApplicationName(JdbcConnectionDetails connectionDetails)
throws ClassNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author He Zean
*/
class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {

Expand All @@ -63,6 +64,17 @@ void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connecti
checkDatabaseAccess(connectionDetails);
}

@DockerComposeTest(composeFile = "postgres-bitnami-empty-password-compose.yaml",
image = TestImage.BITNAMI_POSTGRESQL)
void runWithBitnamiImageCreatesConnectionDetailsWithAllowEmptyPassword(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-application-name-compose.yaml", image = TestImage.POSTGRESQL)
void runCreatesConnectionDetailsApplicationName(R2dbcConnectionDetails connectionDetails) {
assertConnectionDetails(connectionDetails);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '5432'
environment:
- 'POSTGRESQL_USERNAME=myuser'
- 'POSTGRESQL_DATABASE=mydatabase'
- 'ALLOW_EMPTY_PASSWORD=yes'
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.docker.compose.service.connection.postgres;

import java.util.Map;
import java.util.Objects;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -67,8 +68,9 @@ private String extractPassword(Map<String, String> 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;
boolean allowEmpty = env.containsKey("ALLOW_EMPTY_PASSWORD");
Assert.state(allowEmpty || StringUtils.hasLength(password), "PostgreSQL password must be provided");
return Objects.requireNonNullElse(password, "");
}

private boolean isUsingTrustHostAuthMethod(Map<String, String> env) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ void getPasswordWhenHasTrustHostAuthMethod() {
assertThat(environment.getPassword()).isNull();
}

@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("ALLOW_EMPTY_PASSWORD", "yes"));
assertThat(environment.getPassword()).isEmpty();
}

@Test
void getDatabaseWhenNoPostgresDbOrPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
Expand Down
Loading