2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 # Copyright (c) 2017 Luke Hinds <lhinds@redhat.com>, Red Hat
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
13 Accepts the --patchset argument and iterates through each line of the
14 patchset file to perform various checks such as if the file is a binary, or
15 contains a blacklisted string. If any violations are found, the script
16 exits with code 1 and logs the violation(s) found.
19 from __future__ import division, print_function, absolute_import
20 from binaryornot.check import is_binary
23 import six.moves.configparser
27 from . import get_lists
29 logger = logging.getLogger(__name__)
30 config = six.moves.configparser.RawConfigParser()
31 config.read('anteater.conf')
32 reports_dir = config.get('config', 'reports_dir')
34 hasher = hashlib.sha256()
37 def prepare_patchset(project, patchset):
38 """ Create black/white lists and default / project waivers
39 and iterates over patchset file """
41 # Get Various Lists / Project Waivers
42 lists = get_lists.GetLists()
43 # Get binary white list
44 binary_list = lists.binary_list(project)
46 # Get file name black list and project waivers
47 file_audit_list, file_audit_project_list = lists.file_audit_list(project)
49 # Get file content black list and project waivers
51 file_content_project_list = lists.file_content_list(project)
54 licence_ext = lists.licence_extensions()
55 licence_ignore = lists.licence_ignore()
57 # Open patch set to get file list
58 fo = open(patchset, 'r')
59 lines = fo.readlines()
62 patch_file = line.strip('\n')
63 # Perform binary and file / content checks
64 scan_patch(project, patch_file, binary_list,
65 file_audit_list, file_audit_project_list,
66 file_content_list, file_content_project_list, licence_ext,
69 # Process each file in patch set using waivers generated above
70 # Process final result
74 def scan_patch(project, patch_file, binary_list, file_audit_list,
75 file_audit_project_list, file_content_list,
76 file_content_project_list, licence_ext, licence_ignore):
77 """ Scan actions for each commited file in patch set """
79 if is_binary(patch_file):
80 hashlist = get_lists.GetLists()
81 binary_hash = hashlist.binary_hash(project, patch_file)
82 if not binary_list.search(patch_file):
83 with open(patch_file, 'rb') as afile:
86 if hasher.hexdigest() in binary_hash:
87 logger.info('Found matching file hash for file: %s',
90 logger.error('Non Whitelisted Binary file: %s',
92 logger.error('Submit patch with the following hash: %s',
95 with open(reports_dir + "binaries-" + project + ".log", "a") \
97 gate_report.write('Non Whitelisted Binary file: {0}\n'.
100 # Check file names / extensions
101 if file_audit_list.search(patch_file) and not \
102 file_audit_project_list.search(patch_file):
103 match = file_audit_list.search(patch_file)
104 logger.error('Blacklisted file: %s', patch_file)
105 logger.error('Matched String: %s', match.group())
107 with open(reports_dir + "file-names_" + project + ".log", "a") \
109 gate_report.write('Blacklisted file: {0}\n'.
111 gate_report.write('Matched String: {0}'.
112 format(match.group()))
114 # Open file to check for blacklisted content
115 fo = open(patch_file, 'r')
116 lines = fo.readlines()
119 if file_content_list.search(line) and not \
120 file_content_project_list.search(line):
121 match = file_content_list.search(line)
122 logger.error('File contains violation: %s', patch_file)
123 logger.error('Flagged Content: %s', line.rstrip())
124 logger.error('Matched String: %s', match.group())
126 with open(reports_dir + "contents_" + project + ".log",
128 gate_report.write('File contains violation: {0}\n'.
130 gate_report.write('Flagged Content: {0}'.
132 gate_report.write('Matched String: {0}\n'.
133 format(match.group()))
136 licence_check(project, licence_ext, licence_ignore, patch_file)
139 def licence_check(project, licence_ext,
140 licence_ignore, patch_file):
141 """ Performs licence checks """
143 if patch_file.endswith(tuple(licence_ext)) \
144 and patch_file not in licence_ignore:
145 fo = open(patch_file, 'r')
147 # Note: Hardcoded use of 'copyright' & 'spdx' is the result
148 # of a decision made at 2017 plugfest to limit searches to
149 # just these two strings.
150 if re.search("copyright", content, re.IGNORECASE):
151 logger.info('Contains needed Licence string: %s', patch_file)
152 elif re.search("spdx", content, re.IGNORECASE):
153 logger.info('Contains needed Licence string: %s', patch_file)
155 logger.error('Licence header missing in file: %s', patch_file)
157 with open(reports_dir + "licence-" + project + ".log", "a") \
159 gate_report.write('Licence header missing in file: {0}\n'.
163 def process_failure():
164 """ If any scan operations register a failure, sys.exit(1) is called
165 to allow jjb to register a failure"""
167 logger.error('Please visit: https://wiki.opnfv.org/x/5oey')