Skip to content

Commit 18c052e

Browse files
Update Tests and Readme
1 parent 6a48957 commit 18c052e

File tree

8 files changed

+103
-27
lines changed

8 files changed

+103
-27
lines changed

ojdbc-provider-aws/README.md

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ Any valid secret name.
230230
<tr>
231231
<td><code>fieldName</code></td>
232232
<td>
233-
Optional key to extract a specific field from a JSON-formatted secret.
233+
The field inside the secret that contains the username.
234+
Use this parameter only when the secret is stored as a key-value pairs.
235+
If the secret contains multiple keys, this parameter specifies which key to extract.
236+
If the secret contains only one key and this parameter is not provided, that value is automatically used.
237+
If <code>fieldName</code> is provided but does not match any key, or if the secret is not structured as key/value pairs, an error is thrown.
238+
If the secret is plain text and <code>fieldName</code> is provided, an error is also thrown.
234239
<td>
235240
Key name in JSON
236241
</td>
@@ -275,8 +280,12 @@ Any valid secret name.
275280
<tr>
276281
<td><code>fieldName</code></td>
277282
<td>
278-
Optional key to extract a specific field from a JSON-formatted secret.
279-
<td>
283+
The field inside the secret that contains the password.
284+
Use this parameter only when the secret is structured as key-value pairs.
285+
If the secret contains multiple keys, this parameter specifies which key to extract.
286+
If the secret contains only one key and this parameter is not provided, that value is used automatically.
287+
If <code>fieldName</code> is provided but does not match any key, or if the secret is not structured as key-value pairs, an error is thrown.
288+
If the secret is stored as plain text and <code>fieldName</code> is provided, an error is also thrown.<td>
280289
Key name in JSON
281290
</td>
282291
<td>
@@ -329,17 +338,6 @@ Any valid secret name.
329338
</td>
330339
</tr>
331340
<tr>
332-
<td><code>fieldName</code></td>
333-
<td>
334-
Optional key to extract a specific field from a JSON-formatted secret.
335-
<td>
336-
Key name in JSON
337-
</td>
338-
<td>
339-
<i>Optional</i>
340-
</td>
341-
</tr>
342-
<tr>
343341
<td><code>walletPassword</code></td>
344342
<td>
345343
Optional password for PKCS12 or protected PEM files. If omitted, the file is assumed to be SSO or an non-protected PEM file.
@@ -359,6 +357,21 @@ Specifies the type of the file being used.
359357
<i>No default value. The file type must be specified.</i>
360358
</td>
361359
</tr>
360+
<tr>
361+
<td><code>fieldName</code></td>
362+
<td>
363+
The field inside the secret that contains the base64-encoded TCPS wallet.
364+
Use this parameter only when the secret is structured as key-value pairs.
365+
If the secret contains multiple keys, this parameter specifies which key to extract.
366+
If the secret contains only one key and this parameter is not provided, that value is used automatically.
367+
If <code>fieldName</code> is provided but does not match any key, or if the secret is not structured as key-value pairs, an error is thrown.
368+
If the secret is stored as plain text and <code>fieldName</code> is provided, an error is also thrown.<td>
369+
Key name in JSON
370+
</td>
371+
<td>
372+
<i>Optional</i>
373+
</td>
374+
</tr>
362375
</tbody>
363376
</table>
364377

@@ -430,8 +443,12 @@ Optional parameter to specify the index of the connection string to use when ret
430443
<tr>
431444
<td><code>fieldName</code></td>
432445
<td>
433-
Optional key to extract a specific field from a JSON-formatted secret.
434-
<td>
446+
The field inside the secret that contains the base64-encoded SEPS wallet.
447+
Use this parameter only when the secret is structured as key-value pairs.
448+
If the secret contains multiple keys, this parameter specifies which key to extract.
449+
If the secret contains only one key and this parameter is not provided, that value is used automatically.
450+
If <code>fieldName</code> is provided but does not match any key, or if the secret is not structured as key-value pairs, an error is thrown.
451+
If the secret is stored as plain text and <code>fieldName</code> is provided, an error is also thrown.<td>
435452
Key name in JSON
436453
</td>
437454
<td>
@@ -453,11 +470,15 @@ This provider retrieves and decodes a `tnsnames.ora` file stored as a secret in
453470

454471
You can store the contents of the tnsnames.ora file either as:
455472

456-
- A base64-encoded string, or
473+
- A base64-encoded string, either directly or within a key inside a structured key-value map.
474+
475+
- Plain text, by simply copying and pasting the contents directly into the secret value.
457476

458-
- Plain text, by simply copying and pasting the contents directly into the secret value field.
477+
If the secret is a key-value map, the <code>fieldName</code> parameter must be used to specify the key that holds the base64-encoded
478+
tnsnames.ora content.
459479

460-
The provider will automatically handle either format and extract the appropriate connection string based on the specified alias.
480+
If the secret is stored as plain text, it must be provided as the raw contents of the tnsnames.ora file,
481+
and <code>fieldName</code> should not be set.
461482

462483
This enables flexible configuration for secure database connections using the alias names defined in your `tnsnames.ora` file.
463484

@@ -487,7 +508,15 @@ In addition to the set of [common parameters](#common-parameters-for-resource-pr
487508
</tr>
488509
<tr>
489510
<td><code>fieldName</code></td>
490-
<td>Optional key to extract a specific field from a JSON-formatted secret.</td>
511+
<td>
512+
The field inside the secret that contains the base64-encoded <code>tnsnames.ora</code> content.
513+
Use this parameter only when the secret is structured as key-value pairs.
514+
If the secret contains multiple keys, this parameter specifies which key to extract.
515+
If the secret contains only one key and this parameter is not provided, that value is used automatically.
516+
If <code>fieldName</code> is provided but does not match any key, or if the secret is not structured
517+
as key-value pairs, an error is thrown. If the secret is stored as plain text and <code>fieldName</code> is provided,
518+
an error is also thrown.
519+
</td>
491520
<td>Key name in JSON</td>
492521
<td><i>Optional</i></td>
493522
</tr>

ojdbc-provider-aws/src/main/java/oracle/jdbc/provider/aws/secrets/AwsSecretExtractor.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,20 @@ public class AwsSecretExtractor {
7070
* <li>Throws an exception if multiple keys exist and no field name is specified.</li>
7171
* </ul>
7272
* </li>
73-
* <li>If the string is not valid JSON, returns the original string as-is.</li>
73+
* <li>If the string is not valid JSON:
74+
* <ul>
75+
* <li>Returns the raw string only if {@code fieldName} is not provided.</li>
76+
* <li>Throws an exception if {@code fieldName} is provided.</li>
77+
* </ul>
78+
* </li>
7479
* </ul>
7580
*
7681
* @param secretString the raw secret string from AWS Secrets Manager
7782
* @param fieldName the key to extract from the JSON object (optional)
7883
* @return the extracted secret value
79-
* @throws IllegalStateException if the JSON is valid but ambiguous or the field is missing
84+
* @throws IllegalStateException if the JSON is valid but ambiguous,
85+
* the specified field is missing, or the input is not JSON while
86+
* {@code fieldName} is provided
8087
*/
8188
public static String extractSecret(String secretString, String fieldName) {
8289
try {
@@ -95,7 +102,10 @@ public static String extractSecret(String secretString, String fieldName) {
95102
throw new IllegalStateException("FIELD_NAME is required when multiple keys exist in the secret JSON");
96103
}
97104
} catch (OracleJsonException e) {
98-
// Fallback to plain text if not a JSON object
105+
if (fieldName != null) {
106+
throw new IllegalStateException("FIELD_NAME provided, but secret is not valid JSON.");
107+
}
108+
// Accept fallback to plain text only when fieldName is NOT specified
99109
return secretString;
100110
}
101111
}

ojdbc-provider-aws/src/test/java/oracle/provider/aws/AwsTestProperty.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public enum AwsTestProperty {
4141
AWS_S3_URL,
4242
AWS_SECRETS_MANAGER_URL,
4343
AWS_REGION,
44-
PASSWORD_SECRET_NAME,
45-
USERNAME_SECRET_NAME,
44+
DB_CREDENTIALS_SECRET_NAME,
4645
TNSNAMES_SECRET_NAME,
4746
TNS_ALIAS,
4847
PKCS12_WALLET_SECRET_NAME,

ojdbc-provider-aws/src/test/java/oracle/provider/aws/resource/SecretsManagerConnectionStringProviderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public void testGetParameters() {
8282
assertFalse(regionParam.isSensitive());
8383
assertFalse(regionParam.isRequired());
8484
assertNull(regionParam.defaultValue());
85+
86+
Parameter fieldParam = parameters.stream()
87+
.filter(p -> "fieldName".equals(p.name()))
88+
.findFirst().orElseThrow(AssertionError::new);
89+
assertFalse(fieldParam.isSensitive());
90+
assertFalse(fieldParam.isRequired());
91+
assertNull(fieldParam.defaultValue());
8592
}
8693

8794
@Test

ojdbc-provider-aws/src/test/java/oracle/provider/aws/resource/SecretsManagerPasswordProviderTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,23 @@ public void testGetParameters() {
7676
assertFalse(regionParameter.isSensitive());
7777
assertFalse(regionParameter.isRequired());
7878
assertNull(regionParameter.defaultValue());
79+
80+
Parameter fieldParam = parameters.stream()
81+
.filter(p -> "fieldName".equals(p.name()))
82+
.findFirst().orElseThrow(AssertionError::new);
83+
assertFalse(fieldParam.isSensitive());
84+
assertFalse(fieldParam.isRequired());
85+
assertNull(fieldParam.defaultValue());
7986
}
8087

8188
@Test
8289
public void testGetPassword() {
8390
Map<String, String> testParameters = new HashMap<>();
8491
testParameters.put("secretName",
85-
TestProperties.getOrAbort(AwsTestProperty.PASSWORD_SECRET_NAME));
92+
TestProperties.getOrAbort(AwsTestProperty.DB_CREDENTIALS_SECRET_NAME));
8693
testParameters.put("awsRegion",
8794
TestProperties.getOrAbort(AwsTestProperty.AWS_REGION));
95+
testParameters.put("fieldName", "password");
8896

8997
Map<Parameter, CharSequence> parameterValues =
9098
createParameterValues(PROVIDER, testParameters);

ojdbc-provider-aws/src/test/java/oracle/provider/aws/resource/SecretsManagerSepsProviderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public void testGetParameters() {
9999
.findFirst().orElseThrow(AssertionError::new);
100100
assertFalse(awsRegion.isSensitive());
101101
assertFalse(awsRegion.isRequired());
102+
103+
Parameter fieldParam = usernameParams.stream()
104+
.filter(p -> "fieldName".equals(p.name()))
105+
.findFirst().orElseThrow(AssertionError::new);
106+
assertFalse(fieldParam.isSensitive());
107+
assertFalse(fieldParam.isRequired());
108+
assertNull(fieldParam.defaultValue());
102109
}
103110

104111
@Test
@@ -138,6 +145,7 @@ public void testSsoPasswordWithIndex() {
138145
params.put("connectionStringIndex", "1");
139146
params.put("awsRegion",
140147
TestProperties.getOrAbort(AwsTestProperty.AWS_REGION));
148+
params.put("fieldName", "sso");
141149

142150
Map<Parameter, CharSequence> values = createParameterValues(PASSWORD_PROVIDER, params);
143151
assertNotNull(PASSWORD_PROVIDER.getPassword(values));

ojdbc-provider-aws/src/test/java/oracle/provider/aws/resource/SecretsManagerTcpsProviderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ public void testGetParameters() {
9090
.findFirst().orElseThrow(AssertionError::new);
9191
assertFalse(region.isSensitive());
9292
assertFalse(region.isRequired());
93+
94+
Parameter fieldParam = parameters.stream()
95+
.filter(p -> "fieldName".equals(p.name()))
96+
.findFirst().orElseThrow(AssertionError::new);
97+
assertFalse(fieldParam.isSensitive());
98+
assertFalse(fieldParam.isRequired());
99+
assertNull(fieldParam.defaultValue());
93100
}
94101

95102

ojdbc-provider-aws/src/test/java/oracle/provider/aws/resource/SecretsManagerUsernameProviderTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,23 @@ public void testGetParameters() {
7777
assertFalse(regionParameter.isSensitive());
7878
assertFalse(regionParameter.isRequired());
7979
assertNull(regionParameter.defaultValue());
80+
81+
Parameter fieldParam = parameters.stream()
82+
.filter(p -> "fieldName".equals(p.name()))
83+
.findFirst().orElseThrow(AssertionError::new);
84+
assertFalse(fieldParam.isSensitive());
85+
assertFalse(fieldParam.isRequired());
86+
assertNull(fieldParam.defaultValue());
8087
}
8188

8289
@Test
8390
public void testGetUsername() {
8491
Map<String, String> testParameters = new HashMap<>();
8592
testParameters.put("secretName",
86-
TestProperties.getOrAbort(AwsTestProperty.USERNAME_SECRET_NAME));
93+
TestProperties.getOrAbort(AwsTestProperty.DB_CREDENTIALS_SECRET_NAME));
8794
testParameters.put("awsRegion",
8895
TestProperties.getOrAbort(AwsTestProperty.AWS_REGION));
96+
testParameters.put("fieldName", "username");
8997

9098
Map<Parameter, CharSequence> parameterValues =
9199
createParameterValues(PROVIDER, testParameters);

0 commit comments

Comments
 (0)