-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·81 lines (62 loc) · 1.99 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
#!/usr/bin/env python3
import dnsmonitor
import json
import os
from base64 import b64decode
import boto3
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def decrypt_environment(env=os.environ):
kms = boto3.client("kms")
for item in env.copy().keys():
if item.endswith("_ENC"):
print("Decrypting %s as %s:" % (item, item[:-4]))
env[item[:-4]] = kms.decrypt(CiphertextBlob=b64decode(env[item]))[
"Plaintext"
].decode("utf-8")
def lambda_handler(event, context):
logger.info("Starting lambda...")
# Decrypt encrypted environment vars
env = os.environ.copy()
decrypt_environment(env)
new = dnsmonitor.DNSMonitor(env=env)
new.run()
old = dnsmonitor.DNSMonitor(env=env)
try:
old.load_from_s3(os.environ["AWS_BUCKET_NAME"], os.environ["AWS_OBJECT_PATH"])
except:
logging.error("Old dns file not found for lambda")
old = new
new.save_to_s3(os.environ["AWS_BUCKET_NAME"], os.environ["AWS_OBJECT_PATH"])
# Check for changes
differ = dnsmonitor.DNSMonitor_diff(new=new, old=old, env=env)
differ.run()
# Ship out the info!
if os.environ.get("SUMO_HTTP_ENDPOINT") is not None:
differ.to_sumologic()
if os.environ.get("SLACK_WEBHOOK") is not None:
differ.to_slack()
print("Lambda done!")
return None
def main():
new = dnsmonitor.DNSMonitor()
new.run()
old = dnsmonitor.DNSMonitor()
old.load_from_file("dnsmonitor.json")
# Check for changes
differ = dnsmonitor.DNSMonitor_diff(new=new, old=old)
differ.run()
# Debug output
for change in differ.changes:
j = json.loads(change)
print(j)
# Ship out the info!
if os.environ.get("SUMO_HTTP_ENDPOINT") is not None:
differ.to_sumologic()
if os.environ.get("SLACK_WEBHOOK") is not None:
differ.to_slack()
# Save this run
new.save_to_file("dnsmonitor.json")
if __name__ == "__main__":
main()