-
Notifications
You must be signed in to change notification settings - Fork 45
Add pre-commit configuration and custom hook #149
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf87545
Update expected breaking change date (#114)
jtappa cae65b3
add pre-commit config and custom hook
jcstorms1 a9aa52f
Merge branch 'main' into storms/add-pre-commit-hooks
jcstorms1 4fbeeed
Update README.md
jcstorms1 bb3dccc
Update README.md
jcstorms1 3c6ab59
Update README.md
jcstorms1 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 hidden or 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,73 @@ | ||
#!/usr/bin/env python | ||
from __future__ import print_function | ||
|
||
import argparse | ||
import re | ||
import sys | ||
|
||
|
||
def detect_aws_access_key(line): | ||
match = re.search(r"(?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])", line) | ||
return match, "AWS access key" | ||
|
||
|
||
def detect_aws_secret_key(line): | ||
match = re.search(r"(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])", line) | ||
return match, "AWS secret key" | ||
|
||
|
||
def detect_dd_api_key(line): | ||
match = re.search(r"(?<![a-fA-F0-9])[a-fA-F0-9]{32}(?![a-fA-F0-9])", line) | ||
return match, "Datadog API key" | ||
|
||
|
||
def detect_dd_app_key(line): | ||
match = re.search(r"(?<![a-fA-F0-9])[a-fA-F0-9]{40}(?![a-fA-F0-9])", line) | ||
return match, "Datadog app key" | ||
|
||
|
||
def key_found_message(args): | ||
return ( | ||
"\033[91m" | ||
"Potential {} found in {} at line {} and column {}. " | ||
"Please remove the key before committing these changes." | ||
"\033[0m".format(*args) | ||
) | ||
|
||
|
||
def main(argv=None): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("filenames", nargs="*", help="Filenames to check.") | ||
args = parser.parse_args(argv) | ||
|
||
# add or remove functions here | ||
functions_to_run = [ | ||
detect_aws_access_key, | ||
detect_aws_secret_key, | ||
detect_dd_api_key, | ||
detect_dd_app_key, | ||
] | ||
|
||
files_with_key = [] | ||
|
||
for filename in args.filenames: | ||
with open(filename, "r") as f: | ||
content = f.readlines() | ||
f.close() | ||
|
||
for i, line in enumerate(content): | ||
for func in functions_to_run: | ||
match, name = func(line) | ||
if match != None: | ||
files_with_key.append((name, filename, i + 1, match.end())) | ||
|
||
if files_with_key: | ||
for file in files_with_key: | ||
print(key_found_message(file)) | ||
return 1 | ||
else: | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains hidden or 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,18 @@ | ||
repos: | ||
- repo: git@github.com:pre-commit/pre-commit-hooks | ||
rev: v2.1.0 | ||
hooks: | ||
- id: check-merge-conflict | ||
files: \.py$ | ||
- repo: git@github.com:psf/black | ||
rev: 21.6b0 | ||
hooks: | ||
- id: black | ||
files: \.py$ | ||
- repo: local | ||
hooks: | ||
- id: detect-api-keys | ||
name: detect-api-keys | ||
description: Checks for AWS or Datadog API keys | ||
entry: ".git-hooks/detect-api-keys.py" | ||
language: python |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.