-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Credential helpers #729
Merged
rnorth
merged 16 commits into
testcontainers:master
from
TheIndifferent:credential-helpers
Jun 16, 2018
Merged
Credential helpers #729
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
414a5a0
Docker authentiation using credential store/helpers
rnorth ada02b2
add #648 to CHANGELOG.md
bsideup 60a55f9
Docker authentiation using credential store/helpers
rnorth a181b8b
Update following initial review
rnorth 06ef5f6
Change exception in case of failure
rnorth 80d34a8
Simplify test resource resolution
rnorth 87d9a3a
Add extra logging
rnorth 682b558
Add store_artifacts for debugging (experimental)
rnorth 09ca2f9
Upgrade zt-exec
rnorth a6eaf0d
Explicitly use bash for fake credential helper
rnorth 53acc95
Always store test artifacts
rnorth 878ac17
Fixing the logical condition on when to check for cred helpers.
TheIndifferent 42a4c56
Post-merge fix.
TheIndifferent f48753c
Fallback between 3 supported types of authentication.
TheIndifferent 42fc4cf
Fixing javadoc.
TheIndifferent 7de6be8
Disable untested feature on Windows
rnorth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ node_modules/ | |
|
||
.gradle/ | ||
build/ | ||
out/ | ||
*.class | ||
|
||
# Eclipse IDE files | ||
**/.project | ||
|
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
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
184 changes: 184 additions & 0 deletions
184
core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.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,184 @@ | ||
package org.testcontainers.utility; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.dockerjava.api.model.AuthConfig; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import org.apache.commons.lang.SystemUtils; | ||
import org.slf4j.Logger; | ||
import org.zeroturnaround.exec.ProcessExecutor; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
/** | ||
* Utility to look up registry authentication information for an image. | ||
*/ | ||
public class RegistryAuthLocator { | ||
|
||
private static final Logger log = getLogger(RegistryAuthLocator.class); | ||
|
||
private final AuthConfig defaultAuthConfig; | ||
private final File configFile; | ||
private final String commandPathPrefix; | ||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
@VisibleForTesting | ||
RegistryAuthLocator(AuthConfig defaultAuthConfig, File configFile, String commandPathPrefix) { | ||
this.defaultAuthConfig = defaultAuthConfig; | ||
this.configFile = configFile; | ||
this.commandPathPrefix = commandPathPrefix; | ||
} | ||
|
||
/** | ||
* @param defaultAuthConfig an AuthConfig object that should be returned if there is no overriding authentication | ||
* available for images that are looked up | ||
*/ | ||
public RegistryAuthLocator(AuthConfig defaultAuthConfig) { | ||
this.defaultAuthConfig = defaultAuthConfig; | ||
final String dockerConfigLocation = System.getenv().getOrDefault("DOCKER_CONFIG", | ||
System.getProperty("user.home") + "/.docker"); | ||
this.configFile = new File(dockerConfigLocation + "/config.json"); | ||
this.commandPathPrefix = ""; | ||
} | ||
|
||
/** | ||
* Looks up an AuthConfig for a given image name. | ||
* <p> | ||
* Lookup is performed in following order: | ||
* <ol> | ||
* <li>{@code auths} is checked for existing credentials for the specified registry.</li> | ||
* <li>if no existing auth is found, {@code credHelpers} are checked for helper for the specified registry.</li> | ||
* <li>if no suitable {@code credHelpers} found, {@code credsStore} is used.</li> | ||
* <li>if no {@code credsStore} is found then the default configuration is returned.</li> | ||
* </ol> | ||
* | ||
* @param dockerImageName image name to be looked up (potentially including a registry URL part) | ||
* @return an AuthConfig that is applicable to this specific image OR the defaultAuthConfig that has been set for | ||
* this {@link RegistryAuthLocator}. | ||
*/ | ||
public AuthConfig lookupAuthConfig(DockerImageName dockerImageName) { | ||
|
||
if (SystemUtils.IS_OS_WINDOWS) { | ||
log.debug("RegistryAuthLocator is not supported on Windows. Please help test or improve it and update " + | ||
"https://github.com/testcontainers/testcontainers-java/issues/756"); | ||
return defaultAuthConfig; | ||
} | ||
|
||
log.debug("Looking up auth config for image: {}", dockerImageName); | ||
|
||
log.debug("RegistryAuthLocator has configFile: {} ({}) and commandPathPrefix: {}", | ||
configFile, | ||
configFile.exists() ? "exists" : "does not exist", | ||
commandPathPrefix); | ||
|
||
try { | ||
final JsonNode config = OBJECT_MAPPER.readTree(configFile); | ||
final String reposName = dockerImageName.getRegistry(); | ||
|
||
final AuthConfig existingAuthConfig = findExistingAuthConfig(config, reposName); | ||
if (existingAuthConfig != null) { | ||
return existingAuthConfig; | ||
} | ||
// auths is empty, using helper: | ||
final AuthConfig helperAuthConfig = authConfigUsingHelper(config, reposName); | ||
if (helperAuthConfig != null) { | ||
return helperAuthConfig; | ||
} | ||
// no credsHelper to use, using credsStore: | ||
final AuthConfig storeAuthConfig = authConfigUsingStore(config, reposName); | ||
if (storeAuthConfig != null) { | ||
return storeAuthConfig; | ||
} | ||
// otherwise, defaultAuthConfig should already contain any credentials available | ||
} catch (Exception e) { | ||
log.error("Failure when attempting to lookup auth config (dockerImageName: {}, configFile: {}. " + | ||
"Falling back to docker-java default behaviour", | ||
dockerImageName, | ||
configFile, | ||
e); | ||
} | ||
return defaultAuthConfig; | ||
} | ||
|
||
private AuthConfig findExistingAuthConfig(final JsonNode config, final String reposName) throws Exception { | ||
final Map.Entry<String, JsonNode> entry = findAuthNode(config, reposName); | ||
if (entry != null && entry.getValue() != null && entry.getValue().size() > 0) { | ||
return OBJECT_MAPPER | ||
.treeToValue(entry.getValue(), AuthConfig.class) | ||
.withRegistryAddress(entry.getKey()); | ||
} | ||
return null; | ||
} | ||
|
||
private AuthConfig authConfigUsingHelper(final JsonNode config, final String reposName) throws Exception { | ||
final JsonNode credHelpers = config.get("credHelpers"); | ||
if (credHelpers != null && credHelpers.size() > 0) { | ||
final JsonNode helperNode = credHelpers.get(reposName); | ||
if (helperNode != null && helperNode.isTextual()) { | ||
final String helper = helperNode.asText(); | ||
return runCredentialProvider(reposName, helper); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private AuthConfig authConfigUsingStore(final JsonNode config, final String reposName) throws Exception { | ||
final JsonNode credsStoreNode = config.get("credsStore"); | ||
if (credsStoreNode != null && !credsStoreNode.isMissingNode() && credsStoreNode.isTextual()) { | ||
final String credsStore = credsStoreNode.asText(); | ||
return runCredentialProvider(reposName, credsStore); | ||
} | ||
return null; | ||
} | ||
|
||
private Map.Entry<String, JsonNode> findAuthNode(final JsonNode config, final String reposName) throws Exception { | ||
final JsonNode auths = config.get("auths"); | ||
if (auths != null && auths.size() > 0) { | ||
final Iterator<Map.Entry<String, JsonNode>> fields = auths.fields(); | ||
while (fields.hasNext()) { | ||
final Map.Entry<String, JsonNode> entry = fields.next(); | ||
if (entry.getKey().endsWith("://" + reposName)) { | ||
return entry; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private AuthConfig runCredentialProvider(String hostName, String credHelper) throws Exception { | ||
final String credentialHelperName = commandPathPrefix + "docker-credential-" + credHelper; | ||
String data; | ||
|
||
log.debug("Executing docker credential helper: {} to locate auth config for: {}", | ||
credentialHelperName, hostName); | ||
|
||
try { | ||
data = new ProcessExecutor() | ||
.command(credentialHelperName, "get") | ||
.redirectInput(new ByteArrayInputStream(hostName.getBytes())) | ||
.readOutput(true) | ||
.exitValueNormal() | ||
.timeout(30, TimeUnit.SECONDS) | ||
.execute() | ||
.outputUTF8() | ||
.trim(); | ||
} catch (Exception e) { | ||
log.error("Failure running docker credential helper ({})", credentialHelperName); | ||
throw e; | ||
} | ||
|
||
final JsonNode helperResponse = OBJECT_MAPPER.readTree(data); | ||
log.debug("Credential helper provided auth config for: {}", hostName); | ||
|
||
return new AuthConfig() | ||
.withRegistryAddress(helperResponse.at("/ServerURL").asText()) | ||
.withUsername(helperResponse.at("/Username").asText()) | ||
.withPassword(helperResponse.at("/Secret").asText()); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you shift this up to the
UNRELEASED
section?Also, it looks like this same line appears (more or less the same) three times - merge issue?
I think that for now we should draw a line in the sand and release this for Mac and Linux - could we say so in the changelog entry?