-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add support to configure system password for Oracle database #10601
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
base: main
Are you sure you want to change the base?
Changes from all commits
079a494
1630f95
ec95b8f
6e0ac92
63752c5
3e63f05
bf19086
5171d96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,18 @@ public class OracleContainer extends JdbcDatabaseContainer<OracleContainer> { | |
|
|
||
| private String password = APP_USER_PASSWORD; | ||
|
|
||
| /** | ||
| * Password for Oracle system user (e.g. SYSTEM/SYS). Defaults to {@link #APP_USER_PASSWORD} | ||
| * for backwards compatibility, but can be customized independently via {@link #withSystemPassword(String)}. | ||
| */ | ||
| private String oraclePassword = APP_USER_PASSWORD; | ||
|
|
||
| /** | ||
| * Tracks whether {@link #withSystemPassword(String)} was called to avoid overriding | ||
| * the system password when {@link #withPassword(String)} is used for the application user only. | ||
| */ | ||
| private boolean systemPasswordExplicitlySet = false; | ||
|
Comment on lines
+61
to
+71
|
||
|
|
||
| private boolean usingSid = false; | ||
|
|
||
| public OracleContainer(String dockerImageName) { | ||
|
|
@@ -112,7 +124,8 @@ public String getUsername() { | |
|
|
||
| @Override | ||
| public String getPassword() { | ||
| return password; | ||
| // When connecting via SID we authenticate as SYSTEM. Use the dedicated system password. | ||
| return isUsingSid() ? oraclePassword : password; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -142,6 +155,27 @@ public OracleContainer withPassword(String password) { | |
| throw new IllegalArgumentException("Password cannot be null or empty"); | ||
| } | ||
| this.password = password; | ||
| // Maintain backwards compatibility: if system password wasn't set explicitly, | ||
| // align it with the application user's password. | ||
| if (!systemPasswordExplicitlySet) { | ||
| this.oraclePassword = password; | ||
| } | ||
| return self(); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the password for the Oracle system user (SYSTEM/SYS). This is independent from the | ||
| * application user password set via {@link #withPassword(String)}. | ||
| * | ||
| * @param oraclePassword password for SYSTEM/SYS users inside the container | ||
| * @return this container instance | ||
| */ | ||
| public OracleContainer withSystemPassword(String oraclePassword) { | ||
| if (StringUtils.isEmpty(oraclePassword)) { | ||
| throw new IllegalArgumentException("Oracle password cannot be null or empty"); | ||
| } | ||
| this.oraclePassword = oraclePassword; | ||
| this.systemPasswordExplicitlySet = true; | ||
| return self(); | ||
|
Comment on lines
+166
to
179
|
||
| } | ||
|
|
||
|
|
@@ -185,7 +219,8 @@ public String getTestQueryString() { | |
|
|
||
| @Override | ||
| protected void configure() { | ||
| withEnv("ORACLE_PASSWORD", password); | ||
| // Configure system user password independently from application user's password | ||
| withEnv("ORACLE_PASSWORD", oraclePassword); | ||
|
|
||
| // Only set ORACLE_DATABASE if different than the default. | ||
| if (databaseName != DEFAULT_DATABASE_NAME) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,20 @@ private void runTest(OracleContainer container, String databaseName, String user | |
| assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1); | ||
| } | ||
|
|
||
| private void runTestSystemUser(OracleContainer container, String databaseName, String username, String password) | ||
| throws SQLException { | ||
| //Test config was honored | ||
| assertThat(container.getDatabaseName()).isEqualTo(databaseName); | ||
| assertThat(container.getUsername()).isEqualTo(username); | ||
| assertThat(container.getPassword()).isEqualTo(password); | ||
|
|
||
| //Test we can get a connection and execute a system-level command | ||
| container.start(); | ||
| ResultSet resultSet = performQuery(container, "GRANT DBA TO " + username); | ||
| int resultSetInt = resultSet.getInt(1); | ||
| assertThat(resultSetInt).as("A basic system user query succeeds").isEqualTo(1); | ||
|
Comment on lines
+41
to
+45
|
||
| } | ||
|
|
||
| @Test | ||
| void testDefaultSettings() throws SQLException { | ||
| try ( // container { | ||
|
|
@@ -78,7 +92,7 @@ void testCustomUser() throws SQLException { | |
| @Test | ||
| void testSID() throws SQLException { | ||
| try (OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME).usingSid()) { | ||
| runTest(oracle, "freepdb1", "system", "test"); | ||
| runTestSystemUser(oracle, "freepdb1", "system", "test"); | ||
|
|
||
| // Match against the last ':' | ||
| String urlSuffix = oracle.getJdbcUrl().split("(\\:)(?!.*\\:)", 2)[1]; | ||
|
|
@@ -93,7 +107,30 @@ void testSIDAndCustomPassword() throws SQLException { | |
| .usingSid() | ||
| .withPassword("testPassword") | ||
| ) { | ||
| runTest(oracle, "freepdb1", "system", "testPassword"); | ||
| runTestSystemUser(oracle, "freepdb1", "system", "testPassword"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testWithSystemPassword() throws SQLException { | ||
| try ( | ||
| OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME) | ||
| .usingSid() | ||
| .withSystemPassword("SysP@ss1!") | ||
| .withPassword("AppP@ss1!") | ||
| ) { | ||
| runTestSystemUser(oracle, "freepdb1", "system", "SysP@ss1!"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testWithPassword() throws SQLException { | ||
| try ( | ||
| OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME) | ||
| .withSystemPassword("SysP@ss2!") | ||
| .withPassword("AppP@ss2!") | ||
| ) { | ||
| runTest(oracle, "freepdb1", "test", "AppP@ss2!"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,18 @@ public class OracleContainer extends JdbcDatabaseContainer<OracleContainer> { | |
|
|
||
| private String password = APP_USER_PASSWORD; | ||
|
|
||
| /** | ||
| * Password for Oracle system user (e.g. SYSTEM/SYS). Defaults to {@link #APP_USER_PASSWORD} | ||
| * for backwards compatibility, but can be customized independently via {@link #withSystemPassword(String)}. | ||
| */ | ||
| private String systemPassword = APP_USER_PASSWORD; | ||
|
|
||
| /** | ||
| * Tracks whether {@link #withSystemPassword(String)} was called to avoid overriding | ||
| * the system password when {@link #withPassword(String)} is used for the application user only. | ||
| */ | ||
| private boolean systemPasswordExplicitlySet = false; | ||
|
|
||
| private boolean usingSid = false; | ||
|
|
||
| /** | ||
|
|
@@ -133,7 +145,8 @@ public String getUsername() { | |
|
|
||
| @Override | ||
| public String getPassword() { | ||
| return password; | ||
| // When connecting via SID we authenticate as SYSTEM. Use the dedicated system password. | ||
| return isUsingSid() ? systemPassword : password; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -163,6 +176,27 @@ public OracleContainer withPassword(String password) { | |
| throw new IllegalArgumentException("Password cannot be null or empty"); | ||
| } | ||
| this.password = password; | ||
| // Maintain backwards compatibility: if oracle password wasn't set explicitly, | ||
| // align it with the application user's password. | ||
| if (!systemPasswordExplicitlySet) { | ||
| this.systemPassword = password; | ||
| } | ||
|
Comment on lines
+179
to
+183
|
||
| return self(); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the password for the Oracle system user (SYSTEM/SYS). This is independent from the | ||
| * application user password set via {@link #withPassword(String)}. | ||
| * | ||
| * @param oraclePassword password for SYSTEM/SYS users inside the container | ||
| * @return this container instance | ||
| */ | ||
| public OracleContainer withSystemPassword(String oraclePassword) { | ||
| if (StringUtils.isEmpty(oraclePassword)) { | ||
| throw new IllegalArgumentException("Oracle password cannot be null or empty"); | ||
| } | ||
| this.systemPassword = oraclePassword; | ||
| this.systemPasswordExplicitlySet = true; | ||
| return self(); | ||
|
Comment on lines
+187
to
200
|
||
| } | ||
|
|
||
|
|
@@ -211,7 +245,8 @@ public String getTestQueryString() { | |
|
|
||
| @Override | ||
| protected void configure() { | ||
| withEnv("ORACLE_PASSWORD", password); | ||
| // Configure system user password independently from application user's password | ||
| withEnv("ORACLE_PASSWORD", systemPassword); | ||
|
|
||
| // Only set ORACLE_DATABASE if different than the default. | ||
| if (databaseName != DEFAULT_DATABASE_NAME) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,20 @@ private void runTest(OracleContainer container, String databaseName, String user | |
| assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1); | ||
| } | ||
|
|
||
| private void runTestSystemUser(OracleContainer container, String databaseName, String username, String password) | ||
| throws SQLException { | ||
| assertThat(container.getDatabaseName()).isEqualTo(databaseName); | ||
| assertThat(container.getUsername()).isEqualTo(username); | ||
| assertThat(container.getPassword()).isEqualTo(password); | ||
|
|
||
| container.start(); | ||
| ResultSet resultSet = performQuery(container, "SELECT USER FROM DUAL"); | ||
| String currentUser = resultSet.getString(1); | ||
| assertThat(currentUser) | ||
| .as("Connected session should run as the system user") | ||
| .isEqualToIgnoringCase(username); | ||
| } | ||
|
|
||
| @Test | ||
| void testDefaultSettings() throws SQLException { | ||
| try ( // container { | ||
|
|
@@ -78,7 +92,7 @@ void testCustomUser() throws SQLException { | |
| @Test | ||
| void testSID() throws SQLException { | ||
| try (OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME).usingSid();) { | ||
| runTest(oracle, "xepdb1", "system", "test"); | ||
| runTestSystemUser(oracle, "xepdb1", "system", "test"); | ||
|
|
||
| // Match against the last ':' | ||
| String urlSuffix = oracle.getJdbcUrl().split("(\\:)(?!.*\\:)", 2)[1]; | ||
|
|
@@ -93,7 +107,28 @@ void testSIDAndCustomPassword() throws SQLException { | |
| .usingSid() | ||
| .withPassword("testPassword"); | ||
| ) { | ||
| runTest(oracle, "xepdb1", "system", "testPassword"); | ||
| runTestSystemUser(oracle, "xepdb1", "system", "testPassword"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testSeparateSystemAndAppPasswords() throws SQLException { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should have two tests instead? |
||
| try ( | ||
| OracleContainer oracleSid = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME) | ||
| .usingSid() | ||
| .withSystemPassword("SysP@ss1!") | ||
| .withPassword("AppP@ss1!") | ||
| ) { | ||
| runTestSystemUser(oracleSid, "xepdb1", "system", "SysP@ss1!"); | ||
| } | ||
|
|
||
| // Non-SID mode should use application user's password | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the comment |
||
| try ( | ||
| OracleContainer oraclePdb = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME) | ||
| .withSystemPassword("SysP@ss2!") | ||
| .withPassword("AppP@ss2!") | ||
| ) { | ||
| runTest(oraclePdb, "xepdb1", "test", "AppP@ss2!"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.