Skip to content

Commit c8f2fb0

Browse files
hezeanwilkinsona
authored andcommitted
Fix handling of env vars in Bitnami's Postgres image
See gh-43783 Signed-off-by: He Zean <[email protected]>
1 parent 42ecda2 commit c8f2fb0

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@
3333
class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {
3434

3535
@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL)
36-
void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) {
36+
void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException {
3737
assertConnectionDetails(connectionDetails);
38+
checkDatabaseAccess(connectionDetails);
3839
}
3940

4041
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
41-
void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) {
42+
void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails)
43+
throws ClassNotFoundException {
4244
assertConnectionDetails(connectionDetails);
45+
checkDatabaseAccess(connectionDetails);
4346
}
4447

4548
private void assertConnectionDetails(JdbcConnectionDetails connectionDetails) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {
3737
@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL)
3838
void runCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) {
3939
assertConnectionDetails(connectionDetails);
40+
checkDatabaseAccess(connectionDetails);
4041
}
4142

4243
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
4344
void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) {
4445
assertConnectionDetails(connectionDetails);
46+
checkDatabaseAccess(connectionDetails);
4547
}
4648

4749
private void assertConnectionDetails(R2dbcConnectionDetails connectionDetails) {

spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ services:
44
ports:
55
- '5432'
66
environment:
7-
- 'POSTGRESQL_USER=myuser'
8-
- 'POSTGRESQL_DB=mydatabase'
7+
- 'POSTGRESQL_USERNAME=myuser'
8+
- 'POSTGRESQL_DATABASE=mydatabase'
99
- 'POSTGRESQL_PASSWORD=secret'

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

Lines changed: 30 additions & 3 deletions
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.
@@ -28,19 +28,37 @@
2828
* @author Andy Wilkinson
2929
* @author Phillip Webb
3030
* @author Scott Frederick
31+
* @author He Zean
3132
*/
3233
class PostgresEnvironment {
3334

35+
private static final String[] USERNAME_KEYS = new String[] { "POSTGRES_USER", "POSTGRES_USERNAME",
36+
"POSTGRESQL_USER", "POSTGRESQL_USERNAME" };
37+
38+
private static final String DEFAULT_USERNAME = "postgres";
39+
40+
private static final String[] DATABASE_KEYS = new String[] { "POSTGRES_DB", "POSTGRES_DATABASE",
41+
"POSTGRESQL_DATABASE" };
42+
3443
private final String username;
3544

3645
private final String password;
3746

3847
private final String database;
3948

4049
PostgresEnvironment(Map<String, String> env) {
41-
this.username = env.getOrDefault("POSTGRES_USER", env.getOrDefault("POSTGRESQL_USER", "postgres"));
50+
this.username = extractUsername(env);
4251
this.password = extractPassword(env);
43-
this.database = env.getOrDefault("POSTGRES_DB", env.getOrDefault("POSTGRESQL_DB", this.username));
52+
this.database = extractDatabase(env);
53+
}
54+
55+
private String extractUsername(Map<String, String> env) {
56+
for (String key : USERNAME_KEYS) {
57+
if (env.containsKey(key)) {
58+
return env.get(key);
59+
}
60+
}
61+
return DEFAULT_USERNAME;
4462
}
4563

4664
private String extractPassword(Map<String, String> env) {
@@ -49,6 +67,15 @@ private String extractPassword(Map<String, String> env) {
4967
return password;
5068
}
5169

70+
private String extractDatabase(Map<String, String> env) {
71+
for (String key : DATABASE_KEYS) {
72+
if (env.containsKey(key)) {
73+
return env.get(key);
74+
}
75+
}
76+
return this.username;
77+
}
78+
5279
String getUsername() {
5380
return this.username;
5481
}

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

Lines changed: 32 additions & 3 deletions
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.
@@ -31,6 +31,7 @@
3131
* @author Andy Wilkinson
3232
* @author Phillip Webb
3333
* @author Scott Frederick
34+
* @author He Zean
3435
*/
3536
class PostgresEnvironmentTests {
3637

@@ -66,6 +67,20 @@ void getUsernameWhenHasPostgresqlUser() {
6667
assertThat(environment.getUsername()).isEqualTo("me");
6768
}
6869

70+
@Test
71+
void getUsernameWhenHasPostgresUsername() {
72+
PostgresEnvironment environment = new PostgresEnvironment(
73+
Map.of("POSTGRES_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
74+
assertThat(environment.getUsername()).isEqualTo("me");
75+
}
76+
77+
@Test
78+
void getUsernameWhenHasPostgresqlUsername() {
79+
PostgresEnvironment environment = new PostgresEnvironment(
80+
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
81+
assertThat(environment.getUsername()).isEqualTo("me");
82+
}
83+
6984
@Test
7085
void getPasswordWhenHasPostgresPassword() {
7186
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
@@ -104,6 +119,13 @@ void getDatabaseWhenNoPostgresqlDbAndPostgresUser() {
104119
assertThat(environment.getDatabase()).isEqualTo("me");
105120
}
106121

122+
@Test
123+
void getDatabaseWhenNoPostgresqlDatabaseAndPostgresqlUsername() {
124+
PostgresEnvironment environment = new PostgresEnvironment(
125+
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
126+
assertThat(environment.getDatabase()).isEqualTo("me");
127+
}
128+
107129
@Test
108130
void getDatabaseWhenHasPostgresDb() {
109131
PostgresEnvironment environment = new PostgresEnvironment(
@@ -112,9 +134,16 @@ void getDatabaseWhenHasPostgresDb() {
112134
}
113135

114136
@Test
115-
void getDatabaseWhenHasPostgresqlDb() {
137+
void getDatabaseWhenHasPostgresDatabase() {
138+
PostgresEnvironment environment = new PostgresEnvironment(
139+
Map.of("POSTGRES_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
140+
assertThat(environment.getDatabase()).isEqualTo("db");
141+
}
142+
143+
@Test
144+
void getDatabaseWhenHasPostgresqlDatabase() {
116145
PostgresEnvironment environment = new PostgresEnvironment(
117-
Map.of("POSTGRESQL_DB", "db", "POSTGRESQL_PASSWORD", "secret"));
146+
Map.of("POSTGRESQL_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
118147
assertThat(environment.getDatabase()).isEqualTo("db");
119148
}
120149

0 commit comments

Comments
 (0)