Skip to content

Commit dbb0a49

Browse files
committed
hax
1 parent 88981ec commit dbb0a49

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

dags/nycmesh_generate_omni_cert.py

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,6 @@
33

44
from airflow.sdk import dag, task
55

6-
7-
def generate_certbot_tsig_cert(fqdn_string, dns_server, tsig_key_name, tsig_key, full_chain_path, priv_key_path):
8-
tsig_ini_file_path = "/tmp/tsig.ini"
9-
with open(tsig_ini_file_path, "w") as fd:
10-
fd.write(f"""# Target DNS server
11-
dns_rfc2136_server = {dns_server}
12-
# Target DNS port
13-
dns_rfc2136_port = 53
14-
# TSIG key name
15-
dns_rfc2136_name = {tsig_key_name}
16-
# TSIG key secret
17-
dns_rfc2136_secret = {tsig_key}
18-
# TSIG key algorithm
19-
dns_rfc2136_algorithm = HMAC-SHA512
20-
""")
21-
22-
# Get the cert from Let's Encrypt
23-
completed = subprocess.run([
24-
"certbot",
25-
"certonly",
26-
"--dns-rfc2136",
27-
"--dns-rfc2136-credentials",
28-
tsig_ini_file_path,
29-
"--non-interactive",
30-
"--agree-tos",
31-
"-m",
32-
"jameso@nycmesh.net",
33-
"-d",
34-
fqdn_string,
35-
"--fullchain-path",
36-
full_chain_path,
37-
"--key-path",
38-
priv_key_path,
39-
], check=True)
40-
41-
print(completed.stdout)
42-
print(completed.stderr)
43-
44-
Path(tsig_ini_file_path).unlink()
45-
46-
return full_chain_path, priv_key_path
47-
48-
def deploy_to_omni(ip, password, cert_path, priv_key_path):
49-
with SSHClient() as ssh:
50-
ssh.set_missing_host_key_policy(AutoAddPolicy())
51-
ssh.connect(ip, username="admin", password=password, timeout=10)
52-
53-
with SCPClient(ssh.get_transport()) as scp:
54-
scp.put(cert_path, "fullchain.pem")
55-
scp.put(priv_key_path, "privkey.pem")
56-
57-
stdin, stdout, stderr = ssh.exec_command(
58-
"/certificate/import file-name=fullchain.pem name=LEfullchain trusted=no;"
59-
"/certificate/import file-name=privkey.pem name=LEprivkey trusted=no;"
60-
"/ip/service set www-ssl certificate=LEfullchain disabled=no tls-version=only-1.2 address=10.0.0.0/8,199.167.59.0/24,199.170.132.0/24"
61-
)
62-
print(stdout.read())
63-
print(stderr.read())
64-
656
args = {
667
'owner': 'Airflow',
678
'start_date': datetime.datetime(2021, 1, 1),
@@ -79,6 +20,65 @@ def omni_cert_dag():
7920
task_id="certbot_omni_nn_certv1"
8021
)
8122
def omni_nn_cert_task():
23+
24+
def generate_certbot_tsig_cert(fqdn_string, dns_server, tsig_key_name, tsig_key, full_chain_path, priv_key_path):
25+
tsig_ini_file_path = "/tmp/tsig.ini"
26+
with open(tsig_ini_file_path, "w") as fd:
27+
fd.write(f"""# Target DNS server
28+
dns_rfc2136_server = {dns_server}
29+
# Target DNS port
30+
dns_rfc2136_port = 53
31+
# TSIG key name
32+
dns_rfc2136_name = {tsig_key_name}
33+
# TSIG key secret
34+
dns_rfc2136_secret = {tsig_key}
35+
# TSIG key algorithm
36+
dns_rfc2136_algorithm = HMAC-SHA512
37+
""")
38+
39+
# Get the cert from Let's Encrypt
40+
completed = subprocess.run([
41+
"certbot",
42+
"certonly",
43+
"--dns-rfc2136",
44+
"--dns-rfc2136-credentials",
45+
tsig_ini_file_path,
46+
"--non-interactive",
47+
"--agree-tos",
48+
"-m",
49+
"jameso@nycmesh.net",
50+
"-d",
51+
fqdn_string,
52+
"--fullchain-path",
53+
full_chain_path,
54+
"--key-path",
55+
priv_key_path,
56+
], check=True)
57+
58+
print(completed.stdout)
59+
print(completed.stderr)
60+
61+
Path(tsig_ini_file_path).unlink()
62+
63+
return full_chain_path, priv_key_path
64+
65+
def deploy_to_omni(ip, password, cert_path, priv_key_path):
66+
with SSHClient() as ssh:
67+
ssh.set_missing_host_key_policy(AutoAddPolicy())
68+
ssh.connect(ip, username="admin", password=password, timeout=10)
69+
70+
with SCPClient(ssh.get_transport()) as scp:
71+
scp.put(cert_path, "fullchain.pem")
72+
scp.put(priv_key_path, "privkey.pem")
73+
74+
stdin, stdout, stderr = ssh.exec_command(
75+
"/certificate/import file-name=fullchain.pem name=LEfullchain trusted=no;"
76+
"/certificate/import file-name=privkey.pem name=LEprivkey trusted=no;"
77+
"/ip/service set www-ssl certificate=LEfullchain disabled=no tls-version=only-1.2 address=10.0.0.0/8,199.167.59.0/24,199.170.132.0/24"
78+
)
79+
print(stdout.read())
80+
print(stderr.read())
81+
8282
import subprocess
8383
subprocess.check_call([sys.executable, "-m", "pip", "install", "certbot", "paramiko==3.5.1", "scp==0.15.0", "cryptography==42.0.8"])
8484

0 commit comments

Comments
 (0)