Search in sources :

Example 6 with DockerAuthConfig

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.dto.DockerAuthConfig in project devspaces-images by redhat-developer.

the class ImagePullSecretProvisioner method generateDockerCfg.

/**
 * Generates a dockercfg file with the authentication information contained in the given {@code
 * authConfigs} parameter.
 *
 * <p>The syntax of the produced <code>dockercfg</code> file is as follow:
 *
 * <pre><code>
 *   {
 *     "private.repo.1" : {
 *        "username": "theUsername1",
 *        "password": "thePassword1",
 *        "email": "theEmail1",
 *        "auth": "<base64 encoding of username:password>",
 *      },
 *     "private.repo.2" : {
 *        "username": "theUsername2",
 *        "password": "thePassword2",
 *        "email": "theEmail2",
 *        "auth": "<base64 encoding of username:password>",
 *      }
 *   }
 * </code></pre>
 */
private String generateDockerCfg(Map<String, DockerAuthConfig> authConfigs) throws InfrastructureException {
    try (StringWriter strWriter = new StringWriter();
        JsonWriter jsonWriter = new Gson().newJsonWriter(strWriter)) {
        Base64.Encoder encoder = Base64.getEncoder();
        jsonWriter.beginObject();
        for (Map.Entry<String, DockerAuthConfig> entry : authConfigs.entrySet()) {
            String name = entry.getKey();
            DockerAuthConfig dockerAuthConfig = entry.getValue();
            try {
                if (!name.startsWith("https://") && !name.startsWith("http://")) {
                    name = "https://" + name;
                }
                jsonWriter.name(name);
                jsonWriter.beginObject();
                jsonWriter.name("username");
                jsonWriter.value(dockerAuthConfig.getUsername());
                jsonWriter.name("password");
                jsonWriter.value(dockerAuthConfig.getPassword());
                jsonWriter.name("email");
                jsonWriter.value("email@email");
                String auth = dockerAuthConfig.getUsername() + ':' + dockerAuthConfig.getPassword();
                jsonWriter.name("auth");
                jsonWriter.value(encoder.encodeToString(auth.getBytes()));
                jsonWriter.endObject();
            } catch (IOException e) {
                throw new InfrastructureException("Unexpected exception occurred while building the 'ImagePullSecret' from private docker registry user preferences", e);
            }
        }
        jsonWriter.endObject();
        jsonWriter.flush();
        return strWriter.toString();
    } catch (IOException e) {
        throw new InfrastructureException(e);
    }
}
Also used : Base64(java.util.Base64) DockerAuthConfig(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.dto.DockerAuthConfig) StringWriter(java.io.StringWriter) Gson(com.google.gson.Gson) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) Map(java.util.Map) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Aggregations

DockerAuthConfig (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.dto.DockerAuthConfig)6 DockerAuthConfigs (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.dto.DockerAuthConfigs)4 Gson (com.google.gson.Gson)2 JsonWriter (com.google.gson.stream.JsonWriter)2 Secret (io.fabric8.kubernetes.api.model.Secret)2 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Base64 (java.util.Base64)2 Map (java.util.Map)2 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)2 Traced (org.eclipse.che.commons.annotation.Traced)2 Test (org.testng.annotations.Test)2