@Test public void envVarsAndKeyVaultConfigProvidedCreatesAzureKeyVaultService() { when(envProvider.getEnv(anyString())).thenReturn("envVar"); KeyConfiguration keyConfiguration = mock(KeyConfiguration.class); DefaultKeyVaultConfig keyVaultConfig = mock(DefaultKeyVaultConfig.class); when(keyVaultConfig.getProperty("url")).thenReturn(Optional.of("URL")); when(keyConfiguration.getKeyVaultConfig(KeyVaultType.AZURE)).thenReturn(Optional.of(keyVaultConfig)); when(config.getKeys()).thenReturn(keyConfiguration); KeyVaultService result = azureKeyVaultServiceFactory.create(config, envProvider); assertThat(result).isInstanceOf(AzureKeyVaultService.class); }
@Override public KeyVaultService create(Config config, EnvironmentVariableProvider envProvider) { Objects.requireNonNull(config); Objects.requireNonNull(envProvider); String clientId = envProvider.getEnv(AZURE_CLIENT_ID); String clientSecret = envProvider.getEnv(AZURE_CLIENT_SECRET); if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); } KeyVaultConfig keyVaultConfig = Optional.ofNullable(config.getKeys()) .flatMap(k -> k.getKeyVaultConfig(KeyVaultType.AZURE)) .orElseThrow(() -> new ConfigException(new RuntimeException("Trying to create Azure key vault connection but no Azure configuration provided"))); return new AzureKeyVaultService( keyVaultConfig, new AzureKeyVaultClientDelegate( new AzureKeyVaultClientFactory( new AzureKeyVaultClientCredentials( clientId, clientSecret, Executors.newFixedThreadPool(1) ) ).getAuthenticatedClient() ) ); }
AzureKeyVaultServiceFactory implements KeyVaultServiceFactory { @Override public KeyVaultService create(Config config, EnvironmentVariableProvider envProvider) { Objects.requireNonNull(config); Objects.requireNonNull(envProvider); String clientId = envProvider.getEnv(AZURE_CLIENT_ID); String clientSecret = envProvider.getEnv(AZURE_CLIENT_SECRET); if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); } KeyVaultConfig keyVaultConfig = Optional.ofNullable(config.getKeys()) .flatMap(k -> k.getKeyVaultConfig(KeyVaultType.AZURE)) .orElseThrow(() -> new ConfigException(new RuntimeException("Trying to create Azure key vault connection but no Azure configuration provided"))); return new AzureKeyVaultService( keyVaultConfig, new AzureKeyVaultClientDelegate( new AzureKeyVaultClientFactory( new AzureKeyVaultClientCredentials( clientId, clientSecret, Executors.newFixedThreadPool(1) ) ).getAuthenticatedClient() ) ); } }
AzureKeyVaultServiceFactory implements KeyVaultServiceFactory { @Override public KeyVaultService create(Config config, EnvironmentVariableProvider envProvider) { Objects.requireNonNull(config); Objects.requireNonNull(envProvider); String clientId = envProvider.getEnv(AZURE_CLIENT_ID); String clientSecret = envProvider.getEnv(AZURE_CLIENT_SECRET); if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); } KeyVaultConfig keyVaultConfig = Optional.ofNullable(config.getKeys()) .flatMap(k -> k.getKeyVaultConfig(KeyVaultType.AZURE)) .orElseThrow(() -> new ConfigException(new RuntimeException("Trying to create Azure key vault connection but no Azure configuration provided"))); return new AzureKeyVaultService( keyVaultConfig, new AzureKeyVaultClientDelegate( new AzureKeyVaultClientFactory( new AzureKeyVaultClientCredentials( clientId, clientSecret, Executors.newFixedThreadPool(1) ) ).getAuthenticatedClient() ) ); }  }
AzureKeyVaultServiceFactory implements KeyVaultServiceFactory { @Override public KeyVaultService create(Config config, EnvironmentVariableProvider envProvider) { Objects.requireNonNull(config); Objects.requireNonNull(envProvider); String clientId = envProvider.getEnv(AZURE_CLIENT_ID); String clientSecret = envProvider.getEnv(AZURE_CLIENT_SECRET); if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); } KeyVaultConfig keyVaultConfig = Optional.ofNullable(config.getKeys()) .flatMap(k -> k.getKeyVaultConfig(KeyVaultType.AZURE)) .orElseThrow(() -> new ConfigException(new RuntimeException("Trying to create Azure key vault connection but no Azure configuration provided"))); return new AzureKeyVaultService( keyVaultConfig, new AzureKeyVaultClientDelegate( new AzureKeyVaultClientFactory( new AzureKeyVaultClientCredentials( clientId, clientSecret, Executors.newFixedThreadPool(1) ) ).getAuthenticatedClient() ) ); }  @Override KeyVaultService create(Config config, EnvironmentVariableProvider envProvider); @Override KeyVaultType getType(); }
AzureKeyVaultServiceFactory implements KeyVaultServiceFactory { @Override public KeyVaultService create(Config config, EnvironmentVariableProvider envProvider) { Objects.requireNonNull(config); Objects.requireNonNull(envProvider); String clientId = envProvider.getEnv(AZURE_CLIENT_ID); String clientSecret = envProvider.getEnv(AZURE_CLIENT_SECRET); if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); } KeyVaultConfig keyVaultConfig = Optional.ofNullable(config.getKeys()) .flatMap(k -> k.getKeyVaultConfig(KeyVaultType.AZURE)) .orElseThrow(() -> new ConfigException(new RuntimeException("Trying to create Azure key vault connection but no Azure configuration provided"))); return new AzureKeyVaultService( keyVaultConfig, new AzureKeyVaultClientDelegate( new AzureKeyVaultClientFactory( new AzureKeyVaultClientCredentials( clientId, clientSecret, Executors.newFixedThreadPool(1) ) ).getAuthenticatedClient() ) ); }  @Override KeyVaultService create(Config config, EnvironmentVariableProvider envProvider); @Override KeyVaultType getType();  }