Skip to content

Commit

Permalink
Merge branch '2.4.x'
Browse files Browse the repository at this point in the history
Closes gh-26703
  • Loading branch information
philwebb committed May 28, 2021
2 parents 54c3194 + 49d3ecc commit d3f0f04
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 47 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -16,9 +16,6 @@

package org.springframework.boot.buildpack.platform.docker.type;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.Assert;

/**
Expand All @@ -32,8 +29,6 @@
*/
public class ImageName {

private static final Pattern PATTERN = Regex.IMAGE_NAME.compile();

private static final String DEFAULT_DOMAIN = "docker.io";

private static final String OFFICIAL_REPOSITORY_NAME = "library";
Expand Down Expand Up @@ -132,12 +127,22 @@ private String getNameWithDefaultPath(String domain, String name) {
*/
public static ImageName of(String value) {
Assert.hasText(value, "Value must not be empty");
Matcher matcher = PATTERN.matcher(value);
Assert.isTrue(matcher.matches(),
String domain = parseDomain(value);
String path = (domain != null) ? value.substring(domain.length() + 1) : value;
Assert.isTrue(Regex.PATH.matcher(path).matches(),
() -> "Unable to parse name \"" + value + "\". "
+ "Image name must be in the form '[domainHost:port/][path/]name', "
+ "with 'path' and 'name' containing only [a-z0-9][.][_][-]");
return new ImageName(matcher.group("domain"), matcher.group("path"));
return new ImageName(domain, path);
}

static String parseDomain(String value) {
int firstSlash = value.indexOf('/');
String candidate = (firstSlash != -1) ? value.substring(0, firstSlash) : null;
if (candidate != null && Regex.DOMAIN.matcher(candidate).matches()) {
return candidate;
}
return null;
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -33,8 +33,6 @@
*/
public final class ImageReference {

private static final Pattern PATTERN = Regex.IMAGE_REFERENCE.compile();

private static final Pattern JAR_VERSION_PATTERN = Pattern.compile("^(.*)(\\-\\d+)$");

private static final String LATEST = "latest";
Expand Down Expand Up @@ -225,13 +223,36 @@ public static ImageReference random(String prefix, int randomLength) {
*/
public static ImageReference of(String value) {
Assert.hasText(value, "Value must not be null");
Matcher matcher = PATTERN.matcher(value);
Assert.isTrue(matcher.matches(),
String domain = ImageName.parseDomain(value);
String path = (domain != null) ? value.substring(domain.length() + 1) : value;
String digest = null;
int digestSplit = path.indexOf("@");
if (digestSplit != -1) {
String remainder = path.substring(digestSplit + 1);
Matcher matcher = Regex.DIGEST.matcher(remainder);
if (matcher.find()) {
digest = remainder.substring(0, matcher.end());
remainder = remainder.substring(matcher.end());
path = path.substring(0, digestSplit) + remainder;
}
}
String tag = null;
int tagSplit = path.lastIndexOf(":");
if (tagSplit != -1) {
String remainder = path.substring(tagSplit + 1);
Matcher matcher = Regex.TAG.matcher(remainder);
if (matcher.find()) {
tag = remainder.substring(0, matcher.end());
remainder = remainder.substring(matcher.end());
path = path.substring(0, tagSplit) + remainder;
}
}
Assert.isTrue(Regex.PATH.matcher(path).matches(),
() -> "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][.][_][-]");
ImageName name = new ImageName(matcher.group("domain"), matcher.group("path"));
return new ImageReference(name, matcher.group("tag"), matcher.group("digest"));
ImageName name = new ImageName(domain, path);
return new ImageReference(name, tag, digest);
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -36,15 +36,15 @@
*/
final class Regex implements CharSequence {

private static final Regex DOMAIN;
static final Pattern DOMAIN;
static {
Regex component = Regex.oneOf("[a-zA-Z0-9]", "[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]");
Regex dotComponent = Regex.group("[.]", component);
Regex colonPort = Regex.of("[:][0-9]+");
Regex dottedDomain = Regex.group(component, dotComponent.oneOrMoreTimes());
Regex dottedDomainAndPort = Regex.group(component, dotComponent.oneOrMoreTimes(), colonPort);
Regex nameAndPort = Regex.group(component, colonPort);
DOMAIN = Regex.oneOf(dottedDomain, nameAndPort, dottedDomainAndPort, "localhost");
DOMAIN = Regex.oneOf(dottedDomain, nameAndPort, dottedDomainAndPort, "localhost").compile();
}

private static final Regex PATH_COMPONENT;
Expand All @@ -55,36 +55,18 @@ final class Regex implements CharSequence {
PATH_COMPONENT = Regex.of(segment, Regex.group(separatedSegment).zeroOrOnce());
}

private static final Regex PATH;
static final Pattern PATH;
static {
Regex component = PATH_COMPONENT;
Regex slashComponent = Regex.group("[/]", component);
Regex slashComponents = Regex.group(slashComponent.oneOrMoreTimes());
PATH = Regex.of(component, slashComponents.zeroOrOnce());
PATH = Regex.of(component, slashComponents.zeroOrOnce()).compile();
}

static final Regex IMAGE_NAME;
static {
Regex domain = DOMAIN.capturedAs("domain");
Regex domainSlash = Regex.group(domain, "[/]");
Regex path = PATH.capturedAs("path");
Regex optionalDomainSlash = domainSlash.zeroOrOnce();
IMAGE_NAME = Regex.of(optionalDomainSlash, path);
}

private static final Regex TAG_REGEX = Regex.of("[\\w][\\w.-]{0,127}");

private static final Regex DIGEST_REGEX = Regex
.of("[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[A-Fa-f0-9]]{32,}");
static final Pattern TAG = Regex.of("^[\\w][\\w.-]{0,127}").compile();

static final Regex IMAGE_REFERENCE;
static {
Regex tag = TAG_REGEX.capturedAs("tag");
Regex digest = DIGEST_REGEX.capturedAs("digest");
Regex atDigest = Regex.group("[@]", digest);
Regex colonTag = Regex.group("[:]", tag);
IMAGE_REFERENCE = Regex.of(IMAGE_NAME, colonTag.zeroOrOnce(), atDigest.zeroOrOnce());
}
static final Pattern DIGEST = Regex.of("^[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[A-Fa-f0-9]]{32,}")
.compile();

private final String value;

Expand All @@ -100,10 +82,6 @@ private Regex zeroOrOnce() {
return new Regex(this.value + "?");
}

private Regex capturedAs(String name) {
return new Regex("(?<" + name + ">" + this + ")");
}

Pattern compile() {
return Pattern.compile("^" + this.value + "$");
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -171,6 +171,14 @@ void ofImageNameTagAndDigest() {
"docker.io/library/ubuntu:bionic@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
}

@Test
void ofWhenHasIllegalCharacter() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ImageReference
.of("registry.example.com/example/example-app:1.6.0-dev.2.uncommitted+wip.foo.c75795d"))
.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");
Expand Down

0 comments on commit d3f0f04

Please sign in to comment.