@Test public void unmarshalFilesystemKeyPair() { KeyEncryptor keyEncryptor = mock(KeyEncryptor.class); KeyData keyData = new KeyData(); Path privateKeyPath = mock(Path.class); Path publicKeyPath = mock(Path.class); keyData.setPrivateKeyPath(privateKeyPath); keyData.setPublicKeyPath(publicKeyPath); FilesystemKeyPair result = (FilesystemKeyPair) KeyDataUtil.unmarshal(keyData, keyEncryptor); assertThat(result).isNotNull(); assertThat(result.getPrivateKeyPath()).isSameAs(privateKeyPath); assertThat(result.getPublicKeyPath()).isSameAs(publicKeyPath); verifyZeroInteractions(keyEncryptor); }
public static ConfigKeyPair unmarshal(final KeyData keyData, final KeyEncryptor keyEncryptor) { if (isDirect(keyData)) { return new DirectKeyPair(keyData.getPublicKey(), keyData.getPrivateKey()); } if (isInline(keyData)) { InlineKeypair keyPair = new InlineKeypair(keyData.getPublicKey(), keyData.getConfig(), keyEncryptor); keyPair.withPassword(keyData.getPassword()); return keyPair; } if (isAzure(keyData)) { return new AzureVaultKeyPair( keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion()); } if (isHashicorp(keyData)) { Integer hashicorpVaultSecretVersion; Optional<String> hashicorpVaultSecretVersionStr = Optional.of(keyData).map(KeyData::getHashicorpVaultSecretVersion); if (hashicorpVaultSecretVersionStr.isPresent()) { hashicorpVaultSecretVersion = hashicorpVaultSecretVersionStr .filter(Pattern.compile("^\\d*$").asPredicate()) .map(Integer::parseInt) .orElse(-1); } else { hashicorpVaultSecretVersion = 0; } return new HashicorpVaultKeyPair( keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), hashicorpVaultSecretVersion); } if (isAws(keyData)) { return new AWSKeyPair( keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } if (isFileSystem(keyData)) { FilesystemKeyPair fileSystemKeyPair = new FilesystemKeyPair(keyData.getPublicKeyPath(), keyData.getPrivateKeyPath(), keyEncryptor); fileSystemKeyPair.withPassword(keyData.getPassword()); return fileSystemKeyPair; } return new UnsupportedKeyPair( keyData.getConfig(), keyData.getPrivateKey(), keyData.getPublicKey(), keyData.getPrivateKeyPath(), keyData.getPublicKeyPath(), keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion(), keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), keyData.getHashicorpVaultSecretVersion(), keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); }
KeyDataUtil { public static ConfigKeyPair unmarshal(final KeyData keyData, final KeyEncryptor keyEncryptor) { if (isDirect(keyData)) { return new DirectKeyPair(keyData.getPublicKey(), keyData.getPrivateKey()); } if (isInline(keyData)) { InlineKeypair keyPair = new InlineKeypair(keyData.getPublicKey(), keyData.getConfig(), keyEncryptor); keyPair.withPassword(keyData.getPassword()); return keyPair; } if (isAzure(keyData)) { return new AzureVaultKeyPair( keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion()); } if (isHashicorp(keyData)) { Integer hashicorpVaultSecretVersion; Optional<String> hashicorpVaultSecretVersionStr = Optional.of(keyData).map(KeyData::getHashicorpVaultSecretVersion); if (hashicorpVaultSecretVersionStr.isPresent()) { hashicorpVaultSecretVersion = hashicorpVaultSecretVersionStr .filter(Pattern.compile("^\\d*$").asPredicate()) .map(Integer::parseInt) .orElse(-1); } else { hashicorpVaultSecretVersion = 0; } return new HashicorpVaultKeyPair( keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), hashicorpVaultSecretVersion); } if (isAws(keyData)) { return new AWSKeyPair( keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } if (isFileSystem(keyData)) { FilesystemKeyPair fileSystemKeyPair = new FilesystemKeyPair(keyData.getPublicKeyPath(), keyData.getPrivateKeyPath(), keyEncryptor); fileSystemKeyPair.withPassword(keyData.getPassword()); return fileSystemKeyPair; } return new UnsupportedKeyPair( keyData.getConfig(), keyData.getPrivateKey(), keyData.getPublicKey(), keyData.getPrivateKeyPath(), keyData.getPublicKeyPath(), keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion(), keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), keyData.getHashicorpVaultSecretVersion(), keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } }
KeyDataUtil { public static ConfigKeyPair unmarshal(final KeyData keyData, final KeyEncryptor keyEncryptor) { if (isDirect(keyData)) { return new DirectKeyPair(keyData.getPublicKey(), keyData.getPrivateKey()); } if (isInline(keyData)) { InlineKeypair keyPair = new InlineKeypair(keyData.getPublicKey(), keyData.getConfig(), keyEncryptor); keyPair.withPassword(keyData.getPassword()); return keyPair; } if (isAzure(keyData)) { return new AzureVaultKeyPair( keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion()); } if (isHashicorp(keyData)) { Integer hashicorpVaultSecretVersion; Optional<String> hashicorpVaultSecretVersionStr = Optional.of(keyData).map(KeyData::getHashicorpVaultSecretVersion); if (hashicorpVaultSecretVersionStr.isPresent()) { hashicorpVaultSecretVersion = hashicorpVaultSecretVersionStr .filter(Pattern.compile("^\\d*$").asPredicate()) .map(Integer::parseInt) .orElse(-1); } else { hashicorpVaultSecretVersion = 0; } return new HashicorpVaultKeyPair( keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), hashicorpVaultSecretVersion); } if (isAws(keyData)) { return new AWSKeyPair( keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } if (isFileSystem(keyData)) { FilesystemKeyPair fileSystemKeyPair = new FilesystemKeyPair(keyData.getPublicKeyPath(), keyData.getPrivateKeyPath(), keyEncryptor); fileSystemKeyPair.withPassword(keyData.getPassword()); return fileSystemKeyPair; } return new UnsupportedKeyPair( keyData.getConfig(), keyData.getPrivateKey(), keyData.getPublicKey(), keyData.getPrivateKeyPath(), keyData.getPublicKeyPath(), keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion(), keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), keyData.getHashicorpVaultSecretVersion(), keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } private  KeyDataUtil(); }
KeyDataUtil { public static ConfigKeyPair unmarshal(final KeyData keyData, final KeyEncryptor keyEncryptor) { if (isDirect(keyData)) { return new DirectKeyPair(keyData.getPublicKey(), keyData.getPrivateKey()); } if (isInline(keyData)) { InlineKeypair keyPair = new InlineKeypair(keyData.getPublicKey(), keyData.getConfig(), keyEncryptor); keyPair.withPassword(keyData.getPassword()); return keyPair; } if (isAzure(keyData)) { return new AzureVaultKeyPair( keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion()); } if (isHashicorp(keyData)) { Integer hashicorpVaultSecretVersion; Optional<String> hashicorpVaultSecretVersionStr = Optional.of(keyData).map(KeyData::getHashicorpVaultSecretVersion); if (hashicorpVaultSecretVersionStr.isPresent()) { hashicorpVaultSecretVersion = hashicorpVaultSecretVersionStr .filter(Pattern.compile("^\\d*$").asPredicate()) .map(Integer::parseInt) .orElse(-1); } else { hashicorpVaultSecretVersion = 0; } return new HashicorpVaultKeyPair( keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), hashicorpVaultSecretVersion); } if (isAws(keyData)) { return new AWSKeyPair( keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } if (isFileSystem(keyData)) { FilesystemKeyPair fileSystemKeyPair = new FilesystemKeyPair(keyData.getPublicKeyPath(), keyData.getPrivateKeyPath(), keyEncryptor); fileSystemKeyPair.withPassword(keyData.getPassword()); return fileSystemKeyPair; } return new UnsupportedKeyPair( keyData.getConfig(), keyData.getPrivateKey(), keyData.getPublicKey(), keyData.getPrivateKeyPath(), keyData.getPublicKeyPath(), keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion(), keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), keyData.getHashicorpVaultSecretVersion(), keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } private  KeyDataUtil(); static Class<? extends ConfigKeyPair> getKeyPairTypeFor(KeyData keyData); static boolean isLocked(KeyData keyData); static boolean isDirect(KeyData keyData); static boolean isInline(KeyData keyData); static boolean isAzure(KeyData keyData); static boolean isHashicorp(KeyData keyData); static boolean isAws(KeyData keyData); static boolean isFileSystem(KeyData keyData); static boolean isUnsupported(KeyData keyData); static ConfigKeyPair unmarshal(final KeyData keyData, final KeyEncryptor keyEncryptor); static KeyData marshal(final ConfigKeyPair keyPair); }
KeyDataUtil { public static ConfigKeyPair unmarshal(final KeyData keyData, final KeyEncryptor keyEncryptor) { if (isDirect(keyData)) { return new DirectKeyPair(keyData.getPublicKey(), keyData.getPrivateKey()); } if (isInline(keyData)) { InlineKeypair keyPair = new InlineKeypair(keyData.getPublicKey(), keyData.getConfig(), keyEncryptor); keyPair.withPassword(keyData.getPassword()); return keyPair; } if (isAzure(keyData)) { return new AzureVaultKeyPair( keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion()); } if (isHashicorp(keyData)) { Integer hashicorpVaultSecretVersion; Optional<String> hashicorpVaultSecretVersionStr = Optional.of(keyData).map(KeyData::getHashicorpVaultSecretVersion); if (hashicorpVaultSecretVersionStr.isPresent()) { hashicorpVaultSecretVersion = hashicorpVaultSecretVersionStr .filter(Pattern.compile("^\\d*$").asPredicate()) .map(Integer::parseInt) .orElse(-1); } else { hashicorpVaultSecretVersion = 0; } return new HashicorpVaultKeyPair( keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), hashicorpVaultSecretVersion); } if (isAws(keyData)) { return new AWSKeyPair( keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } if (isFileSystem(keyData)) { FilesystemKeyPair fileSystemKeyPair = new FilesystemKeyPair(keyData.getPublicKeyPath(), keyData.getPrivateKeyPath(), keyEncryptor); fileSystemKeyPair.withPassword(keyData.getPassword()); return fileSystemKeyPair; } return new UnsupportedKeyPair( keyData.getConfig(), keyData.getPrivateKey(), keyData.getPublicKey(), keyData.getPrivateKeyPath(), keyData.getPublicKeyPath(), keyData.getAzureVaultPublicKeyId(), keyData.getAzureVaultPrivateKeyId(), keyData.getAzureVaultPublicKeyVersion(), keyData.getAzureVaultPrivateKeyVersion(), keyData.getHashicorpVaultPublicKeyId(), keyData.getHashicorpVaultPrivateKeyId(), keyData.getHashicorpVaultSecretEngineName(), keyData.getHashicorpVaultSecretName(), keyData.getHashicorpVaultSecretVersion(), keyData.getAwsSecretsManagerPublicKeyId(), keyData.getAwsSecretsManagerPrivateKeyId()); } private  KeyDataUtil(); static Class<? extends ConfigKeyPair> getKeyPairTypeFor(KeyData keyData); static boolean isLocked(KeyData keyData); static boolean isDirect(KeyData keyData); static boolean isInline(KeyData keyData); static boolean isAzure(KeyData keyData); static boolean isHashicorp(KeyData keyData); static boolean isAws(KeyData keyData); static boolean isFileSystem(KeyData keyData); static boolean isUnsupported(KeyData keyData); static ConfigKeyPair unmarshal(final KeyData keyData, final KeyEncryptor keyEncryptor); static KeyData marshal(final ConfigKeyPair keyPair);  }