-
Notifications
You must be signed in to change notification settings - Fork 4
/
lambda_function.py
42 lines (36 loc) · 1.29 KB
/
lambda_function.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
import generator
import boto3
import json
import traceback
from urllib.parse import urlparse
def lambda_handler(event, context):
if "domain" in event:
domain = event["domain"]
try:
if not (domain.startswith("https://") or domain.startswith("http://")):
domain = f"null://{domain}"
domain = urlparse(domain).hostname
body = generator.genSecurityTxtForDomain((0, domain), return_body=True)
if body:
bucket = "gotsecuritytxt.com"
key = f"gen/{domain}"
s3r = boto3.resource("s3")
s3Obj = s3r.Object(bucket, key)
s3Obj.put(
ACL="public-read",
Body=body.encode("utf-8"),
ContentType="text/html",
CacheControl="public, max-age=60",
)
print(json.dumps({"event": event, "target": domain, "outcome": "success"}))
except Exception as e:
print(
json.dumps(
{
"event": event,
"target": domain,
"outcome": "failed",
"error": traceback.format_exc(),
}
)
)