@Test public void bothClientIdAndClientSecretEnvironmentVariablesNotSetThrowsException() { Throwable ex = catchThrowable(() -> azureKeyVaultServiceFactory.create(config, envProvider)); when(envProvider.getEnv(AZURE_CLIENT_ID)).thenReturn(null); when(envProvider.getEnv(AZURE_CLIENT_SECRET)).thenReturn(null); assertThat(ex).isInstanceOf(AzureCredentialNotSetException.class); assertThat(ex.getMessage()).isEqualTo(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); }
@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();  }