Repository: fabric8io/docker-maven-plugin
Issue: [BUG] Can't parse docker-credential-gcloud multiline output

### Description
When using `docker-credential-gcloud`, the plugin fails to parse the JSON credentials returned by the helper.

```
[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.24-SNAPSHOT:build (default-build) on project test-docker-maven-plugin: Execution default-build of goal io.fabric8:docker-maven-plugin:0.24-SNAPSHOT:build failed: A JSONObject text must begin with '{' at 1 [character 2 line 1] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal io.fabric8:docker-maven-plugin:0.24-SNAPSHOT:build (default-build) on project airflow: Execution default-build of goal io.fabric8:docker-maven-plugin:0.24-SNAPSHOT:build failed: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:993)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:345)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:191)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-build of goal io.fabric8:docker-maven-plugin:0.24-SNAPSHOT:build failed: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:145)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:993)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:345)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:191)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError (JSONTokener.java:433)
    at org.json.JSONObject.<init> (JSONObject.java:194)
    at io.fabric8.maven.docker.util.CredentialHelperClient$GetCommand.getCredentialNode (CredentialHelperClient.java:111)
    at io.fabric8.maven.docker.util.CredentialHelperClient.getAuthConfig (CredentialHelperClient.java:38)
    at io.fabric8.maven.docker.util.AuthConfigFactory.extractAuthConfigFromCredentialsHelper (AuthConfigFactory.java:351)
    at io.fabric8.maven.docker.util.AuthConfigFactory.getAuthConfigFromDockerConfig (AuthConfigFactory.java:320)
    at io.fabric8.maven.docker.util.AuthConfigFactory.createAuthConfig (AuthConfigFactory.java:119)
    at io.fabric8.maven.docker.service.RegistryService.createAuthConfig (RegistryService.java:139)
    at io.fabric8.maven.docker.service.RegistryService.pullImageWithPolicy (RegistryService.java:98)
    at io.fabric8.maven.docker.service.BuildService.autoPullBaseImage (BuildService.java:209)
    at io.fabric8.maven.docker.service.BuildService.buildImage (BuildService.java:58)
    at io.fabric8.maven.docker.BuildMojo.buildAndTag (BuildMojo.java:59)
    at io.fabric8.maven.docker.BuildMojo.executeInternal (BuildMojo.java:44)
    at io.fabric8.maven.docker.AbstractDockerMojo.execute (AbstractDockerMojo.java:220)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:993)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:345)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:191)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
```

The helper works correctly when invoked standalone
```
giuseppe@zeug:~$ echo 'https://gcr.io' | docker-credential-gcloud get
{
  "Secret": "[REDACTED]",
  "Username": "oauth2accesstoken"
}
giuseppe@zeug:~$
```

The issue seems to arise from the ExternalCommand class, where the output of the commands are read/returned line by line.
The output of `docker-credential-gcloud` contains multilines, and this confuses the parser into using only the last line - and failing to find a valid json.

### Info

* d-m-p version: 0.24-SNAPSHOT (17/1/2018)
* Maven version (`mvn -v`):
```
Apache Maven 3.5.0
Maven home: /usr/share/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.13.0-21-generic", arch: "amd64", family: "unix"
```
* Docker version: Docker version 17.12.0-ce, build c97c6d6
* If it's a bug, how to reproduce: enable the `docker-credential-gcloud` and try to push/pull an image hosted in GCR

Changed files:
src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.java

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 6b222927..8dc66572 100644
--- a/src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.java
+++ b/src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.java
@@ -1,5 +1,7 @@
 package io.fabric8.maven.docker.util;
 
+import com.google.common.base.Joiner;
+import com.google.common.collect.Lists;
 import io.fabric8.maven.docker.access.AuthConfig;
 import io.fabric8.maven.docker.access.util.ExternalCommand;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -7,6 +9,7 @@ import org.json.JSONObject;
 import org.json.JSONTokener;
 
 import java.io.IOException;
+import java.util.List;
 
 public class CredentialHelperClient {
 
@@ -82,7 +85,7 @@ public class CredentialHelperClient {
     // echo <registryToLookup> | docker-credential-XXX get
     private class GetCommand extends ExternalCommand {
 
-        private String reply;
+        private List<String> reply = Lists.newLinkedList();
 
         GetCommand() {
             super(CredentialHelperClient.this.log);
@@ -95,7 +98,7 @@ public class CredentialHelperClient {
 
         @Override
         protected void processLine(String line) {
-            reply = line;
+            reply.add(line);
         }
 
         public JSONObject getCredentialNode(String registryToLookup) throws IOException {
@@ -108,7 +111,7 @@ public class CredentialHelperClient {
                     throw ex;
                 }
             }
-            JSONObject credentials = new JSONObject(new JSONTokener(reply));
+            JSONObject credentials = new JSONObject(new JSONTokener(Joiner.on('\\n').join(reply)));
             if (!credentials.has(SECRET_KEY) || !credentials.has(USERNAME_KEY)) {
                 return null;
             }