forked from Yelp/detect-secrets
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathibm_cloud_iam.py
58 lines (48 loc) · 1.82 KB
/
ibm_cloud_iam.py
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
import requests
from .base import RegexBasedDetector
from detect_secrets.core.constants import VerifiedResult
class IbmCloudIamDetector(RegexBasedDetector):
""" Scans for IBM Cloud IAM credentials """
secret_type = 'IBM Cloud IAM Key'
# opt means optional
opt_ibm_cloud_iam = r'(?:ibm(?:_|-|)cloud(?:_|-|)iam|cloud(?:_|-|)iam|' + \
r'ibm(?:_|-|)cloud|ibm(?:_|-|)iam|ibm|iam|cloud|test|)'
opt_dash_undrscr = r'(?:_|-|)'
opt_api = r'(?:api|)'
key_or_pass = r'(?:key|keyid|pwd|password|pass|token|authenticator|creds|credentials|cred|auth)'
secret = r'([a-zA-Z0-9_\-]{44})'
denylist = [
RegexBasedDetector.assign_regex_generator(
prefix_regex=opt_ibm_cloud_iam + opt_dash_undrscr + opt_api,
password_keyword_regex=key_or_pass,
password_regex=secret,
),
]
def verify(self, token, *args, **kwargs):
response = verify_cloud_iam_api_key(token)
try:
if response.status_code != 200:
return VerifiedResult.UNVERIFIED
if 'active' not in response.json():
return VerifiedResult.UNVERIFIED
if response.json()['active']:
return VerifiedResult.VERIFIED_TRUE
else:
return VerifiedResult.VERIFIED_FALSE
except requests.exceptions.RequestException:
return VerifiedResult.UNVERIFIED
def verify_cloud_iam_api_key(apikey): # pragma: no cover
if type(apikey) == bytes:
apikey = apikey.decode('UTF-8')
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
}
response = requests.post(
'https://iam.cloud.ibm.com/identity/introspect',
headers=headers,
data={
'apikey': apikey,
},
)
return response