diff --git a/plugin/src/main/java/com/spotify/plugin/dockerfile/AbstractDockerMojo.java b/plugin/src/main/java/com/spotify/plugin/dockerfile/AbstractDockerMojo.java index c81928fb..fe895b7f 100644 --- a/plugin/src/main/java/com/spotify/plugin/dockerfile/AbstractDockerMojo.java +++ b/plugin/src/main/java/com/spotify/plugin/dockerfile/AbstractDockerMojo.java @@ -26,6 +26,7 @@ import com.google.common.io.Files; import com.spotify.docker.client.DefaultDockerClient; import com.spotify.docker.client.DockerClient; +import com.spotify.docker.client.DockerConfigReader; import com.spotify.docker.client.auth.ConfigFileRegistryAuthSupplier; import com.spotify.docker.client.auth.MultiRegistryAuthSupplier; import com.spotify.docker.client.auth.RegistryAuthSupplier; @@ -94,6 +95,18 @@ public String getFileName() { property = "dockerfile.dockerInfoDirectory", required = true) protected File dockerInfoDirectory; + /** + * Path to docker config file, if the default is not acceptable. + */ + @Parameter(property = "dockerfile.dockerConfigFile") + protected File dockerConfigFile; + + /** + * A maven server id, in order to use maven settings to supply server auth. + */ + @Parameter(defaultValue = "false", property = "dockerfile.useMavenSettingsForAuth") + protected boolean useMavenSettingsForAuth; + /** * Directory where test metadata will be written during build. */ @@ -407,7 +420,21 @@ private DockerClient openDockerClient() throws MojoExecutionException { @Nonnull private RegistryAuthSupplier createRegistryAuthSupplier() { final List suppliers = new ArrayList<>(); - suppliers.add(new ConfigFileRegistryAuthSupplier()); + + if (useMavenSettingsForAuth) { + suppliers.add(new MavenRegistryAuthSupplier(session.getSettings())); + } + + if (dockerConfigFile == null || "".equals(dockerConfigFile.getName())) { + suppliers.add(new ConfigFileRegistryAuthSupplier()); + } else { + suppliers.add( + new ConfigFileRegistryAuthSupplier( + new DockerConfigReader(), + dockerConfigFile.toPath() + ) + ); + } if (googleContainerRegistryEnabled) { try { diff --git a/plugin/src/main/java/com/spotify/plugin/dockerfile/MavenRegistryAuthSupplier.java b/plugin/src/main/java/com/spotify/plugin/dockerfile/MavenRegistryAuthSupplier.java new file mode 100644 index 00000000..7908a5ec --- /dev/null +++ b/plugin/src/main/java/com/spotify/plugin/dockerfile/MavenRegistryAuthSupplier.java @@ -0,0 +1,79 @@ +/*- + * -\-\- + * Dockerfile Maven Plugin + * -- + * Copyright (C) 2017 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ + +package com.spotify.plugin.dockerfile; + +import com.spotify.docker.client.ImageRef; +import com.spotify.docker.client.auth.RegistryAuthSupplier; +import com.spotify.docker.client.exceptions.DockerException; +import com.spotify.docker.client.messages.RegistryAuth; +import com.spotify.docker.client.messages.RegistryConfigs; +import java.util.HashMap; +import java.util.Map; +import org.apache.maven.settings.Server; +import org.apache.maven.settings.Settings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MavenRegistryAuthSupplier implements RegistryAuthSupplier { + + private static final Logger log = LoggerFactory.getLogger(MavenRegistryAuthSupplier.class); + + private final Settings settings; + + public MavenRegistryAuthSupplier(final Settings settings) { + this.settings = settings; + } + + @Override + public RegistryAuth authFor(final String imageName) throws DockerException { + final ImageRef ref = new ImageRef(imageName); + Server server = settings.getServer(ref.getRegistryName()); + if (server != null) { + return RegistryAuth.builder() + .username(server.getUsername()) + .password(server.getPassword()) + .build(); + } + log.warn("Did not find maven server configuration for docker server " + ref.getRegistryName()); + return null; + } + + @Override + public RegistryAuth authForSwarm() throws DockerException { + return null; + } + + @Override + public RegistryConfigs authForBuild() throws DockerException { + final Map allConfigs = new HashMap<>(); + for (Server server : settings.getServers()) { + allConfigs.put( + server.getId(), + RegistryAuth.builder() + .username(server.getUsername()) + .password(server.getPassword()) + .build() + ); + } + return RegistryConfigs.create(allConfigs); + } + +}