Skip to content

Commit 63932f4

Browse files
author
张宇鹏(执风)
committed
fix deal with repositoryName with SHA256
1 parent f196c02 commit 63932f4

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/main/java/com/github/dockerjava/core/NameParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.regex.Pattern;
77

8+
import org.apache.commons.lang.StringUtils;
89
import org.apache.commons.lang.builder.EqualsBuilder;
910
import org.apache.commons.lang.builder.ToStringBuilder;
1011
import org.apache.commons.lang.builder.ToStringStyle;
@@ -20,6 +21,8 @@ private NameParser() {
2021
// CHECKSTYLE:OFF
2122
private static final int RepositoryNameTotalLengthMax = 255;
2223

24+
private static final String SHA256_SEPARATOR = "@sha256:";
25+
2326
private static final Pattern RepositoryNameComponentRegexp = Pattern.compile("[a-z0-9]+(?:[._-][a-z0-9]+)*");
2427

2528
private static final Pattern RepositoryNameComponentAnchoredRegexp = Pattern.compile("^"
@@ -37,6 +40,9 @@ public static ReposTag parseRepositoryTag(String name) {
3740
return new ReposTag(name, "");
3841
}
3942
String tag = name.substring(n + 1);
43+
if (StringUtils.containsIgnoreCase(name, SHA256_SEPARATOR)) {
44+
return new ReposTag(name, "");
45+
}
4046
if (!tag.contains("/")) {
4147
return new ReposTag(name.substring(0, n), tag);
4248
}
@@ -110,6 +116,9 @@ public static HostnameReposName resolveRepositoryName(String reposName) {
110116
throw new InvalidRepositoryNameException(String.format("Invalid repository name, try \"%s\" instead",
111117
reposName));
112118
}
119+
if (StringUtils.containsIgnoreCase(reposName, SHA256_SEPARATOR)) {
120+
reposName = StringUtils.substringBeforeLast(reposName, SHA256_SEPARATOR);
121+
}
113122

114123
validateRepoName(reposName);
115124
return new HostnameReposName(hostname, reposName);

src/test/java/com/github/dockerjava/core/NameParserTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,24 @@ public void testResolveRepositoryNameWithNamespace() throws Exception {
8989
assertEquals(resolved, new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "namespace/repository"));
9090
}
9191

92+
@Test
93+
public void testResolveRepositoryNameWithNamespaceAndSHA256() throws Exception {
94+
HostnameReposName resolved = NameParser.resolveRepositoryName("namespace/repository@sha256:sha256");
95+
assertEquals(resolved, new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "namespace/repository@sha256:sha256"));
96+
}
97+
9298
@Test
9399
public void testResolveRepositoryNameWithNamespaceAndHostname() throws Exception {
94100
HostnameReposName resolved = NameParser.resolveRepositoryName("localhost:5000/namespace/repository");
95101
assertEquals(resolved, new HostnameReposName("localhost:5000", "namespace/repository"));
96102
}
97103

104+
@Test
105+
public void testResolveRepositoryNameWithNamespaceAndHostnameAndSHA256() throws Exception {
106+
HostnameReposName resolved = NameParser.resolveRepositoryName("localhost:5000/namespace/repository@sha256:sha256");
107+
assertEquals(resolved, new HostnameReposName("localhost:5000", "namespace/repository"));
108+
}
109+
98110
@Test(expected = InvalidRepositoryNameException.class)
99111
public void testResolveRepositoryNameWithIndex() throws Exception {
100112
NameParser.resolveRepositoryName("index.docker.io/repository");
@@ -123,4 +135,16 @@ public void testResolveReposTagWithTag() throws Exception {
123135
resolved = NameParser.parseRepositoryTag("localhost:5000/namespace/repository:tag");
124136
assertEquals(resolved, new ReposTag("localhost:5000/namespace/repository", "tag"));
125137
}
138+
139+
@Test
140+
public void testResolveReposTagWithSHA256() throws Exception {
141+
ReposTag resolved = NameParser.parseRepositoryTag("repository@sha256:sha256");
142+
assertEquals(resolved, new ReposTag("repository@sha256:sha256", ""));
143+
144+
resolved = NameParser.parseRepositoryTag("namespace/repository@sha256:sha256");
145+
assertEquals(resolved, new ReposTag("namespace/repository@sha256:sha256", ""));
146+
147+
resolved = NameParser.parseRepositoryTag("localhost:5000/namespace/repository@sha256:sha256");
148+
assertEquals(resolved, new ReposTag("localhost:5000/namespace/repository@sha256:sha256", ""));
149+
}
126150
}

0 commit comments

Comments
 (0)