-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for docker hub private registry credentials (#845)
* Fix authentication for use of base64-encoded credentials and credential helpers with private registries and docker hub * Update following review by @bsideup * Update to docker-java 3.1.0-rc-4 * fix shading
- Loading branch information
Showing
16 changed files
with
332 additions
and
63 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
52 changes: 52 additions & 0 deletions
52
.../src/main/java/org/testcontainers/dockerclient/auth/AuthDelegatingDockerClientConfig.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,52 @@ | ||
package org.testcontainers.dockerclient.auth; | ||
|
||
import com.github.dockerjava.api.model.AuthConfig; | ||
import com.github.dockerjava.core.DockerClientConfig; | ||
import lombok.experimental.Delegate; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.RegistryAuthLocator; | ||
|
||
import static org.testcontainers.utility.AuthConfigUtil.toSafeString; | ||
|
||
/** | ||
* Facade implementation for {@link DockerClientConfig} which overrides how authentication | ||
* configuration is obtained. A delegate {@link DockerClientConfig} will be called first | ||
* to try and obtain auth credentials, but after that {@link RegistryAuthLocator} will be | ||
* used to try and improve the auth resolution (e.g. using credential helpers). | ||
*/ | ||
@Slf4j | ||
public class AuthDelegatingDockerClientConfig implements DockerClientConfig { | ||
|
||
@Delegate(excludes = DelegateExclusions.class) | ||
private DockerClientConfig delegate; | ||
|
||
public AuthDelegatingDockerClientConfig(DockerClientConfig delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public AuthConfig effectiveAuthConfig(String imageName) { | ||
// allow docker-java auth config to be used as a fallback | ||
AuthConfig fallbackAuthConfig; | ||
try { | ||
fallbackAuthConfig = delegate.effectiveAuthConfig(imageName); | ||
} catch (Exception e) { | ||
log.debug("Delegate call to effectiveAuthConfig failed with cause: '{}'. " + | ||
"Resolution of auth config will continue using RegistryAuthLocator.", | ||
e.getMessage()); | ||
fallbackAuthConfig = new AuthConfig(); | ||
} | ||
|
||
// try and obtain more accurate auth config using our resolution | ||
final DockerImageName parsed = new DockerImageName(imageName); | ||
final AuthConfig effectiveAuthConfig = RegistryAuthLocator.instance() | ||
.lookupAuthConfig(parsed, fallbackAuthConfig); | ||
|
||
log.debug("Effective auth config [{}]", toSafeString(effectiveAuthConfig)); | ||
return effectiveAuthConfig; | ||
} | ||
|
||
private interface DelegateExclusions { | ||
AuthConfig effectiveAuthConfig(String imageName); | ||
} | ||
} |
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
8 changes: 3 additions & 5 deletions
8
...rc/main/java/org/testcontainers/dockerclient/transport/okhttp/NamedPipeSocketFactory.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
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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/org/testcontainers/utility/AuthConfigUtil.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,34 @@ | ||
package org.testcontainers.utility; | ||
|
||
import com.github.dockerjava.api.model.AuthConfig; | ||
import com.google.common.base.MoreObjects; | ||
import lombok.experimental.UtilityClass; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
||
/** | ||
* TODO: Javadocs | ||
*/ | ||
@UtilityClass | ||
public class AuthConfigUtil { | ||
public static String toSafeString(AuthConfig authConfig) { | ||
if (authConfig == null) { | ||
return "null"; | ||
} | ||
|
||
return MoreObjects.toStringHelper(authConfig) | ||
.add("username", authConfig.getUsername()) | ||
.add("password", obfuscated(authConfig.getPassword())) | ||
.add("auth", obfuscated(authConfig.getAuth())) | ||
.add("email", authConfig.getEmail()) | ||
.add("registryAddress", authConfig.getRegistryAddress()) | ||
.add("registryToken", obfuscated(authConfig.getRegistrytoken())) | ||
.toString(); | ||
} | ||
|
||
@NotNull | ||
private static String obfuscated(String value) { | ||
return isNullOrEmpty(value) ? "blank" : "hidden non-blank value"; | ||
} | ||
} |
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
Oops, something went wrong.