diff --git a/doc/changelog.md b/doc/changelog.md index c579e79d3..b86588218 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -2,7 +2,8 @@ * **0.22-SNAPSHOT** - Support relative paths when binding volumes in `docker-compose.yml` (#846) - - Allo the session token for AWS authetication to be oncluded in order to allow temporary security credentials provided by the AWS Security Token Service (AWS STS) to sign requests (#883) + - Allow the session token for AWS authetication to be oncluded in order to allow temporary security credentials provided by the AWS Security Token Service (AWS STS) to sign requests (#883) + - Add support for credential helper to authenticate against a registry (#821) * **0.22.1** (2017-08-28) - Allow Docker compose version "2", too ([#829](https://github.com/fabric8io/docker-maven-plugin/issues/829)) diff --git a/src/main/java/io/fabric8/maven/docker/util/AuthConfigFactory.java b/src/main/java/io/fabric8/maven/docker/util/AuthConfigFactory.java index e36f0efc0..b7a4d4f83 100644 --- a/src/main/java/io/fabric8/maven/docker/util/AuthConfigFactory.java +++ b/src/main/java/io/fabric8/maven/docker/util/AuthConfigFactory.java @@ -317,36 +317,40 @@ private AuthConfig getAuthConfigFromDockerConfig(String registry) throws MojoExe if (dockerConfig.has("credHelpers")) { final JSONObject credHelpers = dockerConfig.getJSONObject("credHelpers"); if (credHelpers.has(registryToLookup)) { - CredentialHelperClient credentialHelper = new CredentialHelperClient(log,credHelpers.getString(registryToLookup)); - log.debug("AuthConfig: credentials from credential helper %s version %s",credentialHelper.getName(),credentialHelper.getVersion()); - - // "The default credential store will not be used for operations concerning credentials of the specified registries." - return credentialHelper.getCredentialNode(registryToLookup); + return extractAuthConfigFromCredentialsHelper(registryToLookup, credHelpers.getString(registryToLookup)); } } if (dockerConfig.has("credsStore")) { - CredentialHelperClient credentialStore = new CredentialHelperClient(log,dockerConfig.getString("credsStore")); - log.debug("AuthConfig: credentials from credentials store %s version %s",credentialStore.getName(),credentialStore.getVersion()); - - return credentialStore.getCredentialNode(registryToLookup); + return extractAuthConfigFromCredentialsHelper(registryToLookup, dockerConfig.getString("credsStore")); } return null; } if (dockerConfig.has("auths")) { - JSONObject auths = dockerConfig.getJSONObject("auths"); - JSONObject credentials = getCredentialsNode(auths,registryToLookup); - if (credentials == null || !credentials.has("auth")) { - return null; - } - String auth = credentials.getString("auth"); - String email = credentials.has("email") ? credentials.getString("email") : null; - return new AuthConfig(auth,email); + return extractAuthConfigFromAuths(registryToLookup, dockerConfig.getJSONObject("auths")); } return null; } + private AuthConfig extractAuthConfigFromAuths(String registryToLookup, JSONObject auths) { + JSONObject credentials = getCredentialsNode(auths,registryToLookup); + if (credentials == null || !credentials.has("auth")) { + return null; + } + String auth = credentials.getString("auth"); + String email = credentials.has("email") ? credentials.getString("email") : null; + return new AuthConfig(auth,email); + } + + private AuthConfig extractAuthConfigFromCredentialsHelper(String registryToLookup, String credConfig) throws MojoExecutionException { + CredentialHelperClient credentialHelper = new CredentialHelperClient(log, credConfig); + log.debug("AuthConfig: credentials from credential helper/store %s version %s", + credentialHelper.getName(), + credentialHelper.getVersion()); + return credentialHelper.getAuthConfig(registryToLookup); + } + private JSONObject getCredentialsNode(JSONObject auths,String registryToLookup) { if (auths.has(registryToLookup)) { return auths.getJSONObject(registryToLookup); diff --git a/src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.java b/src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.java index c53aeb1ec..e4a0d2ce4 100644 --- a/src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.java +++ b/src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.java @@ -15,7 +15,7 @@ public class CredentialHelperClient { private final String credentialHelperName; private final Logger log; - public CredentialHelperClient(Logger log,String credentialsStore) { + public CredentialHelperClient(Logger log, String credentialsStore) { this.log = log; credentialHelperName = "docker-credential-" + credentialsStore; } @@ -32,7 +32,7 @@ public String getVersion() throws MojoExecutionException { } } - public AuthConfig getCredentialNode(String registryToLookup) throws MojoExecutionException { + public AuthConfig getAuthConfig(String registryToLookup) throws MojoExecutionException { try { final GetCommand getCommand = new GetCommand(); return toAuthConfig(getCommand.getCredentialNode("https://" + registryToLookup));