Commit 27095d90 authored by Phillip Webb's avatar Phillip Webb

Polish

parent 10ad53af
...@@ -45,18 +45,36 @@ public class Builder { ...@@ -45,18 +45,36 @@ public class Builder {
private final DockerApi docker; private final DockerApi docker;
/**
* Create a new builder instance.
*/
public Builder() { public Builder() {
this(BuildLog.toSystemOut()); this(BuildLog.toSystemOut());
} }
/**
* Create a new builder instance.
* @param dockerConfiguration the docker configuration
* @since 2.4.0
*/
public Builder(DockerConfiguration dockerConfiguration) { public Builder(DockerConfiguration dockerConfiguration) {
this(BuildLog.toSystemOut(), dockerConfiguration); this(BuildLog.toSystemOut(), dockerConfiguration);
} }
/**
* Create a new builder instance.
* @param log a logger used to record output
*/
public Builder(BuildLog log) { public Builder(BuildLog log) {
this(log, new DockerApi()); 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) { public Builder(BuildLog log, DockerConfiguration dockerConfiguration) {
this(log, new DockerApi(dockerConfiguration)); this(log, new DockerApi(dockerConfiguration));
} }
......
...@@ -74,7 +74,8 @@ public class DockerApi { ...@@ -74,7 +74,8 @@ public class DockerApi {
/** /**
* Create a new {@link DockerApi} instance. * 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) { public DockerApi(DockerConfiguration dockerConfiguration) {
this(HttpTransport.create(dockerConfiguration)); this(HttpTransport.create(dockerConfiguration));
......
...@@ -16,26 +16,18 @@ ...@@ -16,26 +16,18 @@
package org.springframework.boot.buildpack.platform.docker.configuration; 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. * Docker registry authentication configuration.
* *
* @author Scott Frederick * @author Scott Frederick
* @since 2.4.0 * @since 2.4.0
*/ */
public abstract class DockerRegistryAuthentication { public interface DockerRegistryAuthentication {
public String createAuthHeader() { /**
try { * Create the auth header that should be used for docker authentication.
return Base64Utils.encodeToUrlSafeString(SharedObjectMapper.get().writeValueAsBytes(this)); * @return the auth header
} */
catch (JsonProcessingException ex) { String createAuthHeader();
throw new IllegalStateException("Error creating Docker registry authentication header", ex);
}
}
} }
...@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; ...@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
* *
* @author Scott Frederick * @author Scott Frederick
*/ */
class DockerRegistryTokenAuthentication extends DockerRegistryAuthentication { class DockerRegistryTokenAuthentication extends JsonEncodedDockerRegistryAuthentication {
@JsonProperty("identitytoken") @JsonProperty("identitytoken")
private final String token; private final String token;
......
...@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; ...@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
* *
* @author Scott Frederick * @author Scott Frederick
*/ */
class DockerRegistryUserAuthentication extends DockerRegistryAuthentication { class DockerRegistryUserAuthentication extends JsonEncodedDockerRegistryAuthentication {
@JsonProperty @JsonProperty
private final String username; private final String username;
......
/*
* 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; ...@@ -37,6 +37,7 @@ import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration; 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.Content;
import org.springframework.boot.buildpack.platform.io.IOConsumer; import org.springframework.boot.buildpack.platform.io.IOConsumer;
import org.springframework.boot.buildpack.platform.json.SharedObjectMapper; import org.springframework.boot.buildpack.platform.json.SharedObjectMapper;
...@@ -122,11 +123,9 @@ abstract class HttpClientTransport implements HttpTransport { ...@@ -122,11 +123,9 @@ abstract class HttpClientTransport implements HttpTransport {
} }
private String buildRegistryAuthHeader(DockerConfiguration dockerConfiguration) { private String buildRegistryAuthHeader(DockerConfiguration dockerConfiguration) {
if (dockerConfiguration == null || dockerConfiguration.getRegistryAuthentication() == null) { DockerRegistryAuthentication authentication = (dockerConfiguration != null)
return null; ? dockerConfiguration.getRegistryAuthentication() : null;
} String authHeader = (authentication != null) ? authentication.createAuthHeader() : null;
String authHeader = dockerConfiguration.getRegistryAuthentication().createAuthHeader();
return (StringUtils.hasText(authHeader)) ? authHeader : null; return (StringUtils.hasText(authHeader)) ? authHeader : null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment