Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws_auth): Add support of awsSecurityCredentialsSupplierClass in ExternalAccountCredentials when create from GcpCredentials from file. #1540

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@
import com.google.auth.RequestMetadataCallback;
import com.google.auth.http.HttpTransportFactory;
import com.google.common.base.MoreObjects;
import com.google.common.reflect.Reflection;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.net.URI;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -402,6 +405,8 @@ static ExternalAccountCredentials fromJson(
String tokenUrl = (String) json.get("token_url");

Map<String, Object> credentialSourceMap = (Map<String, Object>) json.get("credential_source");
String awsSecurityCredentialsSupplierClass =
(String) json.get("security_credentials_supplier_class");

// Optional params.
String serviceAccountImpersonationUrl = (String) json.get("service_account_impersonation_url");
Expand All @@ -419,20 +424,26 @@ static ExternalAccountCredentials fromJson(
}

if (isAwsCredential(credentialSourceMap)) {
return AwsCredentials.newBuilder()
.setHttpTransportFactory(transportFactory)
.setAudience(audience)
.setSubjectTokenType(subjectTokenType)
.setTokenUrl(tokenUrl)
.setTokenInfoUrl(tokenInfoUrl)
.setCredentialSource(new AwsCredentialSource(credentialSourceMap))
.setServiceAccountImpersonationUrl(serviceAccountImpersonationUrl)
.setQuotaProjectId(quotaProjectId)
.setClientId(clientId)
.setClientSecret(clientSecret)
.setServiceAccountImpersonationOptions(impersonationOptionsMap)
.setUniverseDomain(universeDomain)
.build();
AwsCredentials.Builder builder =
AwsCredentials.newBuilder()
.setHttpTransportFactory(transportFactory)
.setAudience(audience)
.setSubjectTokenType(subjectTokenType)
.setTokenUrl(tokenUrl)
.setTokenInfoUrl(tokenInfoUrl)
.setServiceAccountImpersonationUrl(serviceAccountImpersonationUrl)
.setQuotaProjectId(quotaProjectId)
.setClientId(clientId)
.setClientSecret(clientSecret)
.setServiceAccountImpersonationOptions(impersonationOptionsMap)
.setUniverseDomain(universeDomain);
if (awsSecurityCredentialsSupplierClass != null) {
builder.setAwsSecurityCredentialsSupplier(
createAwsSecurityCredentialsSupplierClass(awsSecurityCredentialsSupplierClass));
} else {
builder.setCredentialSource(new AwsCredentialSource(credentialSourceMap));
}
return builder.build();
} else if (isPluggableAuthCredential(credentialSourceMap)) {
return PluggableAuthCredentials.newBuilder()
.setHttpTransportFactory(transportFactory)
Expand Down Expand Up @@ -467,6 +478,21 @@ static ExternalAccountCredentials fromJson(
.build();
}

private static AwsSecurityCredentialsSupplier createAwsSecurityCredentialsSupplierClass(
String awsSecurityCredentialsSupplierClass) {
try {
return (AwsSecurityCredentialsSupplier)
Class.forName(awsSecurityCredentialsSupplierClass).getConstructor().newInstance();
} catch (ClassNotFoundException
| NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
throw new IllegalArgumentException(
"AwsCredentials can not initialize an AwsSecurityCredentialsSupplier", e);
}
}

private static boolean isPluggableAuthCredential(Map<String, Object> credentialSource) {
// Pluggable Auth is enabled via a nested executable field in the credential source.
return credentialSource.containsKey(EXECUTABLE_SOURCE_KEY);
Expand Down Expand Up @@ -570,7 +596,9 @@ public String getServiceAccountImpersonationUrl() {
return serviceAccountImpersonationUrl;
}

/** @return The service account email to be impersonated, if available */
/**
* @return The service account email to be impersonated, if available
*/
@Nullable
public String getServiceAccountEmail() {
if (serviceAccountImpersonationUrl == null || serviceAccountImpersonationUrl.isEmpty()) {
Expand Down