|
| 1 | +# ------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2020-2023 Siemens |
| 3 | +# All Rights Reserved. |
| 4 | + |
| 5 | +# |
| 6 | +# SPDX-License-Identifier: MIT |
| 7 | +# ------------------------------------------------------------------------------- |
| 8 | + |
| 9 | +import logging |
| 10 | +import os |
| 11 | +import pathlib |
| 12 | +import sys |
| 13 | + |
| 14 | +import sw360.sw360_api |
| 15 | +from cyclonedx.model import ExternalReferenceType |
| 16 | +from cyclonedx.model.bom import Bom |
| 17 | +from cyclonedx.model.component import Component |
| 18 | + |
| 19 | +import capycli.common.json_support |
| 20 | +import capycli.common.script_base |
| 21 | +from capycli.common.capycli_bom_support import CaPyCliBom, CycloneDxSupport, SbomWriter |
| 22 | +from capycli.common.print import print_red, print_text, print_yellow |
| 23 | +from capycli.common.script_support import ScriptSupport |
| 24 | +from capycli.main.result_codes import ResultCode |
| 25 | + |
| 26 | +LOG = capycli.get_logger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +class BomDownloadAttachments(capycli.common.script_base.ScriptBase): |
| 30 | + """ |
| 31 | + Download SW360 attachments as specified in the SBOM. |
| 32 | + """ |
| 33 | + |
| 34 | + def download_attachments(self, sbom: Bom, source_folder: str) -> Bom: |
| 35 | + for component in sbom.components: |
| 36 | + item_name = ScriptSupport.get_full_name_from_component(component) |
| 37 | + print_text(" " + item_name) |
| 38 | + |
| 39 | + for ext_ref in component.external_references: |
| 40 | + if not ext_ref.comment: |
| 41 | + continue |
| 42 | + if (not ext_ref.comment.startswith(CaPyCliBom.CLI_FILE_COMMENT) |
| 43 | + and not ext_ref.comment.startswith(CaPyCliBom.CRT_FILE_COMMENT)): |
| 44 | + continue |
| 45 | + |
| 46 | + attachment_id = ext_ref.comment.split(", sw360Id: ") |
| 47 | + if len(attachment_id) != 2: |
| 48 | + print_red(" No sw360Id for attachment!") |
| 49 | + continue |
| 50 | + attachment_id = attachment_id[1] |
| 51 | + |
| 52 | + release_id = CycloneDxSupport.get_property_value(component, CycloneDxSupport.CDX_PROP_SW360ID) |
| 53 | + if not release_id: |
| 54 | + print_red(" No sw360Id for release!") |
| 55 | + continue |
| 56 | + print(" ", ext_ref.url, release_id, attachment_id) |
| 57 | + filename = os.path.join(source_folder, ext_ref.url) |
| 58 | + |
| 59 | + try: |
| 60 | + at_info = self.client.get_attachment(attachment_id) |
| 61 | + at_info = {k: v for k, v in at_info.items() |
| 62 | + if k.startswith("check") |
| 63 | + or k.startswith("created")} |
| 64 | + print(at_info) |
| 65 | + |
| 66 | + self.client.download_release_attachment(filename, release_id, attachment_id) |
| 67 | + ext_ref.url = filename |
| 68 | + except sw360.sw360_api.SW360Error as swex: |
| 69 | + print_red(" Error getting", swex.url, swex.response) |
| 70 | + return sbom |
| 71 | + |
| 72 | + def have_relative_source_file_path(self, component: Component, bompath: str): |
| 73 | + ext_ref = CycloneDxSupport.get_ext_ref( |
| 74 | + component, ExternalReferenceType.DISTRIBUTION, CaPyCliBom.SOURCE_FILE_COMMENT) |
| 75 | + if not ext_ref: |
| 76 | + return |
| 77 | + |
| 78 | + bip = pathlib.PurePath(ext_ref.url) |
| 79 | + try: |
| 80 | + CycloneDxSupport.update_or_set_property( |
| 81 | + component, |
| 82 | + CycloneDxSupport.CDX_PROP_FILENAME, |
| 83 | + bip.name) |
| 84 | + file = bip.as_posix() |
| 85 | + if os.path.isfile(file): |
| 86 | + CycloneDxSupport.update_or_set_ext_ref( |
| 87 | + component, |
| 88 | + ExternalReferenceType.DISTRIBUTION, |
| 89 | + CaPyCliBom.SOURCE_FILE_COMMENT, |
| 90 | + "file://" + bip.relative_to(bompath).as_posix()) |
| 91 | + except ValueError: |
| 92 | + print_yellow( |
| 93 | + " SBOM file is not relative to source file " + ext_ref.url) |
| 94 | + # .relative_to |
| 95 | + pass |
| 96 | + |
| 97 | + def update_local_path(self, sbom: Bom, bomfile: str): |
| 98 | + bompath = pathlib.Path(bomfile).parent |
| 99 | + for component in sbom.components: |
| 100 | + self.have_relative_source_file_path(component, bompath) |
| 101 | + |
| 102 | + def run(self, args): |
| 103 | + """Main method |
| 104 | +
|
| 105 | + @params: |
| 106 | + args - command line arguments |
| 107 | + """ |
| 108 | + if args.debug: |
| 109 | + global LOG |
| 110 | + LOG = capycli.get_logger(__name__) |
| 111 | + else: |
| 112 | + # suppress (debug) log output from requests and urllib |
| 113 | + logging.getLogger("requests").setLevel(logging.WARNING) |
| 114 | + logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 115 | + logging.getLogger("urllib3.connectionpool").setLevel(logging.WARNING) |
| 116 | + |
| 117 | + print_text( |
| 118 | + "\n" + capycli.APP_NAME + ", " + capycli.get_app_version() + |
| 119 | + " - Download SW360 attachments as specified in the SBOM\n") |
| 120 | + |
| 121 | + if args.help: |
| 122 | + print("usage: capycli bom downloadattachments -i bom.json [-source <folder>]") |
| 123 | + print("") |
| 124 | + print("optional arguments:") |
| 125 | + print(" -h, --help show this help message and exit") |
| 126 | + print(" -i INPUTFILE, input SBOM file to read from (JSON)") |
| 127 | + print(" -source SOURCE source folder or additional source file") |
| 128 | + print(" -o OUTPUTFILE output file to write to") |
| 129 | + print(" -v be verbose") |
| 130 | + return |
| 131 | + |
| 132 | + if not args.inputfile: |
| 133 | + print_red("No input file specified!") |
| 134 | + sys.exit(ResultCode.RESULT_COMMAND_ERROR) |
| 135 | + |
| 136 | + if not os.path.isfile(args.inputfile): |
| 137 | + print_red("Input file not found!") |
| 138 | + sys.exit(ResultCode.RESULT_FILE_NOT_FOUND) |
| 139 | + |
| 140 | + print_text("Loading SBOM file " + args.inputfile) |
| 141 | + try: |
| 142 | + bom = CaPyCliBom.read_sbom(args.inputfile) |
| 143 | + except Exception as ex: |
| 144 | + print_red("Error reading input SBOM file: " + repr(ex)) |
| 145 | + sys.exit(ResultCode.RESULT_ERROR_READING_BOM) |
| 146 | + |
| 147 | + if args.verbose: |
| 148 | + print_text(" " + str(len(bom.components)) + "components read from SBOM file") |
| 149 | + |
| 150 | + source_folder = "./" |
| 151 | + if args.source: |
| 152 | + source_folder = args.source |
| 153 | + if (not source_folder) or (not os.path.isdir(source_folder)): |
| 154 | + print_red("Target source code folder does not exist!") |
| 155 | + sys.exit(ResultCode.RESULT_COMMAND_ERROR) |
| 156 | + |
| 157 | + if args.sw360_token and args.oauth2: |
| 158 | + self.analyze_token(args.sw360_token) |
| 159 | + |
| 160 | + print_text(" Checking access to SW360...") |
| 161 | + if not self.login(token=args.sw360_token, url=args.sw360_url, oauth2=args.oauth2): |
| 162 | + print_red("ERROR: login failed!") |
| 163 | + sys.exit(ResultCode.RESULT_AUTH_ERROR) |
| 164 | + |
| 165 | + print_text("Downloading source files to folder " + source_folder + " ...") |
| 166 | + |
| 167 | + self.download_attachments(bom, source_folder) |
| 168 | + |
| 169 | + if args.outputfile: |
| 170 | + print_text("Updating path information") |
| 171 | + self.update_local_path(bom, args.outputfile) |
| 172 | + |
| 173 | + print_text("Writing updated SBOM to " + args.outputfile) |
| 174 | + try: |
| 175 | + SbomWriter.write_to_json(bom, args.outputfile, True) |
| 176 | + except Exception as ex: |
| 177 | + print_red("Error writing updated SBOM file: " + repr(ex)) |
| 178 | + sys.exit(ResultCode.RESULT_ERROR_WRITING_BOM) |
| 179 | + |
| 180 | + if args.verbose: |
| 181 | + print_text(" " + str(len(bom.components)) + " components written to SBOM file") |
| 182 | + |
| 183 | + print("\n") |
0 commit comments