Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/main/java/com/github/dockerjava/core/AuthConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,29 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
}

Map<String, AuthConfig> configMap = null;
/*
Registry v2 expects config expects config.json while v2 expects .dockercfg
The only difference between them is that config.json wraps "auths" around the AuthConfig
*/
try {
// try registry version 2
final ObjectNode node = filterNonAuthsFromJSON(confFile);
Map<String, Map<String, AuthConfig>> configJson = MAPPER.convertValue(node, CONFIG_JSON_MAP_TYPE);
if (configJson != null && !configJson.isEmpty()) {
configMap = configJson.get(AUTHS_PROPERTY);
}

} catch (IOException e1) {
if (isJSONFile(confFile)) {
/*
Registry v2 expects config expects config.json while v2 expects .dockercfg
The only difference between them is that config.json wraps "auths" around the AuthConfig
*/
try {
// try registry version 1
configMap = MAPPER.readValue(confFile, CONFIG_CFG_MAP_TYPE);
} catch (IOException e2) {
// pass
// try registry version 2
final ObjectNode node = filterNonAuthsFromJSON(confFile);
Map<String, Map<String, AuthConfig>> configJson = MAPPER.convertValue(node, CONFIG_JSON_MAP_TYPE);
if (configJson != null && !configJson.isEmpty()) {
configMap = configJson.get(AUTHS_PROPERTY);
}

} catch (IOException e1) {
try {
// try registry version 1
configMap = MAPPER.readValue(confFile, CONFIG_CFG_MAP_TYPE);
} catch (IOException e2) {
// we know it is JSON so it does not make sense to check for the old format
// probably Docker writes some unstructured contents here, see #806
// at least it is not worse than the totally absent file
return new AuthConfigFile();
}
}
}

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

}

private static boolean isJSONFile(final File confFile) {
try {
return MAPPER.readValue(confFile, ObjectNode.class) != null;
} catch (IOException e) {
return false;
}
}

private static ObjectNode filterNonAuthsFromJSON(final File confFile) throws IOException {
final ObjectNode node = MAPPER.readValue(confFile, ObjectNode.class);
if (!node.has(AUTHS_PROPERTY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public void validJsonWithUnknown() throws IOException {
runTest("validJsonWithUnknown.json");
}

@Test
public void validJsonWithOnlyUnknown() throws IOException {
AuthConfigFile expected = new AuthConfigFile();
AuthConfigFile actual = runTest("validJsonWithOnlyUnknown.json");
Assert.assertEquals(actual, expected);
}

@Test
public void validLegacy() throws IOException {
AuthConfig authConfig = new AuthConfig()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"credsStore" : "osxkeychain"
}