/
main.py
420 lines (336 loc) · 15 KB
/
main.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from logging import log
from typing import List, Optional
from fastapi import (
BackgroundTasks,
UploadFile, File,
Form,
Query,
Body,
Depends,
WebSocket
)
from fastapi.middleware.cors import CORSMiddleware
from mail import simple_send, conf
from starlette.responses import JSONResponse
from starlette.requests import Request
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from pydantic import EmailStr, BaseModel
from typing import List
from fastapi_mail.email_utils import DefaultChecker
from fastapi import FastAPI, HTTPException, Response, status, Depends
from fastapi.security import OAuth2PasswordRequestForm
from models import (MonitoringConfig, MonitoringStatus, MonitoringStatusConfig, User_Pydantic, User, UserIn, Token, EmailSchema,
HealthCheck, HealthCheckConfig, HealthCheckStatus, Monitoring, MonitoringStatus, MonitoringConfig, MonitoringStatusConfig, Logger, LoggerStatusConfig, LoggerStatus, LoggerConfig)
from crypto import valid_password, hash_password, verify_password
from crypto import create_access_token, get_current_active_user
from uuid import UUID
import time
import asyncio
from datetime import datetime, timedelta
from tortoise.contrib.fastapi import HTTPNotFoundError, register_tortoise
from db_check import check_db_every_x_seconds, clean_late_entries
import datetime
import time
COLORS = ["hsl(58, 70%, 50%)",
"hsl(114, 70%, 50%)",
"hsl(66, 70%, 50%)"]
app = FastAPI(title="Tortoise ORM FastAPI example")
origins = [
"http://localhost",
"https://localhost",
"http://localhost",
"http://localhost:3000",
"http://localhost:8000",
"http://127.0.0.1:8000"
"http://127.0.0.1:3000"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@ app.on_event("startup")
def initial_task():
task = asyncio.create_task(check_db_every_x_seconds(5, clean_late_entries))
@ app.post("/register")
async def register_user(user: UserIn):
"""
Registers a user,
takes a user_in model which is just {"email": ..., "password": ...}
"""
if not valid_password(user.password):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Password must have uppercase and lowercase letter,be at least 8 characters long and contain at least a number and an uppercase character",
)
user = user.dict()
# hash the user's password
user["password_hash"] = hash_password(user["password"])
# remove the normal password, os only password_hash remains
user.pop("password")
# Create the user in the database
user_obj = await User.create(**user)
return {"detail": "User created"}
@app.post("/token", response_model=Token)
async def get_token(form_data: OAuth2PasswordRequestForm = Depends()):
"""
Used for getting the jwt token,
takes a form data with {"username": ... , "password": ...}
but username is email in our case
"""
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
# checks if the username is in the db
user = await User.get(email=form_data.username).values_list("email", "password_hash")
if not user:
raise credentials_exception
# list of tuples
user_email = user[0][0]
user_hash = user[0][1]
# checks if password matches
if not verify_password(user_hash, form_data.password):
raise credentials_exception
access_token = create_access_token(data={"sub": user_email})
return Token(access_token=access_token, token_type="bearer")
# only as an example for jwt auth
# if you want to make a route "protected"
# add this function's signature in your route (Depends(...))
@app.get("/users/me", response_model=User_Pydantic)
async def read_users_mode(current_user: User_Pydantic = Depends(get_current_active_user)):
return current_user
# only for testing purposes, to see all the users
@app.get("/users", response_model=List[User_Pydantic])
async def get_users():
return await User_Pydantic.from_queryset(User.all())
# get current time
@app.get("/unixtime")
async def get_unix_time():
return {"time": int(time.time())}
# Health check stuff
@app.post("/app/{app_id}")
async def create_app(health_check_config: HealthCheckConfig, app_id: UUID, current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
health_check = await HealthCheck.create(name=health_check_config.app_name,
user=user_obj,
uuid=app_id,
period=health_check_config.period,
grace=health_check_config.grace)
current_time = datetime.datetime.now()
next_receive = current_time + \
timedelta(minutes=health_check_config.period+health_check_config.grace)
health_check_status = await HealthCheckStatus.create(
last_received=current_time,
next_receive=next_receive,
health_check=health_check
)
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail="App created",
)
return health_check_config
@app.get("/app/{app_id}")
async def ping_app(app_id: UUID):
health_check = await HealthCheck.get_or_none(uuid=app_id)
if health_check is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="App does not exist",
)
hc_status = await HealthCheckStatus.filter(health_check_id=health_check.id)
last_entry = hc_status[len(hc_status)-1]
curr_time = datetime.datetime.now()
if curr_time < last_entry.next_receive.replace(tzinfo=None):
last_entry.done = True
await last_entry.save()
next_receive = curr_time + \
timedelta(minutes=health_check.period+health_check.grace)
health_check_status = await HealthCheckStatus.create(
last_received=curr_time,
next_receive=next_receive,
health_check=health_check
)
raise HTTPException(
status_code=status.HTTP_200_OK,
)
@app.get("/apps-hc")
async def list_healtchecks(current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
healthchecks = await HealthCheck.all().filter(user=user_obj)
if healthchecks == []:
return []
return healthchecks
@app.get("/apps-hc-status/{app_id}")
async def list_healthcheck_status(app_id: UUID, current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
healthcheck = await HealthCheck.get(uuid=app_id).filter(user=user_obj)
healthcheck_statuses = await HealthCheckStatus.all().filter(health_check=healthcheck)
if healthcheck_statuses == []:
return []
return healthcheck_statuses
# Logger stuff
# http://127.0.0.1:8000/app-logging/18ff372e-8eb9-49ff-a835-2c602309f0bd?app_name=test
@app.post("/app-logging/{app_id}")
async def create_logger(logger_config: LoggerConfig, app_id: UUID, current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
logger_app = await Logger.create(name=logger_config.app_name,
user=user_obj,
severity_threshold=logger_config.severity_threshold,
uuid=app_id)
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail=f"Logger app created: {dict(name = logger_app.name)}",
)
@app.post("/app-logging-status/{app_id}")
async def create_logger_status(logger_config: LoggerStatusConfig, app_id: UUID):
'''
logger_config: {
device_id: str,
severity_level: int,
message: str}
'''
logger_app = await Logger.get(uuid=app_id)
if logger_app is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Logger doesn't exist",
)
logger_status = await LoggerStatus.create(device_id=logger_config.device_id,
severity_level=logger_config.severity_level,
message=logger_config.message,
logger=logger_app)
if logger_app.severity_threshold != 0 and logger_config.severity_level >= logger_app.severity_threshold:
user = await logger_app.user
print(user.email)
await simple_send([user.email],
f"Your logging applicaton <b>{logger_app.name}</b> has encountered a severe log! At {logger_status.timestamp.strftime('%H:%M:%S')} the device with the id <b>{logger_status.device_id}</b> has received a log with a severity level of <b style=\"color:red;\">{logger_status.severity_level}</b>: <br>{logger_status.message}. <br>Please investigate!",
subject=f"{logger_app.name} severe encounter!.")
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail=f"Logger status added: {dict(device = logger_status.device_id, severity_level = logger_status.severity_level, message = logger_status.message)}",
)
@app.get("/app-logging/{app_id}")
async def list_logger_statuses(app_id: UUID, current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
logger_app = await Logger.get(uuid=app_id).filter(user=user_obj)
if logger_app is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Logger doesn't exist",
)
logger_statuses = await LoggerStatus.all().filter(logger=logger_app)
if logger_statuses == []:
return []
return logger_statuses
@app.get("/apps-log")
async def list_loggers(current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
loggers = await Logger.all().filter(user=user_obj)
if loggers == []:
return []
return loggers
# Monitoring stuff
@app.post("/app-mon/{app_id}")
async def create_monitoring(monitoring_config: MonitoringConfig, app_id: UUID, current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
monitoring_app = await Monitoring.create(name=monitoring_config.app_name,
user=user_obj,
uuid=app_id)
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail=f"Monitoring app created: {dict(name = monitoring_app.name)}",
)
@app.delete("/app-delete/{app_id}")
async def delete_app(app_id: UUID, current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
print("hello")
hc_app_exists = await HealthCheck.get(uuid=app_id).filter(user=user_obj).exists()
if hc_app_exists:
await HealthCheck.get(uuid=app_id).filter(user=user_obj).delete()
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail=f"Healthcheck app deleted",
)
monitoring_app_exists = await Monitoring.get(uuid=app_id).filter(user=user_obj).exists()
if monitoring_app_exists:
await Monitoring.get(uuid=app_id).filter(user=user_obj).delete()
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail=f"Monitoring app deleted",
)
logger_app_exists = await Logger.get(uuid=app_id).filter(user=user_obj).exists()
if logger_app_exists:
await Logger.get(uuid=app_id).filter(user=user_obj).delete()
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail=f"Logger app deleted",
)
@app.get("/apps-mon")
async def list_monitoring(current_user: User_Pydantic = Depends(get_current_active_user)):
user_obj = await User.get(id=current_user.id)
monitoring_apps = await Monitoring.all().filter(user=user_obj)
if monitoring_apps == []:
return {}
return monitoring_apps
@app.post("/app-mon-status/{app_id}")
async def create_monitoring_status(monitoring_config: MonitoringStatusConfig, app_id: UUID):
monitoring_app = await Monitoring.get(uuid=app_id)
if monitoring_app is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Logger doesn't exist",
)
monitoring_statuses = await MonitoringStatus.all().filter(monitoring=monitoring_app).order_by("timestamp")
if (len(monitoring_statuses) > 10):
await monitoring_statuses[0].delete()
monitoring_status = await MonitoringStatus.create(cpu=monitoring_config.cpu,
memory=monitoring_config.memory,
monitoring=monitoring_app)
raise HTTPException(
status_code=status.HTTP_201_CREATED,
detail=f"Monitoring status added: {dict(cpu= monitoring_status.cpu, memory= monitoring_status.memory)}",
)
@app.get("/app-mon-status/{app_id}")
async def list_monitoring_statuses(app_id: UUID):
monitoring_app = await Monitoring.get(uuid=app_id)
if monitoring_app is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Monitoring app doesn't exist",
)
monitoring_statuses = await MonitoringStatus.all().filter(monitoring=monitoring_app).order_by("timestamp")
if len(monitoring_statuses) == 0:
return {}
times = [x.timestamp.strftime("%H:%M:%S")
for x in list(monitoring_statuses)]
cpus = [x.cpu for x in list(monitoring_statuses)]
memory = [x.memory for x in list(monitoring_statuses)]
data = [{"id": "cpu", "color": COLORS[0], "data": [
{"x": x, "y": y} for x, y in zip(times, cpus)]},
{"id": "memory", "color": COLORS[1], "data": [
{"x": x, "y": y} for x, y in zip(times, memory)]}]
return data
@ app.get("/")
async def root():
return Response(content="OK", media_type="text/plain")
html = """
<p>merge?</p>
"""
@ app.post("/email")
async def send_email(
email: EmailSchema
) -> JSONResponse:
await simple_send(email.dict().get('email'), email.dict().get('content'))
return JSONResponse(status_code=200, content={"message": "email has been sent"})
# Register the ORM
register_tortoise(
app,
db_url="sqlite://.db.sqlite3",
modules={"models": ["models"]},
generate_schemas=True,
add_exception_handlers=True,
)