|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +''' |
| 4 | +Created on Feb 21, 2020 |
| 5 | +
|
| 6 | +@author: gsnyder |
| 7 | +
|
| 8 | +Retrieve BOM computed notifications |
| 9 | +
|
| 10 | +Note: The user account you run this under will determine the scope (i.e. projects, versions) of |
| 11 | +the notifications that can be received. |
| 12 | +
|
| 13 | +''' |
| 14 | + |
| 15 | +# TODO: Use startDate filter on /api/notifications to limit the notifications retrieved when using -n |
| 16 | + |
| 17 | +import argparse |
| 18 | +from datetime import datetime |
| 19 | +import json |
| 20 | +import logging |
| 21 | +import pytz |
| 22 | +import sys |
| 23 | +import timestring |
| 24 | + |
| 25 | +from terminaltables import AsciiTable |
| 26 | + |
| 27 | +from blackduck.HubRestApi import HubInstance, object_id |
| 28 | + |
| 29 | +# |
| 30 | +# Example usage: |
| 31 | +# |
| 32 | +# To get all the vulnerability notices, |
| 33 | +# python examples/get_vulnerability_notifications.py > all_vuln_notifications.json |
| 34 | +# |
| 35 | +# To get all the vulnerability notices and save the date/time of the last run, |
| 36 | +# python examples/get_vulnerability_notifications.py -s > all_vuln_notifications.json |
| 37 | +# |
| 38 | +# To get all the vulnerability notices since the last run, |
| 39 | +# python examples/get_vulnerability_notifications.py -n `cat .last_run` > all_vuln_notifications.json |
| 40 | +# |
| 41 | +# To get all the vulnerability notices since a date/time, |
| 42 | +# python examples/get_vulnerability_notifications.py -n "March 29, 2019 12:00" > since_mar_29_at_noon_vuln_notifications.json |
| 43 | +# |
| 44 | +# To get all the vulnerability notices for a given project, |
| 45 | +# python examples/get_vulnerability_notifications.py -p my-project > all_vuln_notifications_for_my_project.json |
| 46 | +# |
| 47 | +# To get all the vulnerability notices for a given project and version, |
| 48 | +# python examples/get_vulnerability_notifications.py -p my-project -v 1.0 > all_vuln_notifications_for_my_project_v1.0.json |
| 49 | +# |
| 50 | +# |
| 51 | + |
| 52 | + |
| 53 | +parser = argparse.ArgumentParser("Retreive BOM computed notifications") |
| 54 | +parser.add_argument("-p", "--project", help="If supplied, filter the notifications to this project") |
| 55 | +parser.add_argument("-v", "--version", help="If supplied, filter the notifications to this version (requires a project)") |
| 56 | +parser.add_argument("-n", "--newer_than", |
| 57 | + default=None, |
| 58 | + type=str, |
| 59 | + help="Set this option to see all vulnerability notifications published since the given date/time.") |
| 60 | +parser.add_argument("-d", "--save_dt", |
| 61 | + action='store_true', |
| 62 | + help="If set, the date/time will be saved to a file named '.last_run' in the current directory which can be used later with the -n option to see vulnerabilities published since the last run.") |
| 63 | +parser.add_argument("-l", "--limit", default=100000, help="To change the limit on the number of notifications to retrieve") |
| 64 | +parser.add_argument("-s", "--system", action='store_true', help="Pull notifications from the system as opposed to the user's account") |
| 65 | +args = parser.parse_args() |
| 66 | + |
| 67 | +if args.newer_than: |
| 68 | + newer_than = timestring.Date(args.newer_than).date |
| 69 | + # adjust to UTC so the comparison is normalized |
| 70 | + newer_than = newer_than.astimezone(pytz.utc) |
| 71 | +else: |
| 72 | + newer_than = None |
| 73 | + |
| 74 | +if args.save_dt: |
| 75 | + with open(".last_run", "w") as f: |
| 76 | + f.write(datetime.now().isoformat()) |
| 77 | + |
| 78 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 79 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 80 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 81 | + |
| 82 | +hub = HubInstance() |
| 83 | +current_user = hub.get_current_user() |
| 84 | + |
| 85 | +# Construct the URL to either pull from the system or user account scope, |
| 86 | +# and then narrow the search to only include BOM computed |
| 87 | +if args.system: |
| 88 | + notifications_url = "{}/api/notifications".format(hub.get_urlbase()) |
| 89 | +else: |
| 90 | + notifications_url = hub.get_link(current_user, "notifications") |
| 91 | + |
| 92 | +notifications_url = "{}?limit={}&filter=notificationType:VERSION_BOM_CODE_LOCATION_BOM_COMPUTED".format( |
| 93 | + notifications_url, args.limit) |
| 94 | + |
| 95 | +if newer_than: |
| 96 | + start_date = newer_than.strftime("%Y-%m-%dT%H:%M:%S.%fZ") |
| 97 | + notifications_url += "&startDate=" + start_date |
| 98 | + |
| 99 | +bom_computed_notifications = hub.execute_get(notifications_url).json().get('items', []) |
| 100 | + |
| 101 | +# if newer_than: |
| 102 | +# bom_computed_notifications = list( |
| 103 | +# filter(lambda n: timestring.Date(n['createdAt']) > newer_than, bom_computed_notifications)) |
| 104 | +if args.project: |
| 105 | + bom_computed_notifications = list( |
| 106 | + filter(lambda n: args.project in [apv['projectName'] for apv in n['content']['affectedProjectVersions']], |
| 107 | + bom_computed_notifications)) |
| 108 | + if args.version: |
| 109 | + bom_computed_notifications = list( |
| 110 | + filter(lambda n: args.version in [apv['projectVersionName'] for apv in n['content']['affectedProjectVersions']], |
| 111 | + bom_computed_notifications)) |
| 112 | + |
| 113 | +print(json.dumps(bom_computed_notifications)) |
0 commit comments