Search in sources :

Example 6 with ResourceNotFoundException

use of com.amazonaws.services.secretsmanager.model.ResourceNotFoundException in project spring-cloud-config by spring-cloud.

the class AwsSecretsManagerEnvironmentRepository method findProperties.

private Map<Object, Object> findProperties(String path) {
    Map<Object, Object> properties = new HashMap<>();
    GetSecretValueRequest request = new GetSecretValueRequest().withSecretId(path);
    try {
        GetSecretValueResult response = awsSmClient.getSecretValue(request);
        if (response != null) {
            Map<String, Object> secretMap = objectMapper.readValue(response.getSecretString(), new TypeReference<Map<String, Object>>() {
            });
            for (Map.Entry<String, Object> secretEntry : secretMap.entrySet()) {
                properties.put(secretEntry.getKey(), secretEntry.getValue());
            }
        }
    } catch (ResourceNotFoundException | IOException e) {
        log.debug(String.format("Skip adding propertySource. Unable to load secrets from AWS Secrets Manager for secretId=%s", path), e);
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) ResourceNotFoundException(com.amazonaws.services.secretsmanager.model.ResourceNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ResourceNotFoundException

use of com.amazonaws.services.secretsmanager.model.ResourceNotFoundException in project fernet-java8 by l0s.

the class MultiFernetKeyRotatorTest method verifyCreateSecretAddsKeyAndRemovesOldest.

@Test
public final void verifyCreateSecretAddsKeyAndRemovesOldest() throws IOException {
    // given
    final Key key0 = Key.generateKey(random);
    final Key key1 = Key.generateKey(random);
    final Key key2 = Key.generateKey(random);
    final DescribeSecretResult description = new DescribeSecretResult();
    description.setRotationEnabled(true);
    description.setVersionIdsToStages(ImmutableMap.of("version", Arrays.asList("AWSPENDING")));
    final InputStream input = new StringInputStream("{\"Step\": \"createSecret\",\"ClientRequestToken\": \"version\",\"SecretId\":\"secret\"}");
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    final Context context = mock(Context.class);
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        // pending
        key0.writeTo(stream);
        // primary
        key1.writeTo(stream);
        // old key
        key2.writeTo(stream);
        given(secretsManager.getSecretStage("secret", CURRENT)).willReturn(ByteBuffer.wrap(stream.toByteArray()));
        given(secretsManager.describeSecret("secret")).willReturn(description);
        given(secretsManager.getSecretVersion("secret", "version")).willThrow(new ResourceNotFoundException(""));
        // when
        rotator.handleRequest(input, output, context);
        // then
        verify(secretsManager).putSecretValue(eq("secret"), eq("version"), keyCollector.capture(), eq(PENDING));
        final Collection<? extends Key> keys = keyCollector.getValue();
        assertEquals(3, keys.size());
        // new pending key
        assertTrue(keys.contains(key0));
        // primary key (old pending)
        assertTrue(keys.contains(key1));
        // old key (old primary)
        assertFalse(keys.contains(key2));
        new ObjectMapper().readTree(output.toByteArray());
    }
}
Also used : Context(com.amazonaws.services.lambda.runtime.Context) DescribeSecretResult(com.amazonaws.services.secretsmanager.model.DescribeSecretResult) StringInputStream(com.amazonaws.util.StringInputStream) StringInputStream(com.amazonaws.util.StringInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceNotFoundException(com.amazonaws.services.secretsmanager.model.ResourceNotFoundException) Key(com.macasaet.fernet.Key) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with ResourceNotFoundException

use of com.amazonaws.services.secretsmanager.model.ResourceNotFoundException in project fernet-java8 by l0s.

the class MultiFernetKeyRotatorTest method verifyCreateClearsIntermediateSecret.

@Test
public final void verifyCreateClearsIntermediateSecret() throws IOException {
    // given
    final byte[] secretBytes = new byte[32];
    random.nextBytes(secretBytes);
    final int originalHashCode = Arrays.hashCode(secretBytes);
    final ByteBuffer secretByteBuffer = ByteBuffer.wrap(secretBytes);
    assertTrue(Arrays.equals(secretByteBuffer.array(), secretBytes));
    final DescribeSecretResult description = new DescribeSecretResult();
    description.setRotationEnabled(true);
    description.setVersionIdsToStages(ImmutableMap.of("clientRequestToken", Arrays.asList("AWSPENDING")));
    given(secretsManager.getSecretStage("secretId", CURRENT)).willReturn(secretByteBuffer);
    given(secretsManager.describeSecret("secretId")).willReturn(description);
    given(secretsManager.getSecretVersion("secretId", "clientRequestToken")).willThrow(new ResourceNotFoundException(""));
    final InputStream input = new StringInputStream("{\"Step\": \"createSecret\",\"ClientRequestToken\": \"clientRequestToken\",\"SecretId\":\"secretId\"}");
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    final Context context = mock(Context.class);
    // when
    rotator.handleRequest(input, output, context);
    // then
    final byte[] modifiedBytes = secretByteBuffer.array();
    assertEquals(32, modifiedBytes.length);
    assertNotEquals(originalHashCode, Arrays.hashCode(secretBytes));
    new ObjectMapper().readTree(output.toByteArray());
}
Also used : Context(com.amazonaws.services.lambda.runtime.Context) DescribeSecretResult(com.amazonaws.services.secretsmanager.model.DescribeSecretResult) StringInputStream(com.amazonaws.util.StringInputStream) StringInputStream(com.amazonaws.util.StringInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceNotFoundException(com.amazonaws.services.secretsmanager.model.ResourceNotFoundException) ByteBuffer(java.nio.ByteBuffer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 9 with ResourceNotFoundException

use of com.amazonaws.services.secretsmanager.model.ResourceNotFoundException in project fernet-java8 by l0s.

the class SecretsManagerTest method verifyAssertCurrentStageExistsThrowsException.

@Test
public final void verifyAssertCurrentStageExistsThrowsException() {
    // given
    final GetSecretValueRequest request = new GetSecretValueRequest();
    request.setSecretId("secret");
    request.setVersionStage("AWSCURRENT");
    given(delegate.getSecretValue(eq(request))).willThrow(new ResourceNotFoundException("not found"));
    // when / then (exception thrown)
    assertThrows(ResourceNotFoundException.class, () -> manager.assertCurrentStageExists("secret"));
}
Also used : GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) ResourceNotFoundException(com.amazonaws.services.secretsmanager.model.ResourceNotFoundException) Test(org.junit.Test)

Example 10 with ResourceNotFoundException

use of com.amazonaws.services.secretsmanager.model.ResourceNotFoundException in project spring-cloud-aws by awspring.

the class AwsSecretsManagerPropertySourceTest method throwsExceptionWhenSecretNotFound.

@Test
void throwsExceptionWhenSecretNotFound() {
    when(client.getSecretValue(any(GetSecretValueRequest.class))).thenThrow(new ResourceNotFoundException("secret not found"));
    assertThatThrownBy(() -> propertySource.init()).isInstanceOf(ResourceNotFoundException.class);
}
Also used : GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) ResourceNotFoundException(com.amazonaws.services.secretsmanager.model.ResourceNotFoundException) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceNotFoundException (com.amazonaws.services.secretsmanager.model.ResourceNotFoundException)10 Test (org.junit.Test)5 GetSecretValueRequest (com.amazonaws.services.secretsmanager.model.GetSecretValueRequest)4 Context (com.amazonaws.services.lambda.runtime.Context)3 DescribeSecretResult (com.amazonaws.services.secretsmanager.model.DescribeSecretResult)3 SecretNotFoundException (io.datarouter.secret.exception.SecretNotFoundException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 GetSecretValueResult (com.amazonaws.services.secretsmanager.model.GetSecretValueResult)2 StringInputStream (com.amazonaws.util.StringInputStream)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Key (com.macasaet.fernet.Key)2 DeleteSecretRequest (com.amazonaws.services.secretsmanager.model.DeleteSecretRequest)1 ResourceExistsException (com.amazonaws.services.secretsmanager.model.ResourceExistsException)1 UpdateSecretRequest (com.amazonaws.services.secretsmanager.model.UpdateSecretRequest)1 Secret (io.datarouter.secret.client.Secret)1 SecretExistsException (io.datarouter.secret.exception.SecretExistsException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1