|
| 1 | +package oracle.jdbc.provider.hashicorp.configuration; |
| 2 | + |
| 3 | +import oracle.jdbc.provider.configuration.JsonSecretUtil; |
| 4 | +import oracle.jdbc.provider.hashicorp.secrets.HashiVaultSecretsManagerFactory; |
| 5 | +import oracle.jdbc.provider.parameter.ParameterSet; |
| 6 | +import oracle.jdbc.spi.OracleConfigurationJsonSecretProvider; |
| 7 | +import oracle.sql.json.OracleJsonObject; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.Base64; |
| 12 | + |
| 13 | +import static oracle.jdbc.provider.hashicorp.configuration.HashiVaultSecretsManagerConfigurationProvider.PARAMETER_SET_PARSER; |
| 14 | +import static oracle.jdbc.provider.hashicorp.secrets.HashiVaultSecretsManagerFactory.FIELD_NAME; |
| 15 | + |
| 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 | + */ |
| 31 | +public class HashiJsonVaultProvider implements OracleConfigurationJsonSecretProvider { |
| 32 | + |
| 33 | + @Override |
| 34 | + public char[] getSecret(OracleJsonObject jsonObject) { |
| 35 | + // 1) Convert the JSON object to named key-value pairs |
| 36 | + ParameterSet parameterSet = |
| 37 | + PARAMETER_SET_PARSER.parseNamedValues( |
| 38 | + JsonSecretUtil.toNamedValues(jsonObject) |
| 39 | + ); |
| 40 | + |
| 41 | + // 2) Call the Vault factory to fetch the raw secret string |
| 42 | + String secretString = HashiVaultSecretsManagerFactory |
| 43 | + .getInstance() |
| 44 | + .request(parameterSet) |
| 45 | + .getContent(); |
| 46 | + |
| 47 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(secretString.getBytes(StandardCharsets.UTF_8)); |
| 48 | + |
| 49 | + |
| 50 | + // 3) Parse that JSON to find "myPassword" |
| 51 | + // Using the Oracle JSON library, for example: |
| 52 | + OracleJsonObject secretJsonObj = |
| 53 | + new oracle.sql.json.OracleJsonFactory() |
| 54 | + .createJsonTextValue(inputStream) |
| 55 | + .asJsonObject(); |
| 56 | + |
| 57 | + System.out.println(secretJsonObj); |
| 58 | + |
| 59 | + // 4) Retrieve the field we want |
| 60 | + //String myPasswordValue = secretJsonObj.getString("myPassword"); |
| 61 | + String myPasswordValue = parameterSet.getOptional(FIELD_NAME); |
| 62 | + System.out.println(myPasswordValue); |
| 63 | + |
| 64 | + // 5) Base64-encode just that field |
| 65 | + return Base64.getEncoder() |
| 66 | + .encodeToString(myPasswordValue.getBytes()) |
| 67 | + .toCharArray(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String getSecretType() { |
| 72 | + // Must match the "type" field in your JSON. |
| 73 | + // E.g. "hashicorpvault" or "hashicorsecret"—your choice. |
| 74 | + return "hashicorpvault"; |
| 75 | + } |
| 76 | +} |
0 commit comments