Add support for Postgres trust host auth method with Docker Compose

See gh-41511
This commit is contained in:
sid
2024-07-16 00:25:09 -03:00
committed by Andy Wilkinson
parent d2cd5c9b98
commit af89924582
2 changed files with 15 additions and 0 deletions

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 (hasTrustAuthMethod(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 hasTrustAuthMethod(Map<String, String> env) {
String hostAuthMethod = env.get("POSTGRES_HOST_AUTH_METHOD");
return "trust".equals(hostAuthMethod);
}
String getUsername() {
return this.username;
}

View File

@@ -78,6 +78,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"));