Skip to content

Commit b3e78c9

Browse files
committed
[ISV-5787] Remove child digests from externalRefs
1 parent 63cd601 commit b3e78c9

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

sbom-utility-scripts/scripts/index-image-sbom-script/index_image_sbom_script.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,36 @@ def digest_hex_val(self) -> str:
5555
_, val = self.digest.split(":")
5656
return val
5757

58-
def purls(self, index_digest: Optional[str] = None) -> list[str]:
59-
ans = []
60-
if index_digest and self.arch:
61-
ans.append(
62-
PackageURL(
63-
type="oci",
64-
name=self.name,
65-
version=index_digest,
66-
qualifiers={"arch": self.arch, "repository_url": self.repository},
67-
).to_string()
68-
)
69-
ans.append(
70-
PackageURL(
71-
type="oci",
72-
name=self.name,
73-
version=self.digest,
74-
qualifiers={"repository_url": self.repository},
75-
).to_string()
76-
)
77-
return ans
58+
def purls(self, index_digest: str) -> list[str]:
59+
purl = PackageURL(
60+
type="oci",
61+
name=self.name,
62+
version=index_digest,
63+
qualifiers={"arch": self.arch, "repository_url": self.repository},
64+
).to_string()
65+
66+
# HACK: There's a bug in PackageURL python that incorrectly handles
67+
# encoding of ':' characters. When this PR is merged, the hack should be
68+
# removed: https://github.com/package-url/packageurl-python/pull/178
69+
return [self._hack_purl_encoding(purl)]
7870

7971
def propose_spdx_id(self) -> str:
8072
purl_hex_digest = hashlib.sha256(self.purls()[0].encode()).hexdigest()
8173
return f"SPDXRef-image-{self.name}-{purl_hex_digest}"
8274

75+
def _hack_purl_encoding(purl: str) -> str:
76+
"""
77+
Encode ':' characters in PURL that are not the scheme and type separator.
78+
"""
79+
if purl.count(":") == 1:
80+
return purl
81+
82+
first_idx = purl.find(":")
83+
after_first = purl[first_idx + 1 :]
84+
after_first = after_first.replace(":", "%3A")
85+
86+
return f"{purl[:first_idx]}:{after_first}"
87+
8388

8489
def create_package(image: Image, spdxid: Optional[str] = None, image_index_digest: Optional[str] = None) -> dict:
8590
return {

sbom-utility-scripts/scripts/index-image-sbom-script/test_image_index_sbom_script.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@
118118
"referenceType": "purl",
119119
"referenceLocator": "pkg:oci/ubi9-micro-container@sha256:1c8483e0fda0e990175eb9855a5f15e0910d2038dd397d9e2b357630f0321e6d?arch=ppc64le&repository_url=quay.io/ubi9-micro-container",
120120
},
121-
{
122-
"referenceCategory": "PACKAGE-MANAGER",
123-
"referenceType": "purl",
124-
"referenceLocator": "pkg:oci/ubi9-micro-container@sha256:f08722139c4da653b870272a192fac700960a3315baa1f79f83a4712a436d4?repository_url=quay.io/ubi9-micro-container",
125-
},
126121
],
127122
"checksums": [
128123
{
@@ -251,11 +246,6 @@ def test_main(
251246
"referenceType": "purl",
252247
"referenceLocator": "pkg:oci/bar@sha256:456?arch=arm64&repository_url=quay.io/foo/bar",
253248
},
254-
{
255-
"referenceCategory": "PACKAGE-MANAGER",
256-
"referenceType": "purl",
257-
"referenceLocator": "pkg:oci/bar@sha256:123?repository_url=quay.io/foo/bar",
258-
},
259249
],
260250
"checksums": [{"algorithm": "SHA256", "checksumValue": "123"}],
261251
},

0 commit comments

Comments
 (0)