def text_pattern_search(row, *args): for line in args[1]: if row.text_pattern in line: scan_results.append( ScanItem(file_name=args[0], file_category=row.file_category, file_type=row.file_type, refactor_rating=row.refactor_rating))
def xml_file_scan(file_lines, filename): xmlrules = rulebase.loc[(rulebase['file_type'] == "config") & (rulebase['file_id'] == "*.xml") & (rulebase['text_pattern'] != "NONE")] global scan_results xml_matches = xmlrules.loc[xmlrules['text_pattern'].isin(file_lines)] for index, row in xml_matches.iterrows(): scan_results.append( ScanItem(file_name=file, file_category=row['file_category'], file_type=row['file_type'], refactor_rating=row['refactor_rating']))
def load_rules(rules_csv): global rulebase rulebase = [] with open(rules_csv, 'rU') as csvfile: rulereader = csv.DictReader(csvfile, delimiter=',') for row in rulereader: rulebase.append( ScanItem(app_type=row['app_type'], file_type=row['file_type'], file_category=row['file_category'], file_name=row['file_name'], refactor_rating=row['refactor_rating'], description=row['description'], text_pattern=row['text_pattern']))
def config_scan(file_path_list): configrules = rulebase.loc[(rulebase['file_type'] == "config") & (rulebase['app_type'] == "java")] global scan_results file_names = [] for path in file_path_list: if path.endswith('/'): path = path[:-1] file_names.append(path.split('/')[-1]) config_matches = configrules.loc[configrules['file_id'].isin(file_names)] for index, row in config_matches.iterrows(): scan_results.append( ScanItem(file_name=row['file_id'], file_category=row['file_category'], file_type=row['file_type'], refactor_rating=row['refactor_rating']))
def load_yaml_rules_stream(yaml_stream): rules = [] yaml_rules = yaml.load(yaml_stream) for rule in yaml_rules: app_type = rule.get('app_type') category = rule.get('category') file_type = rule.get('file_type') file_pattern = rule.get("file_pattern") refactor_rating = rule.get('refactor_rating') description = rule.get('description') replatform_advice = rule.get('replatform_advice') if file_pattern is not None: text_patterns = rule.get("text_patterns") if text_patterns is not None: for text_pattern in text_patterns: # Text patterns can have overrides, so we have to account for that override_refactor_rating = refactor_rating override_description = description override_replatform_advice = replatform_advice text_pattern_key = text_pattern if not isinstance(text_pattern, basestring): text_pattern_key = text_pattern.keys()[0] pattern_override_map = text_pattern[ text_pattern.keys()[0]] if 'refactor_rating' in pattern_override_map: override_refactor_rating = pattern_override_map.get( 'refactor_rating') if 'description' in pattern_override_map: override_description = pattern_override_map.get( 'description') if 'replatform_advice' in pattern_override_map: override_replatform_advice = pattern_override_map.get( 'replatform_advice') rules.append( ScanItem(app_type, category, file_type, file_pattern, override_refactor_rating, override_description, text_pattern_key, override_replatform_advice)) else: rules.append( ScanItem(app_type, category, file_type, file_pattern, refactor_rating, description, None, replatform_advice)) elif rule.get("files") is not None: text_patterns = rule.get("text_patterns") for file_item in rule.get("files"): # Files can have overrides, so we have to account for that file_override_refactor_rating = refactor_rating file_override_description = description file_override_replatform_advice = replatform_advice file_item_key = file_item if not isinstance(file_item, basestring): file_item_key = file_item.keys()[0] file_override_map = file_item[file_item.keys()[0]] if 'refactor_rating' in file_override_map: file_override_refactor_rating = file_override_map.get( 'refactor_rating') if 'description' in file_override_map: file_override_description = file_override_map.get( 'description') if 'replatform_advice' in file_override_map: file_override_replatform_advice = file_override_map.get( 'replatform_advice') if text_patterns is None: rules.append( ScanItem(app_type, category, file_type, file_item_key, file_override_refactor_rating, file_override_description, None, file_override_replatform_advice)) else: for text_pattern in text_patterns: # Text patterns can have overrides, so we have to account for that # We'll use the file_override data because at this point it will have # either the overriden data from a file entry, or the original rule data pattern_override_refactor_rating = file_override_refactor_rating pattern_override_description = file_override_description pattern_override_replatform_advice = file_override_replatform_advice text_pattern_key = text_pattern if not isinstance(text_pattern, basestring): text_pattern_key = text_pattern.keys()[0] pattern_override_map = text_pattern[ text_pattern.keys()[0]] if 'refactor_rating' in pattern_override_map: pattern_override_refactor_rating = pattern_override_map.get( 'refactor_rating') if 'description' in pattern_override_map: pattern_override_description = pattern_override_map.get( 'description') if 'replatform_advice' in pattern_override_map: pattern_override_replatform_advice = pattern_override_map.get( 'replatform_advice') rules.append( ScanItem(app_type, category, file_type, file_item_key, pattern_override_refactor_rating, pattern_override_description, text_pattern_key, pattern_override_replatform_advice)) else: raise RuntimeError( "Error parsing rule. No file_pattern value or files array specified.", rule) set_rulebase(rules)