Skip to content

Commit

Permalink
Fix fabric8io#541: Allow @sha256 digest for tags in FROM
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Nov 29, 2018
1 parent f9b73b7 commit 881112d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String loadImage() {
public String pullImage(ImageName name, String registry) {
return u("images/create")
.p("fromImage", name.getNameWithoutTag(registry))
.p("tag", name.getTag())
.p("tag", name.getDigest() != null ? name.getDigest() : name.getTag())
.build();
}

Expand Down
81 changes: 51 additions & 30 deletions src/main/java/io/fabric8/maven/docker/util/ImageName.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class ImageName {
// Tag name
private String tag;

// Digest
private String digest;

// User name
private String user;

Expand All @@ -60,38 +63,46 @@ public ImageName(String fullName, String givenTag) {
if (fullName == null) {
throw new NullPointerException("Image name must not be null");
}
Pattern tagPattern = Pattern.compile("^(.+?)(?::([^:/]+))?$");
Matcher matcher = tagPattern.matcher(fullName);
if (!matcher.matches()) {
throw new IllegalArgumentException(fullName + " is not a proper image name ([registry/][repo][:port]");
}
tag = givenTag != null ? givenTag : matcher.group(2);
String rest = matcher.group(1);

String[] parts = rest.split("\\s*/\\s*");
if (parts.length == 1) {
registry = null;
user = null;
repository = parts[0];
} else if (parts.length >= 2) {
if (isRegistry(parts[0])) {
registry = parts[0];
if (parts.length > 2) {
user = parts[1];
repository = joinTail(parts);

if(fullName.contains("@sha256")) { // Of it contains digest
String[] digestParts = fullName.split("@");
digest = digestParts[1];
repository = digestParts[0];
} else {
digest = null;
Pattern tagPattern = Pattern.compile("^(.+?)(?::([^:/]+))?$");
Matcher matcher = tagPattern.matcher(fullName);
if (!matcher.matches()) {
throw new IllegalArgumentException(fullName + " is not a proper image name ([registry/][repo][:port]");
}
tag = givenTag != null ? givenTag : matcher.group(2);
String rest = matcher.group(1);

String[] parts = rest.split("\\s*/\\s*");
if (parts.length == 1) {
registry = null;
user = null;
repository = parts[0];
} else if (parts.length >= 2) {
if (isRegistry(parts[0])) {
registry = parts[0];
if (parts.length > 2) {
user = parts[1];
repository = joinTail(parts);
} else {
user = null;
repository = parts[1];
}
} else {
user = null;
repository = parts[1];
registry = null;
user = parts[0];
repository = rest;
}
} else {
registry = null;
user = parts[0];
repository = rest;
}
}

if (tag == null) {
tag = "latest";
if (tag == null) {
tag = "latest";
}
}

doValidate();
Expand All @@ -109,6 +120,10 @@ public String getTag() {
return tag;
}

public String getDigest() {
return digest;
}

public boolean hasRegistry() {
return registry != null && registry.length() > 0;
}
Expand Down Expand Up @@ -177,6 +192,9 @@ public String getFullName() {
* @return full name with original registry (if set) or optional registry (if not <code>null</code>).
*/
public String getFullName(String optionalRegistry) {
if(digest != null) {
return getNameWithoutTag(optionalRegistry) + "@" + digest;
}
return getNameWithoutTag(optionalRegistry) + ":" + tag;
}

Expand Down Expand Up @@ -214,13 +232,14 @@ public static void validate(String image) {
// Validate parts and throw an IllegalArgumentException if a part is not valid
private void doValidate() {
List<String> errors = new ArrayList<>();
// Stripp of user from repository name
// Strip off user from repository name
String image = user != null ? repository.substring(user.length() + 1) : repository;
Object[] checks = new Object[] {
"registry", DOMAIN_REGEXP, registry,
"image", IMAGE_NAME_REGEXP, image,
"user", NAME_COMP_REGEXP, user,
"tag", TAG_REGEXP, tag
"tag", TAG_REGEXP, tag,
"digest", DIGEST_REGEXP, digest
};
for (int i = 0; i < checks.length; i +=3) {
String value = (String) checks[i + 2];
Expand Down Expand Up @@ -268,4 +287,6 @@ private void doValidate() {

// https://github.com/docker/docker/blob/04da4041757370fb6f85510c8977c5a18ddae380/vendor/github.com/docker/distribution/reference/regexp.go#L37
private final Pattern TAG_REGEXP = Pattern.compile("^[\\w][\\w.-]{0,127}$");

private final Pattern DIGEST_REGEXP = Pattern.compile("^sha256:[a-z0-9]{32,}$");
}
2 changes: 2 additions & 0 deletions src/test/java/io/fabric8/maven/docker/util/ImageNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public void testRegistryNamingExtended() throws Exception {
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo").getFullName("another.registry.org"));
assertEquals("docker.jolokia.org/org/jolokia/jolokia_demo:latest",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo").getFullName(null));
assertEquals("docker.jolokia.org/org/jolokia/jolokia_demo@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getFullName(null));
}


Expand Down

0 comments on commit 881112d

Please sign in to comment.