-
Notifications
You must be signed in to change notification settings - Fork 13
/
webapi.py
67 lines (60 loc) · 1.89 KB
/
webapi.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
# -*- coding:utf-8 -*-
from fastapi import FastAPI, Header, Request, Response
import uvicorn
import hmac
from libs.git import MyGitlab
from libs.murphy import MurphyScan
from datetime import datetime
from pydantic import BaseModel
from uuid import UUID
import configs
app = FastAPI()
APP_NAME = "webhook-listener"
WEBHOOK_SECRET = "My precious"
class WebhookData(BaseModel):
username: str
data: dict
event: str
timestamp: datetime
model: str
request_id: UUID
@app.post("/gitlab/webhook")
async def webhook(
request: Request,
response: Response,
content_length: int = Header(...),
x_hook_signature: str = Header(None)
):
if content_length > 1000000:
response.status_code = 400
return {"result": "Content too long"}
if x_hook_signature:
raw_input = await request.body()
input_hmac = hmac.new(
key=WEBHOOK_SECRET.encode(),
msg=raw_input,
digestmod="sha512"
)
if not hmac.compare_digest(input_hmac.hexdigest(), x_hook_signature):
#logger.error("Invalid message signature")
response.status_code = 400
return {"result": "Invalid message signature"}
#logger.info("Message signature checked ok")
else:
pass
#logger.info("No message signature to check")
body = await request.json()
if body['event_name'] == 'event_name':
branch = body['body'].split('/')[-1]
user_name = body['user_name']
git_http_url = body['git_http_url']
my_gl = MyGitlab(addr=configs.gitlab.address, token=configs.gitlab.token)
mf = MurphyScan(token=configs.murphy.token)
path = my_gl.clone_branch(git_http_url, branch)
scan_res = mf.scan(path)
my_gl.del_code(path)
else:
pass
return {"result": "ok"}
if __name__ == "__main__":
uvicorn.run("webapi:app", host="0.0.0.0", port=8888)