Skip to content

Commit c04a11f

Browse files
authored
Merge pull request docker-java#884 from borlander/docker-java#806-only-unknown-config-v3
docker-java#806 - config.json now may have unstructured content without `auths`
2 parents a2f7776 + db00c49 commit c04a11f

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,29 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
110110
}
111111

112112
Map<String, AuthConfig> configMap = null;
113-
/*
114-
Registry v2 expects config expects config.json while v2 expects .dockercfg
115-
The only difference between them is that config.json wraps "auths" around the AuthConfig
116-
*/
117-
try {
118-
// try registry version 2
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);
123-
}
124-
125-
} catch (IOException e1) {
113+
if (isJSONFile(confFile)) {
114+
/*
115+
Registry v2 expects config expects config.json while v2 expects .dockercfg
116+
The only difference between them is that config.json wraps "auths" around the AuthConfig
117+
*/
126118
try {
127-
// try registry version 1
128-
configMap = MAPPER.readValue(confFile, CONFIG_CFG_MAP_TYPE);
129-
} catch (IOException e2) {
130-
// pass
119+
// try registry version 2
120+
final ObjectNode node = filterNonAuthsFromJSON(confFile);
121+
Map<String, Map<String, AuthConfig>> configJson = MAPPER.convertValue(node, CONFIG_JSON_MAP_TYPE);
122+
if (configJson != null && !configJson.isEmpty()) {
123+
configMap = configJson.get(AUTHS_PROPERTY);
124+
}
125+
126+
} catch (IOException e1) {
127+
try {
128+
// try registry version 1
129+
configMap = MAPPER.readValue(confFile, CONFIG_CFG_MAP_TYPE);
130+
} catch (IOException e2) {
131+
// we know it is JSON so it does not make sense to check for the old format
132+
// probably Docker writes some unstructured contents here, see #806
133+
// at least it is not worse than the totally absent file
134+
return new AuthConfigFile();
135+
}
131136
}
132137
}
133138

@@ -165,6 +170,14 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
165170

166171
}
167172

173+
private static boolean isJSONFile(final File confFile) {
174+
try {
175+
return MAPPER.readValue(confFile, ObjectNode.class) != null;
176+
} catch (IOException e) {
177+
return false;
178+
}
179+
}
180+
168181
private static ObjectNode filterNonAuthsFromJSON(final File confFile) throws IOException {
169182
final ObjectNode node = MAPPER.readValue(confFile, ObjectNode.class);
170183
if (!node.has(AUTHS_PROPERTY)) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public void validJsonWithUnknown() throws IOException {
8181
runTest("validJsonWithUnknown.json");
8282
}
8383

84+
@Test
85+
public void validJsonWithOnlyUnknown() throws IOException {
86+
AuthConfigFile expected = new AuthConfigFile();
87+
AuthConfigFile actual = runTest("validJsonWithOnlyUnknown.json");
88+
Assert.assertEquals(actual, expected);
89+
}
90+
8491
@Test
8592
public void validLegacy() throws IOException {
8693
AuthConfig authConfig = new AuthConfig()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"credsStore" : "osxkeychain"
3+
}

0 commit comments

Comments
 (0)