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 --path argument and iterates the root directory using os.walk
14 If a file is a binary, or contains a blacklisted string. If any violations
15 are found, the script adds the violation to a log file.
18 from __future__ import division, print_function, absolute_import
23 import anteater.utils.anteater_logger as antlog
24 import anteater.src.get_lists as get_lists
25 from binaryornot.check import is_binary
27 logger = antlog.Logger(__name__).getLogger()
28 config = ConfigParser.RawConfigParser()
29 config.read('anteater.conf')
30 reports_dir = config.get('config', 'reports_dir')
31 master_list = config.get('config', 'master_list')
32 ignore_dirs = ['.git']
33 hasher = hashlib.sha256()
36 def prepare_project(project, project_dir):
37 """ Generates blacklists / whitelists and calls main functions """
39 # Get Various Lists / Project Waivers
40 lists = get_lists.GetLists()
42 # Get binary white list
43 binary_list = lists.binary_list(project)
45 # Get file name black list and project waivers
46 file_audit_list, file_audit_project_list = lists.file_audit_list(project)
48 # Get file content black list and project waivers
49 file_content_list, project_content_list = lists.file_content_list(project)
52 licence_ext = lists.licence_extensions()
53 licence_ignore = lists.licence_ignore()
55 # Perform rudimentary scans
56 scan_file(project_dir, project, binary_list,file_audit_list,
57 file_audit_project_list, file_content_list,
60 # Perform licence header checks
61 licence_check(licence_ext, licence_ignore, project, project_dir)
62 licence_root_check(project_dir, project)
65 def scan_file(project_dir, project, binary_list, file_audit_list,
66 file_audit_project_list, file_content_list,
67 project_content_list):
68 """Searches for banned strings and files that are listed """
69 for root, dirs, files in os.walk(project_dir):
70 # Filter out ignored directories from list.
71 dirs[:] = [d for d in dirs if d not in ignore_dirs]
73 full_path = os.path.join(root, items)
74 # Check for Blacklisted file names
75 if file_audit_list.search(full_path) and not \
76 file_audit_project_list.search(full_path):
77 match = file_audit_list.search(full_path)
78 logger.error('Blacklisted filename: {0}'.
80 logger.error('Matched String: {0}'.
81 format(match.group()))
82 with open(reports_dir + "file-names_" + project + ".log",
85 write('Blacklisted filename: {0}\n'.
88 write('Matched String: {0}'.
89 format(match.group()))
91 if not is_binary(full_path):
92 fo = open(full_path, 'r')
93 lines = fo.readlines()
95 # Check for sensitive content in project files
96 if file_content_list.search(line) and not \
97 project_content_list.search(line):
98 match = file_content_list.search(line)
99 logger.error('File contains violation: {0}'.
101 logger.error('Flagged Content: {0}'.
102 format(line.rstrip()))
103 logger.error('Matched String: {0}'.
104 format(match.group()))
105 with open(reports_dir + "contents-" + project + ".log",
109 write('File contains violation: {0}\n'.
112 write('Flagged Content: {0}'.
115 write('Matched String: {0}\n'.
116 format(match.group()))
118 # Check if Binary is whitelisted
119 hashlist = get_lists.GetLists()
120 binary_hash = hashlist.binary_hash(project, full_path)
121 if not binary_list.search(full_path):
122 with open(full_path, 'rb') as afile:
125 if hasher.hexdigest() in binary_hash:
126 logger.info('Found matching file hash for file: {0}'.
129 logger.error('Non Whitelisted Binary file: {0}'.
131 logger.error('Please submit patch with this hash: {0}'.
132 format(hasher.hexdigest()))
133 with open(reports_dir + "binaries-" + project + ".log",
136 gate_report.write('Non Whitelisted Binary: {0}\n'.
140 def licence_root_check(project_dir, project):
141 if os.path.isfile(project_dir + '/LICENSE'):
142 logger.info('LICENSE file present in: {0}'.
145 logger.error('LICENSE file missing in: {0}'.
147 with open(reports_dir + "licence-" + project + ".log",
150 gate_report.write('LICENSE file missing in: {0}\n'.
154 def licence_check(licence_ext, licence_ignore, project, project_dir):
155 """ Peform basic checks for the presence of licence strings """
156 for root, dirs, files in os.walk(project_dir):
157 dirs[:] = [d for d in dirs if d not in ignore_dirs]
159 if file.endswith(tuple(licence_ext)) \
160 and file not in licence_ignore:
161 full_path = os.path.join(root, file)
162 if not is_binary(full_path):
163 fo = open(full_path, 'r')
165 # Note: Hardcoded use of 'copyright' & 'spdx' is the result
166 # of a decision made at 2017 plugfest to limit searches to
167 # just these two strings.
168 if re.search("copyright", content, re.IGNORECASE):
169 logger.info('Licence string present: {0}'.
171 elif re.search("spdx", content, re.IGNORECASE):
172 logger.info('Licence string present: {0}'.
175 logger.error('Licence header missing: {0}'.
177 with open(reports_dir + "licence-" + project + ".log",
180 gate_report.write('Licence header missing: {0}\n'.