forked from Yelp/detect-secrets
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathcloudant.py
More file actions
130 lines (112 loc) · 3.84 KB
/
cloudant.py
File metadata and controls
130 lines (112 loc) · 3.84 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import re
import requests
from .base import RegexBasedDetector
from detect_secrets.core.constants import VerifiedResult
class CloudantDetector(RegexBasedDetector):
""" Scans for Cloudant Credentials """
secret_type = 'Cloudant Credentials'
# opt means optional
dot = r'\.'
cl_account = r'[\w\-]+'
cl = r'(?:cloudant|cl|clou)'
opt_api = r'(?:api|)'
cl_key_or_pass = opt_api + r'(?:key|pwd|pw|password|pass|token|creds|credentials|cred|auth)'
cl_pw = r'([0-9a-f]{64})'
cl_api_key = r'([a-z]{24})'
colon = r'\:'
at = r'\@'
http = r'(?:https?\:\/\/)'
cloudant_api_url = r'cloudant\.com'
denylist = [
RegexBasedDetector.assign_regex_generator(
prefix_regex=cl,
password_keyword_regex=cl_key_or_pass,
password_regex=cl_pw,
),
RegexBasedDetector.assign_regex_generator(
prefix_regex=cl,
password_keyword_regex=cl_key_or_pass,
password_regex=cl_api_key,
),
re.compile(
r'{http}{cl_account}{colon}{cl_pw}{at}{cl_account}{dot}{cloudant_api_url}'.format(
http=http,
colon=colon,
cl_account=cl_account,
cl_pw=cl_pw,
at=at,
dot=dot,
cloudant_api_url=cloudant_api_url,
),
flags=re.IGNORECASE,
),
re.compile(
r'{http}{cl_account}{colon}{cl_api_key}{at}{cl_account}{dot}{cloudant_api_url}'.format(
http=http,
colon=colon,
cl_account=cl_account,
cl_api_key=cl_api_key,
at=at,
dot=dot,
cloudant_api_url=cloudant_api_url,
),
flags=re.IGNORECASE,
),
]
def verify(self, token, content, potential_secret=None):
hosts = find_account(content)
if not hosts:
return VerifiedResult.UNVERIFIED
for host in hosts:
return verify_cloudant_key(host, token, potential_secret)
return VerifiedResult.VERIFIED_FALSE
def find_account(content):
opt_hostname_keyword = r'(?:hostname|host|username|id|user|userid|user-id|user-name|' \
'name|user_id|user_name|uname|account)'
account = r'(\w[\w\-]*)'
opt_basic_auth = r'(?:[\w\-:%]*\@)?'
regexes = (
RegexBasedDetector.assign_regex_generator(
prefix_regex=CloudantDetector.cl,
password_keyword_regex=opt_hostname_keyword,
password_regex=account,
),
re.compile(
r'{http}{opt_basic_auth}{cl_account}{dot}{cloudant_api_url}'.format(
http=CloudantDetector.http,
opt_basic_auth=opt_basic_auth,
cl_account=account,
dot=CloudantDetector.dot,
cloudant_api_url=CloudantDetector.cloudant_api_url,
),
flags=re.IGNORECASE,
),
)
return [
match
for line in content.splitlines()
for regex in regexes
for match in regex.findall(line)
]
def verify_cloudant_key(hostname, token, potential_secret=None):
try:
headers = {'Content-type': 'application/json'}
request_url = 'https://{hostname}:' \
'{token}' \
'@{hostname}.' \
'cloudant.com'.format(
hostname=hostname,
token=token,
)
response = requests.get(
request_url,
headers=headers,
)
if response.status_code == 200:
if potential_secret:
potential_secret.other_factors['hostname'] = hostname
return VerifiedResult.VERIFIED_TRUE
else:
return VerifiedResult.VERIFIED_FALSE
except requests.exceptions.RequestException:
return VerifiedResult.UNVERIFIED