Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --audit Crash on Missing File #56

Merged
merged 3 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import print_function

import json
import os
import subprocess
import sys
import textwrap
Expand Down Expand Up @@ -31,6 +32,7 @@ def audit_baseline(baseline_filename):

if 'is_secret' not in secret:
current_secret_index += 1

try:
_print_context(
filename,
Expand Down Expand Up @@ -164,7 +166,7 @@ def _secret_generator(baseline):
list(
filter(
lambda secret: 'is_secret' not in secret,
baseline['results'][filename],
baseline['results'][filename] if os.path.exists(filename) else [],
),
),
),
Expand All @@ -173,6 +175,9 @@ def _secret_generator(baseline):
)

for filename, secrets in baseline['results'].items():
if not os.path.exists(filename):
print('{} does not exist.'.format(filename))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm debating the use of this case.

  1. print statements shouldn't be in the _secret_generator.
  2. If the file is gone, there's nothing to audit (and we should probably just delete its entry in the updated baseline). After all, we already exclude it from the count (from your change above).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was ambivalent too, wasn't sure if we wanted audit to only have is_secret diffs, but yeah, removing all dead files in audit_baseline before calling _secret_generator etc. is cleaner, I'll change.

continue
for secret in secrets:
yield filename, secret, num_secrets_to_parse

Expand Down
1 change: 0 additions & 1 deletion detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def merge_baseline(old_baseline, new_baseline):
:type new_baseline: dict
:param new_baseline: most recent scan
"""
# TODO: merge_results does not take into account `is_secret`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

new_baseline['results'] = merge_results(
old_baseline['results'],
new_baseline['results'],
Expand Down
22 changes: 19 additions & 3 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def test_no_baseline(self, mock_printer):
assert mock_printer.message == ''

def test_quit_before_making_decision(self, mock_printer):
with self.mock_env(['q']) as m:
with mock.patch(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's throw this in mock_env. After all, we're already patching a bunch of things there.

'detect_secrets.core.audit.os.path.exists',
return_value=True,
), self.mock_env(
['q'],
) as m:
audit.audit_baseline('will_be_mocked')

assert m.call_args[0][1] == self.baseline
Expand All @@ -40,7 +45,12 @@ def test_nothing_to_audit(self, mock_printer):
modified_baseline['results']['filenameA'][1]['is_secret'] = False
modified_baseline['results']['filenameB'][0]['is_secret'] = False

with self.mock_env(baseline=modified_baseline):
with mock.patch(
'detect_secrets.core.audit.os.path.exists',
return_value=True,
), self.mock_env(
baseline=modified_baseline,
):
audit.audit_baseline('will_be_mocked')

assert mock_printer.message == 'Nothing to audit!\n'
Expand Down Expand Up @@ -101,7 +111,13 @@ def test_leapfrog_decision(self, mock_printer):

@contextmanager
def run_logic(self, inputs, modified_baseline=None, input_baseline=None):
with self.mock_env(inputs, baseline=input_baseline) as m:
with mock.patch(
'detect_secrets.core.audit.os.path.exists',
return_value=True,
), self.mock_env(
inputs,
baseline=input_baseline,
) as m:
audit.audit_baseline('will_be_mocked')

if not modified_baseline:
Expand Down