use of com.quorum.tessera.config.KeyDataConfig in project tessera by ConsenSys.
the class PicoCliDelegateTest method updatingPasswordsDoesntProcessOtherOptions.
@Test
public void updatingPasswordsDoesntProcessOtherOptions() throws Exception {
final InputStream oldIn = System.in;
final InputStream inputStream = new ByteArrayInputStream((System.lineSeparator() + System.lineSeparator()).getBytes());
System.setIn(inputStream);
final KeyDataConfig startingKey = JaxbUtil.unmarshal(getClass().getResourceAsStream("/lockedprivatekey.json"), KeyDataConfig.class);
final Path key = Files.createTempFile("key", ".key");
Files.write(key, JaxbUtil.marshalToString(startingKey).getBytes());
final CliResult result = cliDelegate.execute("-updatepassword", "--keys.keyData.privateKeyPath", key.toString(), "--keys.passwords", "testpassword");
assertThat(result).isNotNull();
System.setIn(oldIn);
}
use of com.quorum.tessera.config.KeyDataConfig in project tessera by ConsenSys.
the class FileKeyGenerator method generate.
@Override
public FilesystemKeyPair generate(final String filename, final ArgonOptions encryptionOptions, final KeyVaultOptions keyVaultOptions) {
final char[] password = this.passwordReader.requestUserPassword();
final KeyPair generated = this.encryptor.generateNewKeys();
final String publicKeyBase64 = Base64.getEncoder().encodeToString(generated.getPublicKey().getKeyBytes());
final KeyData finalKeys = new KeyData();
final KeyDataConfig keyDataConfig;
if (password.length > 0) {
final PrivateKeyData encryptedPrivateKey = this.keyEncryptor.encryptPrivateKey(generated.getPrivateKey(), password, encryptionOptions);
keyDataConfig = new KeyDataConfig(new PrivateKeyData(null, encryptedPrivateKey.getSnonce(), encryptedPrivateKey.getAsalt(), encryptedPrivateKey.getSbox(), encryptedPrivateKey.getArgonOptions()), LOCKED);
LOGGER.info("Newly generated private key has been encrypted");
} else {
String keyData = Base64.getEncoder().encodeToString(generated.getPrivateKey().getKeyBytes());
keyDataConfig = new KeyDataConfig(new PrivateKeyData(keyData, null, null, null, null), UNLOCKED);
}
finalKeys.setConfig(keyDataConfig);
finalKeys.setPrivateKey(generated.getPrivateKey().encodeToBase64());
finalKeys.setPublicKey(publicKeyBase64);
final String privateKeyJson = JaxbUtil.marshalToString(finalKeys.getConfig());
final Path resolvedPath = Paths.get(filename).toAbsolutePath();
final Path parentPath;
if (EMPTY_FILENAME.equals(filename)) {
parentPath = resolvedPath;
} else {
parentPath = resolvedPath.getParent();
}
final Path publicKeyPath = parentPath.resolve(filename + ".pub");
final Path privateKeyPath = parentPath.resolve(filename + ".key");
IOCallback.execute(() -> Files.write(publicKeyPath, publicKeyBase64.getBytes(UTF_8), CREATE_NEW));
IOCallback.execute(() -> Files.write(privateKeyPath, privateKeyJson.getBytes(UTF_8), CREATE_NEW));
LOGGER.info("Saved public key to {}", publicKeyPath.toAbsolutePath().toString());
LOGGER.info("Saved private key to {}", privateKeyPath.toAbsolutePath().toString());
final FilesystemKeyPair keyPair = new FilesystemKeyPair(publicKeyPath, privateKeyPath, keyEncryptor);
keyPair.withPassword(password);
return keyPair;
}
use of com.quorum.tessera.config.KeyDataConfig in project tessera by ConsenSys.
the class UnsupportedKeyPairValidatorTest method directViolationIsDefaultIfNoDirectPrivateEvenIfMultipleIncompleteKeyPairTypesProvided.
@Test
public void directViolationIsDefaultIfNoDirectPrivateEvenIfMultipleIncompleteKeyPairTypesProvided() {
KeyDataConfig keyDataConfig = mock(KeyDataConfig.class);
Path path = mock(Path.class);
keyPair.setConfig(keyDataConfig);
keyPair.setPublicKey("public");
keyPair.setPublicKeyPath(path);
keyPair.setAzureVaultPublicKeyId("pubAzure");
keyPair.setHashicorpVaultPublicKeyId("pubHashicorp");
keyPair.setAwsSecretsManagerPublicKeyId("pubAWS");
validator.isValid(keyPair, context);
verify(context).buildConstraintViolationWithTemplate("{UnsupportedKeyPair.bothDirectKeysRequired.message}");
}
use of com.quorum.tessera.config.KeyDataConfig in project tessera by ConsenSys.
the class InlineKeypairTest method unlockedKeyGetsValue.
@Test
public void unlockedKeyGetsValue() {
PrivateKeyData privateKeyData = mock(PrivateKeyData.class);
final KeyDataConfig privKeyDataConfig = mock(KeyDataConfig.class);
when(privKeyDataConfig.getPrivateKeyData()).thenReturn(privateKeyData);
when(privKeyDataConfig.getType()).thenReturn(PrivateKeyType.UNLOCKED);
String value = "I love sparrows";
when(privKeyDataConfig.getValue()).thenReturn(value);
final InlineKeypair result = new InlineKeypair("public", privKeyDataConfig, keyEncryptor);
assertThat(result.getPrivateKey()).isEqualTo(value);
verifyZeroInteractions(keyEncryptor);
}
use of com.quorum.tessera.config.KeyDataConfig in project tessera by ConsenSys.
the class InlineKeypairTest method updatingPasswordsAttemptsToDecryptAgain.
@Test
public void updatingPasswordsAttemptsToDecryptAgain() {
PrivateKeyData privateKeyData = mock(PrivateKeyData.class);
final KeyDataConfig privKeyDataConfig = mock(KeyDataConfig.class);
when(privKeyDataConfig.getPrivateKeyData()).thenReturn(privateKeyData);
when(privKeyDataConfig.getType()).thenReturn(PrivateKeyType.LOCKED);
when(keyEncryptor.decryptPrivateKey(privateKeyData, "wrong-password".toCharArray())).thenThrow(new EncryptorException("WHAT YOU TALKING ABOUT WILLIS"));
final InlineKeypair inlineKeypair = new InlineKeypair("public", privKeyDataConfig, keyEncryptor);
inlineKeypair.withPassword("wrong-password".toCharArray());
String result = inlineKeypair.getPrivateKey();
assertThat(result).isEqualTo("NACL_FAILURE");
// change password and attempt again
inlineKeypair.withPassword("testpassword".toCharArray());
PrivateKey privateKey = mock(PrivateKey.class);
when(privateKey.encodeToBase64()).thenReturn("SUCCESS");
when(keyEncryptor.decryptPrivateKey(privateKeyData, "testpassword".toCharArray())).thenReturn(privateKey);
assertThat(inlineKeypair.getPrivateKey()).isEqualTo("SUCCESS");
verify(keyEncryptor).decryptPrivateKey(privateKeyData, "wrong-password".toCharArray());
verify(keyEncryptor).decryptPrivateKey(privateKeyData, "testpassword".toCharArray());
}
Aggregations