Skip to content

Commit

Permalink
Merge pull request #82 from JoshuaRLi/audit-ux-enhancements
Browse files Browse the repository at this point in the history
audit: minor UX enhancements
  • Loading branch information
KevinHock committed Oct 5, 2018
2 parents 303ba94 + cc6bddb commit 3d7c059
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
50 changes: 19 additions & 31 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import subprocess
import sys
import textwrap
from builtins import input
from collections import defaultdict

Expand All @@ -18,7 +17,11 @@


class SecretNotFoundOnSpecifiedLineError(Exception):
pass
def __init__(self, line):
super(SecretNotFoundOnSpecifiedLineError, self).__init__(
"ERROR: Secret not found on line {}!\n".format(line) +
"Try recreating your baseline to fix this issue.",
)


def audit_baseline(baseline_filename):
Expand Down Expand Up @@ -128,27 +131,13 @@ def _print_context(filename, secret, count, total, plugin_settings): # pragma:
:raises: SecretNotFoundOnSpecifiedLineError
"""
secrets_left = '{}/{}'.format(
count,
total,
)
print('{} {}\n{} {}'.format(
BashColor.color(
'Secrets Left:',
Color.BOLD,
),
BashColor.color(
secrets_left,
Color.PURPLE,
),
BashColor.color(
'Filename: ',
Color.BOLD,
),
BashColor.color(
filename,
Color.PURPLE,
),
print('{} {} {} {}\n{} {}'.format(
BashColor.color('Secret', Color.BOLD),
BashColor.color(str(count), Color.PURPLE),
BashColor.color('of', Color.BOLD),
BashColor.color(str(total), Color.PURPLE),
BashColor.color('Filename:', Color.BOLD),
BashColor.color(filename, Color.PURPLE),
))
print('-' * 10)

Expand Down Expand Up @@ -274,6 +263,7 @@ def _get_secret_with_context(

output[index_of_secret_in_output] = _highlight_secret(
output[index_of_secret_in_output],
secret_lineno,
secret,
filename,
plugin_settings,
Expand All @@ -294,10 +284,13 @@ def _get_secret_with_context(
)


def _highlight_secret(secret_line, secret, filename, plugin_settings):
def _highlight_secret(secret_line, secret_lineno, secret, filename, plugin_settings):
"""
:type secret_line: str
:param secret_line: the line on whcih the secret is found
:param secret_line: the line on which the secret is found
:type secret_lineno: int
:param secret_lineno: secret_line's line number in the source file
:type secret: dict
:param secret: see caller's docstring
Expand Down Expand Up @@ -329,12 +322,7 @@ def _highlight_secret(secret_line, secret, filename, plugin_settings):
if secret_obj.secret_hash == secret['hashed_secret']:
break
else:
raise SecretNotFoundOnSpecifiedLineError(
textwrap.dedent("""
ERROR: Secret not found on specified line number!
Try recreating your baseline to fix this issue.
""")[1:-1],
)
raise SecretNotFoundOnSpecifiedLineError(secret_lineno)

index_of_secret = secret_line.index(raw_secret)
return '{}{}{}'.format(
Expand Down
18 changes: 9 additions & 9 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ def test_basic(self, mock_printer):
assert sed_call.call_args[0][0] == 'sed -n 10,20p filenameA'.split()

assert mock_printer.message == textwrap.dedent("""
Secrets Left: 1/2
Filename: filenameA
Secret 1 of 2
Filename: filenameA
----------
10:a
11:b
Expand Down Expand Up @@ -394,8 +394,8 @@ def test_secret_at_top_of_file(self, mock_printer):
assert sed_call.call_args[0][0] == 'sed -n 1,6p filenameA'.split()

assert mock_printer.message == textwrap.dedent("""
Secrets Left: 1/2
Filename: filenameA
Secret 1 of 2
Filename: filenameA
----------
1:-----BEGIN PRIVATE KEY-----
2:e
Expand All @@ -421,10 +421,10 @@ def test_secret_not_found(self, mock_printer):
)

assert mock_printer.message == textwrap.dedent("""
Secrets Left: 1/2
Filename: filenameA
Secret 1 of 2
Filename: filenameA
----------
ERROR: Secret not found on specified line number!
ERROR: Secret not found on line 15!
Try recreating your baseline to fix this issue.
----------
Expand All @@ -450,8 +450,8 @@ def test_secret_in_yaml_file(self, mock_printer):
)

assert mock_printer.message == textwrap.dedent("""
Secrets Left: 1/2
Filename: filenameB
Secret 1 of 2
Filename: filenameB
----------
10:a
11:b
Expand Down
4 changes: 2 additions & 2 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ def test_audit_short_file(self, filename, expected_output):
main('audit will_be_mocked'.split())

assert printer_shim.message == textwrap.dedent("""
Secrets Left: 1/1
Filename: {}
Secret 1 of 1
Filename: {}
----------
{}
----------
Expand Down

0 comments on commit 3d7c059

Please sign in to comment.