forked from adiii717/docker-python-cronjob
-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract.py
72 lines (60 loc) · 2 KB
/
extract.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
#!/usr/bin/env python3
import os
import sys
import json
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
INPUT_PATH = "/input"
CERTS_FOLDER = os.getenv('CERT_SUBFOLDER', '/certs')
CERTS_PATH = "/output" + CERTS_FOLDER
curr_valid_until = None
class ConfigFileHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path == INPUT_PATH + "/cosmos.config.json" and os.path.getsize(event.src_path) > 0:
check_certificate()
def check_certificate():
global curr_valid_until
config_object = load_config()
if config_object:
cert = config_object["HTTPConfig"]["TLSCert"]
key = config_object["HTTPConfig"]["TLSKey"]
valid_until = config_object["HTTPConfig"]["TLSValidUntil"]
if valid_until != curr_valid_until:
write_certificates(cert, key)
curr_valid_until = valid_until
else:
print("Cosmos config file not found.")
sys.exit()
def load_config():
try:
with open(INPUT_PATH + "/cosmos.config.json", "r") as conf_file:
return json.load(conf_file)
except OSError:
return None
def write_certificates(cert, key):
with open(CERTS_PATH + "/cert.pem", "w") as cert_file:
cert_file.write(cert)
with open(CERTS_PATH + "/key.pem", "w") as key_file:
key_file.write(key)
print("Cert extracted successfully.")
def main():
if not os.path.isdir(INPUT_PATH):
print("Config folder not found.")
sys.exit()
if not os.path.isdir(CERTS_PATH):
print("Certs output folder not found.")
sys.exit()
observer = Observer()
event_handler = ConfigFileHandler()
observer.schedule(event_handler, INPUT_PATH, recursive=False)
observer.start()
print("Starting to watch for certificate updates.")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
main()