-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #821 from denisa/credHelper
use credential helper if defined
- Loading branch information
Showing
6 changed files
with
310 additions
and
40 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
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
118 changes: 118 additions & 0 deletions
118
src/main/java/io/fabric8/maven/docker/util/CredentialHelperClient.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,118 @@ | ||
package io.fabric8.maven.docker.util; | ||
|
||
import io.fabric8.maven.docker.access.AuthConfig; | ||
import io.fabric8.maven.docker.access.util.ExternalCommand; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
|
||
import java.io.IOException; | ||
|
||
public class CredentialHelperClient { | ||
|
||
static final String SECRET_KEY = "Secret"; | ||
static final String USERNAME_KEY = "Username"; | ||
private final String credentialHelperName; | ||
private final Logger log; | ||
|
||
public CredentialHelperClient(Logger log, String credentialsStore) { | ||
this.log = log; | ||
credentialHelperName = "docker-credential-" + credentialsStore; | ||
} | ||
|
||
public String getName() { | ||
return credentialHelperName; | ||
} | ||
|
||
public String getVersion() throws MojoExecutionException { | ||
try { | ||
return new VersionCommand().getVersion(); | ||
} catch (IOException e) { | ||
throw new MojoExecutionException("Error getting the version of the configured credential helper",e); | ||
} | ||
} | ||
|
||
public AuthConfig getAuthConfig(String registryToLookup) throws MojoExecutionException { | ||
try { | ||
final GetCommand getCommand = new GetCommand(); | ||
return toAuthConfig(getCommand.getCredentialNode("https://" + registryToLookup)); | ||
} catch (IOException e) { | ||
throw new MojoExecutionException("Error getting the credentials for " + registryToLookup + " from the configured credential helper",e); | ||
} | ||
} | ||
|
||
private AuthConfig toAuthConfig(JSONObject credential){ | ||
if (credential == null) { | ||
return null; | ||
} | ||
String password = credential.getString(CredentialHelperClient.SECRET_KEY); | ||
String userKey = credential.getString(CredentialHelperClient.USERNAME_KEY); | ||
return new AuthConfig(userKey,password, null,null); | ||
} | ||
|
||
// docker-credential-XXX version | ||
private class VersionCommand extends ExternalCommand { | ||
|
||
private String version; | ||
|
||
VersionCommand() { | ||
super(CredentialHelperClient.this.log); | ||
} | ||
|
||
@Override | ||
protected String[] getArgs() { | ||
return new String[]{CredentialHelperClient.this.credentialHelperName,"version"}; | ||
} | ||
|
||
@Override | ||
protected void processLine(String line) { | ||
log.info("Credentials helper reply for \"%s\" is %s",CredentialHelperClient.this.credentialHelperName,line); | ||
version = line; | ||
} | ||
|
||
public String getVersion() throws IOException { | ||
execute(); | ||
if (version == null) { | ||
throw new IOException("No reply information returned by " + getCommandAsString()); | ||
} | ||
return version; | ||
} | ||
} | ||
|
||
// echo <registryToLookup> | docker-credential-XXX get | ||
private class GetCommand extends ExternalCommand { | ||
|
||
private String reply; | ||
|
||
GetCommand() { | ||
super(CredentialHelperClient.this.log); | ||
} | ||
|
||
@Override | ||
protected String[] getArgs() { | ||
return new String[]{CredentialHelperClient.this.credentialHelperName,"get"}; | ||
} | ||
|
||
@Override | ||
protected void processLine(String line) { | ||
reply = line; | ||
} | ||
|
||
public JSONObject getCredentialNode(String registryToLookup) throws IOException { | ||
try { | ||
execute(registryToLookup); | ||
} catch (IOException ex) { | ||
if (getStatusCode() == 1) { | ||
return null; | ||
} else { | ||
throw ex; | ||
} | ||
} | ||
JSONObject credentials = new JSONObject(new JSONTokener(reply)); | ||
if (!credentials.has(SECRET_KEY) || !credentials.has(USERNAME_KEY)) { | ||
return null; | ||
} | ||
return credentials; | ||
} | ||
} | ||
} |
Oops, something went wrong.