-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:Yelp/detect-secrets
- Loading branch information
Showing
13 changed files
with
591 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from . import analytics # noqa: F401 | ||
from . import report # noqa: F401 | ||
from .audit import audit_baseline # noqa: F401 | ||
from .compare import compare_baselines # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from enum import Enum | ||
from typing import Any | ||
from typing import Callable | ||
from typing import Dict | ||
from typing import List | ||
from typing import Tuple | ||
|
||
from ..constants import VerifiedResult | ||
from .common import get_baseline_from_file | ||
from .common import get_raw_secrets_from_file | ||
from .common import LineGetter | ||
from .common import open_file | ||
|
||
|
||
class SecretClassToPrint(Enum): | ||
REAL_SECRET = 1 | ||
FALSE_POSITIVE = 2 | ||
|
||
@staticmethod | ||
def from_class(secret_class: VerifiedResult) -> 'SecretClassToPrint': | ||
if secret_class in [VerifiedResult.UNVERIFIED, VerifiedResult.VERIFIED_TRUE]: | ||
return SecretClassToPrint.REAL_SECRET | ||
else: | ||
return SecretClassToPrint.FALSE_POSITIVE | ||
|
||
|
||
def generate_report( | ||
baseline_file: str, | ||
class_to_print: SecretClassToPrint = None, | ||
line_getter_factory: Callable[[str], 'LineGetter'] = open_file, | ||
) -> List[Dict[str, Any]]: | ||
|
||
secrets: Dict[Tuple[str, str], Any] = {} | ||
for filename, secret in get_baseline_from_file(baseline_file): | ||
verified_result = VerifiedResult.from_secret(secret) | ||
if ( | ||
class_to_print is not None and | ||
SecretClassToPrint.from_class(verified_result) != class_to_print | ||
): | ||
continue | ||
# Removal of the stored line number is required to force the complete file scanning to obtain all the secret occurrences. # noqa: E501 | ||
secret.line_number = 0 | ||
detections = get_raw_secrets_from_file(secret) | ||
line_getter = line_getter_factory(filename) | ||
for detection in detections: | ||
if (secret.secret_hash, filename) in secrets: | ||
secrets[(secret.secret_hash, filename)]['lines'][detection.line_number] = line_getter.lines[detection.line_number - 1] # noqa: E501 | ||
if secret.type not in secrets[(secret.secret_hash, filename)]['types']: | ||
secrets[(secret.secret_hash, filename)]['types'].append(secret.type) | ||
secrets[(secret.secret_hash, filename)]['category'] = get_prioritized_verified_result( # noqa: E501 | ||
verified_result, | ||
VerifiedResult[secrets[(secret.secret_hash, filename)]['category']], | ||
).name | ||
else: | ||
secrets[(secret.secret_hash, filename)] = { | ||
'secrets': detection.secret_value, | ||
'filename': filename, | ||
'lines': { | ||
detection.line_number: line_getter.lines[detection.line_number - 1], | ||
}, | ||
'types': [ | ||
secret.type, | ||
], | ||
'category': verified_result.name, | ||
} | ||
|
||
return list(secrets.values()) | ||
|
||
|
||
def get_prioritized_verified_result( | ||
result1: VerifiedResult, | ||
result2: VerifiedResult, | ||
) -> VerifiedResult: | ||
if result1.value > result2.value: | ||
return result1 | ||
else: | ||
return result2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
from enum import Enum | ||
|
||
from .core.potential_secret import PotentialSecret | ||
|
||
|
||
class VerifiedResult(Enum): | ||
VERIFIED_FALSE = 1 | ||
UNVERIFIED = 2 | ||
VERIFIED_TRUE = 3 | ||
|
||
@staticmethod | ||
def from_secret(secret: PotentialSecret) -> 'VerifiedResult': | ||
if secret.is_secret is None: | ||
return VerifiedResult.UNVERIFIED | ||
elif secret.is_secret: | ||
return VerifiedResult.VERIFIED_TRUE | ||
else: | ||
return VerifiedResult.VERIFIED_FALSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.