Skip to content

Commit

Permalink
Make is_secret an attribute of PotentialSecret
Browse files Browse the repository at this point in the history
And make _load_baseline_from_dict pick it up, so that it does not get removed when e.g. lines move around.
Add is_secret attribute to _create_baseline() in pre_commit_hook_test.py so that TestPreCommitHook.test_writes_new_baseline_if_modified covers the change.
Fixes Yelp#60
  • Loading branch information
KevinHock committed Jul 31, 2018
1 parent 06db4b0 commit 710d96e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
16 changes: 15 additions & 1 deletion detect_secrets/core/potential_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ class PotentialSecret(object):
without actually knowing what the secret is.
"""

def __init__(self, typ, filename, lineno, secret):
def __init__(
self,
typ,
filename,
lineno,
secret,
is_secret=None,
):
"""
:type typ: str
:param typ: human-readable secret type, defined by the plugin
Expand All @@ -30,11 +37,15 @@ def __init__(self, typ, filename, lineno, secret):
:type secret: str
:param secret: the actual secret identified
:type is_secret: bool|None
:param is_secret: whether or not the secret is a true- or false- positive
"""
self.type = typ
self.filename = filename
self.lineno = lineno
self.secret_hash = self.hash_secret(secret)
self.is_secret = is_secret

# If two PotentialSecrets have the same values for these fields,
# they are considered equal. Note that line numbers aren't included
Expand All @@ -60,6 +71,9 @@ def json(self):
'hashed_secret': self.secret_hash,
}

if self.is_secret is not None:
attributes['is_secret'] = self.is_secret

return attributes

def __eq__(self, other):
Expand Down
3 changes: 2 additions & 1 deletion detect_secrets/core/secrets_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def _load_baseline_from_dict(cls, data):
item['type'],
filename,
item['line_number'],
'will be replaced',
secret='will be replaced',
is_secret=item.get('is_secret'),
)
secret.secret_hash = item['hashed_secret']
result.data[filename][secret] = secret
Expand Down
1 change: 1 addition & 0 deletions tests/pre_commit_hook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def _create_baseline():
'test_data/files/file_with_secrets.py': [
{
'type': 'Base64 High Entropy String',
'is_secret': True,
'line_number': 3,
'hashed_secret': PotentialSecret.hash_secret(base64_secret),
},
Expand Down

0 comments on commit 710d96e

Please sign in to comment.