X Tutup
Skip to content
Closed
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
19 changes: 16 additions & 3 deletions src/main/java/com/github/dockerjava/core/AuthConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthConfigurations;

Expand All @@ -26,6 +27,7 @@ public class AuthConfigFile {
private static final TypeReference<Map<String, Map<String, AuthConfig>>> CONFIG_JSON_MAP_TYPE =
new TypeReference<Map<String, Map<String, AuthConfig>>>() {
};
private static final String AUTHS_PROPERTY = "auths";

private final Map<String, AuthConfig> authConfigMap;

Expand Down Expand Up @@ -114,10 +116,12 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
*/
try {
// try registry version 2
Map<String, Map<String, AuthConfig>> configJson = MAPPER.readValue(confFile, CONFIG_JSON_MAP_TYPE);
if (configJson != null) {
configMap = configJson.get("auths");
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
Expand Down Expand Up @@ -158,6 +162,15 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {

}

private static ObjectNode filterNonAuthsFromJSON(final File confFile) throws IOException {
final ObjectNode node = MAPPER.readValue(confFile, ObjectNode.class);
if (!node.has(AUTHS_PROPERTY)) {
throw new IOException("No Auth Config contained");
}
node.retain(AUTHS_PROPERTY);
return node;
}

static void decodeAuth(String auth, AuthConfig config) throws IOException {
String str = new String(Base64.decodeBase64(auth), Charset.forName("UTF-8"));
String[] parts = str.split(":", 2);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/github/dockerjava/core/AuthConfigFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public void validJson() throws IOException {

}

@Test
public void validJsonWithUnknown() throws IOException {
AuthConfig authConfig1 = new AuthConfig()
.withEmail("foo@example.com")
.withUsername("foo")
.withPassword("bar")
.withRegistryAddress("quay.io");

AuthConfigFile expected = new AuthConfigFile();
expected.addConfig(authConfig1);
runTest("validJsonWithUnknown.json");
}

@Test
public void validLegacy() throws IOException {
AuthConfig authConfig = new AuthConfig()
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/testAuthConfigFile/validJsonWithUnknown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"auths": {
"https://index.docker.io/v1/": {
"auth" : "Zm9vOmJhcg==",
"email" :"foo@example.com"
}
},
"HttpHeaders": {
"User-Agent": "user agent"
}
}
X Tutup