-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathget_bundle_images_sbom.py
More file actions
73 lines (53 loc) · 1.89 KB
/
get_bundle_images_sbom.py
File metadata and controls
73 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import argparse
import logging
import os
from pathlib import Path
import subprocess
import tarfile
SBOM_DIR = "images_SBOM"
log = logging.getLogger(__name__)
def get_images_sbom(images: list[str]) -> None:
log.info(f"Images received as input: {images}")
sbom_path = Path(SBOM_DIR)
sbom_path.mkdir(parents=True, exist_ok=True)
total = len(images)
for idx, image in enumerate(images, start=1):
output_file = sbom_path / f"{image}.spdx.json"
if output_file.exists():
log.info(
f"[{idx}/{total}] Skipping {image}, SBOM already exists at {output_file}"
)
continue
log.info(f"[{idx}/{total}] Creating SBOM for {image}")
try:
subprocess.check_call(
["syft", "scan", image, "-o", f"spdx-json={image}.spdx.json"],
cwd=SBOM_DIR,
)
except subprocess.CalledProcessError as e:
log.error(f"Error scanning {image}: {e}")
raise
sbom_files = os.listdir(SBOM_DIR)
# Make .tar.gz from all SBOMs
with tarfile.open(f"{SBOM_DIR}.tar.gz", "w:gz") as tar:
for file in sbom_files:
tar.add(f"{SBOM_DIR}/{file}")
def main():
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s - %(message)s",
)
parser = argparse.ArgumentParser(description="Get SBOMs for a list of images.")
parser.add_argument(
"images_file",
help="Path to a file containing container images (one per line).",
)
args = parser.parse_args()
images_file_path = Path(args.images_file)
if not images_file_path.exists():
raise FileNotFoundError(f"Images file not found: {images_file_path}")
with images_file_path.open("r") as f:
images_list = [line.strip() for line in f if line.strip()]
get_images_sbom(images_list)
if __name__ == "__main__":
main()