|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import requests |
| 6 | +import urllib3 |
| 7 | +from snyk import SnykClient |
| 8 | +from utils import get_default_token_path, get_token |
| 9 | + |
| 10 | +def parse_command_line_args(): |
| 11 | + parser = argparse.ArgumentParser(description="Snyk API Examples") |
| 12 | + parser.add_argument("--orgId", type=str, |
| 13 | + help="The Snyk Organisation Id", required=True) |
| 14 | + # Store issueId as list (--issueIdList=SNYK-JS-HANDLEBARS-173692,SNYK-JS-JSYAML-174129 as example) |
| 15 | + parser.add_argument("--issueIdList", type=str, |
| 16 | + help="The Snyk Issue IdList", required=True) |
| 17 | + parser.add_argument("--reasonType", type=str, |
| 18 | + help="Ignore Reason Type", required=True) |
| 19 | + parser.add_argument("--expirationTime", type=str, |
| 20 | + help="Optional. Expiration time of ignore. e.g. yyyy-mm-dd or yyyy-mm-ddThh:mm:ss.aaaZ",) |
| 21 | + parser.add_argument("--reason", type=str, |
| 22 | + help="Optional. Reason for ignoring e.g. \"We do not use this library.\"",) |
| 23 | + args = parser.parse_args() |
| 24 | + return args |
| 25 | +snyk_token_path = get_default_token_path() |
| 26 | +snyk_token = get_token(snyk_token_path) |
| 27 | +args = parse_command_line_args() |
| 28 | +org_id = args.orgId |
| 29 | +issue_ids = args.issueIdList.split(',') # split issue list to run the loop |
| 30 | +reason_type = args.reasonType |
| 31 | +time = args.expirationTime |
| 32 | +reason = args.reason |
| 33 | +# Regex to check if the date is valid |
| 34 | +datere = "[2-9][0-9][0-9][0-9]-[0-2][0-9]-[0-3][0-9]" |
| 35 | +datetimere = "[2-9][0-9][0-9][0-9]-[0-2][0-9]-[0-3][0-9]T[0-2][0-4]:[0-5][0-9]:[0-6][0-9].[0-9][0-9][0-9]Z" |
| 36 | +expires = None |
| 37 | +# Logic to check if a reason and/or time was added |
| 38 | +if time is None: |
| 39 | + confirm = 0 |
| 40 | +else: |
| 41 | + if re.match(datere, time) or re.match(datetimere, time): |
| 42 | + print("Valid Time Arguments") |
| 43 | + expires = time |
| 44 | + confirm = 1 |
| 45 | + else: |
| 46 | + print("Please use a date in yyyy-mm-ddThh or yyyy-mm-ddThh:mm:ss.aaaZ format") |
| 47 | + sys.exit() |
| 48 | +if reason is None: |
| 49 | + print("No reason given") |
| 50 | +else: |
| 51 | + if confirm == 1: |
| 52 | + print("Reason given!") |
| 53 | + confirm = 2 |
| 54 | + else: |
| 55 | + confirm = 3 |
| 56 | +client = SnykClient(token=snyk_token) |
| 57 | +# API call to collect every project in all of a customers orgs |
| 58 | + |
| 59 | +for proj in client.organizations.get(org_id).projects.all(): |
| 60 | + print("\nProject name: %s" % proj.name) |
| 61 | + print(" Issues Found:") |
| 62 | + print(" High : %s" % proj.issueCountsBySeverity.high) |
| 63 | + print(" Medium: %s" % proj.issueCountsBySeverity.medium) |
| 64 | + print(" Low : %s" % proj.issueCountsBySeverity.low) |
| 65 | + url = "org/" + org_id + "/project/" + proj.id + "/issues" |
| 66 | + print(url) |
| 67 | + # API call to grab all of the issue |
| 68 | + r = client.post(url, None) |
| 69 | + # Converts JSON to a python dict |
| 70 | + parsed_input = r.json() |
| 71 | + print (parsed_input) |
| 72 | + issues = parsed_input["issues"] |
| 73 | + print("List the Vulnerbilities") |
| 74 | + print (issues["vulnerabilities"]) |
| 75 | + for i in issues["vulnerabilities"]: |
| 76 | + # HERE |
| 77 | + if i["id"] in issue_ids: |
| 78 | + values_object = { |
| 79 | + "ignorePath": "", |
| 80 | + "reasonType": reason_type, |
| 81 | + "disregardIfFixable": False |
| 82 | + } |
| 83 | + if reason is not None: |
| 84 | + values_object["reason"] = reason |
| 85 | + if expires is not None: |
| 86 | + values_object["expires"] = expires |
| 87 | + api_url = "org/%s/project/%s/ignore/%s" % (org_id, proj.id , i["id"]) |
| 88 | + r2 = client.post(api_url, values_object) |
0 commit comments