HadoopCredentialConfiguration { public static CredentialFactory getCredentialFactory( Configuration config, String... keyPrefixesVararg) { List<String> keyPrefixes = getConfigKeyPrefixes(keyPrefixesVararg); CredentialOptions credentialOptions = CredentialOptions.builder() .setServiceAccountEnabled( ENABLE_SERVICE_ACCOUNTS_SUFFIX .withPrefixes(keyPrefixes) .get(config, config::getBoolean)) .setServiceAccountPrivateKeyId( RedactedString.create( SERVICE_ACCOUNT_PRIVATE_KEY_ID_SUFFIX .withPrefixes(keyPrefixes) .getPassword(config))) .setServiceAccountPrivateKey( RedactedString.create( SERVICE_ACCOUNT_PRIVATE_KEY_SUFFIX .withPrefixes(keyPrefixes) .getPassword(config))) .setServiceAccountEmail( SERVICE_ACCOUNT_EMAIL_SUFFIX.withPrefixes(keyPrefixes).getPassword(config)) .setServiceAccountKeyFile( SERVICE_ACCOUNT_KEYFILE_SUFFIX.withPrefixes(keyPrefixes).get(config, config::get)) .setServiceAccountJsonKeyFile( SERVICE_ACCOUNT_JSON_KEYFILE_SUFFIX .withPrefixes(keyPrefixes) .get(config, config::get)) .setClientId( RedactedString.create( CLIENT_ID_SUFFIX.withPrefixes(keyPrefixes).get(config, config::get))) .setClientSecret( RedactedString.create( CLIENT_SECRET_SUFFIX.withPrefixes(keyPrefixes).get(config, config::get))) .setOAuthCredentialFile( OAUTH_CLIENT_FILE_SUFFIX.withPrefixes(keyPrefixes).get(config, config::get)) .setNullCredentialEnabled( ENABLE_NULL_CREDENTIAL_SUFFIX .withPrefixes(keyPrefixes) .get(config, config::getBoolean)) .setTransportType( HTTP_TRANSPORT_SUFFIX.withPrefixes(keyPrefixes).get(config, config::getEnum)) .setTokenServerUrl( TOKEN_SERVER_URL_SUFFIX.withPrefixes(keyPrefixes).get(config, config::get)) .setProxyAddress( PROXY_ADDRESS_SUFFIX.withPrefixes(keyPrefixes).get(config, config::get)) .setProxyUsername( RedactedString.create( PROXY_USERNAME_SUFFIX.withPrefixes(keyPrefixes).getPassword(config))) .setProxyPassword( RedactedString.create( PROXY_PASSWORD_SUFFIX.withPrefixes(keyPrefixes).getPassword(config))) .build(); return new CredentialFactory(credentialOptions); } protected  HadoopCredentialConfiguration(); static CredentialFactory getCredentialFactory(
      Configuration config, String... keyPrefixesVararg); static Class<? extends AccessTokenProvider> getAccessTokenProviderImplClass(
      Configuration config, String... keyPrefixes); static ImmutableList<String> getConfigKeyPrefixes(String... keyPrefixes); static final String BASE_KEY_PREFIX; static final HadoopConfigurationProperty<Boolean> ENABLE_SERVICE_ACCOUNTS_SUFFIX; static final HadoopConfigurationProperty<String> SERVICE_ACCOUNT_EMAIL_SUFFIX; static final HadoopConfigurationProperty<String> SERVICE_ACCOUNT_PRIVATE_KEY_ID_SUFFIX; static final HadoopConfigurationProperty<String> SERVICE_ACCOUNT_PRIVATE_KEY_SUFFIX; static final HadoopConfigurationProperty<String> SERVICE_ACCOUNT_KEYFILE_SUFFIX; static final HadoopConfigurationProperty<String> SERVICE_ACCOUNT_JSON_KEYFILE_SUFFIX; static final HadoopConfigurationProperty<String> CLIENT_ID_SUFFIX; static final HadoopConfigurationProperty<String> CLIENT_SECRET_SUFFIX; static final HadoopConfigurationProperty<String> OAUTH_CLIENT_FILE_SUFFIX; static final HadoopConfigurationProperty<Boolean> ENABLE_NULL_CREDENTIAL_SUFFIX; static final HadoopConfigurationProperty<String> TOKEN_SERVER_URL_SUFFIX; static final HadoopConfigurationProperty<String> PROXY_ADDRESS_SUFFIX; static final HadoopConfigurationProperty<String> PROXY_USERNAME_SUFFIX; static final HadoopConfigurationProperty<String> PROXY_PASSWORD_SUFFIX; static final HadoopConfigurationProperty<HttpTransportType> HTTP_TRANSPORT_SUFFIX; static final HadoopConfigurationProperty<Class<? extends AccessTokenProvider>> ACCESS_TOKEN_PROVIDER_IMPL_SUFFIX; static final HadoopConfigurationProperty<String> IMPERSONATION_SERVICE_ACCOUNT_SUFFIX; static final HadoopConfigurationProperty<Map<String, String>> USER_IMPERSONATION_SERVICE_ACCOUNT_SUFFIX; static final HadoopConfigurationProperty<Map<String, String>> GROUP_IMPERSONATION_SERVICE_ACCOUNT_SUFFIX; }
@Test public void nullCredentialsAreCreatedForTesting() throws Exception { configuration.setBoolean(getConfigKey(ENABLE_SERVICE_ACCOUNTS_SUFFIX), false); configuration.setBoolean(getConfigKey(ENABLE_NULL_CREDENTIAL_SUFFIX), true); CredentialFactory credentialFactory = getCredentialFactory(); assertThat(credentialFactory.getCredential(TEST_SCOPES)).isNull(); }
@Test public void metadataServiceIsUsedByDefault() throws Exception { TokenResponse token = new TokenResponse().setAccessToken("metadata-test-token"); MockHttpTransport transport = mockTransport(jsonDataResponse(token)); CredentialFactory.setStaticHttpTransport(transport); CredentialFactory credentialFactory = getCredentialFactory(); Credential credential = credentialFactory.getCredential(TEST_SCOPES); assertThat(credential.getAccessToken()).isEqualTo("metadata-test-token"); }
@Test public void applicationDefaultServiceAccountWhenConfigured() throws Exception { CredentialFactory credentialFactory = getCredentialFactory(); GoogleCredentialWithRetry credential = (GoogleCredentialWithRetry) withEnvironmentVariable(CREDENTIAL_ENV_VAR, getStringPath("test-credential.json")) .execute(() -> credentialFactory.getCredential(TEST_SCOPES)); assertThat(credential.getServiceAccountId()).isEqualTo("test-email@gserviceaccount.com"); assertThat(credential.getServiceAccountPrivateKeyId()).isEqualTo("test-key-id"); }
@Test public void p12KeyFileUsedWhenConfigured() throws Exception { configuration.set(getConfigKey(SERVICE_ACCOUNT_EMAIL_SUFFIX), "foo@example.com"); configuration.set(getConfigKey(SERVICE_ACCOUNT_KEYFILE_SUFFIX), getStringPath("test-key.p12")); CredentialFactory credentialFactory = getCredentialFactory(); GoogleCredentialWithRetry credential = (GoogleCredentialWithRetry) credentialFactory.getCredential(TEST_SCOPES); assertThat(credential.getServiceAccountId()).isEqualTo("foo@example.com"); }
@Test public void jsonKeyFileUsedWhenConfigured() throws Exception { configuration.set( getConfigKey(SERVICE_ACCOUNT_JSON_KEYFILE_SUFFIX), getStringPath("test-credential.json")); CredentialFactory credentialFactory = getCredentialFactory(); GoogleCredentialWithRetry credential = (GoogleCredentialWithRetry) credentialFactory.getCredential(TEST_SCOPES); assertThat(credential.getServiceAccountId()).isEqualTo("test-email@gserviceaccount.com"); assertThat(credential.getServiceAccountPrivateKeyId()).isEqualTo("test-key-id"); }
@Test public void configurationSAUsedWhenConfigured() throws Exception { configuration.set(getConfigKey(SERVICE_ACCOUNT_EMAIL_SUFFIX), "foo@example.com"); configuration.set(getConfigKey(SERVICE_ACCOUNT_PRIVATE_KEY_ID_SUFFIX), "privatekey"); configuration.set( getConfigKey(SERVICE_ACCOUNT_PRIVATE_KEY_SUFFIX), Resources.toString(getPath("test-key.txt").toUri().toURL(), UTF_8)); CredentialFactory credentialFactory = getCredentialFactory(); GoogleCredentialWithRetry credential = (GoogleCredentialWithRetry) credentialFactory.getCredential(TEST_SCOPES); assertThat(credential.getServiceAccountId()).isEqualTo("foo@example.com"); assertThat(credential.getServiceAccountPrivateKeyId()).isEqualTo("privatekey"); }
@Test public void installedAppWorkflowUsedWhenConfigured() throws Exception { configuration.setBoolean(getConfigKey(ENABLE_SERVICE_ACCOUNTS_SUFFIX), false); configuration.set(getConfigKey(CLIENT_ID_SUFFIX), "aClientId"); configuration.set(getConfigKey(CLIENT_SECRET_SUFFIX), "aClientSecret"); configuration.set( getConfigKey(OAUTH_CLIENT_FILE_SUFFIX), getStringPath("test-client-credential.json")); CredentialFactory credentialFactory = getCredentialFactory(); Credential credential = credentialFactory.getCredential(TEST_SCOPES); assertThat(credential.getAccessToken()).isEqualTo("test-client-access-token"); assertThat(credential.getRefreshToken()).isEqualTo("test-client-refresh-token"); }
@Test public void customTokenServerUrl() throws Exception { configuration.set( getConfigKey(SERVICE_ACCOUNT_JSON_KEYFILE_SUFFIX), getStringPath("test-credential.json")); configuration.set(getConfigKey(TOKEN_SERVER_URL_SUFFIX), "https: CredentialFactory credentialFactory = getCredentialFactory(); GoogleCredentialWithRetry credential = (GoogleCredentialWithRetry) credentialFactory.getCredential(TEST_SCOPES); assertThat(credential.getTokenServerEncodedUrl()).isEqualTo("https: }