All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dockerjava.core.DockerConfigFile Maven / Gradle / Ivy

There is a newer version: 3.4.1
Show newest version
package com.github.dockerjava.core;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthConfigurations;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public class DockerConfigFile {
    private static final String DOCKER_LEGACY_CFG = ".dockercfg";
    private static final String DOCKER_CFG = "config.json";

    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final TypeReference> CONFIG_MAP_TYPE = new TypeReference>() {
    };

    @JsonProperty()
    private final Map auths;

    public DockerConfigFile() {
        this(new HashMap());
    }

    private DockerConfigFile(Map authConfigMap) {
        auths = authConfigMap;
    }

    public Map getAuths() {
        return auths;
    }

    void addAuthConfig(AuthConfig config) {
        auths.put(config.getRegistryAddress(), config);
    }

    public AuthConfig resolveAuthConfig(String hostname) {
        if (StringUtils.isEmpty(hostname) || AuthConfig.DEFAULT_SERVER_ADDRESS.equals(hostname)) {
            return auths.get(AuthConfig.DEFAULT_SERVER_ADDRESS);
        }

        AuthConfig c = auths.get(hostname);
        if (c != null) {
            return c;
        }

        // Maybe they have a legacy config file, we will iterate the keys converting
        // them to the new format and testing
        String normalizedHostname = convertToHostname(hostname);
        for (Map.Entry entry : auths.entrySet()) {
            String registry = entry.getKey();
            AuthConfig config = entry.getValue();
            if (convertToHostname(registry).equals(normalizedHostname)) {
                return config;
            }
        }

        return null;
    }

    public AuthConfigurations getAuthConfigurations() {
        final AuthConfigurations authConfigurations = new AuthConfigurations();
        for (Map.Entry authConfigEntry : auths.entrySet()) {
            authConfigurations.addConfig(authConfigEntry.getValue());
        }

        return authConfigurations;
    }

    // CHECKSTYLE:OFF
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((auths == null) ? 0 : auths.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DockerConfigFile other = (DockerConfigFile) obj;
        if (auths == null) {
            if (other.auths != null)
                return false;
        } else if (!auths.equals(other.auths))
            return false;
        return true;
    }

    // CHECKSTYLE:ON

    @Override
    public String toString() {
        return "DockerConfigFile [auths=" + auths + "]";
    }

    public static DockerConfigFile loadConfig(File dockerConfigPath) throws IOException {
        //parse new docker config file format
        DockerConfigFile dockerConfig = loadCurrentConfig(dockerConfigPath);

        //parse old auth config file format
        if (dockerConfig == null) {
            dockerConfig = loadLegacyConfig(dockerConfigPath);
        }

        //otherwise create default config
        if (dockerConfig == null) {
            dockerConfig = new DockerConfigFile();
        }

        for (Map.Entry entry : dockerConfig.getAuths().entrySet()) {
            AuthConfig authConfig = entry.getValue();
            decodeAuth(authConfig);
            authConfig.withAuth(null);
            authConfig.withRegistryAddress(entry.getKey());
        }

        return dockerConfig;
    }

    private static DockerConfigFile loadCurrentConfig(File dockerConfigPath) throws IOException {
        File dockerCfgFile = new File(dockerConfigPath, File.separator + DOCKER_CFG);

        if (!dockerCfgFile.exists() || !dockerCfgFile.isFile()) {
            return null;
        }

        try {
            return MAPPER.readValue(dockerCfgFile, DockerConfigFile.class);
        } catch (IOException e) {
            throw new IOException("Failed to parse docker " + DOCKER_CFG, e);
        }
    }

    private static DockerConfigFile loadLegacyConfig(File dockerConfigPath) throws IOException {
        File dockerLegacyCfgFile = new File(dockerConfigPath, File.separator + DOCKER_LEGACY_CFG);

        if (!dockerLegacyCfgFile.exists() || !dockerLegacyCfgFile.isFile()) {
            return null;
        }

        //parse legacy auth config file format
        try {
            return new DockerConfigFile(MAPPER.>readValue(dockerLegacyCfgFile, CONFIG_MAP_TYPE));
        } catch (IOException e) {
            // pass
        }

        List authFileContent = FileUtils.readLines(dockerLegacyCfgFile, StandardCharsets.UTF_8);
        if (authFileContent.size() < 2) {
            throw new IOException("The Auth Config file is empty");
        }

        AuthConfig config = new AuthConfig();
        String[] origAuth = authFileContent.get(0).split(" = ");
        if (origAuth.length != 2) {
            throw new IOException("Invalid Auth config file");
        }

        config.withAuth(origAuth[1]);

        String[] origEmail = authFileContent.get(1).split(" = ");
        if (origEmail.length != 2) {
            throw new IOException("Invalid Auth config file");
        }
        config.withEmail(origEmail[1]);

        return new DockerConfigFile(new HashMap<>(Collections.singletonMap(config.getRegistryAddress(), config)));
    }

    private static void decodeAuth(AuthConfig config) throws IOException {
        if (config.getAuth() == null) {
            return;
        }

        String str = new String(Base64.decodeBase64(config.getAuth()), StandardCharsets.UTF_8);
        String[] parts = str.split(":", 2);
        if (parts.length != 2) {
            throw new IOException("Invalid auth configuration file");
        }
        config.withUsername(parts[0]);
        config.withPassword(parts[1]);
    }

    static String convertToHostname(String server) {
        String stripped = server;
        if (server.startsWith("http://")) {
            stripped = server.substring(7);
        } else if (server.startsWith("https://")) {
            stripped = server.substring(8);
        }
        String[] numParts = stripped.split("/", 2);
        return numParts[0];
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy