Skip to content

Commit

Permalink
Cleanup and Update authentication parsing to use switch-case logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MouhsinElmajdouby committed Jan 3, 2025
1 parent 9d3a252 commit 49d620b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ public enum DedicatedVaultAuthenticationMethod {
* environment variables, or system properties.
* </p>
*/
TOKEN
VAULT_TOKEN
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static DedicatedVaultCredentials getCredential(ParameterSet parameterSet
parameterSet.getRequired(AUTHENTICATION_METHOD);

switch (method) {
case TOKEN:
case VAULT_TOKEN:
return createTokenCredentials(parameterSet);
default:
throw new IllegalArgumentException(
Expand All @@ -117,15 +117,13 @@ private static DedicatedVaultCredentials getCredential(ParameterSet parameterSet
private static DedicatedVaultCredentials createTokenCredentials(ParameterSet parameterSet) {
String vaultToken = getRequiredOrFallback(
parameterSet,
DedicatedVaultSecretsManagerFactory.VAULT_TOKEN,
"VAULT_TOKEN"
DedicatedVaultSecretsManagerFactory.VAULT_TOKEN, "VAULT_TOKEN"
);

if (vaultToken == null || vaultToken.isEmpty()) {
throw new IllegalStateException("Vault Token not found in parameters, " +
"system properties, or environment variables");
}

return new DedicatedVaultCredentials(vaultToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static ParameterSetParser.Builder configureBuilder(ParameterSetParser.Bui
// Tied to HashicorpCredentialsFactory.AUTHENTICATION_METHOD
DedicatedVaultCredentialsFactory.AUTHENTICATION_METHOD,
// Default value if none is specified:
DedicatedVaultAuthenticationMethod.TOKEN,
DedicatedVaultAuthenticationMethod.VAULT_TOKEN,
DedicatedVaultConfigurationParameters::parseAuthentication)
;
}
Expand All @@ -86,10 +86,12 @@ public static ParameterSetParser.Builder configureBuilder(ParameterSetParser.Bui
* @throws IllegalArgumentException if the value is unrecognized.
*/
private static DedicatedVaultAuthenticationMethod parseAuthentication(String value) {
if ("TOKEN".equalsIgnoreCase(value) || "VAULT_TOKEN".equalsIgnoreCase(value)) {
return DedicatedVaultAuthenticationMethod.TOKEN;
switch (value.toUpperCase()) {
case "VAULT_TOKEN":
return DedicatedVaultAuthenticationMethod.VAULT_TOKEN;
default:
throw new IllegalArgumentException(
"Unrecognized Hashicorp authentication value: " + value);
}
throw new IllegalArgumentException(
"Unrecognized Hashicorp authentication value: " + value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ public class DedicatedVaultSecretsManagerConfigurationProvider extends OracleCon

static final ParameterSetParser PARAMETER_SET_PARSER =
DedicatedVaultConfigurationParameters.configureBuilder(
ParameterSetParser.builder()
.addParameter("value",
DedicatedVaultSecretsManagerFactory.SECRET_PATH)
.addParameter("key",
DedicatedVaultSecretsManagerFactory.KEY)
.addParameter("VAULT_ADDR",
DedicatedVaultSecretsManagerFactory.VAULT_ADDR)
.addParameter("VAULT_TOKEN",
DedicatedVaultSecretsManagerFactory.VAULT_TOKEN)
.addParameter("FILED_NAME",
DedicatedVaultSecretsManagerFactory.FIELD_NAME))
.build();
ParameterSetParser.builder()
.addParameter("value",
DedicatedVaultSecretsManagerFactory.SECRET_PATH)
.addParameter("key",
DedicatedVaultSecretsManagerFactory.KEY)
.addParameter("VAULT_ADDR",
DedicatedVaultSecretsManagerFactory.VAULT_ADDR)
.addParameter("VAULT_TOKEN",
DedicatedVaultSecretsManagerFactory.VAULT_TOKEN)
.addParameter("FIELD_NAME",
DedicatedVaultSecretsManagerFactory.FIELD_NAME))
.build();

@Override
public InputStream getJson(String secretPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
import oracle.sql.json.OracleJsonValue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

import static java.nio.charset.StandardCharsets.UTF_8;
import static oracle.jdbc.provider.parameter.Parameter.CommonAttribute.REQUIRED;
Expand Down Expand Up @@ -145,8 +145,10 @@ private static String fetchSecretFromVault(String vaultUrl, String token) {
"Failed to fetch secret. HTTP error code: " + conn.getResponseCode());
}

try (InputStream in = conn.getInputStream()) {
return readStream(in);
try (InputStream in = conn.getInputStream();
Scanner scanner = new Scanner(in, UTF_8.name())) {
scanner.useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
} finally {
conn.disconnect();
}
Expand All @@ -156,17 +158,6 @@ private static String fetchSecretFromVault(String vaultUrl, String token) {
}
}

private static String readStream(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
return new String(baos.toByteArray(), UTF_8);
}


/**
* Extracts a specific key's value from a JSON-formatted secret.
*
Expand Down

0 comments on commit 49d620b

Please sign in to comment.