Search in sources :

Example 21 with Key

use of com.macasaet.fernet.Key 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 22 with Key

use of com.macasaet.fernet.Key in project fernet-java8 by l0s.

the class SimpleFernetKeyRotator method createSecret.

protected void createSecret(final String secretId, final String clientRequestToken) {
    final Key key = Key.generateKey(getRandom());
    getSecretsManager().putSecretValue(secretId, clientRequestToken, key, PENDING);
    getLogger().info("createSecret: Successfully put secret for ARN {} and version {}.", secretId, clientRequestToken);
}
Also used : Key(com.macasaet.fernet.Key)

Example 23 with Key

use of com.macasaet.fernet.Key in project fernet-java8 by l0s.

the class SimpleFernetKeyRotator method testSecret.

protected void testSecret(final String secretId, final String clientRequestToken) {
    final ByteBuffer buffer = getSecretsManager().getSecretVersion(secretId, clientRequestToken);
    try {
        if (buffer.remaining() != fernetKeySize) {
            throw new IllegalStateException("Fernet key must be exactly " + fernetKeySize + " bytes");
        }
        final byte[] signingKey = new byte[16];
        buffer.get(signingKey);
        final byte[] encryptionKey = new byte[16];
        buffer.get(encryptionKey);
        if (buffer.hasRemaining()) {
            throw new IllegalStateException("Encountered extra bytes.");
        }
        new Key(signingKey, encryptionKey);
        wipe(signingKey);
        wipe(encryptionKey);
    } finally {
        wipe(buffer);
    }
    getLogger().info("testSecret: Successfully validated Fernet Key for ARN {} and version {}.", secretId, clientRequestToken);
}
Also used : ByteBuffer(java.nio.ByteBuffer) Key(com.macasaet.fernet.Key)

Aggregations

Key (com.macasaet.fernet.Key)23 Test (org.junit.Test)12 Token (com.macasaet.fernet.Token)10 SecureRandom (java.security.SecureRandom)5 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ByteBuffer (java.nio.ByteBuffer)4 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)4 Context (com.amazonaws.services.lambda.runtime.Context)3 DescribeSecretResult (com.amazonaws.services.secretsmanager.model.DescribeSecretResult)3 PutSecretValueRequest (com.amazonaws.services.secretsmanager.model.PutSecretValueRequest)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 StringInputStream (com.amazonaws.util.StringInputStream)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 FileOutputStream (java.io.FileOutputStream)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2