|
1 | 1 | package org.utplsql.maven.plugin; |
2 | 2 |
|
| 3 | +import oracle.security.pki.OracleSecretStore; |
| 4 | +import oracle.security.pki.OracleWallet; |
3 | 5 | import org.apache.maven.plugin.MojoExecutionException; |
4 | 6 | import org.apache.maven.plugin.testing.MojoRule; |
5 | 7 | import org.junit.Rule; |
6 | 8 | import org.junit.Test; |
| 9 | +import org.junit.rules.TemporaryFolder; |
7 | 10 | import org.utplsql.api.FileMapperOptions; |
8 | 11 | import org.utplsql.maven.plugin.model.ReporterParameter; |
9 | 12 |
|
10 | 13 | import java.io.ByteArrayOutputStream; |
11 | 14 | import java.io.File; |
12 | 15 | import java.io.PrintStream; |
| 16 | +import java.nio.file.Files; |
13 | 17 | import java.util.List; |
14 | 18 |
|
15 | 19 | import static org.junit.Assert.assertEquals; |
|
20 | 24 |
|
21 | 25 | public class UtPlsqlMojoTest { |
22 | 26 |
|
| 27 | + private static final String WALLET_TNS_ALIAS = "UTPLSQL_MAVEN_WALLET"; |
| 28 | + |
23 | 29 | @Rule |
24 | 30 | public final MojoRule rule = new MojoRule(); |
25 | 31 |
|
| 32 | + @Rule |
| 33 | + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 34 | + |
26 | 35 | /** |
27 | 36 | * Invalid Sources Directory |
28 | 37 | * <p> |
@@ -322,6 +331,78 @@ public void db_config_using_system_properties() throws Exception { |
322 | 331 | System.clearProperty("dbPass"); |
323 | 332 | } |
324 | 333 |
|
| 334 | + /** |
| 335 | + * Connection using Oracle Wallet (Secure External Password Store) |
| 336 | + * <p> |
| 337 | + * Given : a pom.xml without dbUser and dbPass and a dbUrl pointing to a TNS alias stored in an Oracle Wallet |
| 338 | + * When : tests are run |
| 339 | + * Then : credentials are taken from the wallet |
| 340 | + */ |
| 341 | + @Test |
| 342 | + public void wallet_connection_without_credentials() throws Exception { |
| 343 | + System.setProperty("dbUrl", walletDbUrl()); |
| 344 | + try { |
| 345 | + UtPlsqlMojo utPlsqlMojo = createUtPlsqlMojo("wallet_connection"); |
| 346 | + assertNotNull(utPlsqlMojo); |
| 347 | + |
| 348 | + utPlsqlMojo.execute(); |
| 349 | + } finally { |
| 350 | + System.clearProperty("dbUrl"); |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + /** |
| 355 | + * Connection using Oracle Wallet with empty credentials |
| 356 | + * <p> |
| 357 | + * Given : a dbUrl pointing to a TNS alias stored in an Oracle Wallet and empty dbUser and dbPass (e.g. -DdbUser= -DdbPass=) |
| 358 | + * When : tests are run |
| 359 | + * Then : empty credentials are ignored and credentials are taken from the wallet |
| 360 | + */ |
| 361 | + @Test |
| 362 | + public void wallet_connection_with_empty_credentials() throws Exception { |
| 363 | + System.setProperty("dbUrl", walletDbUrl()); |
| 364 | + System.setProperty("dbUser", ""); |
| 365 | + System.setProperty("dbPass", ""); |
| 366 | + try { |
| 367 | + UtPlsqlMojo utPlsqlMojo = createUtPlsqlMojo("wallet_connection"); |
| 368 | + assertNotNull(utPlsqlMojo); |
| 369 | + |
| 370 | + utPlsqlMojo.execute(); |
| 371 | + } finally { |
| 372 | + System.clearProperty("dbUrl"); |
| 373 | + System.clearProperty("dbUser"); |
| 374 | + System.clearProperty("dbPass"); |
| 375 | + } |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * Creates an auto-login Oracle Wallet holding the UT3 credentials for {@link #WALLET_TNS_ALIAS}, |
| 380 | + * with tnsnames.ora and ojdbc.properties next to it, so no Oracle client tooling (mkstore/orapki) is needed. |
| 381 | + * |
| 382 | + * @return JDBC URL of the TNS alias, with the generated directory as TNS_ADMIN |
| 383 | + */ |
| 384 | + private String walletDbUrl() throws Exception { |
| 385 | + File tnsAdmin = temporaryFolder.newFolder("tns_admin"); |
| 386 | + |
| 387 | + OracleWallet wallet = new OracleWallet(); |
| 388 | + wallet.create("Wallet_Pwd_123".toCharArray()); |
| 389 | + OracleSecretStore secretStore = wallet.getSecretStore(); |
| 390 | + secretStore.createCredential(WALLET_TNS_ALIAS.toCharArray(), "UT3".toCharArray(), "ut3".toCharArray()); |
| 391 | + wallet.setSecretStore(secretStore); |
| 392 | + wallet.saveAs(tnsAdmin.getPath()); |
| 393 | + wallet.createSSO(); |
| 394 | + wallet.saveSSO(); |
| 395 | + |
| 396 | + Files.writeString(tnsAdmin.toPath().resolve("tnsnames.ora"), |
| 397 | + WALLET_TNS_ALIAS + " = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))" |
| 398 | + + "(CONNECT_DATA = (SERVICE_NAME = FREEPDB1)))\n"); |
| 399 | + // ${TNS_ADMIN} is resolved by the JDBC driver, the same way as in wallets downloaded for Oracle Autonomous Database |
| 400 | + Files.writeString(tnsAdmin.toPath().resolve("ojdbc.properties"), |
| 401 | + "oracle.net.wallet_location=(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=${TNS_ADMIN})))\n"); |
| 402 | + |
| 403 | + return "jdbc:oracle:thin:@" + WALLET_TNS_ALIAS + "?TNS_ADMIN=" + tnsAdmin.getAbsolutePath().replace('\\', '/'); |
| 404 | + } |
| 405 | + |
325 | 406 | /** |
326 | 407 | * Exclude a list of objects |
327 | 408 | * <p> |
|
0 commit comments