-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Embed SBOM into wheels #9679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+144
−27
Merged
Embed SBOM into wheels #9679
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
59e5883
Embed SBOM into wheels
hugovk 33a66fd
Move sbom job before others that need it
hugovk 662d5f1
Read SBOM inside embed()
radarhere 02c716c
Avoid decoding and re-encoding
radarhere 9481f2e
Avoid decoding and re-encoding (#174)
hugovk 195e28f
Replace print+SystemExit with parser.error
hugovk 287a17e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b2c4126
Run with `python3` not `python`
hugovk a5d4f3d
Apply suggestions from code review
hugovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """Embed Pillow's SBOM into each wheel's `.dist-info/sboms/` directory, | ||
| as specified by PEP 770. | ||
|
|
||
| The SBOM (produced by `generate-sbom.py`) is injected into every `.whl` in | ||
| the given directory, updating each wheel's `RECORD` so the result remains a | ||
| valid, installable wheel. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import base64 | ||
| import hashlib | ||
| import zipfile | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def record_entry(path: str, data: bytes) -> bytes: | ||
| """Build a RECORD line: `path,sha256=<base64url-nopad>,<size>`.""" | ||
| digest = base64.urlsafe_b64encode(hashlib.sha256(data).digest()) | ||
| return ( | ||
| path.encode("utf-8") | ||
| + b",sha256=" | ||
| + digest.rstrip(b"=") | ||
| + b"," | ||
| + str(len(data)).encode() | ||
| ) | ||
|
|
||
|
|
||
| def embed(wheel: Path, sbom: Path) -> None: | ||
| with zipfile.ZipFile(wheel) as zf: | ||
| infos = zf.infolist() | ||
| contents = {info.filename: zf.read(info.filename) for info in infos} | ||
|
|
||
| record_name = next( | ||
| name | ||
| for name in contents | ||
| if name.endswith(".dist-info/RECORD") and name.count("/") == 1 | ||
| ) | ||
| dist_info = record_name.split("/", 1)[0] | ||
|
|
||
| sbom_bytes = sbom.read_bytes() | ||
| sbom_path = f"{dist_info}/sboms/{sbom.name}" | ||
|
|
||
| # Append a matching RECORD line for the SBOM (RECORD's own line has no hash). | ||
| lines = contents[record_name].splitlines() | ||
| lines.append(record_entry(sbom_path, sbom_bytes)) | ||
| contents[record_name] = b"\n".join(lines) + b"\n" | ||
|
|
||
| with zipfile.ZipFile(wheel, "w", zipfile.ZIP_DEFLATED) as zf: | ||
| # Re-use each original ZipInfo to preserve timestamps, mode bits and | ||
| # compression; only RECORD's contents change | ||
| for info in infos: | ||
| zf.writestr(info, contents[info.filename]) | ||
| zf.writestr(sbom_path, sbom_bytes) | ||
|
|
||
| print(f"Embedded {sbom.name} in {wheel.name}") | ||
|
|
||
|
|
||
| def scan_dir(path: Path) -> Path: | ||
| """If `path` is a directory, return the path of the SBOM within.""" | ||
| if path.is_dir(): | ||
| candidates = list(path.glob("*.cdx.json")) | ||
| if len(candidates) != 1: | ||
| msg = f"expected exactly one *.cdx.json in {path}, found {len(candidates)}" | ||
| raise SystemExit(msg) | ||
| return candidates[0] | ||
| return path | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
| ) | ||
| parser.add_argument( | ||
| "wheelhouse", type=Path, help="directory of wheels to embed the SBOM into" | ||
| ) | ||
| parser.add_argument( | ||
| "sbom", | ||
| type=Path, | ||
| help="SBOM file, or a directory containing a single `.cdx.json`", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| sbom = scan_dir(args.sbom) | ||
|
|
||
| wheels = sorted(args.wheelhouse.glob("*.whl")) | ||
| if not wheels: | ||
| parser.error(f"no wheels found in {args.wheelhouse}") | ||
|
|
||
| for wheel in wheels: | ||
| embed(wheel, sbom) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.