|
| 1 | +''' |
| 2 | +Created on May 20, 2022 |
| 3 | +@author: kumykov |
| 4 | +
|
| 5 | +Copyright (C) 2021 Synopsys, Inc. |
| 6 | +http://www.blackducksoftware.com/ |
| 7 | +
|
| 8 | +Licensed to the Apache Software Foundation (ASF) under one |
| 9 | +or more contributor license agreements. See the NOTICE file |
| 10 | +distributed with this work for additional information |
| 11 | +regarding copyright ownership. The ASF licenses this file |
| 12 | +to you under the Apache License, Version 2.0 (the |
| 13 | +"License"); you may not use this file except in compliance |
| 14 | +with the License. You may obtain a copy of the License at |
| 15 | +
|
| 16 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +
|
| 18 | +Unless required by applicable law or agreed to in writing, |
| 19 | +software distributed under the License is distributed on an |
| 20 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 21 | +KIND, either express or implied. See the License for the |
| 22 | +specific language governing permissions and limitations |
| 23 | +under the License. |
| 24 | +
|
| 25 | +This scrit will identify project versions that are using specified components |
| 26 | +and override policies for that component in each project version. |
| 27 | +Status will change from IN_VIOLATION to IN_VIOLATION_OVERRIDEN |
| 28 | +
|
| 29 | +identification of a project version is done with componet version url |
| 30 | +
|
| 31 | +''' |
| 32 | +import csv |
| 33 | +from pprint import pprint |
| 34 | +import sys |
| 35 | +import argparse |
| 36 | +import json |
| 37 | +import logging |
| 38 | +import arrow |
| 39 | +import re |
| 40 | +from pprint import pprint |
| 41 | + |
| 42 | +from itertools import islice |
| 43 | +from datetime import timedelta |
| 44 | +from datetime import datetime |
| 45 | +from blackduck import Client |
| 46 | + |
| 47 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 48 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 49 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 50 | +logging.getLogger("blackduck").setLevel(logging.DEBUG) |
| 51 | + |
| 52 | +def override_policy_violaton(version, component_name, component_version, override_rationale): |
| 53 | + params = {"q":f"componentOrVersionName:{component_name}"} |
| 54 | + components = bd.get_resource('components', version, params=params) |
| 55 | + for component in components: |
| 56 | + policy_status = bd.get_resource('policy-status', component, items=False) |
| 57 | + url = bd.list_resources(policy_status)['href'] |
| 58 | + data = { |
| 59 | + "approvalStatus" : "IN_VIOLATION_OVERRIDDEN", |
| 60 | + "comment" : f"{override_rationale}", |
| 61 | + "updatedAt" : datetime.now().isoformat() |
| 62 | + } |
| 63 | + headers = {"Content-Type": "application/vnd.blackducksoftware.bill-of-materials-6+json", |
| 64 | + "Accept": "application/vnd.blackducksoftware.bill-of-materials-6+json" } |
| 65 | + r = bd.session.put(url, headers = headers, json=data) |
| 66 | + # r.raise_for_status() |
| 67 | + logging.info(f"Policy status update completion code {r.status_code}") |
| 68 | + |
| 69 | +def parse_command_args(): |
| 70 | + |
| 71 | + parser = argparse.ArgumentParser("Print copyrights for BOM using upstream origin or prior version if not available.") |
| 72 | + parser.add_argument("-u", "--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url") |
| 73 | + parser.add_argument("-t", "--token-file", required=True, help="File containing access token") |
| 74 | + parser.add_argument("-cu", "--component-url", required=True, help="Project Name") |
| 75 | + parser.add_argument("-nv", "--no-verify", action='store_false', help="Disable TLS certificate verification") |
| 76 | + parser.add_argument("-or", "--override-rationale", required=True, help="Override rationale to dicplay") |
| 77 | + |
| 78 | + return parser.parse_args() |
| 79 | + |
| 80 | +def main(): |
| 81 | + args = parse_command_args() |
| 82 | + with open(args.token_file, 'r') as tf: |
| 83 | + access_token = tf.readline().strip() |
| 84 | + global bd |
| 85 | + bd = Client(base_url=args.base_url, token=access_token, verify=args.no_verify, timeout=60.0, retries=4) |
| 86 | + |
| 87 | + response = bd.session.get(args.component_url) |
| 88 | + print(response) |
| 89 | + component_version = response.json() |
| 90 | + component = bd.get_resource("component", component_version, items=False) |
| 91 | + component_name = component['name'] |
| 92 | + component_version_name = component_version['versionName'] |
| 93 | + print(f"processing references for {component_name} version {component_version_name}") |
| 94 | + override_rationale = args.override_rationale |
| 95 | + |
| 96 | + references = bd.get_resource("references", component_version) |
| 97 | + for project_version in references: |
| 98 | + override_policy_violaton(project_version, component_name, component_version_name, override_rationale ) |
| 99 | + # undo_override_policy_violaton(project_version, component_name, component_version_name, override_rationale ) |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + sys.exit(main()) |
0 commit comments