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:
Scott Frederick
2020-06-17 12:21:54 -05:00
parent 63423e7d71
commit 28643e4d2d
9 changed files with 367 additions and 57 deletions

View File

@@ -135,6 +135,16 @@ class BootBuildImageIntegrationTests {
assertThat(result.getOutput()).containsPattern("Builder lifecycle '.*' failed with status code");
}
@TestTemplate
void failsWithInvalidImageName() {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.buildAndFail("bootBuildImage", "--imageName=example/Invalid-Image-Name");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.FAILED);
assertThat(result.getOutput()).containsPattern("Unable to parse image reference")
.containsPattern("example/Invalid-Image-Name");
}
private void writeMainClass() {
File examplePackage = new File(this.gradleBuild.getProjectDir(), "src/main/java/example");
examplePackage.mkdirs();