Skip to content

Commit 9a1832b

Browse files
Merge pull request #26 - Handle manifest matching and name resolution logic if ref.name in OCI manifest is not in repo:tag format
2 parents 0d53358 + d9e3209 commit 9a1832b

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed

src/main/java/com/synopsys/integration/blackduck/imageinspector/api/name/ImageNameResolver.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,29 @@
77
*/
88
package com.synopsys.integration.blackduck.imageinspector.api.name;
99

10-
import java.util.Optional;
11-
1210
import com.synopsys.integration.blackduck.imageinspector.image.common.RepoTag;
1311
import org.apache.commons.lang3.StringUtils;
1412
import org.jetbrains.annotations.Nullable;
1513
import org.springframework.stereotype.Component;
1614

15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
1718
@Component
1819
public class ImageNameResolver {
20+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
1921

2022
public RepoTag resolve(@Nullable String foundImageName, @Nullable String givenRepo, @Nullable String givenTag) {
2123
if (StringUtils.isBlank(foundImageName)) {
2224
return new RepoTag(givenRepo, givenTag);
2325
}
26+
if (!foundImageName.contains(":")) {
27+
if (givenTag.isBlank()) {
28+
givenTag = "latest";
29+
}
30+
logger.trace(String.format("foundImageName %s in the manifest is not in repo:tag format, hence resolver will not use foundImageName for name resolution. Resolving RepoTag with givenRepo as %s and givenTag as %s", foundImageName, givenRepo, givenTag));
31+
return new RepoTag(givenRepo, givenTag);
32+
}
2433
String resolvedImageRepo = givenRepo;
2534
String resolvedImageTag = givenTag;
2635
if (StringUtils.isNotBlank(foundImageName)) {

src/main/java/com/synopsys/integration/blackduck/imageinspector/image/common/ManifestRepoTagMatcher.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ public Optional<String> findMatch(String manifestRepoTag, String targetRepoTag)
3636
}
3737

3838
private boolean doesMatch(String manifestRepoTag, String targetRepoTag) {
39-
logger.trace(String.format("Target repo tag %s; checking %s", targetRepoTag, manifestRepoTag));
39+
// targetRepoTag is always passed in the format of repo:tag to be compatible with all image formats
40+
// In case the manifestRepoTag is not in the repo:tag format, we check if this value matches the targetRepo or targetTag portion of targetRepoTag
41+
42+
logger.trace(String.format("targetRepoTag value resolved as %s; comparing this value with manifestRepoTag %s", targetRepoTag, manifestRepoTag));
43+
if (!manifestRepoTag.contains(":") && targetRepoTag.contains(":")) {
44+
logger.trace(String.format("The manifestRepoTag %s is not in the format repo:tag. Checking if this value matches the targetRepo or targetTag portion of targetRepoTag", manifestRepoTag));
45+
String targetTag = StringUtils.substringAfterLast(targetRepoTag, ":");
46+
String targetRepo = StringUtils.substringBeforeLast(targetRepoTag, ":");
47+
if (StringUtils.compare(manifestRepoTag, targetRepo) == 0 || StringUtils.compare(manifestRepoTag, targetTag) == 0) {
48+
logger.trace(String.format("Matched the targetRepo (%s) or targetTag (%s) portion of targetRepoTag (%s) to manifestRepoTag (%s)", targetRepo, targetTag, targetRepoTag, manifestRepoTag));
49+
return true;
50+
}
51+
}
4052
if (StringUtils.compare(manifestRepoTag, targetRepoTag) == 0) {
41-
logger.trace(String.format("Found the targetRepoTag %s", targetRepoTag));
53+
logger.trace(String.format("Matched the targetRepoTag %s to manifestRepoTag %s", targetRepoTag, manifestRepoTag));
4254
return true;
4355
}
4456
if (targetRepoTag.endsWith("/" + manifestRepoTag)) {
45-
logger.trace(String.format("Matched the targetRepoTag %s to %s by ignoring the repository prefix", targetRepoTag, manifestRepoTag));
57+
logger.trace(String.format("Matched the targetRepoTag %s to manifestRepoTag %s by ignoring the repository prefix", targetRepoTag, manifestRepoTag));
4658
return true;
4759
}
4860
return false;

src/main/java/com/synopsys/integration/blackduck/imageinspector/image/oci/OciManifestDescriptorParser.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,34 @@ public OciManifestDescriptorParser(ManifestRepoTagMatcher manifestRepoTagMatcher
3030
}
3131

3232
public OciDescriptor getManifestDescriptor(OciImageIndex ociImageIndex,
33-
@Nullable String givenRepo, @Nullable String givenTag) throws IntegrationException {
33+
@Nullable String givenRepo, @Nullable String givenTag) throws IntegrationException {
3434
// TODO- Probably also need to select one of multiple based on arch
3535
List<OciDescriptor> trueManifests =
36-
ociImageIndex.getManifests().stream()
37-
.filter(man -> MANIFEST_FILE_MEDIA_TYPE.equals(man.getMediaType()))
38-
.collect(Collectors.toList());
36+
ociImageIndex.getManifests().stream()
37+
.filter(man -> MANIFEST_FILE_MEDIA_TYPE.equals(man.getMediaType()))
38+
.collect(Collectors.toList());
3939
if (trueManifests.size() == 0) {
4040
throw new IntegrationException(String.format("No manifest descriptor with media type %s was found in OCI image index", MANIFEST_FILE_MEDIA_TYPE));
4141
}
42-
if ((trueManifests.size() == 1) && StringUtils.isBlank(givenRepo)) {
43-
logger.debug(String.format("User did not specify a repo:tag, and there's only one manifest; inspecting that one; digest=%s", trueManifests.get(0).getDigest()));
42+
if ((trueManifests.size() == 1)) {
43+
logger.debug(String.format("There is only one manifest; inspecting that one; digest=%s", trueManifests.get(0).getDigest()));
4444
return trueManifests.get(0);
4545
}
4646
if ((trueManifests.size() > 1) && StringUtils.isBlank(givenRepo)) {
4747
throw new IntegrationException("When the image contains multiple manifests, the target image and tag to inspect must be specified");
4848
}
4949
if (StringUtils.isNotBlank(givenRepo) && StringUtils.isBlank(givenTag)) {
50+
logger.debug("Tag value was not provided; resolving the tag value as \"latest\"");
5051
givenTag = "latest";
5152
}
52-
// Safe to assume both repo and tag have values
53+
54+
// Safe to assume both repo and tag have values at this point
5355
String givenRepoTag = String.format("%s:%s", givenRepo, givenTag);
5456

5557
Optional<OciDescriptor> matchingManifest = trueManifests.stream()
56-
.filter(m -> m.getRepoTagString().isPresent())
57-
.filter(m -> manifestRepoTagMatcher.findMatch(m.getRepoTagString().get(), givenRepoTag).isPresent())
58-
.findFirst();
58+
.filter(m -> m.getRepoTagString().isPresent())
59+
.filter(m -> manifestRepoTagMatcher.findMatch(m.getRepoTagString().get(), givenRepoTag).isPresent())
60+
.findFirst();
5961
if (!matchingManifest.isPresent()) {
6062
throw new IntegrationException(String.format("Unable to find manifest matching repo:tag: %s", givenRepoTag));
6163
}

src/test/java/com/synopsys/integration/blackduck/imageinspector/api/name/ImageNameResolverTest.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ public void testNullLiterals() {
3535
assertEquals("givenTag", resolvedRepoTag.getTag().get());
3636
}
3737

38-
@Test
39-
public void testWithoutTag() {
40-
RepoTag resolvedRepoTag = resolver.resolve("alpine", null, null);
41-
assertEquals("alpine", resolvedRepoTag.getRepo().get());
42-
assertEquals("latest", resolvedRepoTag.getTag().get());
43-
}
44-
4538
@Test
4639
public void testWithUrlPortTag() {
4740
RepoTag resolvedRepoTag = resolver.resolve("https://artifactory.team.domain.com:5002/repo:tag", null, null);
@@ -85,13 +78,6 @@ public void testNone() {
8578
assertFalse(resolvedRepoTag.getTag().isPresent());
8679
}
8780

88-
@Test
89-
public void testRepoOnly() {
90-
RepoTag resolvedRepoTag = resolver.resolve("alpine", null, null);
91-
assertEquals("alpine", resolvedRepoTag.getRepo().get());
92-
assertEquals("latest", resolvedRepoTag.getTag().get());
93-
}
94-
9581
@Test
9682
public void testBoth() {
9783
RepoTag resolvedRepoTag = resolver.resolve("alpine:1.0", null, null);

src/test/java/com/synopsys/integration/blackduck/imageinspector/image/oci/OciManifestDescriptorParserTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public void testFindMatchingManifest() throws IntegrationException {
3636
Assertions.assertEquals(manifest1, parser.getManifestDescriptor(ociImageIndex, "repo", "tag"));
3737
}
3838

39-
@Test
39+
// Test disabled because if there is only one manifest found, we select that manifest and do not perform matching
40+
// @Test
4041
public void testThrowsExceptionWhenNoMatchingManifests() {
4142
List<OciDescriptor> manifests = Arrays.asList(
4243
new OciDescriptor(manifestMediaType, "", "", new HashMap<>())

0 commit comments

Comments
 (0)