Skip to content

Commit 86fa814

Browse files
Polish "Support authentication to private Docker registry"
See gh-22972
1 parent e8f555e commit 86fa814

File tree

40 files changed

+1192
-746
lines changed

40 files changed

+1192
-746
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/Builder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Builder(DockerConfiguration dockerConfiguration) {
5454
}
5555

5656
public Builder(BuildLog log) {
57-
this(log, new DockerApi(new DockerConfiguration()));
57+
this(log, new DockerApi());
5858
}
5959

6060
public Builder(BuildLog log, DockerConfiguration dockerConfiguration) {

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/DockerApi.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@
2424
import java.util.Collections;
2525
import java.util.List;
2626

27-
import org.apache.http.Header;
2827
import org.apache.http.client.utils.URIBuilder;
29-
import org.apache.http.message.BasicHeader;
3028

3129
import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration;
32-
import org.springframework.boot.buildpack.platform.docker.configuration.DockerRegistryConfiguration;
3330
import org.springframework.boot.buildpack.platform.docker.transport.HttpTransport;
3431
import org.springframework.boot.buildpack.platform.docker.transport.HttpTransport.Response;
3532
import org.springframework.boot.buildpack.platform.docker.type.ContainerConfig;
@@ -72,15 +69,15 @@ public class DockerApi {
7269
* Create a new {@link DockerApi} instance.
7370
*/
7471
public DockerApi() {
75-
this(new DockerConfiguration());
72+
this(DockerConfiguration.withDefaults());
7673
}
7774

7875
/**
7976
* Create a new {@link DockerApi} instance.
80-
* @param dockerConfiguration the Docker configuration options.
77+
* @param dockerConfiguration the Docker configuration options
8178
*/
8279
public DockerApi(DockerConfiguration dockerConfiguration) {
83-
this(HttpTransport.create(createDockerEngineAuthenticationHeaders(dockerConfiguration)));
80+
this(HttpTransport.create(dockerConfiguration));
8481
}
8582

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

99-
static Collection<Header> createDockerEngineAuthenticationHeaders(DockerConfiguration dockerConfiguration) {
100-
Assert.notNull(dockerConfiguration, "Docker configuration must not be null");
101-
102-
DockerRegistryConfiguration dockerRegistryConfiguration = dockerConfiguration.getDockerRegistryConfiguration();
103-
if (dockerRegistryConfiguration == null) {
104-
return Collections.emptyList();
105-
}
106-
107-
String dockerRegistryAuthToken = dockerRegistryConfiguration.createDockerRegistryAuthToken();
108-
if (StringUtils.isEmpty(dockerRegistryAuthToken)) {
109-
return Collections.emptyList();
110-
}
111-
112-
return Arrays.asList(new BasicHeader("X-Registry-Auth", dockerRegistryAuthToken));
113-
}
114-
11596
private HttpTransport http() {
11697
return this.http;
11798
}

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerConfiguration.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,41 @@
1616

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

19+
import org.springframework.util.Assert;
20+
1921
/**
2022
* Docker configuration options.
2123
*
2224
* @author Wei Jiang
25+
* @author Scott Frederick
2326
* @since 2.4.0
2427
*/
25-
public class DockerConfiguration {
28+
public final class DockerConfiguration {
29+
30+
private final DockerRegistryAuthentication authentication;
2631

27-
/**
28-
* The docker registry configuration.
29-
*/
30-
private DockerRegistryConfiguration dockerRegistryConfiguration;
32+
private DockerConfiguration(DockerRegistryAuthentication authentication) {
33+
this.authentication = authentication;
34+
}
3135

32-
public DockerConfiguration() {
33-
super();
36+
public DockerRegistryAuthentication getRegistryAuthentication() {
37+
return this.authentication;
3438
}
3539

36-
public DockerConfiguration(DockerRegistryConfiguration dockerRegistryConfiguration) {
37-
super();
38-
this.dockerRegistryConfiguration = dockerRegistryConfiguration;
40+
public static DockerConfiguration withDefaults() {
41+
return new DockerConfiguration(null);
3942
}
4043

41-
public DockerRegistryConfiguration getDockerRegistryConfiguration() {
42-
return this.dockerRegistryConfiguration;
44+
public static DockerConfiguration withRegistryTokenAuthentication(String token) {
45+
Assert.notNull(token, "Token must not be null");
46+
return new DockerConfiguration(new DockerRegistryTokenAuthentication(token));
4347
}
4448

45-
public void setDockerRegistryConfiguration(DockerRegistryConfiguration dockerRegistryConfiguration) {
46-
this.dockerRegistryConfiguration = dockerRegistryConfiguration;
49+
public static DockerConfiguration withRegistryUserAuthentication(String username, String password, String url,
50+
String email) {
51+
Assert.notNull(username, "Username must not be null");
52+
Assert.notNull(password, "Password must not be null");
53+
return new DockerConfiguration(new DockerRegistryUserAuthentication(username, password, url, email));
4754
}
4855

4956
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.buildpack.platform.docker.configuration;
18+
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
21+
import org.springframework.boot.buildpack.platform.json.SharedObjectMapper;
22+
import org.springframework.util.Base64Utils;
23+
24+
/**
25+
* Docker registry authentication configuration.
26+
*
27+
* @author Scott Frederick
28+
* @since 2.4.0
29+
*/
30+
public abstract class DockerRegistryAuthentication {
31+
32+
public String createAuthHeader() {
33+
try {
34+
return Base64Utils.encodeToUrlSafeString(SharedObjectMapper.get().writeValueAsBytes(this));
35+
}
36+
catch (JsonProcessingException ex) {
37+
throw new IllegalStateException("Error creating Docker registry authentication header", ex);
38+
}
39+
}
40+
41+
}

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryConfiguration.java

Lines changed: 0 additions & 129 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.buildpack.platform.docker.configuration;
18+
19+
import com.fasterxml.jackson.annotation.JsonProperty;
20+
21+
/**
22+
* Docker registry authentication configuration using a token.
23+
*
24+
* @author Scott Frederick
25+
*/
26+
class DockerRegistryTokenAuthentication extends DockerRegistryAuthentication {
27+
28+
@JsonProperty("identitytoken")
29+
private final String token;
30+
31+
DockerRegistryTokenAuthentication(String token) {
32+
this.token = token;
33+
}
34+
35+
String getToken() {
36+
return this.token;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.buildpack.platform.docker.configuration;
18+
19+
import com.fasterxml.jackson.annotation.JsonProperty;
20+
21+
/**
22+
* Docker registry authentication configuration using user credentials.
23+
*
24+
* @author Scott Frederick
25+
*/
26+
class DockerRegistryUserAuthentication extends DockerRegistryAuthentication {
27+
28+
@JsonProperty
29+
private final String username;
30+
31+
@JsonProperty
32+
private final String password;
33+
34+
@JsonProperty("serveraddress")
35+
private final String url;
36+
37+
@JsonProperty
38+
private final String email;
39+
40+
DockerRegistryUserAuthentication(String username, String password, String url, String email) {
41+
this.username = username;
42+
this.password = password;
43+
this.url = url;
44+
this.email = email;
45+
}
46+
47+
String getUsername() {
48+
return this.username;
49+
}
50+
51+
String getPassword() {
52+
return this.password;
53+
}
54+
55+
String getUrl() {
56+
return this.url;
57+
}
58+
59+
String getEmail() {
60+
return this.email;
61+
}
62+
63+
}

0 commit comments

Comments
 (0)