-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
97 lines (83 loc) · 2.88 KB
/
handler.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
import os
import json
import boto3
import secrets
import traceback
import subprocess
import onepassword
from pathlib import Path
from slack import WebClient
from itsdangerous import URLSafeSerializer
with open(Path(__file__).parent / "views" / "form.json") as form_view_file:
form_view = form_view_file.read()
with open(Path(__file__).parent / "views" / "end.json") as end_view_file:
end_view = end_view_file.read()
configuration = json.loads(
boto3.client("secretsmanager").get_secret_value(
SecretId=os.environ.get("SECRET_ARN")
).get("SecretString")
)
serializer = URLSafeSerializer(os.environ.get("TRANSIT_KEY", " "))
slack_client = WebClient(configuration["slack_token"])
function_prefix = os.environ.get("FUNCTION_PREFIX")
def slack_interaction(event, context):
action = json.loads(event["body"]["payload"])
if action["type"] == "view_submission":
payload = [
action["user"],
action["view"]["state"]["values"]["address"]["address"]["value"],
action["view"]["state"]["values"]["reason"]["reason"]["value"]
]
boto3.client('lambda').invoke(
FunctionName=os.environ.get("FUNCTION_PREFIX") + "create_account",
Payload=json.dumps({"payload": serializer.dumps(payload)}),
InvocationType='Event'
)
return {
"response_action": "update",
"view": end_view
}
elif action["type"] == "shortcut":
slack_client.views_open(
trigger_id=action["trigger_id"],
view=form_view
)
return ""
def create_account(event, context):
user, address, reason = serializer.loads(event["payload"])
try:
onepassword.run("create", "user", address, user["name"])
except Exception as error:
trace = ''.join(traceback.format_exception(
tb=error.__traceback__,
etype=type(error),
value=error
))
slack_client.chat_postMessage(
channel=configuration["slack_channel"],
link_names=True,
text=(
f""":warning: <@{user["id"]}> """
"*has issued an invalid request* "
f"(`{(identifier := secrets.token_hex(4))}`)"
)
)
print(json.dumps({
"request": identifier,
"user": user["id"],
"address": address,
"reason": reason,
"trace": trace
}))
finally:
notification = (
f"""<@{user["id"]}> requested an account because…"""
"\n\n" + "\n".join("> " + line for line in reason.split("\n"))
)
slack_client.chat_postMessage(
channel=configuration["slack_channel"],
text=notification,
link_names=True
)
def refresh_token(event, context):
onepassword.authenticate()