Validate image references before passing to CNB builder
Prior to this commit, an image name or run image name derived from the project name or provided by the user would be passed to the CNB builder without validation by the Maven plugin build-image goal or Gradle plugin bootBuildImage task. This could lead to error messages from the plugins that are difficult to understand and diagnose. This commit makes parsing of the image names more strict, based on the grammar implemented by the Docker go library. This provides validation of the image names before passing them to the builder, with a more descriptive error message when parsing and validation fails. Fixes gh-21495
This commit is contained in:
@@ -100,9 +100,9 @@ public class BuildRequestTests {
|
||||
@Test
|
||||
void withBuilderWhenHasDigestUpdatesBuilder() throws IOException {
|
||||
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar")).withBuilder(ImageReference
|
||||
.of("spring/builder:@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d"));
|
||||
.of("spring/builder@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d"));
|
||||
assertThat(request.getBuilder().toString()).isEqualTo(
|
||||
"docker.io/spring/builder:@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
|
||||
"docker.io/spring/builder@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -115,9 +115,9 @@ public class BuildRequestTests {
|
||||
@Test
|
||||
void withRunImageWhenHasDigestUpdatesRunImage() throws IOException {
|
||||
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar")).withRunImage(ImageReference
|
||||
.of("example.com/custom/run-image:@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d"));
|
||||
.of("example.com/custom/run-image@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d"));
|
||||
assertThat(request.getRunImage().toString()).isEqualTo(
|
||||
"example.com/custom/run-image:@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
|
||||
"example.com/custom/run-image@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -115,7 +115,7 @@ class BuilderTests {
|
||||
given(docker.image().pull(eq(ImageReference.of(BuildRequest.DEFAULT_BUILDER_IMAGE_NAME)), any()))
|
||||
.willAnswer(withPulledImage(builderImage));
|
||||
given(docker.image().pull(eq(ImageReference.of(
|
||||
"docker.io/cloudfoundry/run:@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d")),
|
||||
"docker.io/cloudfoundry/run@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d")),
|
||||
any())).willAnswer(withPulledImage(runImage));
|
||||
Builder builder = new Builder(BuildLog.to(out), docker);
|
||||
BuildRequest request = getTestRequest();
|
||||
|
||||
@@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
* Tests for {@link ImageName}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class ImageNameTests {
|
||||
|
||||
@@ -99,11 +100,13 @@ class ImageNameTests {
|
||||
void hashCodeAndEquals() {
|
||||
ImageName n1 = ImageName.of("ubuntu");
|
||||
ImageName n2 = ImageName.of("library/ubuntu");
|
||||
ImageName n3 = ImageName.of("docker.io/library/ubuntu");
|
||||
ImageName n4 = ImageName.of("index.docker.io/library/ubuntu");
|
||||
ImageName n5 = ImageName.of("alpine");
|
||||
assertThat(n1.hashCode()).isEqualTo(n2.hashCode()).isEqualTo(n3.hashCode()).isEqualTo(n4.hashCode());
|
||||
assertThat(n1).isEqualTo(n1).isEqualTo(n2).isEqualTo(n3).isEqualTo(n4).isNotEqualTo(n5);
|
||||
ImageName n3 = ImageName.of("docker.io/ubuntu");
|
||||
ImageName n4 = ImageName.of("docker.io/library/ubuntu");
|
||||
ImageName n5 = ImageName.of("index.docker.io/library/ubuntu");
|
||||
ImageName n6 = ImageName.of("alpine");
|
||||
assertThat(n1.hashCode()).isEqualTo(n2.hashCode()).isEqualTo(n3.hashCode()).isEqualTo(n4.hashCode())
|
||||
.isEqualTo(n5.hashCode());
|
||||
assertThat(n1).isEqualTo(n1).isEqualTo(n2).isEqualTo(n3).isEqualTo(n4).isNotEqualTo(n6);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* 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.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link ImageReferenceParser}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class ImageReferenceParserTests {
|
||||
|
||||
@Test
|
||||
void unableToParseWithUppercaseInName() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> ImageReferenceParser.of("Test"))
|
||||
.withMessageContaining("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesName() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("ubuntu");
|
||||
assertThat(parser.getDomain()).isNull();
|
||||
assertThat(parser.getName()).isEqualTo("ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesNameWithPath() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("library/ubuntu");
|
||||
assertThat(parser.getDomain()).isNull();
|
||||
assertThat(parser.getName()).isEqualTo("library/ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesNameWithLongPath() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("path1/path2/path3/ubuntu");
|
||||
assertThat(parser.getDomain()).isNull();
|
||||
assertThat(parser.getName()).isEqualTo("path1/path2/path3/ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesDomainAndNameWithPath() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("repo.example.com/library/ubuntu");
|
||||
assertThat(parser.getDomain()).isEqualTo("repo.example.com");
|
||||
assertThat(parser.getName()).isEqualTo("library/ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesDomainWithPortAndNameWithPath() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("repo.example.com:8080/library/ubuntu");
|
||||
assertThat(parser.getDomain()).isEqualTo("repo.example.com:8080");
|
||||
assertThat(parser.getName()).isEqualTo("library/ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesSimpleDomainWithPortAndName() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("repo:8080/ubuntu");
|
||||
assertThat(parser.getDomain()).isEqualTo("repo:8080");
|
||||
assertThat(parser.getName()).isEqualTo("ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesSimpleDomainWithPortAndNameWithPath() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("repo:8080/library/ubuntu");
|
||||
assertThat(parser.getDomain()).isEqualTo("repo:8080");
|
||||
assertThat(parser.getName()).isEqualTo("library/ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesLocalhostDomainAndName() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("localhost/ubuntu");
|
||||
assertThat(parser.getDomain()).isEqualTo("localhost");
|
||||
assertThat(parser.getName()).isEqualTo("ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesLocalhostDomainAndNameWithPath() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("localhost/library/ubuntu");
|
||||
assertThat(parser.getDomain()).isEqualTo("localhost");
|
||||
assertThat(parser.getName()).isEqualTo("library/ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesNameAndTag() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("ubuntu:v1");
|
||||
assertThat(parser.getDomain()).isNull();
|
||||
assertThat(parser.getName()).isEqualTo("ubuntu");
|
||||
assertThat(parser.getTag()).isEqualTo("v1");
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesNameAndDigest() {
|
||||
ImageReferenceParser parser = ImageReferenceParser
|
||||
.of("ubuntu@sha256:47bfdb88c3ae13e488167607973b7688f69d9e8c142c2045af343ec199649c09");
|
||||
assertThat(parser.getDomain()).isNull();
|
||||
assertThat(parser.getName()).isEqualTo("ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest())
|
||||
.isEqualTo("sha256:47bfdb88c3ae13e488167607973b7688f69d9e8c142c2045af343ec199649c09");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesReferenceWithTag() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of("repo.example.com:8080/library/ubuntu:v1");
|
||||
assertThat(parser.getDomain()).isEqualTo("repo.example.com:8080");
|
||||
assertThat(parser.getName()).isEqualTo("library/ubuntu");
|
||||
assertThat(parser.getTag()).isEqualTo("v1");
|
||||
assertThat(parser.getDigest()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesReferenceWithDigest() {
|
||||
ImageReferenceParser parser = ImageReferenceParser.of(
|
||||
"repo.example.com:8080/library/ubuntu@sha256:47bfdb88c3ae13e488167607973b7688f69d9e8c142c2045af343ec199649c09");
|
||||
assertThat(parser.getDomain()).isEqualTo("repo.example.com:8080");
|
||||
assertThat(parser.getName()).isEqualTo("library/ubuntu");
|
||||
assertThat(parser.getTag()).isNull();
|
||||
assertThat(parser.getDigest())
|
||||
.isEqualTo("sha256:47bfdb88c3ae13e488167607973b7688f69d9e8c142c2045af343ec199649c09");
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user