Search in sources :

Example 6 with KeyVaultServiceFactory

use of com.quorum.tessera.key.vault.KeyVaultServiceFactory in project tessera by ConsenSys.

the class DefaultKeyGeneratorFactoryTest method createKeyGeneratorsFromTypes.

@Test
public void createKeyGeneratorsFromTypes() throws Exception {
    for (KeyVaultType keyVaultType : KeyVaultType.values()) {
        DefaultKeyVaultConfig keyVaultConfig = mock(DefaultKeyVaultConfig.class);
        when(keyVaultConfig.getKeyVaultType()).thenReturn(keyVaultType);
        EncryptorConfig encryptorConfig = mock(EncryptorConfig.class);
        when(encryptorConfig.getType()).thenReturn(EncryptorType.NACL);
        DefaultKeyGeneratorFactory defaultKeyGeneratorFactory = new DefaultKeyGeneratorFactory();
        try (MockedStatic<KeyVaultServiceFactory> mockedKeyVaultServiceFactory = mockStatic(KeyVaultServiceFactory.class)) {
            KeyVaultServiceFactory keyVaultServiceFactory = mock(KeyVaultServiceFactory.class);
            when(keyVaultServiceFactory.create(any(), any())).thenReturn(mock(KeyVaultService.class));
            mockedKeyVaultServiceFactory.when(() -> KeyVaultServiceFactory.getInstance(keyVaultType)).thenReturn(keyVaultServiceFactory);
            final KeyGenerator keyGenerator = defaultKeyGeneratorFactory.create(keyVaultConfig, encryptorConfig);
            assertThat(keyGenerator).isNotNull();
            assertThat(keyGenerator).isExactlyInstanceOf(resultsLookup.get(keyVaultType));
        }
    }
}
Also used : KeyVaultService(com.quorum.tessera.key.vault.KeyVaultService) KeyVaultServiceFactory(com.quorum.tessera.key.vault.KeyVaultServiceFactory) Test(org.junit.Test)

Example 7 with KeyVaultServiceFactory

use of com.quorum.tessera.key.vault.KeyVaultServiceFactory in project tessera by ConsenSys.

the class DefaultKeyGeneratorFactoryTest method awsRequiresThatKeyConfigIsOfTypeDefaultKeyVaultConfig.

@Test
public void awsRequiresThatKeyConfigIsOfTypeDefaultKeyVaultConfig() {
    KeyVaultConfig keyVaultConfig = mock(KeyVaultConfig.class);
    when(keyVaultConfig.getKeyVaultType()).thenReturn(KeyVaultType.AWS);
    EncryptorConfig encryptorConfig = mock(EncryptorConfig.class);
    when(encryptorConfig.getType()).thenReturn(EncryptorType.NACL);
    DefaultKeyGeneratorFactory defaultKeyGeneratorFactory = new DefaultKeyGeneratorFactory();
    try (MockedStatic<KeyVaultServiceFactory> mockedKeyVaultServiceFactory = mockStatic(KeyVaultServiceFactory.class)) {
        KeyVaultServiceFactory keyVaultServiceFactory = mock(KeyVaultServiceFactory.class);
        mockedKeyVaultServiceFactory.when(() -> KeyVaultServiceFactory.getInstance(KeyVaultType.AWS)).thenReturn(keyVaultServiceFactory);
        try {
            defaultKeyGeneratorFactory.create(keyVaultConfig, encryptorConfig);
            failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
        } catch (IllegalArgumentException ex) {
            assertThat(ex).hasMessage("AWS key vault config not instance of DefaultKeyVaultConfig");
        }
    }
}
Also used : KeyVaultServiceFactory(com.quorum.tessera.key.vault.KeyVaultServiceFactory) Test(org.junit.Test)

Example 8 with KeyVaultServiceFactory

use of com.quorum.tessera.key.vault.KeyVaultServiceFactory in project tessera by ConsenSys.

the class KeyPairConverterTest method convertSingleAzureVaultKeyPair.

@Test
public void convertSingleAzureVaultKeyPair() {
    try (var staticKeyVaultServiceFactory = mockStatic(KeyVaultServiceFactory.class)) {
        KeyVaultServiceFactory keyVaultServiceFactory = mock(KeyVaultServiceFactory.class);
        KeyVaultService keyVaultService = mock(KeyVaultService.class);
        when(keyVaultService.getSecret(any(Map.class))).thenReturn("publicSecret").thenReturn("privSecret");
        when(keyVaultServiceFactory.create(any(Config.class), any(EnvironmentVariableProvider.class))).thenReturn(keyVaultService);
        staticKeyVaultServiceFactory.when(() -> KeyVaultServiceFactory.getInstance(KeyVaultType.AZURE)).thenReturn(keyVaultServiceFactory);
        final AzureVaultKeyPair keyPair = new AzureVaultKeyPair("pub", "priv", null, null);
        Collection<KeyPair> result = converter.convert(List.of(keyPair));
        assertThat(result).hasSize(1);
        KeyPair resultKeyPair = result.iterator().next();
        KeyPair expected = new KeyPair(PublicKey.from(decodeBase64("publicSecret")), PrivateKey.from(decodeBase64("privSecret")));
        assertThat(resultKeyPair).isEqualToComparingFieldByField(expected);
        verify(keyVaultService, times(2)).getSecret(any(Map.class));
        verify(keyVaultServiceFactory).create(any(Config.class), any(EnvironmentVariableProvider.class));
        staticKeyVaultServiceFactory.verify(() -> KeyVaultServiceFactory.getInstance(KeyVaultType.AZURE));
        staticKeyVaultServiceFactory.verifyNoMoreInteractions();
        verifyNoMoreInteractions(keyVaultService);
        verifyNoMoreInteractions(keyVaultServiceFactory);
    }
}
Also used : EnvironmentVariableProvider(com.quorum.tessera.config.util.EnvironmentVariableProvider) KeyVaultService(com.quorum.tessera.key.vault.KeyVaultService) KeyPair(com.quorum.tessera.encryption.KeyPair) Config(com.quorum.tessera.config.Config) KeyVaultServiceFactory(com.quorum.tessera.key.vault.KeyVaultServiceFactory) Test(org.junit.Test)

Aggregations

KeyVaultServiceFactory (com.quorum.tessera.key.vault.KeyVaultServiceFactory)8 KeyVaultService (com.quorum.tessera.key.vault.KeyVaultService)7 Test (org.junit.Test)6 EnvironmentVariableProvider (com.quorum.tessera.config.util.EnvironmentVariableProvider)4 KeyPair (com.quorum.tessera.encryption.KeyPair)4 Config (com.quorum.tessera.config.Config)3 DefaultKeyVaultConfig (com.quorum.tessera.config.DefaultKeyVaultConfig)1 EncryptorConfig (com.quorum.tessera.config.EncryptorConfig)1 AWSKeyPair (com.quorum.tessera.config.keypairs.AWSKeyPair)1 AzureVaultKeyPair (com.quorum.tessera.config.keypairs.AzureVaultKeyPair)1 ConfigKeyPair (com.quorum.tessera.config.keypairs.ConfigKeyPair)1 HashicorpVaultKeyPair (com.quorum.tessera.config.keypairs.HashicorpVaultKeyPair)1 KeyEncryptor (com.quorum.tessera.config.keys.KeyEncryptor)1 KeyEncryptorFactory (com.quorum.tessera.config.keys.KeyEncryptorFactory)1 Encryptor (com.quorum.tessera.encryption.Encryptor)1 EncryptorFactory (com.quorum.tessera.encryption.EncryptorFactory)1