Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-63243] DockerRegistryEndpoint Pattern should accept registry v2 references #88

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,23 @@ public class DockerRegistryEndpoint extends AbstractDescribableImpl<DockerRegist
* Some regex magic to parse docker registry:port/namespace/name:tag into parts, using the same constraints as
* docker push.
*
* registry must not contain / and must contain a .
* registry must not contain /, may contain dashes and must contain a .
*
* everything but name is optional
*
* See also https://github.com/docker/distribution/blob/release/2.7/reference/regexp.go
*/
private static final Pattern DOCKER_REGISTRY_PATTERN = Pattern
.compile("(([^/]+\\.[^/]+)/)?(([a-z0-9_]+)/)?([a-zA-Z0-9-_\\.]+)(:([a-z0-9-_\\.]+))?");
.compile("^" +
// Domain
"(?:([a-zA-Z0-9]+(?:(?:[-.][a-zA-Z0-9]+)+)?(?::([0-9]+))?)/)?" +
// Repo
"([a-z0-9-_.]+)(?:/([a-z0-9-_.]+))*" +
// Tag
"(:[a-zA-Z0-9-_.]+)?" +
// Digest
"(@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][A-Za-z0-9]*)?$");


private static final Logger LOGGER = Logger.getLogger(DockerRegistryEndpoint.class.getName());

Expand Down Expand Up @@ -119,13 +130,16 @@ public DockerRegistryEndpoint(String url, String credentialsId) {
*/
public static DockerRegistryEndpoint fromImageName(String s, @CheckForNull String credentialsId) {
Matcher matcher = DOCKER_REGISTRY_PATTERN.matcher(s);
if (!matcher.matches() || matcher.groupCount() < 7) {
if (!matcher.matches() || matcher.groupCount() < 5) {
throw new IllegalArgumentException(s + " does not match regex " + DOCKER_REGISTRY_PATTERN);
}
String url;
try {
// docker push always uses https
url = matcher.group(2) == null ? null : new URL("https://" + matcher.group(2)).toString();
String domain = matcher.group(1);
url = (domain == null || !(domain.contains(".") || domain.contains(":") || "localhost".equalsIgnoreCase(domain)))
? null
: new URL("https://" + matcher.group(1)).toString();
} catch (MalformedURLException e) {
throw new IllegalArgumentException(s + " can not be parsed as URL: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public void testParse() throws Exception {
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/acme/test");
assertRegistry("https://docker.acme.com", "docker.acme.com/acme/test");
assertRegistry("https://docker.acme.com", "docker.acme.com/busybox");
// Registry v2
assertRegistry("https://docker.acme.com", "docker.acme.com/path/to/busybox");
assertRegistry("https://localhost:8080", "localhost:8080/path/to/busybox");
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox");
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox");
}

@Test
Expand All @@ -81,6 +86,12 @@ public void testParseWithTags() throws Exception {
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/acme/test:tag");
assertRegistry("https://docker.acme.com", "docker.acme.com/acme/test:tag");
assertRegistry("https://docker.acme.com", "docker.acme.com/busybox:tag");
assertRegistry("https://docker.acme.com", "docker.acme.com/busybox@sha256:sha256");
// Registry v2
assertRegistry("https://docker.acme.com", "docker.acme.com/path/to/busybox:tag");
assertRegistry("https://localhost:8080", "localhost:8080/path/to/busybox:tag");
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox:tag");
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox@sha256:sha256");
}

@Issue("JENKINS-39181")
Expand Down