Skip to content

Commit a63a1fe

Browse files
committed
Merge branch '3.2.x' into 3.3.x
Closes gh-41641
2 parents 16ba01a + 47465f6 commit a63a1fe

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -246,9 +246,10 @@ public void execute() throws MojoExecutionException {
246246
private void buildImage() throws MojoExecutionException {
247247
Libraries libraries = getLibraries(Collections.emptySet());
248248
try {
249-
DockerConfiguration dockerConfiguration = (this.docker != null) ? this.docker.asDockerConfiguration()
250-
: new Docker().asDockerConfiguration();
251249
BuildRequest request = getBuildRequest(libraries);
250+
DockerConfiguration dockerConfiguration = (this.docker != null)
251+
? this.docker.asDockerConfiguration(request.isPublish())
252+
: new Docker().asDockerConfiguration(request.isPublish());
252253
Builder builder = new Builder(new MojoBuildLog(this::getLog), dockerConfiguration);
253254
builder.build(request);
254255
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Docker.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -140,14 +140,15 @@ void setPublishRegistry(DockerRegistry builderRegistry) {
140140
* Returns this configuration as a {@link DockerConfiguration} instance. This method
141141
* should only be called when the configuration is complete and will no longer be
142142
* changed.
143+
* @param publish whether the image should be published
143144
* @return the Docker configuration
144145
*/
145-
DockerConfiguration asDockerConfiguration() {
146+
DockerConfiguration asDockerConfiguration(boolean publish) {
146147
DockerConfiguration dockerConfiguration = new DockerConfiguration();
147148
dockerConfiguration = customizeHost(dockerConfiguration);
148149
dockerConfiguration = dockerConfiguration.withBindHostToBuilder(this.bindHostToBuilder);
149150
dockerConfiguration = customizeBuilderAuthentication(dockerConfiguration);
150-
dockerConfiguration = customizePublishAuthentication(dockerConfiguration);
151+
dockerConfiguration = customizePublishAuthentication(dockerConfiguration, publish);
151152
return dockerConfiguration;
152153
}
153154

@@ -180,7 +181,11 @@ private DockerConfiguration customizeBuilderAuthentication(DockerConfiguration d
180181
"Invalid Docker builder registry configuration, either token or username/password must be provided");
181182
}
182183

183-
private DockerConfiguration customizePublishAuthentication(DockerConfiguration dockerConfiguration) {
184+
private DockerConfiguration customizePublishAuthentication(DockerConfiguration dockerConfiguration,
185+
boolean publish) {
186+
if (!publish) {
187+
return dockerConfiguration;
188+
}
184189
if (this.publishRegistry == null || this.publishRegistry.isEmpty()) {
185190
return dockerConfiguration.withEmptyPublishRegistryAuthentication();
186191
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DockerTests.java

+40-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ class DockerTests {
3737
@Test
3838
void asDockerConfigurationWithDefaults() {
3939
Docker docker = new Docker();
40-
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
40+
DockerConfiguration dockerConfiguration = createDockerConfiguration(docker);
4141
assertThat(dockerConfiguration.getHost()).isNull();
4242
assertThat(dockerConfiguration.getBuilderRegistryAuthentication()).isNull();
4343
assertThat(decoded(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()))
@@ -53,14 +53,14 @@ void asDockerConfigurationWithHostConfiguration() {
5353
docker.setHost("docker.example.com");
5454
docker.setTlsVerify(true);
5555
docker.setCertPath("/tmp/ca-cert");
56-
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
56+
DockerConfiguration dockerConfiguration = createDockerConfiguration(docker);
5757
DockerHostConfiguration host = dockerConfiguration.getHost();
5858
assertThat(host.getAddress()).isEqualTo("docker.example.com");
5959
assertThat(host.isSecure()).isTrue();
6060
assertThat(host.getCertificatePath()).isEqualTo("/tmp/ca-cert");
6161
assertThat(host.getContext()).isNull();
6262
assertThat(dockerConfiguration.isBindHostToBuilder()).isFalse();
63-
assertThat(docker.asDockerConfiguration().getBuilderRegistryAuthentication()).isNull();
63+
assertThat(createDockerConfiguration(docker).getBuilderRegistryAuthentication()).isNull();
6464
assertThat(decoded(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()))
6565
.contains("\"username\" : \"\"")
6666
.contains("\"password\" : \"\"")
@@ -72,14 +72,14 @@ void asDockerConfigurationWithHostConfiguration() {
7272
void asDockerConfigurationWithContextConfiguration() {
7373
Docker docker = new Docker();
7474
docker.setContext("test-context");
75-
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
75+
DockerConfiguration dockerConfiguration = createDockerConfiguration(docker);
7676
DockerHostConfiguration host = dockerConfiguration.getHost();
7777
assertThat(host.getContext()).isEqualTo("test-context");
7878
assertThat(host.getAddress()).isNull();
7979
assertThat(host.isSecure()).isFalse();
8080
assertThat(host.getCertificatePath()).isNull();
8181
assertThat(dockerConfiguration.isBindHostToBuilder()).isFalse();
82-
assertThat(docker.asDockerConfiguration().getBuilderRegistryAuthentication()).isNull();
82+
assertThat(createDockerConfiguration(docker).getBuilderRegistryAuthentication()).isNull();
8383
assertThat(decoded(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()))
8484
.contains("\"username\" : \"\"")
8585
.contains("\"password\" : \"\"")
@@ -92,7 +92,7 @@ void asDockerConfigurationWithHostAndContextFails() {
9292
Docker docker = new Docker();
9393
docker.setContext("test-context");
9494
docker.setHost("docker.example.com");
95-
assertThatIllegalArgumentException().isThrownBy(docker::asDockerConfiguration)
95+
assertThatIllegalArgumentException().isThrownBy(() -> createDockerConfiguration(docker))
9696
.withMessageContaining("Invalid Docker configuration");
9797
}
9898

@@ -103,13 +103,13 @@ void asDockerConfigurationWithBindHostToBuilder() {
103103
docker.setTlsVerify(true);
104104
docker.setCertPath("/tmp/ca-cert");
105105
docker.setBindHostToBuilder(true);
106-
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
106+
DockerConfiguration dockerConfiguration = createDockerConfiguration(docker);
107107
DockerHostConfiguration host = dockerConfiguration.getHost();
108108
assertThat(host.getAddress()).isEqualTo("docker.example.com");
109109
assertThat(host.isSecure()).isTrue();
110110
assertThat(host.getCertificatePath()).isEqualTo("/tmp/ca-cert");
111111
assertThat(dockerConfiguration.isBindHostToBuilder()).isTrue();
112-
assertThat(docker.asDockerConfiguration().getBuilderRegistryAuthentication()).isNull();
112+
assertThat(createDockerConfiguration(docker).getBuilderRegistryAuthentication()).isNull();
113113
assertThat(decoded(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()))
114114
.contains("\"username\" : \"\"")
115115
.contains("\"password\" : \"\"")
@@ -124,7 +124,7 @@ void asDockerConfigurationWithUserAuth() {
124124
new Docker.DockerRegistry("user1", "secret1", "https://docker1.example.com", "[email protected]"));
125125
docker.setPublishRegistry(
126126
new Docker.DockerRegistry("user2", "secret2", "https://docker2.example.com", "[email protected]"));
127-
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
127+
DockerConfiguration dockerConfiguration = createDockerConfiguration(docker);
128128
assertThat(decoded(dockerConfiguration.getBuilderRegistryAuthentication().getAuthHeader()))
129129
.contains("\"username\" : \"user1\"")
130130
.contains("\"password\" : \"secret1\"")
@@ -142,7 +142,7 @@ void asDockerConfigurationWithIncompleteBuilderUserAuthFails() {
142142
Docker docker = new Docker();
143143
docker.setBuilderRegistry(
144144
new Docker.DockerRegistry("user", null, "https://docker.example.com", "[email protected]"));
145-
assertThatIllegalArgumentException().isThrownBy(docker::asDockerConfiguration)
145+
assertThatIllegalArgumentException().isThrownBy(() -> createDockerConfiguration(docker))
146146
.withMessageContaining("Invalid Docker builder registry configuration");
147147
}
148148

@@ -151,16 +151,25 @@ void asDockerConfigurationWithIncompletePublishUserAuthFails() {
151151
Docker docker = new Docker();
152152
docker.setPublishRegistry(
153153
new Docker.DockerRegistry("user", null, "https://docker.example.com", "[email protected]"));
154-
assertThatIllegalArgumentException().isThrownBy(docker::asDockerConfiguration)
154+
assertThatIllegalArgumentException().isThrownBy(() -> createDockerConfiguration(docker))
155155
.withMessageContaining("Invalid Docker publish registry configuration");
156156
}
157157

158+
@Test
159+
void asDockerConfigurationWithIncompletePublishUserAuthDoesNotFailIfPublishIsDisabled() {
160+
Docker docker = new Docker();
161+
docker.setPublishRegistry(
162+
new Docker.DockerRegistry("user", null, "https://docker.example.com", "[email protected]"));
163+
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration(false);
164+
assertThat(dockerConfiguration.getPublishRegistryAuthentication()).isNull();
165+
}
166+
158167
@Test
159168
void asDockerConfigurationWithTokenAuth() {
160169
Docker docker = new Docker();
161170
docker.setBuilderRegistry(new Docker.DockerRegistry("token1"));
162171
docker.setPublishRegistry(new Docker.DockerRegistry("token2"));
163-
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration();
172+
DockerConfiguration dockerConfiguration = createDockerConfiguration(docker);
164173
assertThat(decoded(dockerConfiguration.getBuilderRegistryAuthentication().getAuthHeader()))
165174
.contains("\"identitytoken\" : \"token1\"");
166175
assertThat(decoded(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()))
@@ -175,10 +184,27 @@ void asDockerConfigurationWithUserAndTokenAuthFails() {
175184
dockerRegistry.setToken("token");
176185
Docker docker = new Docker();
177186
docker.setBuilderRegistry(dockerRegistry);
178-
assertThatIllegalArgumentException().isThrownBy(docker::asDockerConfiguration)
187+
assertThatIllegalArgumentException().isThrownBy(() -> createDockerConfiguration(docker))
179188
.withMessageContaining("Invalid Docker builder registry configuration");
180189
}
181190

191+
@Test
192+
void asDockerConfigurationWithUserAndTokenAuthDoesNotFailIfPublishingIsDisabled() {
193+
Docker.DockerRegistry dockerRegistry = new Docker.DockerRegistry();
194+
dockerRegistry.setUsername("user");
195+
dockerRegistry.setPassword("secret");
196+
dockerRegistry.setToken("token");
197+
Docker docker = new Docker();
198+
docker.setPublishRegistry(dockerRegistry);
199+
DockerConfiguration dockerConfiguration = docker.asDockerConfiguration(false);
200+
assertThat(dockerConfiguration.getPublishRegistryAuthentication()).isNull();
201+
}
202+
203+
private DockerConfiguration createDockerConfiguration(Docker docker) {
204+
return docker.asDockerConfiguration(true);
205+
206+
}
207+
182208
String decoded(String value) {
183209
return new String(Base64.getDecoder().decode(value));
184210
}

0 commit comments

Comments
 (0)