-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdetect.py
140 lines (109 loc) · 3.08 KB
/
detect.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
137
138
139
140
from __future__ import print_function
from slackclient import SlackClient
import boto3
import json
import os
# Global Variables
channel = '#%s' % os.environ['SLACK_CHANNEL']
token_bot = '%s' % os.environ['SLACK_TOKEN_NAME']
def getSlackToken (user):
ssm = boto3.client('ssm')
token = ssm.get_parameter(
Name='%s' % user,
WithDecryption=True
)
token = token['Parameter']['Value']
return token
def getSevColor (sev):
if sev >= 8:
color = '#ff0000'
elif sev < 8 and sev >= 4:
color = '#ffa500'
else:
color = '0000ff'
return color
def getRemColor (rem):
if rem == False:
color = '#ff0000'
else:
color = '#83F52C'
return color
def PostMessage(channel, token_bot, message, thread_ts):
# Get Bot Token
gd_token = getSlackToken(token_bot)
# Slack Client for Web API Requests
slack_client = SlackClient(gd_token)
if thread_ts == 'NA':
# Post Slack Message
post = slack_client.api_call(
"chat.postMessage",
channel=channel,
as_user='true',
attachments=message
)
else:
# Post Slack Message
post = slack_client.api_call(
"chat.postMessage",
channel=channel,
as_user='true',
thread_ts=thread_ts,
attachments=message
)
return post
def PublishEvent(event, context):
# Log Event
print("log -- Event: %s " % json.dumps(event))
# Set Event Variables
gd_sev = event['detail']['severity']
gd_account = event['detail']['accountId']
gd_region = event['detail']['region']
gd_desc = event['detail']['description']
gd_type = event['detail']['type']
thread_ts = 'NA'
# Set Severity Color
gd_color = getSevColor(gd_sev)
# Set Generic GD Finding Message
message = [
{
"title": gd_type,
"fields": [
{
"title": "AccountID",
"value": gd_account,
"short": 'true'
},
{
"title": "Region",
"value": gd_region,
"short": 'true'
}
],
"fallback": "Required plain-text summary of the attachment.",
"color": gd_color,
"text": gd_desc,
}]
# Post Slack Message
post = PostMessage(channel, token_bot, message, thread_ts)
# Add Slack Thread Id to Event
event["ts"] = post['message']['ts']
return event
def PublishRemediation(event, context):
# Log Event
print("log -- Event: %s " % json.dumps(event))
# Set Event Variables
gd_rem = event['remediation']['success']
gd_rem_desc = event['remediation']['description']
gd_rem_title = event['remediation']['title']
# Set Severity Color
gd_color = getRemColor(gd_rem)
# Set Generic GD Finding Message
message = [
{
"title": gd_rem_title,
"color": gd_color,
"text": gd_rem_desc
}]
# Post Slack Message
post = PostMessage(channel, token_bot, message, event["ts"])
return event