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)
}
}
}
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);
}
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);
}
Aggregations