use of com.macasaet.fernet.Key in project fernet-java8 by l0s.
the class MultiFernetKeyRotator method testSecret.
protected void testSecret(final String secretId, final String clientRequestToken) {
final ByteBuffer currentSecret = getSecretsManager().getSecretVersion(secretId, clientRequestToken);
try {
if (currentSecret.remaining() % fernetKeySize != 0) {
throw new IllegalStateException("There must be a multiple of " + fernetKeySize + " bytes.");
}
// first key will become the staged key
final byte[] signingKey = new byte[16];
currentSecret.get(signingKey);
final byte[] encryptionKey = new byte[16];
currentSecret.get(encryptionKey);
new Key(signingKey, encryptionKey);
wipe(signingKey);
wipe(encryptionKey);
} finally {
wipe(currentSecret);
}
}
use of com.macasaet.fernet.Key in project fernet-java8 by l0s.
the class SecretsManager method putSecretValue.
/**
* Store Fernet keys in the secret. This requires the permission <code>secretsmanager:PutSecretValue</code>
*
* @param secretId
* the ARN of the secret
* @param clientRequestToken
* the secret version identifier
* @param keys
* the keys to store in the secret
* @param stage
* the stage with which to tag the version
*/
public void putSecretValue(final String secretId, final String clientRequestToken, final Collection<? extends Key> keys, final Stage stage) {
final PutSecretValueRequest putSecretValueRequest = new PutSecretValueRequest();
putSecretValueRequest.setSecretId(secretId);
putSecretValueRequest.setClientRequestToken(clientRequestToken);
putSecretValueRequest.setVersionStages(singletonList(stage.getAwsName()));
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(32 * keys.size())) {
for (final Key key : keys) {
key.writeTo(outputStream);
}
final ByteBuffer buffer = ByteBuffer.wrap(outputStream.toByteArray());
putSecretValueRequest.setSecretBinary(buffer);
outputStream.reset();
for (int i = keys.size(); --i >= 0; outputStream.write(0)) ;
} catch (final IOException ioe) {
// this really should not happen as I/O is to memory only
throw new IllegalStateException(ioe.getMessage(), ioe);
}
getDelegate().putSecretValue(putSecretValueRequest);
}
use of com.macasaet.fernet.Key in project fernet-java8 by l0s.
the class CreateMultiKey method main.
public static final void main(final String... arguments) throws Exception {
final SecureRandom random = new SecureRandom();
try (FileOutputStream outputStream = new FileOutputStream("multi-key")) {
for (int i = 3; --i >= 0; ) {
final Key key = Key.generateKey(random);
key.writeTo(outputStream);
}
}
/*
aws secretsmanager create-secret --name multi-fernet-key --secret-binary fileb://multi-key
{
"ARN": "arn:aws:secretsmanager:<region>:<account_id>:secret:multi-fernet-key-<random_value>",
"Name": "multi-fernet-key",
"VersionId": "<uuidv4>"
}
*/
}
use of com.macasaet.fernet.Key in project fernet-java8 by l0s.
the class TokenHeaderUtilityTest method verifyGetAuthorizationTokenIgnoresX.
@Test
public final void verifyGetAuthorizationTokenIgnoresX() {
// given
final Key key = Key.generateKey(random);
final Token token = Token.generate(random, key, "hello");
final ContainerRequest request = mock(ContainerRequest.class);
given(request.getHeaderString("X-Authorization")).willReturn(token.serialise());
// when
final Token result = utility.getAuthorizationToken(request);
// then
assertNull(result);
}
use of com.macasaet.fernet.Key in project fernet-java8 by l0s.
the class ProtectedResource method issueToken.
/**
* @param username a valid username
* @param password the password for the user <em>username</em>
* @return a new Fernet token if and only if the credentials are valid
* @throws NotAuthorizedException if invalid credentials are provided
*/
@POST
@Path("token")
public String issueToken(final String username, final String password) {
if ("username".equals(username) && "password".equals(password)) {
// might be nice to have Token.generate(repository, payload)
final Key primaryKey = getKeyRepository().getPrimaryKey();
final Token token = Token.generate(random, primaryKey, username);
return token.serialise();
}
throw new NotAuthorizedException("Bearer realm=\"secrets\"");
}
Aggregations