Skip to content

Commit 23d7e80

Browse files
David LeischnigKostyaSha
authored andcommitted
fix(docker-java#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 2f28677 commit 23d7e80

File tree

7 files changed

+70
-20
lines changed

7 files changed

+70
-20
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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

@@ -26,11 +27,12 @@ public class AuthConfigFile {
2627
private static final TypeReference<Map<String, Map<String, AuthConfig>>> CONFIG_JSON_MAP_TYPE =
2728
new TypeReference<Map<String, Map<String, AuthConfig>>>() {
2829
};
30+
private static final String AUTHS_PROPERTY = "auths";
2931

3032
private final Map<String, AuthConfig> authConfigMap;
3133

3234
public AuthConfigFile() {
33-
authConfigMap = new HashMap<String, AuthConfig>();
35+
authConfigMap = new HashMap<>();
3436
}
3537

3638
void addConfig(AuthConfig config) {
@@ -114,10 +116,12 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
114116
*/
115117
try {
116118
// 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");
119+
final ObjectNode node = filterNonAuthsFromJSON(confFile);
120+
Map<String, Map<String, AuthConfig>> configJson = MAPPER.convertValue(node, CONFIG_JSON_MAP_TYPE);
121+
if (configJson != null && !configJson.isEmpty()) {
122+
configMap = configJson.get(AUTHS_PROPERTY);
120123
}
124+
121125
} catch (IOException e1) {
122126
try {
123127
// try registry version 1
@@ -130,7 +134,10 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
130134
if (configMap != null) {
131135
for (Map.Entry<String, AuthConfig> entry : configMap.entrySet()) {
132136
AuthConfig authConfig = entry.getValue();
133-
decodeAuth(authConfig.getAuth(), authConfig);
137+
final String auth = authConfig.getAuth();
138+
if (auth == null) continue;
139+
140+
decodeAuth(auth, authConfig);
134141
authConfig.withAuth(null);
135142
authConfig.withRegistryAddress(entry.getKey());
136143
configFile.addConfig(authConfig);
@@ -158,6 +165,15 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
158165

159166
}
160167

168+
private static ObjectNode filterNonAuthsFromJSON(final File confFile) throws IOException {
169+
final ObjectNode node = MAPPER.readValue(confFile, ObjectNode.class);
170+
if (!node.has(AUTHS_PROPERTY)) {
171+
throw new IOException("No Auth Config contained");
172+
}
173+
node.retain(AUTHS_PROPERTY);
174+
return node;
175+
}
176+
161177
static void decodeAuth(String auth, AuthConfig config) throws IOException {
162178
String str = new String(Base64.decodeBase64(auth), Charset.forName("UTF-8"));
163179
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)