From fadc58679dbb37e4f34fbb77d3a9af20c5b04404 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 4 Sep 2023 08:17:44 +0100 Subject: [PATCH] Speed up ImageReference.of when path contains upper-case chars Closes gh-35657 --- .../platform/docker/type/ImageReference.java | 12 +++++++++++- .../platform/docker/type/ImageReferenceTests.java | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageReference.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageReference.java index f562d22f83..01a918895a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageReference.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageReference.java @@ -17,6 +17,7 @@ package org.springframework.boot.buildpack.platform.docker.type; import java.io.File; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -260,7 +261,8 @@ public final class ImageReference { path = path.substring(0, tagSplit) + remainder; } } - Assert.isTrue(Regex.PATH.matcher(path).matches(), + + Assert.isTrue(isLowerCase(path) && matchesPathRegex(path), () -> "Unable to parse image reference \"" + value + "\". " + "Image reference must be in the form '[domainHost:port/][path/]name[:tag][@digest]', " + "with 'path' and 'name' containing only [a-z0-9][.][_][-]"); @@ -268,6 +270,14 @@ public final class ImageReference { return new ImageReference(name, tag, digest); } + private static boolean isLowerCase(String path) { + return path.toLowerCase(Locale.ENGLISH).equals(path); + } + + private static boolean matchesPathRegex(String path) { + return Regex.PATH.matcher(path).matches(); + } + /** * Create a new {@link ImageReference} from the given {@link ImageName}. * @param name the image name diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceTests.java index 7c0c7918e8..8d384da2ba 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceTests.java @@ -180,6 +180,14 @@ class ImageReferenceTests { .withMessageContaining("Unable to parse image reference"); } + @Test + void ofWhenContainsUpperCaseThrowsException() { + assertThatIllegalArgumentException() + .isThrownBy(() -> ImageReference + .of("europe-west1-docker.pkg.dev/aaaaaa-bbbbb-123456/docker-registry/bootBuildImage:0.0.1")) + .withMessageContaining("Unable to parse image reference"); + } + @Test void forJarFile() { assertForJarFile("spring-boot.2.0.0.BUILD-SNAPSHOT.jar", "library/spring-boot", "2.0.0.BUILD-SNAPSHOT");