use of com.quorum.tessera.key.vault.KeyVaultService in project tessera by ConsenSys.
the class KeyGeneratorFactoryTest method awsVaultKeyGeneratorWhenAwsConfigProvided.
@Test
public void awsVaultKeyGeneratorWhenAwsConfigProvided() {
final DefaultKeyVaultConfig keyVaultConfig = new DefaultKeyVaultConfig();
keyVaultConfig.setKeyVaultType(KeyVaultType.AWS);
EncryptorConfig encryptorConfig = mock(EncryptorConfig.class);
when(encryptorConfig.getType()).thenReturn(EncryptorType.NACL);
when(encryptorConfig.getProperties()).thenReturn(Collections.EMPTY_MAP);
KeyGeneratorFactory keyGeneratorFactory = KeyGeneratorFactory.create();
try (MockedStatic<KeyVaultServiceFactory> mockedKeyVaultServiceFactory = mockStatic(KeyVaultServiceFactory.class)) {
KeyVaultService keyVaultService = mock(KeyVaultService.class);
KeyVaultServiceFactory keyVaultServiceFactory = mock(KeyVaultServiceFactory.class);
when(keyVaultServiceFactory.create(any(), any())).thenReturn(keyVaultService);
mockedKeyVaultServiceFactory.when(() -> KeyVaultServiceFactory.getInstance(KeyVaultType.AWS)).thenReturn(keyVaultServiceFactory);
final KeyGenerator keyGenerator = keyGeneratorFactory.create(keyVaultConfig, encryptorConfig);
assertThat(keyGenerator).isNotNull();
assertThat(keyGenerator).isExactlyInstanceOf(AWSSecretManagerKeyGenerator.class);
}
}
use of com.quorum.tessera.key.vault.KeyVaultService in project tessera by ConsenSys.
the class AzureKeyVaultServiceFactoryTest method create.
@Test
public void create() {
final KeyConfiguration keyConfiguration = mock(KeyConfiguration.class);
final DefaultKeyVaultConfig keyVaultConfig = mock(DefaultKeyVaultConfig.class);
when(config.getKeys()).thenReturn(keyConfiguration);
when(keyConfiguration.getKeyVaultConfig(KeyVaultType.AZURE)).thenReturn(Optional.of(keyVaultConfig));
when(keyVaultConfig.getProperty("url")).thenReturn(Optional.of("http://vaulturl"));
KeyVaultService result = keyVaultServiceFactory.create(config, envProvider);
assertThat(result).isInstanceOf(AzureKeyVaultService.class);
}
use of com.quorum.tessera.key.vault.KeyVaultService in project tessera by ConsenSys.
the class DefaultKeyGeneratorFactory method create.
@Override
public KeyGenerator create(KeyVaultConfig keyVaultConfig, EncryptorConfig encryptorConfig) {
Objects.requireNonNull(encryptorConfig, "No encryptor config defined. ");
final EncryptorFactory encryptorFactory = EncryptorFactory.newFactory(encryptorConfig.getType().name());
final Encryptor encryptor = encryptorFactory.create(encryptorConfig.getProperties());
if (keyVaultConfig != null) {
final KeyVaultServiceFactory keyVaultServiceFactory = KeyVaultServiceFactory.getInstance(keyVaultConfig.getKeyVaultType());
final Config config = new Config();
final KeyConfiguration keyConfiguration = new KeyConfiguration();
if (keyVaultConfig.getKeyVaultType().equals(KeyVaultType.AZURE)) {
keyConfiguration.addKeyVaultConfig(keyVaultConfig);
config.setKeys(keyConfiguration);
final KeyVaultService keyVaultService = keyVaultServiceFactory.create(config, new EnvironmentVariableProvider());
return new AzureVaultKeyGenerator(encryptor, keyVaultService);
} else if (keyVaultConfig.getKeyVaultType().equals(KeyVaultType.AWS)) {
if (!(keyVaultConfig instanceof DefaultKeyVaultConfig)) {
throw new IllegalArgumentException("AWS key vault config not instance of DefaultKeyVaultConfig");
}
keyConfiguration.addKeyVaultConfig(keyVaultConfig);
config.setKeys(keyConfiguration);
final KeyVaultService keyVaultService = keyVaultServiceFactory.create(config, new EnvironmentVariableProvider());
return new AWSSecretManagerKeyGenerator(encryptor, keyVaultService);
} else {
keyConfiguration.addKeyVaultConfig(keyVaultConfig);
config.setKeys(keyConfiguration);
final KeyVaultService keyVaultService = keyVaultServiceFactory.create(config, new EnvironmentVariableProvider());
return new HashicorpVaultKeyGenerator(encryptor, keyVaultService);
}
}
KeyEncryptor keyEncyptor = KeyEncryptorFactory.newFactory().create(encryptorConfig);
return new FileKeyGenerator(encryptor, keyEncyptor, PasswordReaderFactory.create());
}
use of com.quorum.tessera.key.vault.KeyVaultService in project tessera by ConsenSys.
the class AWSSecretManagerKeyGeneratorTest method setUp.
@Before
public void setUp() {
final Encryptor encryptor = mock(Encryptor.class);
this.keyVaultService = mock(KeyVaultService.class);
final KeyPair keyPair = new KeyPair(pub, priv);
when(encryptor.generateNewKeys()).thenReturn(keyPair);
awsSecretManagerKeyGenerator = new AWSSecretManagerKeyGenerator(encryptor, keyVaultService);
}
use of com.quorum.tessera.key.vault.KeyVaultService in project tessera by ConsenSys.
the class HashicorpKeyVaultServiceFactory method create.
// This method should not be called directly. It has been left package-private to enable injection
// of util during
// testing
KeyVaultService create(Config config, EnvironmentVariableProvider envProvider, HashicorpKeyVaultServiceFactoryUtil util) {
Objects.requireNonNull(config);
Objects.requireNonNull(envProvider);
Objects.requireNonNull(util);
final String roleId = envProvider.getEnv(HASHICORP_ROLE_ID);
final String secretId = envProvider.getEnv(HASHICORP_SECRET_ID);
final String authToken = envProvider.getEnv(HASHICORP_TOKEN);
if (roleId == null && secretId == null && authToken == null) {
throw new HashicorpCredentialNotSetException("Environment variables must be set to authenticate with Hashicorp Vault. Set the " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables if using the AppRole authentication method. Set the " + HASHICORP_TOKEN + " environment variable if using another authentication method.");
} else if (isOnlyOneInputNull(roleId, secretId)) {
throw new HashicorpCredentialNotSetException("Only one of the " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables to authenticate with Hashicorp Vault using the AppRole method has been set");
}
KeyVaultConfig keyVaultConfig = Optional.ofNullable(config.getKeys()).flatMap(k -> k.getKeyVaultConfig(KeyVaultType.HASHICORP)).orElseThrow(() -> new ConfigException(new RuntimeException("Trying to create Hashicorp Vault connection but no Vault configuration provided")));
VaultEndpoint vaultEndpoint;
try {
URI uri = new URI(keyVaultConfig.getProperty("url").get());
vaultEndpoint = VaultEndpoint.from(uri);
} catch (URISyntaxException | NoSuchElementException | IllegalArgumentException e) {
throw new ConfigException(new RuntimeException("Provided Hashicorp Vault url is incorrectly formatted", e));
}
SslConfiguration sslConfiguration = util.configureSsl(keyVaultConfig, envProvider);
ClientOptions clientOptions = new ClientOptions();
ClientHttpRequestFactory clientHttpRequestFactory = util.createClientHttpRequestFactory(clientOptions, sslConfiguration);
ClientAuthentication clientAuthentication = util.configureClientAuthentication(keyVaultConfig, envProvider, clientHttpRequestFactory, vaultEndpoint);
SessionManager sessionManager = new SimpleSessionManager(clientAuthentication);
VaultOperations vaultOperations = new VaultTemplate(vaultEndpoint, clientHttpRequestFactory, sessionManager);
return new HashicorpKeyVaultService(vaultOperations, () -> new VaultVersionedKeyValueTemplateFactory() {
});
}
Aggregations