Skip to content

Commit

Permalink
Fix: Allow @sha256 digest for tags in FROM (#1131)
Browse files Browse the repository at this point in the history
Fixes #541
  • Loading branch information
rohanKanojia authored and rhuss committed Dec 12, 2018
1 parent da9ee92 commit 7080133
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Update to jnr-unixsocket version to 0.21 (#1089)
- Add 'readOnly' option for docker:run cto mount container's root fs read-only (#1125)
- Provide container properties to the wait configuration execution (#1111)
- Allow @sha256 digest for tags in FROM (#541)

* **0.27.2** (2018-10-05)

Expand Down
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
87 changes: 56 additions & 31 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,26 @@ 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);
} else {
user = null;
repository = parts[1];
}
} else {
registry = null;
user = parts[0];
repository = rest;
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);

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

doValidate();
Expand All @@ -109,6 +100,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 +172,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 +212,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 All @@ -242,6 +241,30 @@ private void doValidate() {
}
}

private void parseComponentsBeforeTag(String rest) {
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 {
registry = null;
user = parts[0];
repository = rest;
}
}
}

// ================================================================================================

// Validations patterns, taken directly from the docker source -->
Expand All @@ -268,4 +291,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,}$");
}
4 changes: 4 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,10 @@ 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));
assertEquals("docker.jolokia.org",
new ImageName("docker.jolokia.org/org/jolokia/jolokia_demo@sha256:2781907cc3ae9bb732076f14392128d4b84ff3ebb66379d268e563b10fbfb9da").getRegistry());
}


Expand Down

0 comments on commit 7080133

Please sign in to comment.