Skip to content
Snippets Groups Projects
Verified Commit 96c18469 authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

blacked

parent cca16eba
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
from collections import OrderedDict from collections import OrderedDict
from pathlib import Path from pathlib import Path
import re import re
import socket
import subprocess import subprocess
import sys import sys
from typing import Any from typing import Any
...@@ -277,9 +278,9 @@ def run_commands(cmds: list): ...@@ -277,9 +278,9 @@ def run_commands(cmds: list):
if not cmd.get("target"): if not cmd.get("target"):
raise Exception("No target file to write in.") raise Exception("No target file to write in.")
if ( if (
cmd.get("backup") and cmd.get("backup")
Path(cmd["target"]).exists() and and Path(cmd["target"]).exists()
not Path(cmd["target"] + ".back").exists() and not Path(cmd["target"] + ".back").exists()
): ):
Path(cmd["target"]).rename(Path(cmd["target"] + ".back")) Path(cmd["target"]).rename(Path(cmd["target"] + ".back"))
log("A backup file has been created for:\n%s" % cmd["target"]) log("A backup file has been created for:\n%s" % cmd["target"])
...@@ -325,30 +326,30 @@ def add_hosts_to_localhost(hosts: list): ...@@ -325,30 +326,30 @@ def add_hosts_to_localhost(hosts: list):
:raises Exception: Houston we have a problem :raises Exception: Houston we have a problem
""" """
rc, hostname = exec_cmd('hostname') rc, hostname = exec_cmd("hostname")
if rc == 0 and hostname not in hosts: if rc == 0 and hostname not in hosts:
hosts.insert(0, hostname) hosts.insert(0, hostname)
with open('/etc/hosts', 'r') as fo: with open("/etc/hosts", "r") as fo:
content = fo.read() content = fo.read()
new_content = list() new_content = list()
found_127 = False found_127 = False
for line in content.split('\n'): for line in content.split("\n"):
if not found_127 and line.startswith('127.0.0.1'): if not found_127 and line.startswith("127.0.0.1"):
found_127 = True found_127 = True
for host in hosts: for host in hosts:
if ' ' + host not in line: if " " + host not in line:
line += ' ' + host line += " " + host
log('Adding host %s to /etc/hosts 127.0.0.1 aliases.' % host) log("Adding host %s to /etc/hosts 127.0.0.1 aliases." % host)
new_content.append(line) new_content.append(line)
if not found_127: if not found_127:
new_content.append('127.0.0.1 %s' % ' '.join(hosts)) new_content.append("127.0.0.1 %s" % " ".join(hosts))
new_content = '\n'.join(new_content) new_content = "\n".join(new_content)
if new_content != content: if new_content != content:
with open('/etc/hosts', 'w') as fo: with open("/etc/hosts", "w") as fo:
fo.write(new_content) fo.write(new_content)
log('/etc/hosts updated.') log("/etc/hosts updated.")
else: else:
log('/etc/hosts is already up to date.') log("/etc/hosts is already up to date.")
OPENSSL_CONFIG_TEMPLATE = """ OPENSSL_CONFIG_TEMPLATE = """
...@@ -392,7 +393,12 @@ subjectAltName = @alternate_names ...@@ -392,7 +393,12 @@ subjectAltName = @alternate_names
""" """
def mkcert(domains: list, ecc: bool = True, days: int = 3650, config_tpl: str = OPENSSL_CONFIG_TEMPLATE): def mkcert(
domains: list,
ecc: bool = True,
days: int = 3650,
config_tpl: str = OPENSSL_CONFIG_TEMPLATE,
):
"""Generate a self-signed certificate for the domains list. """Generate a self-signed certificate for the domains list.
:param domains: Domains for which the certificates will be self-signed :param domains: Domains for which the certificates will be self-signed
...@@ -416,24 +422,30 @@ def mkcert(domains: list, ecc: bool = True, days: int = 3650, config_tpl: str = ...@@ -416,24 +422,30 @@ def mkcert(domains: list, ecc: bool = True, days: int = 3650, config_tpl: str =
config_fh.write(config_tpl) config_fh.write(config_tpl)
# key type: elliptic curve (default) or rsa # key type: elliptic curve (default) or rsa
if ecc: if ecc:
subprocess.check_call([ subprocess.check_call(
"openssl", "ecparam", ["openssl", "ecparam", "-name", "secp384r1", "-out", cert_dir + "/ecparam"]
"-name", "secp384r1", )
"-out", cert_dir + "/ecparam"
])
keytype = "ec:" + cert_dir + "/ecparam" keytype = "ec:" + cert_dir + "/ecparam"
else: else:
keytype = "rsa" keytype = "rsa"
# execute openssl to generate keypair # execute openssl to generate keypair
subprocess.check_call([ subprocess.check_call(
"openssl", "req", [
"-config", cert_dir + "/conf", "openssl",
"-new", "req",
"-x509", "-config",
"-sha256", cert_dir + "/conf",
"-nodes", "-new",
"-newkey", keytype, "-x509",
"-keyout", cert_dir + "/key.pem", "-sha256",
"-days", str(days), "-nodes",
"-out", cert_dir + "/cert.pem" "-newkey",
]) keytype,
"-keyout",
cert_dir + "/key.pem",
"-days",
str(days),
"-out",
cert_dir + "/cert.pem",
]
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment