Skip to content

Commit 881b5d2

Browse files
author
David Leischnig
committed
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 08ac9b6 commit 881b5d2

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

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

Lines changed: 16 additions & 3 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,6 +27,7 @@ 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

@@ -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
@@ -158,6 +162,15 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
158162

159163
}
160164

165+
private static ObjectNode filterNonAuthsFromJSON(final File confFile) throws IOException {
166+
final ObjectNode node = MAPPER.readValue(confFile, ObjectNode.class);
167+
if (!node.has(AUTHS_PROPERTY)) {
168+
throw new IOException("No Auth Config contained");
169+
}
170+
node.retain(AUTHS_PROPERTY);
171+
return node;
172+
}
173+
161174
static void decodeAuth(String auth, AuthConfig config) throws IOException {
162175
String str = new String(Base64.decodeBase64(auth), Charset.forName("UTF-8"));
163176
String[] parts = str.split(":", 2);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ public void validJson() throws IOException {
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()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"auths": {
3+
"https://index.docker.io/v1/": {
4+
"auth" : "Zm9vOmJhcg==",
5+
"email" :"foo@example.com"
6+
}
7+
},
8+
"HttpHeaders": {
9+
"User-Agent": "user agent"
10+
}
11+
}

0 commit comments

Comments
 (0)