package io.fabric8.maven.docker.util.aws;

import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.util.Logger;

public class AwsSdkAuthConfigFactory {

    private final Logger log;

    public AwsSdkAuthConfigFactory(Logger log) {
        this.log = log;
    }

    public AuthConfig createAuthConfig() {
        try {
            Class<?> credentialsProviderChainClass = Class.forName("com.amazonaws.auth.DefaultAWSCredentialsProviderChain");
            Object credentialsProviderChain = credentialsProviderChainClass.getDeclaredConstructor().newInstance();
            Object credentials = credentialsProviderChainClass.getMethod("getCredentials").invoke(credentialsProviderChain);
            if (credentials == null) {
                return null;
            }

            Class<?> sessionCredentialsClass = Class.forName("com.amazonaws.auth.AWSSessionCredentials");
            String sessionToken = sessionCredentialsClass.isInstance(credentials)
                    ? (String) sessionCredentialsClass.getMethod("getSessionToken").invoke(credentials) : null;

            Class<?> credentialsClass = Class.forName("com.amazonaws.auth.AWSCredentials");
            return new AuthConfig(
                    (String) credentialsClass.getMethod("getAWSAccessKeyId").invoke(credentials),
                    (String) credentialsClass.getMethod("getAWSSecretKey").invoke(credentials),
                    "none",
                    sessionToken
            );
        } catch (Throwable t) {
            log.warn("AWS SDK detected, but failed to fetch AWS credentials: %s", t);
            return null;
        }
    }

}
@@ -131,16 +131,146 @@
 ogger;%0A%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0A%0Aimport static java.nio.charset.StandardCharsets.UTF_8;%0A%0A
 public c
@@ -1664,81 +1664,522 @@
     
-log.warn(%22AWS SDK detected, but failed to fetch AWS credentials: %25s%22, t);
+String issueTitle = null;%0A            try %7B%0A                issueTitle = URLEncoder.encode(%22Failed calling AWS SDK: %22 + t.getMessage(), UTF_8.name());%0A            %7D catch (UnsupportedEncodingException ignore) %7B%0A            %7D%0A            log.warn(%22Failed to fetch AWS credentials: %25s%22, t.getMessage());%0A            log.warn(%22Please report a bug at https://github.com/fabric8io/docker-maven-plugin/issues/new%25s%22,%0A                    issueTitle == null ? %22%22 : %22title=?%22 + issueTitle);%0A            log.warn(%22%25s%22, t);  
 %0A   
@@ -2217,9 +2217,10 @@
 %0A    %7D%0A%0A
-
 %7D
+%0A

Update warning when AWS SDK access fails.