Skip to content

Commit

Permalink
Read configuration file from directory supplied in DOCKER_CONFIG envi…
Browse files Browse the repository at this point in the history
…ronment variable (spotify#1145)
  • Loading branch information
albanf authored and davidxia committed May 7, 2019
1 parent f297361 commit 58df667
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/spotify/docker/client/DefaultDockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

package com.spotify.docker.client;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -50,8 +49,10 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.CharStreams;
Expand Down Expand Up @@ -145,6 +146,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -2932,8 +2934,11 @@ public static Builder builder() {
*/
public static Builder fromEnv() throws DockerCertificateException {
final String endpoint = DockerHost.endpointFromEnv();
final Path dockerCertPath = Paths.get(firstNonNull(DockerHost.certPathFromEnv(),
DockerHost.defaultCertPath()));
final Path dockerCertPath = Paths.get(Iterables.find(
Arrays.asList(DockerHost.certPathFromEnv(),
DockerHost.configPathFromEnv(),
DockerHost.defaultCertPath()),
Predicates.notNull()));

final Builder builder = new Builder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ private RegistryAuth authForRegistry(final DockerConfig config, final String reg
}

public Path defaultConfigPath() {
if (DockerHost.configPathFromEnv() != null) {
final Path dockerConfig = Paths.get(DockerHost.configPathFromEnv(), "config.json");
LOG.debug("Using config path from DOCKER_CONFIG: {}", dockerConfig);
return dockerConfig;
}
final String home = System.getProperty("user.home");
final Path dockerConfig = Paths.get(home, ".docker", "config.json");
final Path dockerCfg = Paths.get(home, ".dockercfg");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/spotify/docker/client/DockerHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ static String certPathFromEnv() {
return systemDelegate.getenv("DOCKER_CERT_PATH");
}

static String configPathFromEnv() {
return systemDelegate.getenv("DOCKER_CONFIG");
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,24 @@ public void testDuplicateServerInAuthsAndCredHelpers() throws Exception {
.build();
assertThat(reader.authForAllRegistries(path), is(registryConfigs));
}

@Test
public void testConfigFromEnv() throws IOException {
DockerHost.SystemDelegate systemDelegate = mock(DockerHost.SystemDelegate.class);
when(systemDelegate.getenv("DOCKER_CONFIG"))
.thenReturn("src/test/resources/dockerConfigFromEnv");
when(systemDelegate.getProperty("os.name")).thenReturn(System.getProperty("os.name"));
DockerHost.setSystemDelegate(systemDelegate);
try {
DockerConfigReader dockerConfigReader = new DockerConfigReader();
Path path = dockerConfigReader.defaultConfigPath();
assertThat(path.toString().replace("\\", "/"),
equalTo("src/test/resources/dockerConfigFromEnv/config.json"));

final RegistryAuth registryAuth = dockerConfigReader.anyRegistryAuth();
assertThat(registryAuth, equalTo(DOCKER_AUTH_CONFIG));
} finally {
DockerHost.restoreSystemDelegate();
}
}
}
7 changes: 7 additions & 0 deletions src/test/java/com/spotify/docker/client/DockerHostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public void testEndpointFromEnv() throws Exception {
assertThat(DockerHost.endpointFromEnv(), equalTo("unix:///var/run/docker.sock"));
}

@Test
public void testConfigFromEnv() {
when(systemDelegate.getenv("DOCKER_CONFIG")).thenReturn("foodir");
DockerHost.setSystemDelegate(systemDelegate);
assertThat(DockerHost.configPathFromEnv(), equalTo("foodir"));
}

@Test
public void testDefaultUnixEndpoint() throws Exception {
assertThat(DockerHost.defaultUnixEndpoint(), equalTo("unix:///var/run/docker.sock"));
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/dockerConfigFromEnv/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "ZG9ja2VybWFuOnN3NGd5MGxvCg==",
"email": "[email protected]"
}
}
}

0 comments on commit 58df667

Please sign in to comment.