Skip to content

Commit

Permalink
Polish "Support authentication to private Docker registry"
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick committed Sep 16, 2020
1 parent e8f555e commit 86fa814
Show file tree
Hide file tree
Showing 40 changed files with 1,192 additions and 746 deletions.
Expand Up @@ -54,7 +54,7 @@ public Builder(DockerConfiguration dockerConfiguration) {
}

public Builder(BuildLog log) {
this(log, new DockerApi(new DockerConfiguration()));
this(log, new DockerApi());
}

public Builder(BuildLog log, DockerConfiguration dockerConfiguration) {
Expand Down
Expand Up @@ -24,12 +24,9 @@
import java.util.Collections;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicHeader;

import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration;
import org.springframework.boot.buildpack.platform.docker.configuration.DockerRegistryConfiguration;
import org.springframework.boot.buildpack.platform.docker.transport.HttpTransport;
import org.springframework.boot.buildpack.platform.docker.transport.HttpTransport.Response;
import org.springframework.boot.buildpack.platform.docker.type.ContainerConfig;
Expand Down Expand Up @@ -72,15 +69,15 @@ public class DockerApi {
* Create a new {@link DockerApi} instance.
*/
public DockerApi() {
this(new DockerConfiguration());
this(DockerConfiguration.withDefaults());
}

/**
* Create a new {@link DockerApi} instance.
* @param dockerConfiguration the Docker configuration options.
* @param dockerConfiguration the Docker configuration options
*/
public DockerApi(DockerConfiguration dockerConfiguration) {
this(HttpTransport.create(createDockerEngineAuthenticationHeaders(dockerConfiguration)));
this(HttpTransport.create(dockerConfiguration));
}

/**
Expand All @@ -96,22 +93,6 @@ public DockerApi(DockerConfiguration dockerConfiguration) {
this.volume = new VolumeApi();
}

static Collection<Header> createDockerEngineAuthenticationHeaders(DockerConfiguration dockerConfiguration) {
Assert.notNull(dockerConfiguration, "Docker configuration must not be null");

DockerRegistryConfiguration dockerRegistryConfiguration = dockerConfiguration.getDockerRegistryConfiguration();
if (dockerRegistryConfiguration == null) {
return Collections.emptyList();
}

String dockerRegistryAuthToken = dockerRegistryConfiguration.createDockerRegistryAuthToken();
if (StringUtils.isEmpty(dockerRegistryAuthToken)) {
return Collections.emptyList();
}

return Arrays.asList(new BasicHeader("X-Registry-Auth", dockerRegistryAuthToken));
}

private HttpTransport http() {
return this.http;
}
Expand Down
Expand Up @@ -16,34 +16,41 @@

package org.springframework.boot.buildpack.platform.docker.configuration;

import org.springframework.util.Assert;

/**
* Docker configuration options.
*
* @author Wei Jiang
* @author Scott Frederick
* @since 2.4.0
*/
public class DockerConfiguration {
public final class DockerConfiguration {

private final DockerRegistryAuthentication authentication;

/**
* The docker registry configuration.
*/
private DockerRegistryConfiguration dockerRegistryConfiguration;
private DockerConfiguration(DockerRegistryAuthentication authentication) {
this.authentication = authentication;
}

public DockerConfiguration() {
super();
public DockerRegistryAuthentication getRegistryAuthentication() {
return this.authentication;
}

public DockerConfiguration(DockerRegistryConfiguration dockerRegistryConfiguration) {
super();
this.dockerRegistryConfiguration = dockerRegistryConfiguration;
public static DockerConfiguration withDefaults() {
return new DockerConfiguration(null);
}

public DockerRegistryConfiguration getDockerRegistryConfiguration() {
return this.dockerRegistryConfiguration;
public static DockerConfiguration withRegistryTokenAuthentication(String token) {
Assert.notNull(token, "Token must not be null");
return new DockerConfiguration(new DockerRegistryTokenAuthentication(token));
}

public void setDockerRegistryConfiguration(DockerRegistryConfiguration dockerRegistryConfiguration) {
this.dockerRegistryConfiguration = dockerRegistryConfiguration;
public static DockerConfiguration withRegistryUserAuthentication(String username, String password, String url,
String email) {
Assert.notNull(username, "Username must not be null");
Assert.notNull(password, "Password must not be null");
return new DockerConfiguration(new DockerRegistryUserAuthentication(username, password, url, email));
}

}
@@ -0,0 +1,41 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.buildpack.platform.docker.configuration;

import com.fasterxml.jackson.core.JsonProcessingException;

import org.springframework.boot.buildpack.platform.json.SharedObjectMapper;
import org.springframework.util.Base64Utils;

/**
* Docker registry authentication configuration.
*
* @author Scott Frederick
* @since 2.4.0
*/
public abstract class DockerRegistryAuthentication {

public String createAuthHeader() {
try {
return Base64Utils.encodeToUrlSafeString(SharedObjectMapper.get().writeValueAsBytes(this));
}
catch (JsonProcessingException ex) {
throw new IllegalStateException("Error creating Docker registry authentication header", ex);
}
}

}

This file was deleted.

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.buildpack.platform.docker.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Docker registry authentication configuration using a token.
*
* @author Scott Frederick
*/
class DockerRegistryTokenAuthentication extends DockerRegistryAuthentication {

@JsonProperty("identitytoken")
private final String token;

DockerRegistryTokenAuthentication(String token) {
this.token = token;
}

String getToken() {
return this.token;
}

}
@@ -0,0 +1,63 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.buildpack.platform.docker.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Docker registry authentication configuration using user credentials.
*
* @author Scott Frederick
*/
class DockerRegistryUserAuthentication extends DockerRegistryAuthentication {

@JsonProperty
private final String username;

@JsonProperty
private final String password;

@JsonProperty("serveraddress")
private final String url;

@JsonProperty
private final String email;

DockerRegistryUserAuthentication(String username, String password, String url, String email) {
this.username = username;
this.password = password;
this.url = url;
this.email = email;
}

String getUsername() {
return this.username;
}

String getPassword() {
return this.password;
}

String getUrl() {
return this.url;
}

String getEmail() {
return this.email;
}

}

0 comments on commit 86fa814

Please sign in to comment.