This repository was archived by the owner on Dec 6, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlambda_function.py
More file actions
280 lines (249 loc) · 9.19 KB
/
Copy pathlambda_function.py
File metadata and controls
280 lines (249 loc) · 9.19 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
This file receive incoming event and first process event from Application
"""
import json
import urllib.parse
from aws import slackstash
from aws import constants
import botocore.vendored.requests as requests
def verify_command(options):
"""
Verify command
"""
options_size = len(options)
if options_size >= 2:
return True
return False
def process_schedule_set_option(options):
"""
Process schedule options
"""
options_size = len(options)
# Support only schedule for turnon/turnoff
if 3 < options_size < 8:
schedule = {
"commands": [],
"requester": None
}
i = 2
while i < options_size - 1:
if options[i] == "turnon" or options[i] == "turnoff":
cmd = {
"cmd": "aws",
"cmd_text": options[i],
"instance_tag": options[options_size - 1],
"time": urllib.parse.unquote(options[i+1]).replace("-", " "),
"is_loop": False
}
date_time = cmd["time"]
if date_time is None:
return constants.MESSAGE_WRONG_COMMAND
schedule["commands"].append(cmd)
i = i + 2
else:
return constants.MESSAGE_WRONG_COMMAND
return schedule
else:
return constants.MESSAGE_WRONG_COMMAND
def process_approval_request(payload):
"""
Process event when user click 'approve' or 'reject'
"""
actions = payload["actions"]
original_request_message = payload["original_message"]["text"].replace('+', ' ')
response_text = ""
value = actions[0]["value"].replace('+', ' ').replace("'", "\"")
action_value = json.loads(value)
data = {
"token": payload["token"],
"team_id": payload["team"]["id"],
"channel_id": payload["channel"]["id"],
"user_id": action_value["user_id"],
"user_name": action_value["user_name"],
"command": action_value["command"],
"text": action_value["text"],
"response_url": action_value["response_url"],
"attachments": [],
"approval_user_id": payload["user"]["id"],
"approval_action": None
}
if actions[0]["name"] == "approve":
response_text = "<@{0}> approved this request".format(payload["user"]["name"])
process_command_response = process_slash_command_request(data)
data["approval_action"] = "approved"
data["attachments"] = process_command_response["attachments"]
elif actions[0]["name"] == "reject":
response_text = "<@{0}> rejected this request".format(payload["user"]["name"])
data["approval_action"] = "rejected"
# This send response to original request channel
send_result_message_after_approval(data)
# This response to approval channel
return {
"replace_original": "true",
"text": original_request_message,
"attachments": [
{
"text": response_text,
"color": "#3AA3E3",
"attachment_type": "default",
}
]
}
def process_slash_command_request(data):
"""
Process slash command
"""
text = ""
att_text = ""
response = {
"response_type": "in_channel",
"mrkdwn": "true",
"text": None,
"attachments": []
}
command = data["command"].strip("/")
data["text"] = " ".join(data["text"].replace("+", " ").split())
options = data["text"].split(" ")
command_text = urllib.parse.unquote(data["text"])
if slackstash.check_authorization(data):
text = "User <@{0}> has run `{1} {2}` command".format(
data["user_name"], command, command_text)
else:
text = "User <@{0}> are not have authorization access to this function!".format(
data["user_name"])
if verify_command(options):
action = options[0]
options.pop(0)
value = options
cmd = slackstash.Command()
# Delete schedule if event is auto triggered and schedule is executed once
try:
if data["auto_trigger_event"] == "True" and data["is_loop"] == "False":
cmd.call("aws_delete_schedule", data["rule_name"])
except KeyError:
print("Not auto trigger event!")
pass
if action == "turnon":
if slackstash.check_need_approval(data):
send_approval_message("User <@{0}> request to `{1}`".format(
data["user_name"], command_text), data)
att_text = constants.MESSAGE_APPROVAL_USER
else:
att_text = cmd.call(command + "_turnon", value)
elif action == "turnoff":
if slackstash.check_need_approval(data):
send_approval_message("User <@{0}> request to `{1}`".format(
data["user_name"], command_text), data)
att_text = constants.MESSAGE_APPROVAL_USER
else:
att_text = cmd.call(command + "_turnoff", value)
elif action == "status":
response["attachments"] = cmd.call(command + "_status", value)
return response
elif action == "tags":
response["attachments"] = cmd.call(command + "_tags", value)
return response
elif action == "schedule" and value[0] == "set":
schedule = process_schedule_set_option(options)
if schedule == constants.MESSAGE_WRONG_COMMAND:
att_text = "Command `{0} {1}` wrong".format(
command, data["text"].replace("+", " "))
else:
schedule["requester"] = data["user_name"]
att_text = cmd.call(command + "_set_schedule", schedule)
elif action == "schedule" and value[0] == "delete":
att_text = cmd.call(command + "_delete_schedule", value[1])
elif action == "schedule" and value[0] == "list-all":
att_text = cmd.call(command + "_list_schedule", None)
else:
att_text = "Command `{0} {1}` wrong".format(
command, data["text"].replace("+", " "))
att_text = {"text": att_text, "color": "#3AA3E3"}
response["text"] = text
response["attachments"].append(att_text)
return response
def process_event(event):
"""
Process incoming event
"""
body = event.get('body').replace("=", "\":\"").replace("&", "\",\"")
body = "{\"" + body + "\"}"
unquote_body = urllib.parse.unquote(body)
unquote_body = unquote_body.replace(':"{"', ':{"').replace('"}"', '"}')
data = json.loads(unquote_body)
# Check if this is a approval request
if 'payload' in data:
payload = data["payload"]
if payload["type"] == "interactive_message":
return process_approval_request(payload)
else:
# This request is not a approval request
return process_slash_command_request(data)
def send_approval_message(approval_message, data):
"""
Send a message to approval channel, request to approve command
"""
uri = constants.SLACK_APPROVAL_ROOM_WEEBHOOK
headers = {
'content-type': 'application/json'
}
value_object = {
"command": data["command"],
"text": data["text"],
"user_id": data["user_id"],
"user_name": data["user_name"],
"response_url": data["response_url"],
"attachments": []
}
data = {
"response_type": "in_channel",
"text": approval_message,
"attachments": [
{
"text": "Would you approve this request?",
"fallback": "Would you approve this request?",
"callback_id": "confirm_request",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "approve",
"text": "Approve",
"type": "button",
"style": "primary",
"value": str(value_object)
},
{
"name": "reject",
"text": "Reject",
"type": "button",
"style": "danger",
"value": str(value_object)
}
]
}
]
}
requests.post(uri, data=json.dumps(data), headers=headers)
def send_result_message_after_approval(data):
uri = data["response_url"]
headers = {
'content-type': 'application/json'
}
data = {
"response_type": "in_channel",
"text": '<@{0}> {1} request: `{2}` of user <@{3}>'.format(
data["approval_user_id"], data["approval_action"], data["text"], data["user_id"]),
"attachments": data["attachments"]
}
requests.post(uri, data=json.dumps(data), headers=headers)
def lambda_handler(event, context):
"""
Handler incoming request
"""
response = process_event(event)
return {
"isBase64Encoded": True,
'statusCode': 200,
'body': json.dumps(response)
}