|
| 1 | +# Copyright (C) 2025 Siemens |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +from ..bomwriter import BomWriter |
| 11 | +from .input import GenerateInput, warn_if_tty |
| 12 | +from ..sbom import SBOMType |
| 13 | + |
| 14 | + |
| 15 | +class CompareCmd(GenerateInput): |
| 16 | + """ |
| 17 | + Compare two SBOMs and generate a new SBOM containing only the additional components found in the target |
| 18 | + """ |
| 19 | + |
| 20 | + @classmethod |
| 21 | + def run(cls, args): |
| 22 | + inputs = [ |
| 23 | + ("base", args.base_sbom), |
| 24 | + ("target", args.target_sbom), |
| 25 | + ] |
| 26 | + |
| 27 | + base_json_obj = target_json_obj = None |
| 28 | + base_path = target_path = None |
| 29 | + base_sbom_fmt = target_sbom_fmt = None |
| 30 | + base_sbom_obj = target_sbom_obj = None |
| 31 | + |
| 32 | + json_sboms = [] |
| 33 | + stdin_consumed = False |
| 34 | + |
| 35 | + for kind, sbom_arg in inputs: |
| 36 | + # read from stdin |
| 37 | + if sbom_arg == "-": |
| 38 | + warn_if_tty() |
| 39 | + if args.sbom_type is None: |
| 40 | + raise ValueError("option --sbom-type is required when reading SBOMs from stdin") |
| 41 | + |
| 42 | + if not stdin_consumed: |
| 43 | + decoder = json.JSONDecoder() |
| 44 | + s = sys.stdin.read() |
| 45 | + json_obj, _ = decoder.raw_decode(s) |
| 46 | + len_s = len(s) |
| 47 | + read_total = 0 |
| 48 | + while read_total < len_s: |
| 49 | + json_obj, read = decoder.raw_decode(s[read_total:]) |
| 50 | + read_total += read |
| 51 | + json_sboms.append(json_obj) |
| 52 | + |
| 53 | + stdin_consumed = True |
| 54 | + |
| 55 | + # Pop the next object for this argument |
| 56 | + try: |
| 57 | + json_obj = json_sboms.pop(0) |
| 58 | + except IndexError: |
| 59 | + raise ValueError("Not enough SBOMs provided on stdin") |
| 60 | + |
| 61 | + fmt = args.sbom_type |
| 62 | + |
| 63 | + if kind == "base": |
| 64 | + base_json_obj = json_obj |
| 65 | + base_sbom_fmt = fmt |
| 66 | + else: |
| 67 | + target_json_obj = json_obj |
| 68 | + target_sbom_fmt = fmt |
| 69 | + |
| 70 | + else: |
| 71 | + sbom_path = Path(sbom_arg) |
| 72 | + if ".spdx" in sbom_path.suffixes: |
| 73 | + fmt = "spdx" |
| 74 | + elif ".cdx" in sbom_path.suffixes: |
| 75 | + fmt = "cdx" |
| 76 | + else: |
| 77 | + raise ValueError(f"cannot detect SBOM format for {sbom_arg}") |
| 78 | + |
| 79 | + if kind == "base": |
| 80 | + base_path = sbom_path |
| 81 | + base_sbom_fmt = fmt |
| 82 | + else: |
| 83 | + target_path = sbom_path |
| 84 | + target_sbom_fmt = fmt |
| 85 | + |
| 86 | + if base_sbom_fmt != target_sbom_fmt: |
| 87 | + raise ValueError("can not compare mixed SPDX and CycloneDX documents") |
| 88 | + |
| 89 | + SBOMType.from_str(target_sbom_fmt).validate_dependency_availability() |
| 90 | + |
| 91 | + from ..compare.compare import SbomCompare |
| 92 | + |
| 93 | + bom = SbomCompare.run_compare( |
| 94 | + fmt=target_sbom_fmt, |
| 95 | + args=args, |
| 96 | + base_json_obj=base_json_obj, |
| 97 | + target_json_obj=target_json_obj, |
| 98 | + base_path=base_path, |
| 99 | + target_path=target_path, |
| 100 | + ) |
| 101 | + |
| 102 | + sbom_type = SBOMType.SPDX if target_sbom_fmt == "spdx" else SBOMType.CycloneDX |
| 103 | + |
| 104 | + if args.out == "-": |
| 105 | + BomWriter.write_to_stream(bom, sbom_type, sys.stdout, args.validate) |
| 106 | + else: |
| 107 | + out = args.out |
| 108 | + suffix = ".spdx.json" if target_sbom_fmt == "spdx" else ".cdx.json" |
| 109 | + if not out.endswith(suffix): |
| 110 | + out += suffix |
| 111 | + BomWriter.write_to_file(bom, sbom_type, Path(out), args.validate) |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def setup_parser(cls, parser): |
| 115 | + cls.parser_add_generate_input_args(parser, default_out="extras") |
| 116 | + parser.add_argument( |
| 117 | + "-t", |
| 118 | + "--sbom-type", |
| 119 | + choices=["cdx", "spdx"], |
| 120 | + help="expected SBOM type when reading SBOMs from stdin, required when reading from stdin", |
| 121 | + ) |
| 122 | + parser.add_argument( |
| 123 | + "base_sbom", |
| 124 | + metavar="BASE_SBOM", |
| 125 | + help="Path to the base (reference) SBOM file", |
| 126 | + ) |
| 127 | + parser.add_argument( |
| 128 | + "target_sbom", |
| 129 | + metavar="TARGET_SBOM", |
| 130 | + help="Path to the target (new) SBOM file", |
| 131 | + ) |
0 commit comments