Search in sources :

Example 1 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project JAQU-CAZ-Payments-API by InformedSolutions.

the class CredentialRetrievalManager method getSecretsValue.

private Map<String, String> getSecretsValue(String secretName) {
    GetSecretValueResult getSecretValueResult = getGetSecretValueFor(secretName);
    // this is a sample code provided by AWS in AWS Secret Manager console view
    String secretString = getSecretValueResult.getSecretString() != null ? getSecretValueResult.getSecretString() : new String(Base64.getDecoder().decode(getSecretValueResult.getSecretBinary()).array());
    try {
        return objectMapper.readValue(secretString, new TypeReference<Map<String, String>>() {
        });
    } catch (JsonProcessingException e) {
        log.error("Error while parsing AWS secrets:", e);
        return Collections.emptyMap();
    }
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project JAQU-CAZ-Payments-API by InformedSolutions.

the class CredentialRetrievalManagerTest method returnEmptyIfSecretStringCannotBeProcessed.

@Test
void returnEmptyIfSecretStringCannotBeProcessed() throws JsonProcessingException {
    GetSecretValueResult getSecretValueResponse = mock(GetSecretValueResult.class);
    Mockito.when(client.getSecretValue(Mockito.any(GetSecretValueRequest.class))).thenReturn(getSecretValueResponse);
    Mockito.when(getSecretValueResponse.getSecretString()).thenReturn("{");
    Optional<String> apiKey = credentialRetrievalManager.getCardApiKey(UUID.randomUUID());
    assertThat(apiKey).isEmpty();
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project JAQU-CAZ-Payments-API by InformedSolutions.

the class CredentialRetrievalManagerTest method getApiKeyFromSecretBinaryString.

@Test
void getApiKeyFromSecretBinaryString() throws JsonProcessingException {
    GetSecretValueResult getSecretValueResponse = mock(GetSecretValueResult.class);
    Mockito.when(client.getSecretValue(Mockito.any(GetSecretValueRequest.class))).thenReturn(getSecretValueResponse);
    Mockito.when(getSecretValueResponse.getSecretString()).thenReturn(null);
    UUID cleanAirZoneId = UUID.fromString("105db9f8-cdd0-4b0c-b906-29ce979fdc29");
    ObjectNode node = objectMapper.createObjectNode();
    node.put(cleanAirZoneId.toString().replace("-", ""), "testApiKey");
    Mockito.when(getSecretValueResponse.getSecretBinary()).thenReturn(ByteBuffer.wrap("eyIxMDVkYjlmOGNkZDA0YjBjYjkwNjI5Y2U5NzlmZGMyOSI6ICJ0ZXN0QXBpS2V5In0=".getBytes()));
    Optional<String> apiKey = credentialRetrievalManager.getCardApiKey(cleanAirZoneId);
    assertThat(apiKey).isPresent();
    assertThat(apiKey).contains("testApiKey");
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project JAQU-CAZ-Payments-API by InformedSolutions.

the class CredentialRetrievalManagerTest method cannotGetApiKey.

@Test
void cannotGetApiKey() throws JsonProcessingException {
    GetSecretValueResult getSecretValueResponse = mock(GetSecretValueResult.class);
    Mockito.when(client.getSecretValue(Mockito.any(GetSecretValueRequest.class))).thenReturn(getSecretValueResponse);
    Mockito.when(getSecretValueResponse.getSecretString()).thenReturn("{}");
    Optional<String> apiKey = credentialRetrievalManager.getCardApiKey(UUID.randomUUID());
    assertThat(apiKey).isEmpty();
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project datarouter by hotpads.

the class AwsSecretClient method read.

@Override
public final Secret read(String name) {
    var request = new GetSecretValueRequest().withSecretId(name);
    // .withVersionStage("")// related to AWS rotation
    try {
        GetSecretValueResult result;
        try (var $ = TracerTool.startSpan("AWSSecretsManager getSecretValue", TraceSpanGroupType.CLOUD_STORAGE)) {
            TracerTool.appendToSpanInfo(name);
            result = client.getSecretValue(request);
        }
        return new Secret(name, result.getSecretString());
    } catch (ResourceNotFoundException e) {
        throw new SecretNotFoundException(name, e);
    }
}
Also used : Secret(io.datarouter.secret.client.Secret) GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) ResourceNotFoundException(com.amazonaws.services.secretsmanager.model.ResourceNotFoundException) SecretNotFoundException(io.datarouter.secret.exception.SecretNotFoundException)

Aggregations

GetSecretValueResult (com.amazonaws.services.secretsmanager.model.GetSecretValueResult)60 GetSecretValueRequest (com.amazonaws.services.secretsmanager.model.GetSecretValueRequest)51 AWSSecretsManager (com.amazonaws.services.secretsmanager.AWSSecretsManager)25 Before (org.junit.Before)21 JdbcConnectionFactory (com.amazonaws.athena.connectors.jdbc.connection.JdbcConnectionFactory)18 JdbcCredentialProvider (com.amazonaws.athena.connectors.jdbc.connection.JdbcCredentialProvider)18 AmazonAthena (com.amazonaws.services.athena.AmazonAthena)17 FederatedIdentity (com.amazonaws.athena.connector.lambda.security.FederatedIdentity)16 Connection (java.sql.Connection)13 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)8 AmazonS3 (com.amazonaws.services.s3.AmazonS3)6 DatabaseConnectionConfig (com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionConfig)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Map (java.util.Map)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 QueryStatusChecker (com.amazonaws.athena.connector.lambda.QueryStatusChecker)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 IOException (java.io.IOException)3