Skip to content

Commit

Permalink
Enhance Fix fabric8io#541: Allow @sha256 digest for tags in FROM
Browse files Browse the repository at this point in the history
ImageName.java and respective test class adjusted to be able to handle
image names with tag AND sha256 digest like:
image_name:image_tag@sha256<digest>

Signed-off-by: Marcus Konrad <M.M.Konrad@web.de>
  • Loading branch information
mmkonrad committed Mar 19, 2019
1 parent 55f7697 commit 7021255
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- docs: Correct default image naming
- close api version http connection ([#1152](https://github.com/fabric8io/docker-maven-plugin/issues/1152))
- Update to jnr-unixsocket 0.22
- Enhance @sha256 digest for tags in FROM (image_name:image_tag@sha256<digest>) ([#541](https://github.com/fabric8io/docker-maven-plugin/issues/541))

* **0.28.0** (2018-12-13)
- Update to JMockit 1.43
Expand Down
48 changes: 31 additions & 17 deletions src/main/java/io/fabric8/maven/docker/util/ImageName.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,35 @@ public ImageName(String fullName, String givenTag) {
throw new NullPointerException("Image name must not be null");
}

// set digest to null as default
digest = null;
// check if digest is part of fullName, if so -> extract it
if(fullName.contains("@sha256")) { // Of it contains digest
String[] digestParts = fullName.split("@");
digest = digestParts[1];
parseComponentsBeforeTag(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);

parseComponentsBeforeTag(rest);
fullName = digestParts[0];
}

if (tag == null) {
tag = "latest";
}
// check for tag
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]");
}
// extract tag if it exists
tag = givenTag != null ? givenTag : matcher.group(2);
String rest = matcher.group(1);

// extract registry, repository, user
parseComponentsBeforeTag(rest);

/*
* set tag to latest if tag AND digest are null
* if digest is not null but tag is -> leave it!
* -> in case of "image_name@sha256" it is not required to get resolved to "latest"
*/
if (tag == null && digest == null) {
tag = "latest";
}

doValidate();
Expand Down Expand Up @@ -172,10 +182,14 @@ public String getFullName() {
* @return full name with original registry (if set) or optional registry (if not <code>null</code>).
*/
public String getFullName(String optionalRegistry) {
String fullName = getNameWithoutTag(optionalRegistry);
if (tag != null) {
fullName = fullName + ":" + tag;
}
if(digest != null) {
return getNameWithoutTag(optionalRegistry) + "@" + digest;
fullName = fullName + "@" + digest;
}
return getNameWithoutTag(optionalRegistry) + ":" + tag;
return fullName;
}

/**
Expand Down
16 changes: 16 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 @@ -129,6 +129,22 @@ public void testRegistryNamingExtended() throws Exception {
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getFullName(null));
assertEquals("docker.jolokia.org",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getRegistry());
assertEquals("docker.jolokia.org",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo:alpine").getRegistry());
assertEquals("docker.jolokia.org/org/jolokia/jolokia_demo:alpine",
new ImageName("org/jolokia/jolokia_demo:alpine").getFullName("docker.jolokia.org"));
assertEquals("docker.jolokia.org",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo:alpine").getRegistry());
assertEquals("docker.jolokia.org/org/jolokia/jolokia_demo:1.2.3.4-alpine",
new ImageName("org/jolokia/jolokia_demo:1.2.3.4-alpine").getFullName("docker.jolokia.org"));
assertEquals("docker.jolokia.org",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo:alpine@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getRegistry());
assertEquals("docker.jolokia.org/org/jolokia/jolokia_demo:alpine@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da",
new ImageName("org/jolokia/jolokia_demo:alpine@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getFullName("docker.jolokia.org"));
assertEquals("docker.jolokia.org",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo:1.2.3.4-alpine@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getRegistry());
assertEquals("docker.jolokia.org/org/jolokia/jolokia_demo:1.2.3.4-alpine@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da",
new ImageName("org/jolokia/jolokia_demo:1.2.3.4-alpine@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getFullName("docker.jolokia.org"));
}


Expand Down

0 comments on commit 7021255

Please sign in to comment.