|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +import json |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from builtins import input |
| 7 | +from collections import defaultdict |
| 8 | + |
| 9 | +from .baseline import merge_results |
| 10 | +from .color import BashColor |
| 11 | +from .color import Color |
| 12 | + |
| 13 | + |
| 14 | +def audit_baseline(baseline_filename): |
| 15 | + original_baseline = _get_baseline_from_file(baseline_filename) |
| 16 | + if not original_baseline: |
| 17 | + return |
| 18 | + |
| 19 | + results = defaultdict(list) |
| 20 | + for filename, secret in _secret_generator(original_baseline): |
| 21 | + _clear_screen() |
| 22 | + _print_context(filename, secret) |
| 23 | + |
| 24 | + decision = _get_user_decision() |
| 25 | + if decision == 'q': |
| 26 | + print('Quitting...') |
| 27 | + break |
| 28 | + |
| 29 | + _handle_user_decision(decision, secret) |
| 30 | + results[filename].append(secret) |
| 31 | + |
| 32 | + print('Saving progress...') |
| 33 | + original_baseline['results'] = merge_results( |
| 34 | + original_baseline['results'], |
| 35 | + dict(results), |
| 36 | + ) |
| 37 | + _save_baseline_to_file(baseline_filename, original_baseline) |
| 38 | + |
| 39 | + |
| 40 | +def _clear_screen(): |
| 41 | + subprocess.call(['clear']) |
| 42 | + |
| 43 | + |
| 44 | +def _print_context(filename, secret): # pragma: no cover |
| 45 | + print('{} {}'.format( |
| 46 | + BashColor.color( |
| 47 | + 'Filename:', |
| 48 | + Color.BOLD, |
| 49 | + ), |
| 50 | + BashColor.color( |
| 51 | + filename, |
| 52 | + Color.PURPLE, |
| 53 | + ), |
| 54 | + )) |
| 55 | + print('-' * 10) |
| 56 | + print(_get_secret_with_context( |
| 57 | + filename, |
| 58 | + secret['line_number'], |
| 59 | + 5, |
| 60 | + )) |
| 61 | + print('-' * 10) |
| 62 | + |
| 63 | + |
| 64 | +def _handle_user_decision(decision, secret): |
| 65 | + if decision == 'y': |
| 66 | + secret['is_secret'] = True |
| 67 | + elif decision == 'n': |
| 68 | + secret['is_secret'] = False |
| 69 | + |
| 70 | + |
| 71 | +def _get_baseline_from_file(filename): # pragma: no cover |
| 72 | + try: |
| 73 | + with open(filename) as f: |
| 74 | + return json.loads(f.read()) |
| 75 | + except (IOError, json.decoder.JSONDecodeError): |
| 76 | + print('Not a valid baseline file!', file=sys.stderr) |
| 77 | + return |
| 78 | + |
| 79 | + |
| 80 | +def _save_baseline_to_file(filename, data): |
| 81 | + with open(filename, 'w') as f: |
| 82 | + f.write(json.dumps( |
| 83 | + data, |
| 84 | + indent=2, |
| 85 | + sort_keys=True, |
| 86 | + )) |
| 87 | + |
| 88 | + |
| 89 | +def _secret_generator(baseline): |
| 90 | + for filename, secrets in baseline['results'].items(): |
| 91 | + for secret in secrets: |
| 92 | + try: |
| 93 | + secret['is_secret'] |
| 94 | + except KeyError: |
| 95 | + yield filename, secret |
| 96 | + |
| 97 | + break |
| 98 | + |
| 99 | + |
| 100 | +def _get_secret_with_context(filename, secret_lineno, lines_of_context): |
| 101 | + """ |
| 102 | + Displays the secret, with surrounding lines of code for better context. |
| 103 | +
|
| 104 | + :type filename: str |
| 105 | + :param filename: filename where secret resides in |
| 106 | +
|
| 107 | + :type secret_lineno: int |
| 108 | + :param secret_lineno: line where secret is found |
| 109 | +
|
| 110 | + :type lines_of_context: int |
| 111 | + :param lines_of_context: number of lines displayed before and after |
| 112 | + secret. |
| 113 | + """ |
| 114 | + start_line = 1 if secret_lineno <= lines_of_context \ |
| 115 | + else secret_lineno - lines_of_context |
| 116 | + end_line = secret_lineno + lines_of_context |
| 117 | + |
| 118 | + output = subprocess.check_output([ |
| 119 | + 'sed', |
| 120 | + '-n', '{},{}p'.format(start_line, end_line), |
| 121 | + filename, |
| 122 | + ]).decode('utf-8') |
| 123 | + |
| 124 | + # TODO: Highlight the secret found. |
| 125 | + |
| 126 | + return '\n'.join( |
| 127 | + map( |
| 128 | + lambda x: '{}:{}'.format( |
| 129 | + BashColor.color( |
| 130 | + str(int(x[0]) + start_line), |
| 131 | + Color.LIGHT_GREEN, |
| 132 | + ), |
| 133 | + x[1] |
| 134 | + ), |
| 135 | + enumerate(output.splitlines()), |
| 136 | + ) |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def _get_user_decision(): |
| 141 | + user_input = None |
| 142 | + while user_input not in ['y', 'n', 's', 'q']: |
| 143 | + if user_input: |
| 144 | + print('Invalid input.') |
| 145 | + |
| 146 | + user_input = input('Is this a valid secret? (y)es, (n)o, (s)kip, (q)uit: ') |
| 147 | + if user_input: |
| 148 | + user_input = user_input[0].lower() |
| 149 | + |
| 150 | + return user_input |
0 commit comments