-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
139 lines (111 loc) · 5.66 KB
/
app.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
import csv
import os
import random
import string
from flask import Flask, request, jsonify
from slack_sdk.webhook import WebhookClient
from dotenv import load_dotenv
from datetime import datetime
# Initializing ".env" variables
load_dotenv()
# Initializing datetime
date = datetime.now()
app = Flask(__name__)
URL = os.getenv('SLACK_WEBHOOK')
AUTH_TOKEN = os.getenv('AUTH_TOKEN')
TEST_SUCCESS_MD5='8fab2cca7d6927a6f5f7c866db28ce3e'
RED='#b90909'
GREEN='#09b912'
def slack_notification(URL, serial_number=None, username=None, message=None, color=None):
webhook = WebhookClient(URL)
response = webhook.send(
text="Precommit Deployment Alert",
attachments=[
{
"color": color,
"author_name": f"{message}\n",
"fields": [{"value": f"Username: {username}\nSerial number: {serial_number}"}],
}
]
)
# Linux Deployment Script Endpoint
@app.route('/linux-endpoint', methods=['POST'])
def linux_notify():
if request.headers.get('Authorization') != AUTH_TOKEN:
return jsonify({'message': 'Authorization failed'}), 401
data = {
'serial_number': request.form.get('serial_number'),
'username': request.form.get('username')
}
with open('linux_data.csv', mode='a') as csv_file:
fieldnames = ['serial_number', 'username']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writerow(data)
return jsonify({'message': 'success'}), 200
# MacBook Deployment Script Endpoint
# eg. curl -X POST -d "serial_number=$SERIAL_NUMBER&username=$user&brew_installed=<Error Message> | none>&trufflehog_installed=<Error Message> | none>" https://REPLACE_WITH_ELB:8443/mac-<replace with random endpoint> -k -H "Authorization: token"
@app.route('/mac-d587288a0b3eeda4d63d', methods=['POST'])
def mac_notify():
if request.headers.get('Authorization') != AUTH_TOKEN:
return jsonify({'message': 'Authorization failed'}), 401
data = {
'serial_number': request.form.get('serial_number'),
'username': request.form.get('username'),
'brew_installed': request.form.get('brew_installed'),
'git_installed': request.form.get('git_installed'),
'trufflehog_installed': request.form.get('trufflehog_installed'),
'timestamp': date.strftime("%d/%m/%Y-%H:%M:%S")
}
if data['brew_installed'] == 'BREW_NOT_INSTALLED':
slack_notification(URL, serial_number=data['serial_number'], username=data['username'], message='Brew not Installed', color=RED)
if data['trufflehog_installed'] == 'TRUFFLEHOG_NOT_INSTALLED':
slack_notification(URL, serial_number=data['serial_number'], username=data['username'], message='Trufflehog not Installed', color=RED)
if data['git_installed'] == 'GIT_NOT_INSTALLED':
slack_notification(URL, serial_number=data['serial_number'], username=data['username'], message='Git not Installed', color=RED)
with open('mac_data.csv', mode='a') as csv_file:
fieldnames = ['serial_number', 'username', 'brew_installed', 'trufflehog_installed', 'git_installed', 'timestamp']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writerow(data)
return jsonify({'message': 'success'}), 200
# eg. curl -X POST -d "serial_number=$SERIAL_NUMBER&username=$user&test_log_base64=<Insert Base64 test log>" https://REPLACE_WITH_ELB:8443/mac-test-log-<replace with random endpoint> -k -H "Authorization: token"
@app.route('/mac-test-log-endpoint', methods=['POST'])
def mac_notify_test_log():
if request.headers.get('Authorization') != AUTH_TOKEN:
return jsonify({'message': 'Authorization failed'}), 401
data = {
'serial_number': request.form.get('serial_number'),
'username': request.form.get('username'),
'timestamp': date.strftime("%d/%m/%Y-%H:%M:%S")
}
if TEST_SUCCESS_MD5 == request.form.get('test_log_md5'):
data['status'] = 'Success'
slack_notification(URL, serial_number=data['serial_number'], username=data['username'], message='Pre-commit Configured Successfully', color=GREEN)
else:
data['status'] = 'Fail'
slack_notification(URL, serial_number=data['serial_number'], username=data['username'], message='Pre-commit Configured FAILED', color=RED)
with open('mac_data_test_log.csv', mode='a') as csv_file:
fieldnames = ['serial_number', 'username', 'status', 'timestamp']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writerow(data)
return jsonify({'message': 'success'}), 200
# eg. curl -X POST -d "serial_number=$SERIAL_NUMBER&user_log_base64=<Insert Base64 user log>" https://REPLACE_WITH_ELB:8443/mac-log-<replace with random endpoint> -k -H "Authorization: token"
@app.route('/mac-log-endpoint', methods=['POST'])
def mac_notify_log():
if request.headers.get('Authorization') != AUTH_TOKEN:
return jsonify({'message': 'Authorization failed'}), 401
data = {
'serial_number': request.form.get('serial_number'),
'user_log_base64': request.form.get('user_log_base64'),
'timestamp': date.strftime("%d/%m/%Y-%H:%M:%S")
}
with open('mac_data_log.csv', mode='a') as csv_file:
fieldnames = ['serial_number', 'user_log_base64', 'timestamp']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writerow(data)
return jsonify({'message': 'success'}), 200
# Use to test ELB connection
@app.route('/ping', methods=['GET'])
def ping():
return jsonify({'message': 'pong'}), 200
if __name__ == '__main__':
app.run(host="0.0.0.0",port=8080,ssl_context=('cert.pem','key.pem'))