forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch AWS Credentials from AWS SDK/Environment variables
Add support in AuthConfigFactory to retrieve credentials from different mechanisms for AWS. Port of fabric8io/docker-maven-plugin#1311 Port of fabric8io/docker-maven-plugin#1310 Related to eclipse-jkube#702 Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
32be156
commit 91cf841
Showing
8 changed files
with
599 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...ain/java/org/eclipse/jkube/kit/build/service/docker/auth/ecr/AwsSdkAuthConfigFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.eclipse.jkube.kit.build.service.docker.auth.ecr; | ||
|
||
import org.eclipse.jkube.kit.build.api.auth.AuthConfig; | ||
import org.eclipse.jkube.kit.common.KitLogger; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class AwsSdkAuthConfigFactory { | ||
|
||
private final KitLogger log; | ||
private AwsSdkHelper awsSdkHelper; | ||
|
||
public AwsSdkAuthConfigFactory(KitLogger log, AwsSdkHelper awsSdkHelper) { | ||
this.log = log; | ||
this.awsSdkHelper = awsSdkHelper; | ||
} | ||
|
||
public AuthConfig createAuthConfig() { | ||
try { | ||
Object credentials = awsSdkHelper.getCredentialsFromDefaultAWSCredentialsProviderChain(); | ||
if (credentials == null) { | ||
return null; | ||
} | ||
|
||
return new AuthConfig( | ||
awsSdkHelper.getAWSAccessKeyIdFromCredentials(credentials), | ||
awsSdkHelper.getAwsSecretKeyFromCredentials(credentials), | ||
"none", | ||
awsSdkHelper.getSessionTokenFromCrendentials(credentials) | ||
); | ||
} catch (Exception t) { | ||
String issueTitle = null; | ||
try { | ||
issueTitle = URLEncoder.encode("Failed calling AWS SDK: " + t.getMessage(), UTF_8.name()); | ||
} catch (UnsupportedEncodingException ignore) { | ||
} | ||
log.warn("Failed to fetch AWS credentials: %s", t.getMessage()); | ||
if (t.getCause() != null) { | ||
log.warn("Caused by: %s", t.getCause().getMessage()); | ||
} | ||
log.warn("Please report a bug at https://github.com/eclipse/jkube/issues/new?%s", | ||
issueTitle == null ? "" : "title=?" + issueTitle); | ||
log.warn("%s", t); | ||
return null; | ||
} | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...ocker/src/main/java/org/eclipse/jkube/kit/build/service/docker/auth/ecr/AwsSdkHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.eclipse.jkube.kit.build.service.docker.auth.ecr; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public class AwsSdkHelper { | ||
public static final String ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID"; | ||
public static final String SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY"; | ||
public static final String SESSION_TOKEN = "AWS_SESSION_TOKEN"; | ||
public static final String CONTAINER_CREDENTIALS_RELATIVE_URI = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"; | ||
public static final String METADATA_ENDPOINT = "ECS_METADATA_ENDPOINT"; | ||
|
||
public boolean isDefaultAWSCredentialsProviderChainPresentInClassPath() { | ||
try { | ||
Class.forName("com.amazonaws.auth.DefaultAWSCredentialsProviderChain"); | ||
return true; | ||
} catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
|
||
public String getAwsAccessKeyIdEnvVar() { | ||
return System.getenv(ACCESS_KEY_ID); | ||
} | ||
|
||
public String getAwsSecretAccessKeyEnvVar() { | ||
return System.getenv(SECRET_ACCESS_KEY); | ||
} | ||
|
||
public String getAwsSessionTokenEnvVar() { | ||
return System.getenv(SESSION_TOKEN); | ||
} | ||
|
||
public String getAwsContainerCredentialsRelativeUri() { | ||
return System.getenv(CONTAINER_CREDENTIALS_RELATIVE_URI); | ||
} | ||
|
||
public String getEcsMetadataEndpoint() { | ||
return System.getenv(METADATA_ENDPOINT); | ||
} | ||
|
||
public Object getCredentialsFromDefaultAWSCredentialsProviderChain() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | ||
Class<?> credentialsProviderChainClass = Class.forName("com.amazonaws.auth.DefaultAWSCredentialsProviderChain"); | ||
Object credentialsProviderChain = credentialsProviderChainClass.getDeclaredConstructor().newInstance(); | ||
return credentialsProviderChainClass.getMethod("getCredentials").invoke(credentialsProviderChain); | ||
} | ||
|
||
public String getSessionTokenFromCrendentials(Object credentials) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
Class<?> sessionCredentialsClass = Class.forName("com.amazonaws.auth.AWSSessionCredentials"); | ||
return sessionCredentialsClass.isInstance(credentials) | ||
? (String) sessionCredentialsClass.getMethod("getSessionToken").invoke(credentials) : null; | ||
} | ||
|
||
public String getAWSAccessKeyIdFromCredentials(Object credentials) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
Class<?> credentialsClass = Class.forName("com.amazonaws.auth.AWSCredentials"); | ||
return (String) credentialsClass.getMethod("getAWSAccessKeyId").invoke(credentials); | ||
} | ||
|
||
public String getAwsSecretKeyFromCredentials(Object credentials) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
Class<?> credentialsClass = Class.forName("com.amazonaws.auth.AWSCredentials"); | ||
return (String) credentialsClass.getMethod("getAWSSecretKey").invoke(credentials); | ||
} | ||
} |
Oops, something went wrong.