Rename ImageReferenceParser to Regex
Rename `ImageReferenceParser` to `Regex` and remove state. The regular expressions are now used directly by the `ImageName` and `ImageReference` classes with the values accessed directly from the `Matcher`. See gh-21495pull/22112/head
parent
9843888714
commit
f296f57401
@ -1,159 +0,0 @@
|
||||
/*
|
||||
* 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 java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A parser for Docker image references in the form
|
||||
* {@code [domainHost:port/][path/]name[:tag][@digest]}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
* @see <a href=
|
||||
* "https://github.com/docker/distribution/blob/master/reference/reference.go">Docker
|
||||
* grammar reference</a>
|
||||
* @see <a href=
|
||||
* "https://github.com/docker/distribution/blob/master/reference/regexp.go">Docker grammar
|
||||
* implementation</a>
|
||||
* @see <a href=
|
||||
* "https://stackoverflow.com/questions/37861791/how-are-docker-image-names-parsed">How
|
||||
* are Docker image names parsed?</a>
|
||||
*/
|
||||
final class ImageReferenceParser {
|
||||
|
||||
private static final String DOMAIN_SEGMENT_REGEX = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])";
|
||||
|
||||
private static final String DOMAIN_PORT_REGEX = "[0-9]+";
|
||||
|
||||
private static final String DOMAIN_REGEX = oneOf(
|
||||
groupOf(DOMAIN_SEGMENT_REGEX, repeating("[.]", DOMAIN_SEGMENT_REGEX)),
|
||||
groupOf(DOMAIN_SEGMENT_REGEX, "[:]", DOMAIN_PORT_REGEX),
|
||||
groupOf(DOMAIN_SEGMENT_REGEX, repeating("[.]", DOMAIN_SEGMENT_REGEX), "[:]", DOMAIN_PORT_REGEX),
|
||||
"localhost");
|
||||
|
||||
private static final String NAME_CHARS_REGEX = "[a-z0-9]+";
|
||||
|
||||
private static final String NAME_SEPARATOR_REGEX = "(?:[._]|__|[-]*)";
|
||||
|
||||
private static final String NAME_SEGMENT_REGEX = groupOf(NAME_CHARS_REGEX,
|
||||
optional(repeating(NAME_SEPARATOR_REGEX, NAME_CHARS_REGEX)));
|
||||
|
||||
private static final String NAME_PATH_REGEX = groupOf(NAME_SEGMENT_REGEX,
|
||||
optional(repeating("[/]", NAME_SEGMENT_REGEX)));
|
||||
|
||||
private static final String DIGEST_ALGORITHM_SEGMENT_REGEX = "[A-Za-z][A-Za-z0-9]*";
|
||||
|
||||
private static final String DIGEST_ALGORITHM_SEPARATOR_REGEX = "[-_+.]";
|
||||
|
||||
private static final String DIGEST_ALGORITHM_REGEX = groupOf(DIGEST_ALGORITHM_SEGMENT_REGEX,
|
||||
optional(repeating(DIGEST_ALGORITHM_SEPARATOR_REGEX, DIGEST_ALGORITHM_SEGMENT_REGEX)));
|
||||
|
||||
private static final String DIGEST_VALUE_REGEX = "[0-9A-Fa-f]{32,}";
|
||||
|
||||
private static final String DIGEST_REGEX = groupOf(DIGEST_ALGORITHM_REGEX, "[:]", DIGEST_VALUE_REGEX);
|
||||
|
||||
private static final String TAG_REGEX = "[\\w][\\w.-]{0,127}";
|
||||
|
||||
private static final String DOMAIN_CAPTURE_GROUP = "domain";
|
||||
|
||||
private static final String NAME_CAPTURE_GROUP = "name";
|
||||
|
||||
private static final String TAG_CAPTURE_GROUP = "tag";
|
||||
|
||||
private static final String DIGEST_CAPTURE_GROUP = "digest";
|
||||
|
||||
private static final Pattern REFERENCE_REGEX_PATTERN = patternOf(anchored(
|
||||
optional(captureOf(DOMAIN_CAPTURE_GROUP, DOMAIN_REGEX), "[/]"),
|
||||
captureOf(NAME_CAPTURE_GROUP, NAME_PATH_REGEX), optional("[:]", captureOf(TAG_CAPTURE_GROUP, TAG_REGEX)),
|
||||
optional("[@]", captureOf(DIGEST_CAPTURE_GROUP, DIGEST_REGEX))));
|
||||
|
||||
private final String domain;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String tag;
|
||||
|
||||
private final String digest;
|
||||
|
||||
private ImageReferenceParser(String domain, String name, String tag, String digest) {
|
||||
this.domain = domain;
|
||||
this.name = name;
|
||||
this.tag = tag;
|
||||
this.digest = digest;
|
||||
}
|
||||
|
||||
String getDomain() {
|
||||
return this.domain;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
String getTag() {
|
||||
return this.tag;
|
||||
}
|
||||
|
||||
String getDigest() {
|
||||
return this.digest;
|
||||
}
|
||||
|
||||
static ImageReferenceParser of(String reference) {
|
||||
Matcher matcher = REFERENCE_REGEX_PATTERN.matcher(reference);
|
||||
if (!matcher.matches()) {
|
||||
throw new IllegalArgumentException("Unable to parse image reference \"" + reference + "\". "
|
||||
+ "Image reference must be in the form \"[domainHost:port/][path/]name[:tag][@digest]\", "
|
||||
+ "with \"path\" and \"name\" containing only [a-z0-9][.][_][-]");
|
||||
}
|
||||
return new ImageReferenceParser(matcher.group(DOMAIN_CAPTURE_GROUP), matcher.group(NAME_CAPTURE_GROUP),
|
||||
matcher.group(TAG_CAPTURE_GROUP), matcher.group(DIGEST_CAPTURE_GROUP));
|
||||
}
|
||||
|
||||
private static Pattern patternOf(String... expressions) {
|
||||
return Pattern.compile(join(expressions));
|
||||
}
|
||||
|
||||
private static String groupOf(String... expressions) {
|
||||
return "(?:" + join(expressions) + ')';
|
||||
}
|
||||
|
||||
private static String captureOf(String groupName, String... expressions) {
|
||||
return "(?<" + groupName + ">" + join(expressions) + ')';
|
||||
}
|
||||
|
||||
private static String oneOf(String... expressions) {
|
||||
return groupOf(String.join("|", expressions));
|
||||
}
|
||||
|
||||
private static String optional(String... expressions) {
|
||||
return groupOf(join(expressions)) + '?';
|
||||
}
|
||||
|
||||
private static String repeating(String... expressions) {
|
||||
return groupOf(join(expressions)) + '+';
|
||||
}
|
||||
|
||||
private static String anchored(String... expressions) {
|
||||
return '^' + join(expressions) + '$';
|
||||
}
|
||||
|
||||
private static String join(String... expressions) {
|
||||
return String.join("", expressions);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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 java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Regular Expressions for image names and references based on those found in the Docker
|
||||
* codebase.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
* @author Phillip Webb
|
||||
* @see <a href=
|
||||
* "https://github.com/docker/distribution/blob/master/reference/reference.go">Docker
|
||||
* grammar reference</a>
|
||||
* @see <a href=
|
||||
* "https://github.com/docker/distribution/blob/master/reference/regexp.go">Docker grammar
|
||||
* implementation</a>
|
||||
* @see <a href=
|
||||
* "https://stackoverflow.com/questions/37861791/how-are-docker-image-names-parsed">How
|
||||
* are Docker image names parsed?</a>
|
||||
*/
|
||||
final class Regex implements CharSequence {
|
||||
|
||||
private static final Regex 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");
|
||||
}
|
||||
|
||||
private static final Regex PATH_COMPONENT;
|
||||
static {
|
||||
Regex segment = Regex.of("[a-z0-9]+");
|
||||
Regex separator = Regex.group("[._]|__|[-]*");
|
||||
Regex separatedSegment = Regex.group(separator, segment).oneOrMoreTimes();
|
||||
PATH_COMPONENT = Regex.of(segment, Regex.group(separatedSegment).zeroOrOnce());
|
||||
}
|
||||
|
||||
private static final Regex PATH;
|
||||
static {
|
||||
Regex component = PATH_COMPONENT;
|
||||
Regex slashComponent = Regex.group("[/]", component);
|
||||
Regex slashComponents = Regex.group(slashComponent.oneOrMoreTimes());
|
||||
PATH = Regex.of(component, slashComponents.zeroOrOnce());
|
||||
}
|
||||
|
||||
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 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());
|
||||
}
|
||||
|
||||
private final String value;
|
||||
|
||||
private Regex(CharSequence value) {
|
||||
this.value = value.toString();
|
||||
}
|
||||
|
||||
private Regex oneOrMoreTimes() {
|
||||
return new Regex(this.value + "+");
|
||||
}
|
||||
|
||||
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 + "$");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return this.value.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public char charAt(int index) {
|
||||
return this.value.charAt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence subSequence(int start, int end) {
|
||||
return this.value.subSequence(start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
private static Regex of(CharSequence... expressions) {
|
||||
return new Regex(String.join("", expressions));
|
||||
}
|
||||
|
||||
private static Regex oneOf(CharSequence... expressions) {
|
||||
return new Regex("(?:" + String.join("|", expressions) + ")");
|
||||
}
|
||||
|
||||
private static Regex group(CharSequence... expressions) {
|
||||
return new Regex("(?:" + String.join("", expressions) + ")");
|
||||
}
|
||||
|
||||
}
|
@ -1,158 +0,0 @@
|
||||
/*
|
||||
* 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");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue