forked from DataDog/datadog-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-api-keys.py
More file actions
executable file
·73 lines (53 loc) · 1.83 KB
/
detect-api-keys.py
File metadata and controls
executable file
·73 lines (53 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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())