Skip to content

Commit

Permalink
Merge pull request #88 from Dohbedoh/JENKINS-63243
Browse files Browse the repository at this point in the history
[JENKINS-63243] DockerRegistryEndpoint Pattern should accept registry v2 references
  • Loading branch information
rsandell authored Aug 24, 2022
2 parents c38e8e2 + 6ae336f commit e21f53f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,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 @@ -120,13 +131,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

0 comments on commit e21f53f

Please sign in to comment.