Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Fix for Issue 64 #65

Merged
merged 6 commits into from
Sep 5, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -407,7 +420,21 @@ private DockerClient openDockerClient() throws MojoExecutionException {
@Nonnull
private RegistryAuthSupplier createRegistryAuthSupplier() {
final List<RegistryAuthSupplier> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, RegistryAuth> 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);
}

}