Polish
This commit is contained in:
@@ -45,18 +45,36 @@ public class Builder {
|
||||
|
||||
private final DockerApi docker;
|
||||
|
||||
/**
|
||||
* Create a new builder instance.
|
||||
*/
|
||||
public Builder() {
|
||||
this(BuildLog.toSystemOut());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance.
|
||||
* @param dockerConfiguration the docker configuration
|
||||
* @since 2.4.0
|
||||
*/
|
||||
public Builder(DockerConfiguration dockerConfiguration) {
|
||||
this(BuildLog.toSystemOut(), dockerConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance.
|
||||
* @param log a logger used to record output
|
||||
*/
|
||||
public Builder(BuildLog log) {
|
||||
this(log, new DockerApi());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance.
|
||||
* @param log a logger used to record output
|
||||
* @param dockerConfiguration the docker configuration
|
||||
* @since 2.4.0
|
||||
*/
|
||||
public Builder(BuildLog log, DockerConfiguration dockerConfiguration) {
|
||||
this(log, new DockerApi(dockerConfiguration));
|
||||
}
|
||||
|
||||
@@ -74,7 +74,8 @@ public class DockerApi {
|
||||
|
||||
/**
|
||||
* Create a new {@link DockerApi} instance.
|
||||
* @param dockerConfiguration the Docker configuration options
|
||||
* @param dockerConfiguration the docker configuration
|
||||
* @since 2.4.0
|
||||
*/
|
||||
public DockerApi(DockerConfiguration dockerConfiguration) {
|
||||
this(HttpTransport.create(dockerConfiguration));
|
||||
|
||||
@@ -16,26 +16,18 @@
|
||||
|
||||
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 interface 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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Create the auth header that should be used for docker authentication.
|
||||
* @return the auth header
|
||||
*/
|
||||
String createAuthHeader();
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class DockerRegistryTokenAuthentication extends DockerRegistryAuthentication {
|
||||
class DockerRegistryTokenAuthentication extends JsonEncodedDockerRegistryAuthentication {
|
||||
|
||||
@JsonProperty("identitytoken")
|
||||
private final String token;
|
||||
|
||||
@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class DockerRegistryUserAuthentication extends DockerRegistryAuthentication {
|
||||
class DockerRegistryUserAuthentication extends JsonEncodedDockerRegistryAuthentication {
|
||||
|
||||
@JsonProperty
|
||||
private final String username;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* {@link DockerRegistryAuthentication} that uses creates a Base64 encoded auth header
|
||||
* value based on the JSON created from the instance.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class JsonEncodedDockerRegistryAuthentication implements DockerRegistryAuthentication {
|
||||
|
||||
@Override
|
||||
public String createAuthHeader() {
|
||||
try {
|
||||
return Base64Utils.encodeToUrlSafeString(SharedObjectMapper.get().writeValueAsBytes(this));
|
||||
}
|
||||
catch (JsonProcessingException ex) {
|
||||
throw new IllegalStateException("Error creating Docker registry authentication header", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import org.apache.http.entity.AbstractHttpEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration;
|
||||
import org.springframework.boot.buildpack.platform.docker.configuration.DockerRegistryAuthentication;
|
||||
import org.springframework.boot.buildpack.platform.io.Content;
|
||||
import org.springframework.boot.buildpack.platform.io.IOConsumer;
|
||||
import org.springframework.boot.buildpack.platform.json.SharedObjectMapper;
|
||||
@@ -122,11 +123,9 @@ abstract class HttpClientTransport implements HttpTransport {
|
||||
}
|
||||
|
||||
private String buildRegistryAuthHeader(DockerConfiguration dockerConfiguration) {
|
||||
if (dockerConfiguration == null || dockerConfiguration.getRegistryAuthentication() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String authHeader = dockerConfiguration.getRegistryAuthentication().createAuthHeader();
|
||||
DockerRegistryAuthentication authentication = (dockerConfiguration != null)
|
||||
? dockerConfiguration.getRegistryAuthentication() : null;
|
||||
String authHeader = (authentication != null) ? authentication.createAuthHeader() : null;
|
||||
return (StringUtils.hasText(authHeader)) ? authHeader : null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user