Skip to content

Commit e640aff

Browse files
David LeischnigKostyaSha
authored andcommitted
fix(#806): filter config.json before unmarshalling
As ObjectMapper expects all propeties of json content to match AuthConfig it fails on any other like "credStore" or "HttpHeaders". As only "auths" is used the json is mapped to ObjectNode so we can remove any other property. The content of this filtered node can be used as source for unmarshalling AuthConfig.
1 parent b5422b4 commit e640aff

File tree

7 files changed

+72
-20
lines changed

7 files changed

+72
-20
lines changed

src/main/java/com/github/dockerjava/core/AuthConfigFile.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
import com.fasterxml.jackson.core.type.TypeReference;
1515
import com.fasterxml.jackson.databind.ObjectMapper;
16+
import com.fasterxml.jackson.databind.node.ObjectNode;
1617
import com.github.dockerjava.api.model.AuthConfig;
1718
import com.github.dockerjava.api.model.AuthConfigurations;
1819

20+
import static java.util.Objects.isNull;
21+
1922
public class AuthConfigFile {
2023
private static final ObjectMapper MAPPER = new ObjectMapper();
2124

@@ -26,11 +29,12 @@ public class AuthConfigFile {
2629
private static final TypeReference<Map<String, Map<String, AuthConfig>>> CONFIG_JSON_MAP_TYPE =
2730
new TypeReference<Map<String, Map<String, AuthConfig>>>() {
2831
};
32+
private static final String AUTHS_PROPERTY = "auths";
2933

3034
private final Map<String, AuthConfig> authConfigMap;
3135

3236
public AuthConfigFile() {
33-
authConfigMap = new HashMap<String, AuthConfig>();
37+
authConfigMap = new HashMap<>();
3438
}
3539

3640
void addConfig(AuthConfig config) {
@@ -114,10 +118,12 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
114118
*/
115119
try {
116120
// try registry version 2
117-
Map<String, Map<String, AuthConfig>> configJson = MAPPER.readValue(confFile, CONFIG_JSON_MAP_TYPE);
118-
if (configJson != null) {
119-
configMap = configJson.get("auths");
121+
final ObjectNode node = filterNonAuthsFromJSON(confFile);
122+
Map<String, Map<String, AuthConfig>> configJson = MAPPER.convertValue(node, CONFIG_JSON_MAP_TYPE);
123+
if (configJson != null && !configJson.isEmpty()) {
124+
configMap = configJson.get(AUTHS_PROPERTY);
120125
}
126+
121127
} catch (IOException e1) {
122128
try {
123129
// try registry version 1
@@ -130,7 +136,10 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
130136
if (configMap != null) {
131137
for (Map.Entry<String, AuthConfig> entry : configMap.entrySet()) {
132138
AuthConfig authConfig = entry.getValue();
133-
decodeAuth(authConfig.getAuth(), authConfig);
139+
final String auth = authConfig.getAuth();
140+
if (auth == null) continue;
141+
142+
decodeAuth(auth, authConfig);
134143
authConfig.withAuth(null);
135144
authConfig.withRegistryAddress(entry.getKey());
136145
configFile.addConfig(authConfig);
@@ -158,6 +167,15 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
158167

159168
}
160169

170+
private static ObjectNode filterNonAuthsFromJSON(final File confFile) throws IOException {
171+
final ObjectNode node = MAPPER.readValue(confFile, ObjectNode.class);
172+
if (!node.has(AUTHS_PROPERTY)) {
173+
throw new IOException("No Auth Config contained");
174+
}
175+
node.retain(AUTHS_PROPERTY);
176+
return node;
177+
}
178+
161179
static void decodeAuth(String auth, AuthConfig config) throws IOException {
162180
String str = new String(Base64.decodeBase64(auth), Charset.forName("UTF-8"));
163181
String[] parts = str.split(":", 2);

src/test/java/com/github/dockerjava/core/AuthConfigFileTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,23 @@ public void validJson() throws IOException {
6464
expected.addConfig(authConfig1);
6565
expected.addConfig(authConfig2);
6666

67-
Assert.assertEquals(runTest("validJson"), expected);
67+
Assert.assertEquals(runTest("validJson.json"), expected);
6868

6969
}
7070

71+
@Test
72+
public void validJsonWithUnknown() throws IOException {
73+
AuthConfig authConfig1 = new AuthConfig()
74+
.withEmail("foo@example.com")
75+
.withUsername("foo")
76+
.withPassword("bar")
77+
.withRegistryAddress("quay.io");
78+
79+
AuthConfigFile expected = new AuthConfigFile();
80+
expected.addConfig(authConfig1);
81+
runTest("validJsonWithUnknown.json");
82+
}
83+
7184
@Test
7285
public void validLegacy() throws IOException {
7386
AuthConfig authConfig = new AuthConfig()

src/test/java/com/github/dockerjava/core/command/ListContainersCmdImplTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public void testListContainers() throws Exception {
5757

5858
String testImage = "busybox";
5959

60-
// need to block until image is pulled completely
61-
dockerClient.pullImageCmd(testImage).exec(new PullImageResultCallback()).awaitSuccess();
62-
6360
List<Container> containers = dockerClient.listContainersCmd().withShowAll(true).exec();
6461
assertThat(containers, notNullValue());
6562
LOG.info("Container List: {}", containers);

src/test/java/com/github/dockerjava/netty/exec/ListContainersCmdExecTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ public void testListContainers() throws Exception {
5959

6060
String testImage = "busybox";
6161

62-
// // need to block until image is pulled completely
63-
// dockerClient.pullImageCmd(testImage).exec(new PullImageResultCallback()).awaitSuccess();
64-
6562
List<Container> containers = dockerClient.listContainersCmd().withShowAll(true).exec();
6663
assertThat(containers, notNullValue());
6764
LOG.info("Container List: {}", containers);
@@ -107,9 +104,6 @@ public void testListContainersWithLabelsFilter() throws Exception {
107104

108105
String testImage = "busybox";
109106

110-
// need to block until image is pulled completely
111-
dockerClient.pullImageCmd(testImage).exec(new PullImageResultCallback()).awaitCompletion();
112-
113107
List<Container> containers = dockerClient.listContainersCmd().withShowAll(true).exec();
114108
assertThat(containers, notNullValue());
115109
LOG.info("Container List: {}", containers);
@@ -152,16 +146,25 @@ public void testListContainersWithLabelsFilter() throws Exception {
152146
Map<String, String> labels = ImmutableMap.of("test", "docker-java");
153147

154148
// list with filter by label
155-
dockerClient.createContainerCmd(testImage).withCmd("echo").withLabels(labels).exec();
156-
filteredContainers = dockerClient.listContainersCmd().withShowAll(true)
157-
.withLabelFilter(labels).exec();
149+
dockerClient.createContainerCmd(testImage)
150+
.withCmd("echo")
151+
.withLabels(labels)
152+
.exec();
153+
154+
filteredContainers = dockerClient.listContainersCmd()
155+
.withShowAll(true)
156+
.withLabelFilter(labels)
157+
.exec();
158+
158159
assertThat(filteredContainers.size(), is(equalTo(1)));
159160
Container container3 = filteredContainers.get(0);
160161
assertThat(container3.getCommand(), not(isEmptyString()));
161162
assertThat(container3.getImage(), startsWith(testImage));
162163

163164
filteredContainers = dockerClient.listContainersCmd().withShowAll(true)
164-
.withLabelFilter("test").exec();
165+
.withLabelFilter("test")
166+
.exec();
167+
165168
assertThat(filteredContainers.size(), is(equalTo(1)));
166169
container3 = filteredContainers.get(0);
167170
assertThat(container3.getCommand(), not(isEmptyString()));

src/test/resources/testAuthConfigFile/validJson

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"quay.io": {
3+
"auth": "Zm9vOmJhcg==",
4+
"email": "foo@example.com"
5+
},
6+
"https://index.docker.io/v1/": {
7+
"auth": "Zm9vMTpiYXIx",
8+
"email": "moo@example.com"
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"auths": {
3+
"192.168.99.100:32768": {},
4+
"https://index.docker.io/v1/": {
5+
"auth": "Zm9vOmJhcg==",
6+
"email": "foo@example.com"
7+
}
8+
},
9+
"HttpHeaders": {
10+
"User-Agent": "user agent"
11+
}
12+
}

0 commit comments

Comments
 (0)