forked from dtkav/IAM-watcher-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.py
136 lines (126 loc) · 5.11 KB
/
lambda.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
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
131
132
133
134
135
136
from __future__ import print_function
import urllib.request as urllib
import urllib.parse
import os
import re
import json
import yaml
import boto3
import requests
from io import BytesIO
from gzip import GzipFile
print('Loading function')
s3 = boto3.client('s3')
webhook_url = os.environ['SLACK_HOOK']
slack_channel = os.environ['SLACK_CHANNEL']
ACCEPT = ["iam.amazonaws.com"]
MATCH = (
"^Add",
"^Remove",
"^Set",
"^Delete",
"^Deactivate",
"^Detach",
"^Upload",
"^Update",
"^Put",
"^Create",
"^Attach",
"^Change"
)
IGNORE = (
"^List",
"^Get",
)
def lambda_handler(event, context):
message = json.loads(event['Records'][0]['Sns']['Message'])
bucket = message['Records'][0]['s3']['bucket']['name']
key = message['Records'][0]['s3']['object']['key']
try:
s3response = s3.get_object(Bucket=bucket, Key=key)
bytestream = BytesIO(s3response['Body'].read())
body = GzipFile(None, 'rb', fileobj=bytestream).read().decode('utf-8')
j = json.loads(body)
attachments = []
for record in j["Records"]:
match = re.compile('|'.join(MATCH))
ignore = re.compile('|'.join(IGNORE))
if record["eventSource"] in ACCEPT:
for record_name in re.finditer(ignore, record["eventName"]):
continue
for record_name in re.finditer(match, record["eventName"]):
print("found IAM change in log " + key)
arn = record["userIdentity"]["arn"].split(':')[5]
request_params_json=json.dumps(record["requestParameters"], indent=4, sort_keys=True)
request_params=re.sub('["|[|\]|{|}]', '', re.sub('[\\\\]', '', re.sub('[,]', ', ', re.sub('[\n](\s\s\s)+', '\n', re.sub('[n](\s\s)+', '', request_params_json)))))
event = record["eventName"]
color = "#2eb886"
pretext = "*Event Time*: %s \n\n Event details:" % (record["eventTime"].replace('T', ' ').replace('Z', ' '))
attachment = {
"fallback": "New incoming IAM Alert",
"color": "%s" % (color),
"pretext": "%s" % (pretext),
"text": "*`%s`* --> *`%s`* \n\n " % (arn, event),
"fields": [
{
"title": "Event Source",
"value": "%s" % (record["eventSource"]),
"short": True
},
{
"title": "Account ID",
"value": "%s" % (record["userIdentity"]["accountId"]),
"short": True
},
{
"title": "User",
"value": "%s" % (record["userIdentity"]["principalId"].split(':')[1]),
"short": True
},
{
"title": "Event Name",
"value": "%s" % (record["eventName"]),
"short": True
},
{
"title": "Request Parameters",
"value": "%s" % (request_params),
"short": False
}
],
"mrkdwn_in": ["text", "pretext", "color", "fields", "title"]
}
attachments.append(attachment)
if attachments:
if len(attachments) > 20:
print("warning! too many attachments")
message = {
"channel": slack_channel,
"text": "<!channel>\n*New incoming IAM Alert*",
"attachments": attachments
}
try:
response = requests.post(webhook_url, data=json.dumps(message), headers={'Content-Type': 'application/json'})
print('Response: ' + str(response.text))
print('Response code: ' + str(response.status_code))
print('Message posted to channel "' + slack_channel + '"')
except urllib.error.HTTPError as e:
text=e.reason
status=e.code
message = f"""
Error sending message to Slack channel {slack_channel}
Reason: {text}
Status code: {status}
"""
print(message)
raise error
except urllib.error.URLError as e:
print('Server connection failed: ' + str(e.reason))
return s3response['ContentType']
except Exception as error:
print(error)
message = f"""
Error getting object {key} from bucket {bucket}.
"""
print(message)
raise error