From ab1e52138870fb354e0fdad151734472120d53c5 Mon Sep 17 00:00:00 2001 From: Joe Steel Date: Mon, 21 Aug 2023 11:27:03 +0100 Subject: [PATCH] Fix password property for Oracle container See gh-37044 --- .../connection/oracle/OracleEnvironment.java | 4 +- .../oracle/OracleEnvironmentTests.java | 10 +-- ...nectionDetailsFactoryIntegrationTests.java | 74 +++++++++++++------ .../oracle/oracle-non-default-compose.yaml | 15 ++++ 4 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 spring-boot-project/spring-boot-docker-compose/src/test/resources/org/springframework/boot/docker/compose/service/connection/oracle/oracle-non-default-compose.yaml diff --git a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironment.java b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironment.java index b06fc5aa8c..78011a7d6c 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironment.java +++ b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironment.java @@ -42,12 +42,12 @@ class OracleEnvironment { private String extractPassword(Map env) { if (env.containsKey("APP_USER")) { - String password = env.get("APP_PASSWORD"); + String password = env.get("APP_USER_PASSWORD"); Assert.state(StringUtils.hasLength(password), "No Oracle app password found"); return password; } Assert.state(!env.containsKey("ORACLE_RANDOM_PASSWORD"), - "ORACLE_RANDOM_PASSWORD is not supported without APP_USER and APP_PASSWORD"); + "ORACLE_RANDOM_PASSWORD is not supported without APP_USER and APP_USER_PASSWORD"); String password = env.get("ORACLE_PASSWORD"); Assert.state(StringUtils.hasLength(password), "No Oracle password found"); return password; diff --git a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironmentTests.java b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironmentTests.java index 334ee0812d..6e5b7b974a 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironmentTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleEnvironmentTests.java @@ -34,7 +34,7 @@ class OracleEnvironmentTests { @Test void getUsernameWhenHasAppUser() { - OracleEnvironment environment = new OracleEnvironment(Map.of("APP_USER", "alice", "APP_PASSWORD", "secret")); + OracleEnvironment environment = new OracleEnvironment(Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret")); assertThat(environment.getUsername()).isEqualTo("alice"); } @@ -46,7 +46,7 @@ class OracleEnvironmentTests { @Test void getPasswordWhenHasAppPassword() { - OracleEnvironment environment = new OracleEnvironment(Map.of("APP_USER", "alice", "APP_PASSWORD", "secret")); + OracleEnvironment environment = new OracleEnvironment(Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret")); assertThat(environment.getPassword()).isEqualTo("secret"); } @@ -59,14 +59,14 @@ class OracleEnvironmentTests { @Test void createWhenRandomPasswordAndAppPasswordDoesNotThrow() { assertThatNoException().isThrownBy(() -> new OracleEnvironment( - Map.of("APP_USER", "alice", "APP_PASSWORD", "secret", "ORACLE_RANDOM_PASSWORD", "true"))); + Map.of("APP_USER", "alice", "APP_USER_PASSWORD", "secret", "ORACLE_RANDOM_PASSWORD", "true"))); } @Test void createWhenRandomPasswordThrowsException() { assertThatIllegalStateException() .isThrownBy(() -> new OracleEnvironment(Map.of("ORACLE_RANDOM_PASSWORD", "true"))) - .withMessage("ORACLE_RANDOM_PASSWORD is not supported without APP_USER and APP_PASSWORD"); + .withMessage("ORACLE_RANDOM_PASSWORD is not supported without APP_USER and APP_USER_PASSWORD"); } @Test @@ -78,7 +78,7 @@ class OracleEnvironmentTests { @Test void createWhenAppUserAndEmptyAppPasswordThrowsException() { assertThatIllegalStateException() - .isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice", "APP_PASSWORD", ""))) + .isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice", "APP_USER_PASSWORD", ""))) .withMessage("No Oracle app password found"); } diff --git a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java index 4a56f26972..b5dbe658b7 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/oracle/OracleJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java @@ -20,6 +20,7 @@ import java.sql.Driver; import java.time.Duration; import org.awaitility.Awaitility; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.OS; @@ -41,30 +42,59 @@ import static org.assertj.core.api.Assertions.assertThat; */ @DisabledOnOs(os = { OS.LINUX, OS.MAC }, architecture = "aarch64", disabledReason = "The Oracle image has no ARM support") -class OracleJdbcDockerComposeConnectionDetailsFactoryIntegrationTests extends AbstractDockerComposeIntegrationTests { +class OracleJdbcDockerComposeConnectionDetailsFactoryIntegrationTests { - OracleJdbcDockerComposeConnectionDetailsFactoryIntegrationTests() { - super("oracle-compose.yaml", DockerImageNames.oracleXe()); - } + @Nested + class OracleJdbcDockerComposeConnectionDetailsFactoryDefaultSettingsIntegrationTests extends AbstractDockerComposeIntegrationTests { + OracleJdbcDockerComposeConnectionDetailsFactoryDefaultSettingsIntegrationTests() { + super("oracle-compose.yaml", DockerImageNames.oracleXe()); + } - @Test - @SuppressWarnings("unchecked") - void runCreatesConnectionDetailsThatCanBeUsedToAccessDatabase() throws Exception { - JdbcConnectionDetails connectionDetails = run(JdbcConnectionDetails.class); - assertThat(connectionDetails.getUsername()).isEqualTo("system"); - assertThat(connectionDetails.getPassword()).isEqualTo("secret"); - assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:oracle:thin:@").endsWith("/xepdb1"); - SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); - dataSource.setUrl(connectionDetails.getJdbcUrl()); - dataSource.setUsername(connectionDetails.getUsername()); - dataSource.setPassword(connectionDetails.getPassword()); - dataSource.setDriverClass((Class) ClassUtils.forName(connectionDetails.getDriverClassName(), - getClass().getClassLoader())); - Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptions().untilAsserted(() -> { - JdbcTemplate template = new JdbcTemplate(dataSource); - assertThat(template.queryForObject(DatabaseDriver.ORACLE.getValidationQuery(), String.class)) - .isEqualTo("Hello"); - }); + @Test + @SuppressWarnings("unchecked") + void runCreatesConnectionDetailsThatCanBeUsedToAccessDatabase() throws Exception { + JdbcConnectionDetails connectionDetails = run(JdbcConnectionDetails.class); + assertThat(connectionDetails.getUsername()).isEqualTo("system"); + assertThat(connectionDetails.getPassword()).isEqualTo("secret"); + assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:oracle:thin:@").endsWith("/xepdb1"); + SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); + dataSource.setUrl(connectionDetails.getJdbcUrl()); + dataSource.setUsername(connectionDetails.getUsername()); + dataSource.setPassword(connectionDetails.getPassword()); + dataSource.setDriverClass((Class) ClassUtils.forName(connectionDetails.getDriverClassName(), + getClass().getClassLoader())); + Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptions().untilAsserted(() -> { + JdbcTemplate template = new JdbcTemplate(dataSource); + assertThat(template.queryForObject(DatabaseDriver.ORACLE.getValidationQuery(), String.class)) + .isEqualTo("Hello"); + }); + } } + @Nested + class OracleJdbcDockerComposeConnectionDetailsFactoryNonDefaultSettingsIntegrationTests extends AbstractDockerComposeIntegrationTests { + protected OracleJdbcDockerComposeConnectionDetailsFactoryNonDefaultSettingsIntegrationTests() { + super("oracle-non-default-compose.yaml", DockerImageNames.oracleXe()); + } + + @Test + @SuppressWarnings("unchecked") + void runCreatesConnectionDetailsWithNonDefaultUsernameAndPasswordThatCanBeUsedToAccessDatabase() throws Exception { + JdbcConnectionDetails connectionDetails = run(JdbcConnectionDetails.class); + assertThat(connectionDetails.getUsername()).isEqualTo("app_user"); + assertThat(connectionDetails.getPassword()).isEqualTo("app_user_secret"); + assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:oracle:thin:@").endsWith("/xepdb1"); + SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); + dataSource.setUrl(connectionDetails.getJdbcUrl()); + dataSource.setUsername(connectionDetails.getUsername()); + dataSource.setPassword(connectionDetails.getPassword()); + dataSource.setDriverClass((Class) ClassUtils.forName(connectionDetails.getDriverClassName(), + getClass().getClassLoader())); + Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptions().untilAsserted(() -> { + JdbcTemplate template = new JdbcTemplate(dataSource); + assertThat(template.queryForObject(DatabaseDriver.ORACLE.getValidationQuery(), String.class)) + .isEqualTo("Hello"); + }); + } + } } diff --git a/spring-boot-project/spring-boot-docker-compose/src/test/resources/org/springframework/boot/docker/compose/service/connection/oracle/oracle-non-default-compose.yaml b/spring-boot-project/spring-boot-docker-compose/src/test/resources/org/springframework/boot/docker/compose/service/connection/oracle/oracle-non-default-compose.yaml new file mode 100644 index 0000000000..1cfa3ca87a --- /dev/null +++ b/spring-boot-project/spring-boot-docker-compose/src/test/resources/org/springframework/boot/docker/compose/service/connection/oracle/oracle-non-default-compose.yaml @@ -0,0 +1,15 @@ +services: + database: + image: '{imageName}' + ports: + - '1521' + environment: + - 'APP_USER=app_user' + - 'APP_USER_PASSWORD=app_user_secret' + - 'ORACLE_PASSWORD=secret' + healthcheck: + test: ["CMD-SHELL", "healthcheck.sh"] + interval: 10s + timeout: 5s + retries: 10 +