Skip to content

Commit e21f53f

Browse files
authored
Merge pull request #88 from Dohbedoh/JENKINS-63243
[JENKINS-63243] DockerRegistryEndpoint Pattern should accept registry v2 references
2 parents c38e8e2 + 6ae336f commit e21f53f

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerRegistryEndpoint.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,23 @@ public class DockerRegistryEndpoint extends AbstractDescribableImpl<DockerRegist
8585
* Some regex magic to parse docker registry:port/namespace/name:tag into parts, using the same constraints as
8686
* docker push.
8787
*
88-
* registry must not contain / and must contain a .
88+
* registry must not contain /, may contain dashes and must contain a .
8989
*
9090
* everything but name is optional
91+
*
92+
* See also https://github.com/docker/distribution/blob/release/2.7/reference/regexp.go
9193
*/
9294
private static final Pattern DOCKER_REGISTRY_PATTERN = Pattern
93-
.compile("(([^/]+\\.[^/]+)/)?(([a-z0-9_]+)/)?([a-zA-Z0-9-_\\.]+)(:([a-z0-9-_\\.]+))?");
95+
.compile("^" +
96+
// Domain
97+
"(?:([a-zA-Z0-9]+(?:(?:[-.][a-zA-Z0-9]+)+)?(?::([0-9]+))?)/)?" +
98+
// Repo
99+
"([a-z0-9-_.]+)(?:/([a-z0-9-_.]+))*" +
100+
// Tag
101+
"(:[a-zA-Z0-9-_.]+)?" +
102+
// Digest
103+
"(@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][A-Za-z0-9]*)?$");
104+
94105

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

@@ -120,13 +131,16 @@ public DockerRegistryEndpoint(String url, String credentialsId) {
120131
*/
121132
public static DockerRegistryEndpoint fromImageName(String s, @CheckForNull String credentialsId) {
122133
Matcher matcher = DOCKER_REGISTRY_PATTERN.matcher(s);
123-
if (!matcher.matches() || matcher.groupCount() < 7) {
134+
if (!matcher.matches() || matcher.groupCount() < 5) {
124135
throw new IllegalArgumentException(s + " does not match regex " + DOCKER_REGISTRY_PATTERN);
125136
}
126137
String url;
127138
try {
128139
// docker push always uses https
129-
url = matcher.group(2) == null ? null : new URL("https://" + matcher.group(2)).toString();
140+
String domain = matcher.group(1);
141+
url = (domain == null || !(domain.contains(".") || domain.contains(":") || "localhost".equalsIgnoreCase(domain)))
142+
? null
143+
: new URL("https://" + matcher.group(1)).toString();
130144
} catch (MalformedURLException e) {
131145
throw new IllegalArgumentException(s + " can not be parsed as URL: " + e.getMessage());
132146
}

src/test/java/org/jenkinsci/plugins/docker/commons/credentials/DockerRegistryEndpointTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public void testParse() throws Exception {
7171
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/acme/test");
7272
assertRegistry("https://docker.acme.com", "docker.acme.com/acme/test");
7373
assertRegistry("https://docker.acme.com", "docker.acme.com/busybox");
74+
// Registry v2
75+
assertRegistry("https://docker.acme.com", "docker.acme.com/path/to/busybox");
76+
assertRegistry("https://localhost:8080", "localhost:8080/path/to/busybox");
77+
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox");
78+
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox");
7479
}
7580

7681
@Test
@@ -81,6 +86,12 @@ public void testParseWithTags() throws Exception {
8186
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/acme/test:tag");
8287
assertRegistry("https://docker.acme.com", "docker.acme.com/acme/test:tag");
8388
assertRegistry("https://docker.acme.com", "docker.acme.com/busybox:tag");
89+
assertRegistry("https://docker.acme.com", "docker.acme.com/busybox@sha256:sha256");
90+
// Registry v2
91+
assertRegistry("https://docker.acme.com", "docker.acme.com/path/to/busybox:tag");
92+
assertRegistry("https://localhost:8080", "localhost:8080/path/to/busybox:tag");
93+
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox:tag");
94+
assertRegistry("https://docker.acme.com:8080", "docker.acme.com:8080/path/to/busybox@sha256:sha256");
8495
}
8596

8697
@Issue("JENKINS-39181")

0 commit comments

Comments
 (0)