-
Notifications
You must be signed in to change notification settings - Fork 481
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
Easy plugin development #248
Merged
+368
−355
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from collections import namedtuple | ||
|
||
from detect_secrets import VERSION | ||
from detect_secrets.plugins.common.util import import_plugins | ||
|
||
|
||
def add_exclude_lines_argument(parser): | ||
|
@@ -279,7 +280,6 @@ class PluginDescriptor( | |
], | ||
), | ||
): | ||
|
||
def __new__(cls, related_args=None, **kwargs): | ||
if not related_args: | ||
related_args = [] | ||
|
@@ -290,74 +290,46 @@ def __new__(cls, related_args=None, **kwargs): | |
**kwargs | ||
) | ||
|
||
@classmethod | ||
def from_plugin_class(cls, plugin, name): | ||
""" | ||
:type plugin: Type[TypeVar('Plugin', bound=BasePlugin)] | ||
:type name: str | ||
""" | ||
related_args = None | ||
if plugin.default_options: | ||
related_args = [] | ||
for arg_name, value in plugin.default_options.items(): | ||
related_args.append(( | ||
'--{}'.format(arg_name.replace('_', '-')), | ||
value, | ||
)) | ||
|
||
return cls( | ||
classname=name, | ||
disable_flag_text='--{}'.format(plugin.disable_flag_text), | ||
disable_help_text=cls.get_disabled_help_text(plugin), | ||
related_args=related_args, | ||
) | ||
|
||
@staticmethod | ||
def get_disabled_help_text(plugin): | ||
for line in plugin.__doc__.splitlines(): | ||
line = line.strip().lstrip() | ||
if line: | ||
break | ||
else: | ||
raise NotImplementedError('Plugins must declare a docstring.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
|
||
line = line[0].lower() + line[1:] | ||
return 'Disables {}'.format(line) | ||
|
||
|
||
class PluginOptions(object): | ||
|
||
all_plugins = [ | ||
PluginDescriptor( | ||
classname='HexHighEntropyString', | ||
disable_flag_text='--no-hex-string-scan', | ||
disable_help_text='Disables scanning for hex high entropy strings', | ||
related_args=[ | ||
('--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), | ||
], | ||
), | ||
PluginDescriptor( | ||
classname='PrivateKeyDetector', | ||
disable_flag_text='--no-private-key-scan', | ||
disable_help_text='Disables scanning for private keys.', | ||
), | ||
PluginDescriptor( | ||
classname='BasicAuthDetector', | ||
disable_flag_text='--no-basic-auth-scan', | ||
disable_help_text='Disables scanning for Basic Auth formatted URIs.', | ||
), | ||
PluginDescriptor( | ||
classname='KeywordDetector', | ||
disable_flag_text='--no-keyword-scan', | ||
disable_help_text='Disables scanning for secret keywords.', | ||
related_args=[ | ||
('--keyword-exclude', None), | ||
], | ||
), | ||
PluginDescriptor( | ||
classname='AWSKeyDetector', | ||
disable_flag_text='--no-aws-key-scan', | ||
disable_help_text='Disables scanning for AWS keys.', | ||
), | ||
PluginDescriptor( | ||
classname='SlackDetector', | ||
disable_flag_text='--no-slack-scan', | ||
disable_help_text='Disables scanning for Slack tokens.', | ||
), | ||
PluginDescriptor( | ||
classname='ArtifactoryDetector', | ||
disable_flag_text='--no-artifactory-scan', | ||
disable_help_text='Disable scanning for Artifactory credentials', | ||
), | ||
PluginDescriptor( | ||
classname='StripeDetector', | ||
disable_flag_text='--no-stripe-scan', | ||
disable_help_text='Disable scanning for Stripe keys', | ||
), | ||
PluginDescriptor( | ||
classname='MailchimpDetector', | ||
disable_flag_text='--no-mailchimp-scan', | ||
disable_help_text='Disable scanning for Mailchimp keys', | ||
), | ||
PluginDescriptor( | ||
classname='JwtTokenDetector', | ||
disable_flag_text='--no-jwt-scan', | ||
disable_help_text='Disable scanning for JWTs', | ||
), | ||
PluginDescriptor.from_plugin_class(plugin, name) | ||
for name, plugin in import_plugins().items() | ||
] | ||
|
||
def __init__(self, parser): | ||
|
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
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 |
---|---|---|
|
@@ -19,12 +19,39 @@ | |
LINES_OF_CONTEXT = 5 | ||
|
||
|
||
class classproperty(property): | ||
def __get__(self, cls, owner): | ||
return classmethod(self.fget).__get__(None, owner)() | ||
|
||
|
||
class BasePlugin(object): | ||
"""This is an abstract class to define Plugins API""" | ||
""" | ||
This is an abstract class to define Plugins API. | ||
|
||
:type secret_type: str | ||
:param secret_type: uniquely identifies the type of secret found in the baseline. | ||
e.g. { | ||
"hashed_secret": <hash>, | ||
"line_number": 123, | ||
"type": <secret_type>, | ||
} | ||
|
||
Be warned of modifying the `secret_type` once rolled out to clients since | ||
the hashed_secret uses this value to calculate a unique hash (and the baselines | ||
will no longer match). | ||
|
||
:type disable_flag_text: str | ||
:param disable_flag_text: text used as an command line argument flag to disable | ||
this specific plugin scan. does not include the `--` prefix. | ||
|
||
:type default_options: Dict[str, Any] | ||
:param default_options: configurable options to modify plugin behavior | ||
""" | ||
__metaclass__ = ABCMeta | ||
|
||
secret_type = None | ||
@abstractproperty | ||
def secret_type(self): | ||
raise NotImplementedError | ||
|
||
def __init__(self, exclude_lines_regex=None, should_verify=False, **kwargs): | ||
""" | ||
|
@@ -33,15 +60,31 @@ def __init__(self, exclude_lines_regex=None, should_verify=False, **kwargs): | |
|
||
:type should_verify: bool | ||
""" | ||
if not self.secret_type: | ||
raise ValueError('Plugins need to declare a secret_type.') | ||
|
||
self.exclude_lines_regex = None | ||
if exclude_lines_regex: | ||
self.exclude_lines_regex = re.compile(exclude_lines_regex) | ||
|
||
self.should_verify = should_verify | ||
|
||
@classproperty | ||
def disable_flag_text(cls): | ||
name = cls.__name__ | ||
if name.endswith('Detector'): | ||
name = name[:-len('Detector')] | ||
|
||
# turn camel case into hyphenated strings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super consistency nit: All our other comments start cap'd |
||
name_hyphen = '' | ||
for letter in name: | ||
if letter.upper() == letter and name_hyphen: | ||
name_hyphen += '-' | ||
name_hyphen += letter.lower() | ||
|
||
return 'no-{}-scan'.format(name_hyphen) | ||
|
||
@classproperty | ||
def default_options(cls): | ||
return {} | ||
|
||
def analyze(self, file, filename): | ||
""" | ||
:param file: The File object itself. | ||
|
@@ -213,10 +256,6 @@ class FooDetector(RegexBasedDetector): | |
""" | ||
__metaclass__ = ABCMeta | ||
|
||
@abstractproperty | ||
def secret_type(self): | ||
raise NotImplementedError | ||
|
||
@abstractproperty | ||
def denylist(self): | ||
raise NotImplementedError | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
it's corresponding test,
be equivalent to what you mean?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, yeah. whoops.