|
| 1 | +''' |
| 2 | +Export the vulnerabilites from a project version to CSV. Can be used to apply batch vulnerability |
| 3 | +remediation with vuln_batch_remediation.py |
| 4 | +''' |
| 5 | +from blackduck import Client |
| 6 | +import logging |
| 7 | +import csv |
| 8 | +import argparse |
| 9 | +from pprint import pprint |
| 10 | +import os |
| 11 | +import sys |
| 12 | +from dotenv import load_dotenv |
| 13 | + |
| 14 | +load_dotenv() |
| 15 | + |
| 16 | +API_TOKEN = os.getenv('API_TOKEN') |
| 17 | + |
| 18 | +logging.basicConfig( |
| 19 | + level=logging.DEBUG, |
| 20 | + format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s" |
| 21 | +) |
| 22 | + |
| 23 | +def main(): |
| 24 | + program_name = os.path.basename(sys.argv[0]) |
| 25 | + parser = argparse.ArgumentParser(prog=program_name, usage="%(prog)s [options]", description="Automated Assessment") |
| 26 | + parser.add_argument("--output", required=False,help="csv output path" ) |
| 27 | + parser.add_argument("--project", required=True, help="project name") |
| 28 | + parser.add_argument("--version", required=False, help="project version, e.g. latest") |
| 29 | + parser.add_argument("--component", required=False, help="component name") |
| 30 | + args = parser.parse_args() |
| 31 | + |
| 32 | + component = args.component |
| 33 | + projectname = args.project |
| 34 | + projectversion = args.version |
| 35 | + output = args.output if args.output != None else "output.csv" |
| 36 | + |
| 37 | + csv_file = open(output, mode='w', newline='', encoding='utf-8') |
| 38 | + csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) |
| 39 | + |
| 40 | + bd = Client( |
| 41 | + token=API_TOKEN, |
| 42 | + base_url="https://blackduck.omicron.at", |
| 43 | + verify=False # TLS certificate verification |
| 44 | + ) |
| 45 | + |
| 46 | + for project in bd.get_resource('projects'): |
| 47 | + if (project['name'] == projectname): |
| 48 | + for version in bd.get_resource('versions', project): |
| 49 | + |
| 50 | + if (projectversion == None): |
| 51 | + pprint(version['versionName']) |
| 52 | + |
| 53 | + else: |
| 54 | + if (version['versionName'] == projectversion): |
| 55 | + for vulnverable_component in bd.get_resource('vulnerable-components', version): |
| 56 | + # TODO maybe match component name with regex? |
| 57 | + if (vulnverable_component['componentName'] == component or component == None): |
| 58 | + |
| 59 | + componentName = vulnverable_component["componentName"] |
| 60 | + componentVersion = vulnverable_component["componentVersionName"] |
| 61 | + |
| 62 | + remediation = vulnverable_component['vulnerabilityWithRemediation'] |
| 63 | + |
| 64 | + name = remediation['vulnerabilityName'] |
| 65 | + status = remediation['remediationStatus'] |
| 66 | + description = remediation['description'].replace('\r', '').replace('\n', '') |
| 67 | + |
| 68 | + try: |
| 69 | + comment = remediation['remediationComment'].replace('\r', '').replace('\n', '') |
| 70 | + except KeyError: |
| 71 | + comment = "" |
| 72 | + |
| 73 | + row = [name, status, comment, componentName, componentVersion, description] |
| 74 | + csv_writer.writerow(row) |
| 75 | + break |
| 76 | + break |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments