diff --git a/utils.py b/utils.py index 1004b4c421848c481de9daa2f4a6f795dfc8a359..3f26783e356da1e58018af9739c0cd3c1c79034d 100644 --- a/utils.py +++ b/utils.py @@ -5,6 +5,7 @@ from collections import OrderedDict from pathlib import Path import re +import socket import subprocess import sys from typing import Any @@ -277,9 +278,9 @@ def run_commands(cmds: list): if not cmd.get("target"): raise Exception("No target file to write in.") if ( - cmd.get("backup") and - Path(cmd["target"]).exists() and - not Path(cmd["target"] + ".back").exists() + cmd.get("backup") + and Path(cmd["target"]).exists() + and not Path(cmd["target"] + ".back").exists() ): Path(cmd["target"]).rename(Path(cmd["target"] + ".back")) log("A backup file has been created for:\n%s" % cmd["target"]) @@ -325,30 +326,30 @@ def add_hosts_to_localhost(hosts: list): :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: hosts.insert(0, hostname) - with open('/etc/hosts', 'r') as fo: + with open("/etc/hosts", "r") as fo: content = fo.read() new_content = list() found_127 = False - for line in content.split('\n'): - if not found_127 and line.startswith('127.0.0.1'): + for line in content.split("\n"): + if not found_127 and line.startswith("127.0.0.1"): found_127 = True for host in hosts: - if ' ' + host not in line: - line += ' ' + host - log('Adding host %s to /etc/hosts 127.0.0.1 aliases.' % host) + if " " + host not in line: + line += " " + host + log("Adding host %s to /etc/hosts 127.0.0.1 aliases." % host) new_content.append(line) if not found_127: - new_content.append('127.0.0.1 %s' % ' '.join(hosts)) - new_content = '\n'.join(new_content) + new_content.append("127.0.0.1 %s" % " ".join(hosts)) + new_content = "\n".join(new_content) if new_content != content: - with open('/etc/hosts', 'w') as fo: + with open("/etc/hosts", "w") as fo: fo.write(new_content) - log('/etc/hosts updated.') + log("/etc/hosts updated.") else: - log('/etc/hosts is already up to date.') + log("/etc/hosts is already up to date.") OPENSSL_CONFIG_TEMPLATE = """ @@ -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. :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 = config_fh.write(config_tpl) # key type: elliptic curve (default) or rsa if ecc: - subprocess.check_call([ - "openssl", "ecparam", - "-name", "secp384r1", - "-out", cert_dir + "/ecparam" - ]) + subprocess.check_call( + ["openssl", "ecparam", "-name", "secp384r1", "-out", cert_dir + "/ecparam"] + ) keytype = "ec:" + cert_dir + "/ecparam" else: keytype = "rsa" # execute openssl to generate keypair - subprocess.check_call([ - "openssl", "req", - "-config", cert_dir + "/conf", - "-new", - "-x509", - "-sha256", - "-nodes", - "-newkey", keytype, - "-keyout", cert_dir + "/key.pem", - "-days", str(days), - "-out", cert_dir + "/cert.pem" - ]) + subprocess.check_call( + [ + "openssl", + "req", + "-config", + cert_dir + "/conf", + "-new", + "-x509", + "-sha256", + "-nodes", + "-newkey", + keytype, + "-keyout", + cert_dir + "/key.pem", + "-days", + str(days), + "-out", + cert_dir + "/cert.pem", + ] + )