Commit 44d2be5d authored by Kelly Domico's avatar Kelly Domico Committed by Kishore Battula
Browse files

Add trace and dependency logging to KeyVaultFacade

parent 9a6979c4
......@@ -219,7 +219,7 @@ The following software have components provided under the terms of this license:
- jakarta.xml.bind-api (from )
========================================================================
CC-BY-3.0
CC-BY-2.5
========================================================================
The following software have components provided under the terms of this license:
......@@ -247,7 +247,6 @@ CDDL-1.0
========================================================================
The following software have components provided under the terms of this license:
- JavaBeans(TM) Activation Framework (from http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp)
- JavaMail API (from )
- javax.annotation-api (from http://jcp.org/en/jsr/detail?id=250)
......@@ -257,6 +256,7 @@ CDDL-1.1
The following software have components provided under the terms of this license:
- JavaBeans Activation Framework (from )
- JavaBeans(TM) Activation Framework (from http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp)
- tomcat-embed-core (from http://tomcat.apache.org/)
========================================================================
......@@ -467,6 +467,7 @@ The following software have components provided under the terms of this license:
- Microsoft Azure client library for Blob Storage (from https://github.com/Azure/azure-sdk-for-java)
- Project Lombok (from https://projectlombok.org)
- Spring Web (from https://github.com/spring-projects/spring-framework)
- StAX API (from http://stax.codehaus.org/)
- msal4j (from https://github.com/AzureAD/microsoft-authentication-library-for-java)
- reactive-streams (from http://www.reactive-streams.org/)
......
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.48</version>
<version>0.0.49</version>
<name>core-lib-azure</name>
<properties>
......
......@@ -17,13 +17,16 @@ package org.opengroup.osdu.azure;
import com.azure.core.exception.ResourceNotFoundException;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.opengroup.osdu.azure.logging.DependencyPayload;
import org.opengroup.osdu.common.Validators;
import java.util.logging.Logger;
import java.time.Duration;
/**
* A simpler interface for interacting with keyVault.
*
* <p>
* Usage Examples:
* <pre>
* {@code
......@@ -36,8 +39,7 @@ import java.util.logging.Logger;
* </pre>
*/
public final class KeyVaultFacade {
private static final Logger LOGGER = Logger.getLogger(KeyVaultFacade.class.getName());
private static final String LOGGER_NAME = KeyVaultFacade.class.getName();
/**
* Private constructor -- this class should never be instantiated.
......@@ -51,10 +53,7 @@ public final class KeyVaultFacade {
* @return A guaranteed to be non null secret value
*/
public static String getSecretWithValidation(final SecretClient kv, final String secretName) {
KeyVaultSecret secret = kv.getSecret(secretName);
Validators.checkNotNull(secret, secretName);
String secretValue = secret.getValue();
String secretValue = getSecretWithDefault(kv, secretName, null);
Validators.checkNotNullAndNotEmpty(secretValue, secretName);
return secretValue;
......@@ -68,17 +67,44 @@ public final class KeyVaultFacade {
* @param defaultValue to be used in case the secret is null or empty.
* @return Secret value. It is guaranteed to be returned with either default value or a non null, non empty secret.
*/
public String getSecretWithDefault(final SecretClient kv, final String secretName, final String defaultValue) {
public static String getSecretWithDefault(final SecretClient kv, final String secretName, final String defaultValue) {
Validators.checkNotNull(secretName, "Secret with name " + secretName);
KeyVaultSecret secret;
final long start = System.currentTimeMillis();
int statusCode = HttpStatus.SC_OK;
try {
secret = kv.getSecret(secretName);
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("Successfully retrieved {}.", secretName);
if (secret == null || secret.getValue() == null || secret.getValue().isEmpty()) {
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("Value for {} is empty.", secretName);
return defaultValue;
}
} catch (ResourceNotFoundException secretNotFound) {
statusCode = HttpStatus.SC_NOT_FOUND;
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).warn("Failed to retrieve {}. Not found.", secretName);
return defaultValue;
} finally {
final long timeTaken = System.currentTimeMillis() - start;
logDependency("GET_SECRET", secretName, kv.getVaultUrl(), timeTaken, statusCode);
}
return secret.getValue();
}
/**
* Log dependency.
*
* @param name the name of the command initiated with this dependency call
* @param data the command initiated by this dependency call
* @param target the target of this dependency call
* @param timeTakenInMs the request duration in milliseconds
* @param resultCode the result code of the call
*/
private static void logDependency(final String name, final String data, final String target, final long timeTakenInMs, final int resultCode) {
DependencyPayload payload = new DependencyPayload(name, data, Duration.ofMillis(timeTakenInMs), String.valueOf(resultCode), resultCode == HttpStatus.SC_OK);
payload.setType("KeyVault");
payload.setTarget(target);
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).logDependency(payload);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment