Skip to content

Commit 8292df6

Browse files
Fix compilation failure for dedicated Vault
1 parent d088b34 commit 8292df6

File tree

7 files changed

+52
-84
lines changed

7 files changed

+52
-84
lines changed

ojdbc-provider-common/src/main/java/oracle/jdbc/provider/configuration/JsonSecretUtil.java

+33-30
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private JsonSecretUtil(){}
6868
* }
6969
* </pre>
7070
* If an attribute named 'method' is present, the value is keyed to the
71-
* name 'AUTHENTICATION' in the returned map. This is done to match the
71+
* name 'AUTHENTICATION' in the returned map. This is done to match the
7272
* "AUTHENTICATION" parameter name used by the {@code ojdbc-azure-common}
7373
* module.
7474
* @param secretJsonObject JSON that may contain a 'value' field or/and an
@@ -77,43 +77,46 @@ private JsonSecretUtil(){}
7777
* all the attributes mentioned above are not present.
7878
*/
7979
public static Map<String, String> toNamedValues(
80-
OracleJsonObject secretJsonObject) {
80+
OracleJsonObject secretJsonObject) {
8181

82-
final String valueFieldName = "value";
8382
Map<String, String> options = new HashMap<>();
8483

85-
if (secretJsonObject.containsKey(valueFieldName)) {
86-
OracleJsonValue secretUri = secretJsonObject.get(valueFieldName);
87-
if (secretUri.getOracleJsonType()
88-
.equals(OracleJsonValue.OracleJsonType.STRING)) {
89-
options.put(valueFieldName, secretUri.asJsonString().getString());
90-
} else {
91-
options.put(valueFieldName, secretUri.toString());
84+
secretJsonObject.forEach((key, value) -> {
85+
if (key.equals("type")) {
86+
// Skipped
9287
}
93-
}
94-
95-
if (secretJsonObject.containsKey("authentication")) {
96-
OracleJsonObject authenticationJsonObject = secretJsonObject
97-
.get("authentication")
98-
.asJsonObject();
99-
100-
// Rename "method" to "AUTHENTICATION" to match the parameter names
101-
if (authenticationJsonObject.containsKey("method")) {
102-
OracleJsonValue authentication = authenticationJsonObject.get("method");
103-
authenticationJsonObject.remove("method");
104-
authenticationJsonObject.put("AUTHENTICATION", authentication);
88+
else if (key.equals("value")) {
89+
// Handle the "value" object
90+
options.put("value", getValueAsString(value));
10591
}
92+
else if (key.equals("authentication")) {
93+
// Handle the "authentication" object
94+
OracleJsonObject authenticationJsonObject = value.asJsonObject();
10695

107-
authenticationJsonObject.forEach((key, value) -> {
108-
if (value.getOracleJsonType()
109-
.equals(OracleJsonValue.OracleJsonType.STRING)) {
110-
options.put(key, value.asJsonString().getString());
111-
} else {
112-
options.put(key, value.toString());
96+
// Rename "method" to "AUTHENTICATION" to match the parameter names
97+
if (authenticationJsonObject.containsKey("method")) {
98+
OracleJsonValue authentication = authenticationJsonObject.get("method");
99+
authenticationJsonObject.remove("method");
100+
authenticationJsonObject.put("AUTHENTICATION", authentication);
113101
}
114-
});
115-
}
102+
103+
authenticationJsonObject.forEach((authKey, authValue) -> {
104+
options.put(authKey, getValueAsString(authValue));
105+
});
106+
}
107+
else {
108+
// For the rest of the cases, directly add to the options
109+
options.put(key, getValueAsString(value));
110+
}
111+
});
116112

117113
return options;
118114
}
115+
116+
private static String getValueAsString(OracleJsonValue value) {
117+
if (value.getOracleJsonType().equals(OracleJsonValue.OracleJsonType.STRING))
118+
return value.asJsonString().getString();
119+
else
120+
return value.toString();
121+
}
119122
}

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/authentication/HashicorpCredentialsFactory.java

-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
public final class HashicorpCredentialsFactory implements ResourceFactory<HashiCredentials> {
1515

16-
// Example parameter referencing the authentication method
1716
public static final Parameter<HashicorpAuthenticationMethod> AUTHENTICATION_METHOD =
1817
Parameter.create(REQUIRED);
1918

@@ -33,7 +32,6 @@ public Resource<HashiCredentials> request(ParameterSet parameterSet) {
3332
}
3433

3534
private static HashiCredentials getCredential(ParameterSet parameterSet) {
36-
System.out.println("parameterSet = " + parameterSet);
3735
// Check which authentication method is requested
3836
HashicorpAuthenticationMethod method =
3937
parameterSet.getRequired(AUTHENTICATION_METHOD);
@@ -47,9 +45,6 @@ private static HashiCredentials getCredential(ParameterSet parameterSet) {
4745
}
4846
}
4947

50-
/**
51-
* Example: read Vault token from an environment variable "VAULT_TOKEN".
52-
*/
5348
private static HashiCredentials tokenCredentials(ParameterSet parameterSet) {
5449
// (1) Try parameter
5550
String paramToken = parameterSet.getOptional(HashiVaultSecretsManagerFactory.VAULT_TOKEN);

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/configuration/HashiJsonVaultProvider.java

+2-25
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,6 @@
1313
import static oracle.jdbc.provider.hashicorp.configuration.HashiVaultSecretsManagerConfigurationProvider.PARAMETER_SET_PARSER;
1414
import static oracle.jdbc.provider.hashicorp.secrets.HashiVaultSecretsManagerFactory.FIELD_NAME;
1515

16-
/**
17-
* Mirrors the AWS pattern for retrieving a single secret
18-
* field from HashiCorp Vault, base64-encoding it.
19-
*
20-
* Example JSON input might look like:
21-
* {
22-
* "password": {
23-
* "type": "hashicorpvault",
24-
* "value": "/v1/secret/data/test-config2"
25-
* }
26-
* }
27-
*
28-
* The provider will retrieve the secret from Vault, then
29-
* base64-encode it and return as a char[].
30-
*/
3116
public class HashiJsonVaultProvider implements OracleConfigurationJsonSecretProvider {
3217

3318
@Override
@@ -47,30 +32,22 @@ public char[] getSecret(OracleJsonObject jsonObject) {
4732
ByteArrayInputStream inputStream = new ByteArrayInputStream(secretString.getBytes(StandardCharsets.UTF_8));
4833

4934

50-
// 3) Parse that JSON to find "myPassword"
51-
// Using the Oracle JSON library, for example:
5235
OracleJsonObject secretJsonObj =
5336
new oracle.sql.json.OracleJsonFactory()
5437
.createJsonTextValue(inputStream)
5538
.asJsonObject();
5639

57-
System.out.println(secretJsonObj);
58-
59-
// 4) Retrieve the field we want
60-
//String myPasswordValue = secretJsonObj.getString("myPassword");
6140
String myPasswordValue = parameterSet.getOptional(FIELD_NAME);
62-
System.out.println(myPasswordValue);
41+
String a = String.valueOf(secretJsonObj.get(myPasswordValue));
6342

6443
// 5) Base64-encode just that field
6544
return Base64.getEncoder()
66-
.encodeToString(myPasswordValue.getBytes())
45+
.encodeToString(a.getBytes())
6746
.toCharArray();
6847
}
6948

7049
@Override
7150
public String getSecretType() {
72-
// Must match the "type" field in your JSON.
73-
// E.g. "hashicorpvault" or "hashicorsecret"—your choice.
7451
return "hashicorpvault";
7552
}
7653
}

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/configuration/HashiVaultSecretsManagerConfigurationProvider.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,23 @@
44
import oracle.jdbc.provider.hashicorp.secrets.HashiVaultSecretsManagerFactory;
55
import oracle.jdbc.provider.parameter.ParameterSet;
66
import oracle.jdbc.provider.parameter.ParameterSetParser;
7+
import oracle.jdbc.util.OracleConfigurationCache;
78

89
import java.io.ByteArrayInputStream;
910
import java.io.InputStream;
1011
import java.util.HashMap;
1112
import java.util.Map;
1213

13-
/**
14-
* A provider for JSON payload from Vault, analogous to AWS Secrets Manager version.
15-
*/
1614
public class HashiVaultSecretsManagerConfigurationProvider extends OracleConfigurationJsonProvider {
1715

18-
// Reuse a ParameterSetParser approach
19-
// If you need more parameters, add them below.
2016
static final ParameterSetParser PARAMETER_SET_PARSER =
2117
HashicorpConfigurationParameters.configureBuilder(
2218
ParameterSetParser.builder()
2319
.addParameter("value", HashiVaultSecretsManagerFactory.SECRET_PATH)
2420
.addParameter("key_name", HashiVaultSecretsManagerFactory.KEY_NAME)
2521
.addParameter(
2622
"VAULT_ADDR",
27-
HashiVaultSecretsManagerFactory.VAULT_ADDRESS
23+
HashiVaultSecretsManagerFactory.VAULT_ADDR
2824
)
2925
.addParameter(
3026
"VAULT_TOKEN",
@@ -36,7 +32,6 @@ public class HashiVaultSecretsManagerConfigurationProvider extends OracleConfigu
3632

3733
@Override
3834
public InputStream getJson(String secretPath) {
39-
// 'secretPath' is the location or name of the Vault secret
4035
final String valueFieldName = "value";
4136

4237
// Build a map of user-provided options
@@ -61,4 +56,9 @@ public String getType() {
6156
// We'll reference this in our JDBC URL, e.g. "jdbc:oracle:thin:@config-hashicorpvault://..."
6257
return "hashicorpvault";
6358
}
59+
60+
@Override
61+
public OracleConfigurationCache getCache() {
62+
return CACHE;
63+
}
6464
}

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/configuration/HashicorpConfigurationParameters.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import oracle.jdbc.provider.hashicorp.authentication.HashicorpAuthenticationMethod;
44
import oracle.jdbc.provider.hashicorp.authentication.HashicorpCredentialsFactory;
5-
import oracle.jdbc.provider.hashicorp.secrets.HashiVaultSecretsManagerFactory;
65
import oracle.jdbc.provider.parameter.ParameterSetParser;
76

87
/**
9-
* Defines how we parse common Vault parameters (similar to AWS approach).
8+
* Defines how we parse common Vault parameters.
109
*/
1110
public final class HashicorpConfigurationParameters {
1211

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/secrets/HashiVaultSecretsManagerFactory.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
import static java.nio.charset.StandardCharsets.UTF_8;
2121
import static oracle.jdbc.provider.parameter.Parameter.CommonAttribute.REQUIRED;
2222

23-
/**
24-
* Analogous to AWS SecretsManagerFactory: fetches secrets from HashiCorp Vault.
25-
* Uses a REST call to the Vault HTTP API.
26-
*/
2723
public final class HashiVaultSecretsManagerFactory extends HashiVaultResourceFactory<String> {
2824

2925
/** The path of the secret in Vault. Required. */
@@ -38,13 +34,14 @@ public final class HashiVaultSecretsManagerFactory extends HashiVaultResourceFac
3834
/**
3935
* (Optional) The Vault address. If not specified, fallback to system property or environment var.
4036
*/
41-
public static final Parameter<String> VAULT_ADDRESS = Parameter.create();
37+
public static final Parameter<String> VAULT_ADDR = Parameter.create();
4238

4339
/**
4440
* (Optional) The Vault token. If not specified, fallback to system property or environment var.
4541
*/
4642
public static final Parameter<String> VAULT_TOKEN = Parameter.create();
4743

44+
public static final Parameter<String> FIELD_NAME = Parameter.create();
4845

4946
private static final OracleJsonFactory JSON_FACTORY = new OracleJsonFactory();
5047

@@ -62,10 +59,9 @@ public static ResourceFactory<String> getInstance() {
6259

6360
@Override
6461
public Resource<String> request(HashiCredentials credentials, ParameterSet parameterSet) {
65-
System.out.println("parametrs in SecretManagerFactory" + parameterSet);
6662
String secretPath = parameterSet.getRequired(SECRET_PATH);
6763
String key = parameterSet.getOptional(KEY_NAME);
68-
String vaultAddrParam = parameterSet.getOptional(VAULT_ADDRESS);
64+
String vaultAddrParam = parameterSet.getOptional(VAULT_ADDR);
6965
String vaultTokenParam = parameterSet.getOptional(VAULT_TOKEN);
7066

7167
String vaultAddr = (vaultAddrParam != null)
@@ -85,12 +81,10 @@ public Resource<String> request(HashiCredentials credentials, ParameterSet param
8581
"Vault token not found in URL parameters, system properties, or environment variables");
8682
}
8783

88-
// Use vaultAddr + secretPath as your final endpoint:
8984
String vaultUrl = vaultAddr + secretPath;
9085

9186
// Make the REST call
9287
String secretString = fetchSecretFromVault(vaultUrl, vaultToken);
93-
System.out.println("Raw Vault Response: " + secretString);
9488

9589
/*
9690
* If KEY_NAME is specified, we only want a single field from the nested JSON.
@@ -118,8 +112,6 @@ private static String fetchSecretFromVault(String vaultUrl, String token) {
118112
throw new IllegalStateException(
119113
"Failed to fetch secret. HTTP error code: " + conn.getResponseCode());
120114
}
121-
122-
// Java 8-friendly read of InputStream
123115
try (InputStream in = conn.getInputStream()) {
124116
return readStream(in);
125117
}
@@ -133,9 +125,6 @@ private static String fetchSecretFromVault(String vaultUrl, String token) {
133125
}
134126
}
135127

136-
/**
137-
* Utility method for reading all bytes from an InputStream in Java 8.
138-
*/
139128
private static String readStream(InputStream in) throws IOException {
140129
ByteArrayOutputStream baos = new ByteArrayOutputStream();
141130
byte[] buffer = new byte[1024];
@@ -190,7 +179,7 @@ private static String extractValueFromJson(String secretJson, String key, String
190179
// Return only that key's value
191180
if (!dataData.containsKey(key)) {
192181
throw new IllegalArgumentException(
193-
"Failed to find key \"" + key + "\" in secret: " + secretPath);
182+
"Failed to find key " + key + " in secret: " + secretPath);
194183
}
195184
return dataData.getString(key);
196185

ojdbc-provider-samples/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<artifactId>ojdbc-provider-oci</artifactId>
2626
<version>${project.parent.version}</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>com.oracle.database.jdbc</groupId>
30+
<artifactId>ojdbc-provider-hashicorp</artifactId>
31+
<version>${project.parent.version}</version>
32+
</dependency>
2833
<dependency>
2934
<groupId>com.oracle.database.security</groupId>
3035
<artifactId>oraclepki</artifactId>

0 commit comments

Comments
 (0)