Skip to content

Commit b1fd9ba

Browse files
committed
Add support for empty password in bitnami/postgresql
Signed-off-by: He Zean <[email protected]>
1 parent cc49b30 commit b1fd9ba

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* @author Andy Wilkinson
3636
* @author Phillip Webb
3737
* @author Scott Frederick
38+
* @author He Zean
3839
*/
3940
class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {
4041

@@ -57,6 +58,16 @@ void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectio
5758
assertConnectionDetails(connectionDetails);
5859
}
5960

61+
@DockerComposeTest(composeFile = "postgres-bitnami-empty-password-compose.yaml",
62+
image = TestImage.BITNAMI_POSTGRESQL)
63+
void runWithBitnamiImageCreatesConnectionDetailsWithAllowEmptyPassword(JdbcConnectionDetails connectionDetails)
64+
throws ClassNotFoundException {
65+
assertThat(connectionDetails.getUsername()).isEqualTo("myuser");
66+
assertThat(connectionDetails.getPassword()).isEmpty();
67+
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:postgresql://").endsWith("/mydatabase");
68+
checkDatabaseAccess(connectionDetails);
69+
}
70+
6071
@DockerComposeTest(composeFile = "postgres-application-name-compose.yaml", image = TestImage.POSTGRESQL)
6172
void runCreatesConnectionDetailsApplicationName(JdbcConnectionDetails connectionDetails)
6273
throws ClassNotFoundException {

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Andy Wilkinson
3838
* @author Phillip Webb
3939
* @author Scott Frederick
40+
* @author He Zean
4041
*/
4142
class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {
4243

@@ -61,6 +62,17 @@ void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connecti
6162
assertConnectionDetails(connectionDetails);
6263
}
6364

65+
@DockerComposeTest(composeFile = "postgres-bitnami-empty-password-compose.yaml",
66+
image = TestImage.BITNAMI_POSTGRESQL)
67+
void runWithBitnamiImageCreatesConnectionDetailsWithAllowEmptyPassword(R2dbcConnectionDetails connectionDetails) {
68+
ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
69+
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.USER)).isEqualTo("myuser");
70+
assertThat(connectionFactoryOptions.getValue(ConnectionFactoryOptions.PASSWORD)).isNull();
71+
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.DATABASE))
72+
.isEqualTo("mydatabase");
73+
checkDatabaseAccess(connectionDetails);
74+
}
75+
6476
@DockerComposeTest(composeFile = "postgres-application-name-compose.yaml", image = TestImage.POSTGRESQL)
6577
void runCreatesConnectionDetailsApplicationName(R2dbcConnectionDetails connectionDetails) {
6678
assertConnectionDetails(connectionDetails);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
database:
3+
image: '{imageName}'
4+
ports:
5+
- '5432'
6+
environment:
7+
- 'POSTGRESQL_USERNAME=myuser'
8+
- 'POSTGRESQL_DATABASE=mydatabase'
9+
- 'ALLOW_EMPTY_PASSWORD=yes'

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.docker.compose.service.connection.postgres;
1818

1919
import java.util.Map;
20+
import java.util.Objects;
2021

2122
import org.springframework.util.Assert;
2223
import org.springframework.util.StringUtils;
@@ -49,15 +50,20 @@ private String extractPassword(Map<String, String> env) {
4950
return null;
5051
}
5152
String password = env.getOrDefault("POSTGRES_PASSWORD", env.get("POSTGRESQL_PASSWORD"));
52-
Assert.state(StringUtils.hasLength(password), "PostgreSQL password must be provided");
53-
return password;
53+
Assert.state(isAllowEmptyPassword(env) || StringUtils.hasLength(password),
54+
"PostgreSQL password must be provided");
55+
return Objects.requireNonNullElse(password, "");
5456
}
5557

5658
private boolean isUsingTrustHostAuthMethod(Map<String, String> env) {
5759
String hostAuthMethod = env.get("POSTGRES_HOST_AUTH_METHOD");
5860
return "trust".equals(hostAuthMethod);
5961
}
6062

63+
private boolean isAllowEmptyPassword(Map<String, String> env) {
64+
return env.containsKey("ALLOW_EMPTY_PASSWORD");
65+
}
66+
6167
String getUsername() {
6268
return this.username;
6369
}

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,6 +85,12 @@ void getPasswordWhenHasTrustHostAuthMethod() {
8585
assertThat(environment.getPassword()).isNull();
8686
}
8787

88+
@Test
89+
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
90+
PostgresEnvironment environment = new PostgresEnvironment(Map.of("ALLOW_EMPTY_PASSWORD", "yes"));
91+
assertThat(environment.getPassword()).isEmpty();
92+
}
93+
8894
@Test
8995
void getDatabaseWhenNoPostgresDbOrPostgresUser() {
9096
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));

0 commit comments

Comments
 (0)