@Test public void configureClientAuthenticationIfAllEnvVarsSetThenAppRoleMethod() { KeyVaultConfig keyVaultConfig = mock(KeyVaultConfig.class); EnvironmentVariableProvider envProvider = mock(EnvironmentVariableProvider.class); ClientHttpRequestFactory clientHttpRequestFactory = mock(ClientHttpRequestFactory.class); VaultEndpoint vaultEndpoint = mock(VaultEndpoint.class); when(envProvider.getEnv(HASHICORP_ROLE_ID)).thenReturn("role-id"); when(envProvider.getEnv(HASHICORP_SECRET_ID)).thenReturn("secret-id"); when(envProvider.getEnv(HASHICORP_TOKEN)).thenReturn("token"); when(keyVaultConfig.getProperty("approlePath")).thenReturn(Optional.of("approle")); ClientAuthentication result = util.configureClientAuthentication(keyVaultConfig, envProvider, clientHttpRequestFactory, vaultEndpoint); assertThat(result).isInstanceOf(AppRoleAuthentication.class); }
ClientAuthentication configureClientAuthentication(KeyVaultConfig keyVaultConfig, EnvironmentVariableProvider envProvider, ClientHttpRequestFactory clientHttpRequestFactory, VaultEndpoint vaultEndpoint) { 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) { AppRoleAuthenticationOptions appRoleAuthenticationOptions = AppRoleAuthenticationOptions.builder() .path(keyVaultConfig.getProperty("approlePath").get()) .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId)) .secretId(AppRoleAuthenticationOptions.SecretId.provided(secretId)) .build(); RestOperations restOperations = VaultClients.createRestTemplate(vaultEndpoint, clientHttpRequestFactory); return new AppRoleAuthentication(appRoleAuthenticationOptions, restOperations); } else if (Objects.isNull(roleId) != Objects.isNull(secretId)) { throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method"); } else if (authToken == null){ throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method. Alternatively set " + HASHICORP_TOKEN + " to authenticate using the Token method"); } return new TokenAuthentication(authToken); }
HashicorpKeyVaultServiceFactoryUtil { ClientAuthentication configureClientAuthentication(KeyVaultConfig keyVaultConfig, EnvironmentVariableProvider envProvider, ClientHttpRequestFactory clientHttpRequestFactory, VaultEndpoint vaultEndpoint) { 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) { AppRoleAuthenticationOptions appRoleAuthenticationOptions = AppRoleAuthenticationOptions.builder() .path(keyVaultConfig.getProperty("approlePath").get()) .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId)) .secretId(AppRoleAuthenticationOptions.SecretId.provided(secretId)) .build(); RestOperations restOperations = VaultClients.createRestTemplate(vaultEndpoint, clientHttpRequestFactory); return new AppRoleAuthentication(appRoleAuthenticationOptions, restOperations); } else if (Objects.isNull(roleId) != Objects.isNull(secretId)) { throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method"); } else if (authToken == null){ throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method. Alternatively set " + HASHICORP_TOKEN + " to authenticate using the Token method"); } return new TokenAuthentication(authToken); } }
HashicorpKeyVaultServiceFactoryUtil { ClientAuthentication configureClientAuthentication(KeyVaultConfig keyVaultConfig, EnvironmentVariableProvider envProvider, ClientHttpRequestFactory clientHttpRequestFactory, VaultEndpoint vaultEndpoint) { 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) { AppRoleAuthenticationOptions appRoleAuthenticationOptions = AppRoleAuthenticationOptions.builder() .path(keyVaultConfig.getProperty("approlePath").get()) .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId)) .secretId(AppRoleAuthenticationOptions.SecretId.provided(secretId)) .build(); RestOperations restOperations = VaultClients.createRestTemplate(vaultEndpoint, clientHttpRequestFactory); return new AppRoleAuthentication(appRoleAuthenticationOptions, restOperations); } else if (Objects.isNull(roleId) != Objects.isNull(secretId)) { throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method"); } else if (authToken == null){ throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method. Alternatively set " + HASHICORP_TOKEN + " to authenticate using the Token method"); } return new TokenAuthentication(authToken); }  }
HashicorpKeyVaultServiceFactoryUtil { ClientAuthentication configureClientAuthentication(KeyVaultConfig keyVaultConfig, EnvironmentVariableProvider envProvider, ClientHttpRequestFactory clientHttpRequestFactory, VaultEndpoint vaultEndpoint) { 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) { AppRoleAuthenticationOptions appRoleAuthenticationOptions = AppRoleAuthenticationOptions.builder() .path(keyVaultConfig.getProperty("approlePath").get()) .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId)) .secretId(AppRoleAuthenticationOptions.SecretId.provided(secretId)) .build(); RestOperations restOperations = VaultClients.createRestTemplate(vaultEndpoint, clientHttpRequestFactory); return new AppRoleAuthentication(appRoleAuthenticationOptions, restOperations); } else if (Objects.isNull(roleId) != Objects.isNull(secretId)) { throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method"); } else if (authToken == null){ throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method. Alternatively set " + HASHICORP_TOKEN + " to authenticate using the Token method"); } return new TokenAuthentication(authToken); }   }
HashicorpKeyVaultServiceFactoryUtil { ClientAuthentication configureClientAuthentication(KeyVaultConfig keyVaultConfig, EnvironmentVariableProvider envProvider, ClientHttpRequestFactory clientHttpRequestFactory, VaultEndpoint vaultEndpoint) { 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) { AppRoleAuthenticationOptions appRoleAuthenticationOptions = AppRoleAuthenticationOptions.builder() .path(keyVaultConfig.getProperty("approlePath").get()) .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId)) .secretId(AppRoleAuthenticationOptions.SecretId.provided(secretId)) .build(); RestOperations restOperations = VaultClients.createRestTemplate(vaultEndpoint, clientHttpRequestFactory); return new AppRoleAuthentication(appRoleAuthenticationOptions, restOperations); } else if (Objects.isNull(roleId) != Objects.isNull(secretId)) { throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method"); } else if (authToken == null){ throw new HashicorpCredentialNotSetException("Both " + HASHICORP_ROLE_ID + " and " + HASHICORP_SECRET_ID + " environment variables must be set to use the AppRole authentication method. Alternatively set " + HASHICORP_TOKEN + " to authenticate using the Token method"); } return new TokenAuthentication(authToken); }    }