-
Notifications
You must be signed in to change notification settings - Fork 0
/
halon-deploy-cert.py
66 lines (53 loc) · 1.82 KB
/
halon-deploy-cert.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
#!/usr/bin/env python3.7
"""Deploy certificate to Halon"""
import base64
import json
from datetime import datetime, timezone
import requests
# read configuration
with open("halon-deploy-cert.json") as input_file:
config = json.load(input_file)
timestamp = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S%z")
halon_url = f"https://{config['hostname']}/api/1.0.0"
# read cert & key
with open(config["certfile"]) as cert_file:
pki_certdata = cert_file.read()
with open(config["keyfile"]) as key_file:
pki_keydata = key_file.read()
# construct new PKI parameters
pki_key = f"pki__{config['pki_key']}"
pki_params = [
f"{config['certname']} (certbot {timestamp})",
"x509+privatekey",
base64.b64encode((pki_certdata + pki_keydata).encode()).decode(),
]
# create session for Halon
session = requests.Session()
session.auth = (config["username"], config["password"])
if "cacert" in config:
session.verify = config["cacert"]
# fetch configuration from Halon
response = session.get(f"{halon_url}/config/revisions/HEAD")
response.raise_for_status()
# digest current configuration
last_halon_config = response.json()
last_halon_revision = last_halon_config["id"]
# prepare new configuration
next_halon_config = {}
next_halon_revision = last_halon_revision + 1
next_halon_config["message"] = f"certbot {timestamp}"
next_halon_config["config"] = []
# update new configuration
for c in last_halon_config["config"]:
paramset = c.copy()
if paramset["name"] == pki_key:
paramset["params"] = pki_params
next_halon_config["config"].append(paramset)
# deploy configuration
if config.get("deploy", False):
response = session.post(
f"{halon_url}/config/revisions/{next_halon_revision}", json=next_halon_config
)
response.raise_for_status()
else:
print(json.dumps(next_halon_config, indent=4))