def test_sorts_by_line_number_then_hash(self): output_string = format_baseline_for_output({ 'results': { 'filename': [ { 'hashed_secret': 'a', 'line_number': 3, }, { 'hashed_secret': 'z', 'line_number': 2, }, { 'hashed_secret': 'f', 'line_number': 3, }, ], }, }) ordered_hashes = list( map( lambda x: x['hashed_secret'], json.loads(output_string)['results']['filename'], ), ) assert ordered_hashes == ['z', 'a', 'f']
def write_baseline_to_file(filename, data): """ :type filename: str :type data: dict :rtype: None """ with open(filename, 'w') as f: # pragma: no cover f.write(format_baseline_for_output(data) + '\n')
def test_sorts_by_line_number_then_hash_then_type(self): output_string = format_baseline_for_output({ 'results': { 'filename': [ # Output order is reverse of this { 'hashed_secret': 'f', 'line_number': 3, 'type': 'LetterDetector', }, { 'hashed_secret': 'a', 'line_number': 3, 'type': 'LetterDetector', }, { 'hashed_secret': 'a', 'line_number': 3, 'type': 'DifferentDetector', }, { 'hashed_secret': 'z', 'line_number': 2, 'type': 'LetterDetector', }, ], }, }) assert ''.join(output_string.split()) == ''.join( """ { "results": { "filename": [ { "hashed_secret": "z", "line_number": 2, "type": "LetterDetector" }, { "hashed_secret": "a", "line_number": 3, "type": "DifferentDetector" }, { "hashed_secret": "a", "line_number": 3, "type": "LetterDetector" }, { "hashed_secret": "f", "line_number": 3, "type": "LetterDetector" } ] } } """.split(), )
def main(argv=None): if len(sys.argv) == 1: # pragma: no cover sys.argv.append('-h') args = parse_args(argv) if args.verbose: # pragma: no cover log.set_debug_level(args.verbose) if args.action == 'scan': # Plugins are *always* rescanned with fresh settings, because # we want to get the latest updates. plugins = initialize.from_parser_builder(args.plugins) if args.string: line = args.string if isinstance(args.string, bool): line = sys.stdin.read().splitlines()[0] _scan_string(line, plugins) else: baseline_dict = _perform_scan( args, plugins, ) if args.import_filename: write_baseline_to_file( filename=args.import_filename[0], data=baseline_dict, ) else: print(baseline.format_baseline_for_output(baseline_dict, ), ) elif args.action == 'audit': if not args.diff: audit.audit_baseline(args.filename[0]) return 0 if len(args.filename) != 2: print( 'Must specify two files to compare!', file=sys.stderr, ) return 1 try: audit.compare_baselines(args.filename[0], args.filename[1]) except audit.RedundantComparisonError: print( 'No difference, because it\'s the same file!', file=sys.stderr, ) return 0
def main(argv=sys.argv[1:]): if len(sys.argv) == 1: # pragma: no cover sys.argv.append('--help') args = parse_args(argv) if args.verbose: # pragma: no cover log.set_debug_level(args.verbose) if args.action == 'scan': automaton = None word_list_hash = None if args.word_list_file: automaton, word_list_hash = build_automaton(args.word_list_file) # Plugins are *always* rescanned with fresh settings, because # we want to get the latest updates. plugins = initialize.from_parser_builder( plugins_dict=args.plugins, custom_plugin_paths=args.custom_plugin_paths, exclude_lines_regex=args.exclude_lines, automaton=automaton, should_verify_secrets=not args.no_verify, ) if args.string: line = args.string if isinstance(args.string, bool): line = sys.stdin.read().splitlines()[0] _scan_string(line, plugins) else: baseline_dict = _perform_scan( args, plugins, automaton, word_list_hash, ) if args.import_filename: write_baseline_to_file( filename=args.import_filename[0], data=baseline_dict, ) else: print(baseline.format_baseline_for_output(baseline_dict, ), ) elif args.action == 'audit': if not args.diff and not args.display_results: audit.audit_baseline(args.filename[0]) return 0 if args.display_results: audit.print_audit_results(args.filename[0]) return 0 if len(args.filename) != 2: print( 'Must specify two files to compare!', file=sys.stderr, ) return 1 try: audit.compare_baselines(args.filename[0], args.filename[1]) except audit.RedundantComparisonError: print( 'No difference, because it\'s the same file!', file=sys.stderr, ) return 0
def _write_to_baseline_file(filename, payload): # pragma: no cover """Breaking this function up for mockability.""" with open(filename, 'w') as f: f.write(format_baseline_for_output(payload))