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: update pre-commit hook versions #221

Merged
merged 4 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
rev: v2.3.0
hooks:
- id: check-builtin-literals
args: ['--no-allow-dict-kwargs']
Expand All @@ -14,15 +14,15 @@ repos:
exclude: ^test_data/
- id: trailing-whitespace
- repo: https://github.com/asottile/reorder_python_imports
rev: v1.3.4
rev: v1.6.1
hooks:
- id: reorder-python-imports
language_version: python3
- repo: https://github.com/asottile/add-trailing-comma
rev: v0.7.1
rev: v1.4.1
hooks:
- id: add-trailing-comma
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.4.3
rev: v1.4.4
hooks:
- id: autopep8
40 changes: 21 additions & 19 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,15 @@ def _check_secret(a, b):
if not new_filename:
secrets_to_compare += list(
map(
lambda x: (old_filename, x, True,),
lambda x: (old_filename, x, True),
old_baseline['results'][old_filename],
),
)
continue
elif not old_filename:
secrets_to_compare += list(
map(
lambda x: (new_filename, x, False,),
lambda x: (new_filename, x, False),
new_baseline['results'][new_filename],
),
)
Expand All @@ -374,11 +374,11 @@ def _check_secret(a, b):

if old_secret:
secrets_to_compare.append(
(old_filename, old_secret, True,),
(old_filename, old_secret, True),
)
else:
secrets_to_compare.append(
(new_filename, new_secret, False,),
(new_filename, new_secret, False),
)

return secrets_to_compare
Expand All @@ -404,22 +404,22 @@ def _comparison_generator(old_list, new_list, compare_fn):

status = compare_fn(old_value, new_value)
if status == 0:
yield (old_value, new_value,)
yield (old_value, new_value)
old_index += 1
new_index += 1
elif status == -1:
yield (old_value, None,)
yield (old_value, None)
old_index += 1
else:
yield (None, new_value,)
yield (None, new_value)
new_index += 1

# Catch leftovers. Only one of these while statements should run.
while old_index < len(old_list):
yield (old_list[old_index], None,)
yield (old_list[old_index], None)
old_index += 1
while new_index < len(new_list):
yield (None, new_list[new_index],)
yield (None, new_list[new_index])
new_index += 1


Expand Down Expand Up @@ -462,16 +462,18 @@ def _print_context( # pragma: no cover

:raises: SecretNotFoundOnSpecifiedLineError
"""
print('{} {} {} {}\n{} {}\n{} {}'.format(
colorize('Secret: ', AnsiColor.BOLD),
colorize(str(count), AnsiColor.PURPLE),
colorize('of', AnsiColor.BOLD),
colorize(str(total), AnsiColor.PURPLE),
colorize('Filename: ', AnsiColor.BOLD),
colorize(filename, AnsiColor.PURPLE),
colorize('Secret Type:', AnsiColor.BOLD),
colorize(secret['type'], AnsiColor.PURPLE),
))
print(
'{} {} {} {}\n{} {}\n{} {}'.format(
colorize('Secret: ', AnsiColor.BOLD),
colorize(str(count), AnsiColor.PURPLE),
colorize('of', AnsiColor.BOLD),
colorize(str(total), AnsiColor.PURPLE),
colorize('Filename: ', AnsiColor.BOLD),
colorize(filename, AnsiColor.PURPLE),
colorize('Secret Type:', AnsiColor.BOLD),
colorize(secret['type'], AnsiColor.PURPLE),
),
)
if additional_header_lines:
print(additional_header_lines)

Expand Down
2 changes: 1 addition & 1 deletion detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def format_baseline_for_output(baseline):
for filename, secret_list in baseline['results'].items():
baseline['results'][filename] = sorted(
secret_list,
key=lambda x: (x['line_number'], x['hashed_secret'],),
key=lambda x: (x['line_number'], x['hashed_secret']),
)

return json.dumps(
Expand Down
30 changes: 18 additions & 12 deletions detect_secrets/core/secrets_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ def load_baseline_from_dict(cls, data):
"""
result = SecretsCollection()

if not all(key in data for key in (
'plugins_used',
'results',
)):
if not all(
key in data for key in (
'plugins_used',
'results',
)
):
raise IOError

# In v0.12.0 `exclude_regex` got replaced by `exclude`
if not any(key in data for key in (
'exclude',
'exclude_regex',
)):
if not any(
key in data for key in (
'exclude',
'exclude_regex',
)
):
raise IOError

if 'exclude_regex' in data:
Expand Down Expand Up @@ -259,10 +263,12 @@ def format_for_baseline_output(self):
for key in results:
results[key] = sorted(results[key], key=lambda x: x['line_number'])

plugins_used = list(map(
lambda x: x.__dict__,
self.plugins,
))
plugins_used = list(
map(
lambda x: x.__dict__,
self.plugins,
),
)
plugins_used = sorted(plugins_used, key=lambda x: x['name'])

return {
Expand Down
58 changes: 30 additions & 28 deletions detect_secrets/core/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,32 +233,34 @@ def add_arguments(self):
return self


class PluginDescriptor(namedtuple(
'PluginDescriptor',
[
# Classname of plugin; used for initialization
'classname',

# Flag to disable plugin. e.g. `--no-hex-string-scan`
'disable_flag_text',

# Description for disable flag.
'disable_help_text',

# type: list
# Allows the bundling of all related command line provided
# arguments together, under one plugin name.
# Assumes there is no shared related arg.
#
# Furthermore, each related arg can have its own default
# value (paired together, with a tuple). This allows us to
# distinguish the difference between a default value, and
# whether a user has entered the same value as a default value.
# Therefore, only populate the default value upon consolidation
# (rather than relying on argparse default).
'related_args',
],
)):
class PluginDescriptor(
namedtuple(
'PluginDescriptor',
[
# Classname of plugin; used for initialization
'classname',

# Flag to disable plugin. e.g. `--no-hex-string-scan`
'disable_flag_text',

# Description for disable flag.
'disable_help_text',

# type: list
# Allows the bundling of all related command line provided
# arguments together, under one plugin name.
# Assumes there is no shared related arg.
#
# Furthermore, each related arg can have its own default
# value (paired together, with a tuple). This allows us to
# distinguish the difference between a default value, and
# whether a user has entered the same value as a default value.
# Therefore, only populate the default value upon consolidation
# (rather than relying on argparse default).
'related_args',
],
),
):

def __new__(cls, related_args=None, **kwargs):
if not related_args:
Expand All @@ -279,15 +281,15 @@ class PluginOptions(object):
disable_flag_text='--no-hex-string-scan',
disable_help_text='Disables scanning for hex high entropy strings',
related_args=[
('--hex-limit', 3,),
('--hex-limit', 3),
],
),
PluginDescriptor(
classname='Base64HighEntropyString',
disable_flag_text='--no-base64-string-scan',
disable_help_text='Disables scanning for base64 high entropy strings',
related_args=[
('--base64-limit', 4.5,),
('--base64-limit', 4.5),
],
),
PluginDescriptor(
Expand Down
10 changes: 6 additions & 4 deletions detect_secrets/plugins/common/ini_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ def _get_value_and_line_offset(self, key, values):
continue

if current_value_list_index == 0:
first_line_regex = re.compile(r'^\s*{}[ :=]+{}'.format(
re.escape(key),
re.escape(values_list[current_value_list_index]),
))
first_line_regex = re.compile(
r'^\s*{}[ :=]+{}'.format(
re.escape(key),
re.escape(values_list[current_value_list_index]),
),
)
if first_line_regex.match(line):
output.append((
values_list[current_value_list_index],
Expand Down
4 changes: 2 additions & 2 deletions detect_secrets/plugins/common/yaml_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _tag_dict_values(self, map_node):
new_values = []
for key, value in map_node.value:
if not value.tag.endswith(':str'):
new_values.append((key, value,))
new_values.append((key, value))
continue

augmented_string = yaml.nodes.MappingNode(
Expand All @@ -95,7 +95,7 @@ def _tag_dict_values(self, map_node):
],
)

new_values.append((key, augmented_string,))
new_values.append((key, augmented_string))

output = yaml.nodes.MappingNode(
tag=map_node.tag,
Expand Down
20 changes: 11 additions & 9 deletions detect_secrets/plugins/high_entropy_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def __init__(self, charset, limit, exclude_lines_regex, *args):

def analyze(self, file, filename):
file_type_analyzers = (
(self._analyze_ini_file(), configparser.Error,),
(self._analyze_yaml_file, yaml.YAMLError,),
(super(HighEntropyStringsPlugin, self).analyze, Exception,),
(self._analyze_ini_file(add_header=True), configparser.Error,),
(self._analyze_ini_file(), configparser.Error),
(self._analyze_yaml_file, yaml.YAMLError),
(super(HighEntropyStringsPlugin, self).analyze, Exception),
(self._analyze_ini_file(add_header=True), configparser.Error),
)

for analyze_function, exception_class in file_type_analyzers:
Expand Down Expand Up @@ -169,11 +169,13 @@ def wrapped(file, filename):
add_header,
exclude_lines_regex=self.exclude_lines_regex,
).iterator():
potential_secrets.update(self.analyze_string(
value,
lineno,
filename,
))
potential_secrets.update(
self.analyze_string(
value,
lineno,
filename,
),
)

return potential_secrets

Expand Down
18 changes: 10 additions & 8 deletions testing/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ def _mock_subprocess_git_call(cmds, **kwargs):
yield


class SubprocessMock(namedtuple(
'SubprocessMock',
[
'expected_input',
'mocked_output',
'should_throw_exception',
],
)):
class SubprocessMock(
namedtuple(
'SubprocessMock',
[
'expected_input',
'mocked_output',
'should_throw_exception',
],
),
):
"""For use with mock_subprocess.

:type expected_input: string
Expand Down
8 changes: 4 additions & 4 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,10 @@ class TestGetUserDecision(object):
@pytest.mark.parametrize(
'user_input, expected_value',
[
('y', 'y',),
('N', 'n',),
('Skip', 's',),
('QUIT', 'q',),
('y', 'y'),
('N', 'n'),
('Skip', 's'),
('QUIT', 'q'),
],
)
def test_get_user_decision_valid_input(
Expand Down
20 changes: 12 additions & 8 deletions tests/core/baseline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ def test_basic_usage(self, path):
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 2

def test_with_multiple_files(self):
results = self.get_results(path=[
'test_data/files/file_with_secrets.py',
'test_data/files/tmp/file_with_secrets.py',
])
results = self.get_results(
path=[
'test_data/files/file_with_secrets.py',
'test_data/files/tmp/file_with_secrets.py',
],
)

assert len(results['test_data/files/file_with_secrets.py']) == 1
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 2
Expand Down Expand Up @@ -589,9 +591,11 @@ def test_sorts_by_line_number_then_hash(self):
},
})

ordered_hashes = list(map(
lambda x: x['hashed_secret'],
json.loads(output_string)['results']['filename'],
))
ordered_hashes = list(
map(
lambda x: x['hashed_secret'],
json.loads(output_string)['results']['filename'],
),
)

assert ordered_hashes == ['z', 'a', 'f']
Loading