bunkerweb/core/selfsigned/jobs/self-signed.py
2022-06-03 17:24:14 +02:00

62 lines
2.7 KiB
Python
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python3
import sys, os, subprocess, traceback
sys.path.append("/opt/bunkerweb/deps/python")
sys.path.append("/opt/bunkerweb/utils")
import logger
def generate_cert(first_server, days, subj) :
if os.path.isfile("/opt/bunkerweb/cache/selfsigned/" + first_server + ".pem") :
cmd = "openssl x509 -checkend 86400 -noout -in /opt/bunkerweb/cache/selfsigned/" + first_server + ".pem"
proc = subprocess.run(cmd.split(" "), stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT)
if proc.returncode == 0 :
logger.log("SELF-SIGNED", "", "Self-signed certificate already present for " + first_server)
return True, 0
logger.log("SELF-SIGNED", "", "Generating self-signed certificate for " + first_server)
cmd = "openssl req -nodes -x509 -newkey rsa:4096 -keyout /opt/bunkerweb/cache/selfsigned/" + first_server + ".key -out /opt/bunkerweb/cache/selfsigned/" + first_server + ".pem -days " + days + " -subj " + subj
proc = subprocess.run(cmd.split(" "), stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT)
if proc.returncode != 0 :
logger.log("SELF-SIGNED", "", "Self-signed certificate generation failed for " + first_server)
return False, 2
logger.log("SELF-SIGNED", "", "Successfully generated self-signed certificate for " + first_server)
return True, 1
status = 0
try :
os.makedirs("/opt/bunkerweb/cache/selfsigned/", exist_ok=True)
# Multisite case
if os.getenv("MULTISITE") == "yes" :
for first_server in os.getenv("SERVER_NAME").split(" ") :
if os.getenv(first_server + "_GENERATE_SELF_SIGNED_SSL", os.getenv("GENERATE_SELF_SIGNED_SSL")) != "yes" :
continue
if first_server == "" :
continue
if os.path.isfile("/opt/bunkerweb/cache/selfsigned/" + first_server + ".pem") :
continue
ret, ret_status = generate_cert(first_server, os.getenv(first_server + "_SELF_SIGNED_SSL_EXPIRY"), os.getenv(first_server + "_SELF_SIGNED_SSL_SUBJ"))
if not ret :
status = ret_status
elif ret_status == 1 and ret_status != 2 :
status = 1
# Singlesite case
elif os.getenv("GENERATE_SELF_SIGNED_SSL") == "yes" and os.getenv("SERVER_NAME") != "" :
first_server = os.getenv("SERVER_NAME").split(" ")[0]
ret, ret_status = generate_cert(first_server, os.getenv("SELF_SIGNED_SSL_EXPIRY"), os.getenv("SELF_SIGNED_SSL_SUBJ"))
if not ret :
status = ret_status
elif ret_status == 1 and ret_status != 2 :
status = 1
except :
status = 2
logger.log("SELF-SIGNED", "", "Exception while running certbot-new.py :")
print(traceback.format_exc())
sys.exit(status)