Search in sources :

Example 11 with DescribeSecretResult

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

the class SimpleFernetKeyRotatorTest method verifyHandleRequestTestsValidKey.

@Test
public final void verifyHandleRequestTestsValidKey() throws IOException {
    // given
    final Context context = mock(Context.class);
    final String clientRequestToken = "clientRequestToken";
    final String secretId = "secretId";
    final DescribeSecretResult secretDescription = new DescribeSecretResult();
    secretDescription.setRotationEnabled(true);
    secretDescription.addVersionIdsToStagesEntry(clientRequestToken, singletonList("AWSPENDING"));
    given(secretsManager.describeSecret(secretId)).willReturn(secretDescription);
    final Key key = Key.generateKey(random);
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(32)) {
        key.writeTo(outputStream);
        given(secretsManager.getSecretVersion(secretId, clientRequestToken)).willReturn(ByteBuffer.wrap(outputStream.toByteArray()));
    }
    final RotationRequest testRequest = new RotationRequest();
    testRequest.setClientRequestToken(clientRequestToken);
    testRequest.setSecretId(secretId);
    testRequest.setStep(Step.TEST_SECRET);
    final byte[] testRequestBytes = mapper.writeValueAsBytes(testRequest);
    try (InputStream input = new ByteArrayInputStream(testRequestBytes)) {
        try (OutputStream output = new ByteArrayOutputStream()) {
            // when
            rotator.handleRequest(input, output, context);
        // then (nothing)
        }
    }
}
Also used : Context(com.amazonaws.services.lambda.runtime.Context) DescribeSecretResult(com.amazonaws.services.secretsmanager.model.DescribeSecretResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Key(com.macasaet.fernet.Key) Test(org.junit.Test)

Example 12 with DescribeSecretResult

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

the class SimpleFernetKeyRotatorTest method verifyHandleRequestTestsInsufficientBytes.

@Test
public final void verifyHandleRequestTestsInsufficientBytes() throws IOException {
    // given
    final Context context = mock(Context.class);
    final String clientRequestToken = "clientRequestToken";
    final String secretId = "secretId";
    final DescribeSecretResult secretDescription = new DescribeSecretResult();
    secretDescription.setRotationEnabled(true);
    secretDescription.addVersionIdsToStagesEntry(clientRequestToken, singletonList("AWSPENDING"));
    given(secretsManager.describeSecret(secretId)).willReturn(secretDescription);
    given(secretsManager.getSecretVersion(secretId, clientRequestToken)).willReturn(ByteBuffer.allocateDirect(31));
    final RotationRequest testRequest = new RotationRequest();
    testRequest.setClientRequestToken(clientRequestToken);
    testRequest.setSecretId(secretId);
    testRequest.setStep(Step.TEST_SECRET);
    final byte[] testRequestBytes = mapper.writeValueAsBytes(testRequest);
    try (InputStream input = new ByteArrayInputStream(testRequestBytes)) {
        try (OutputStream output = new ByteArrayOutputStream()) {
            // when / then (exception thrown)
            assertThrows(RuntimeException.class, () -> rotator.handleRequest(input, output, context));
        }
    }
}
Also used : Context(com.amazonaws.services.lambda.runtime.Context) DescribeSecretResult(com.amazonaws.services.secretsmanager.model.DescribeSecretResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

DescribeSecretResult (com.amazonaws.services.secretsmanager.model.DescribeSecretResult)12 Test (org.junit.Test)11 Context (com.amazonaws.services.lambda.runtime.Context)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 InputStream (java.io.InputStream)10 StringInputStream (com.amazonaws.util.StringInputStream)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Key (com.macasaet.fernet.Key)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStream (java.io.OutputStream)4 ResourceNotFoundException (com.amazonaws.services.secretsmanager.model.ResourceNotFoundException)3 DescribeSecretRequest (com.amazonaws.services.secretsmanager.model.DescribeSecretRequest)2 ByteBuffer (java.nio.ByteBuffer)2