From 23aa053003d8016397c6afe1ee61d7ce3227c6c0 Mon Sep 17 00:00:00 2001 From: bunkerity Date: Tue, 25 May 2021 12:18:40 +0200 Subject: [PATCH] templating - auth basic support --- confs2/site/auth-basic-sitewide.conf | 2 +- confs2/site/auth-basic.conf | 2 +- confs2/site/htpasswd | 3 +++ gen/Templator.py | 9 ++++++++- 4 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 confs2/site/htpasswd diff --git a/confs2/site/auth-basic-sitewide.conf b/confs2/site/auth-basic-sitewide.conf index c95e643..cae4c96 100644 --- a/confs2/site/auth-basic-sitewide.conf +++ b/confs2/site/auth-basic-sitewide.conf @@ -1,2 +1,2 @@ auth_basic "{{ AUTH_BASIC_TEXT }}"; -auth_basic_user_file {{ NGINX_PREFIX }}.htpasswd; +auth_basic_user_file {{ NGINX_PREFIX }}htpasswd; diff --git a/confs2/site/auth-basic.conf b/confs2/site/auth-basic.conf index 648f1dc..4007d11 100644 --- a/confs2/site/auth-basic.conf +++ b/confs2/site/auth-basic.conf @@ -1,4 +1,4 @@ location {{ AUTH_BASIC_LOCATION }} { auth_basic "{{ AUTH_BASIC_TEXT }}"; - auth_basic_user_file {{ NGINX_PREFIX }}.htpasswd; + auth_basic_user_file {{ NGINX_PREFIX }}htpasswd; } diff --git a/confs2/site/htpasswd b/confs2/site/htpasswd new file mode 100644 index 0000000..75e5ee0 --- /dev/null +++ b/confs2/site/htpasswd @@ -0,0 +1,3 @@ +{% if USE_AUTH_BASIC == "yes" %} +{{ AUTH_BASIC_USER }}:{{ sha512_crypt(AUTH_BASIC_PASSWORD) }} +{% endif %} diff --git a/gen/Templator.py b/gen/Templator.py index 1777f4f..08d8f4b 100644 --- a/gen/Templator.py +++ b/gen/Templator.py @@ -1,4 +1,4 @@ -import jinja2, glob, os, pathlib, copy +import jinja2, glob, os, pathlib, copy, crypt class Templator : @@ -30,11 +30,15 @@ class Templator : real_config["NGINX_PREFIX"] = self.__target_path if real_config["MULTISITE"] == "yes" and type == "site" : real_config["NGINX_PREFIX"] += first_server + "/" + for variable, value in self.__config.items() : + if variable.startswith(first_server + "_") : + real_config[variable.replace(first_server + "_", "", 1)] = value for filename in glob.iglob(self.__input_path + "/" + type + "**/**", recursive=True) : if os.path.isfile(filename) : relative_filename = filename.replace(self.__input_path, "").replace(type + "/", "") template = self.__template_env.get_template(type + "/" + relative_filename) template.globals["has_value"] = Templator.has_value + template.globals["sha512_crypt"] = Templator.sha512_crypt output = template.render(real_config, all=real_config) if real_config["MULTISITE"] == "yes" and type == "site" : relative_filename = first_server + "/" + relative_filename @@ -50,3 +54,6 @@ class Templator : if (k == name or k.endswith("_" + name)) and v == value : return True return False + + def sha512_crypt(password) : + return crypt.crypt(password, crypt.mksalt(crypt.METHOD_SHA512))