mirror of
https://git.nolog.cz/NoLog.cz/nginx-configurator.git
synced 2025-01-31 03:43:35 +01:00
First ssl attempt, unfinished
This commit is contained in:
parent
dd8bd0078d
commit
f7c8eed701
5 changed files with 119 additions and 4 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -2,4 +2,6 @@
|
||||||
*.pyc
|
*.pyc
|
||||||
__pycache__
|
__pycache__
|
||||||
.vscode
|
.vscode
|
||||||
clusters.json
|
clusters.json
|
||||||
|
/nginx
|
||||||
|
/autossl
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
* Add a way to generate autoincrementing config ID
|
* Add a way to generate autoincrementing config ID
|
||||||
|
* document dhparam.pem generation
|
||||||
|
|
||||||
|
|
||||||
# Contributions
|
# Contributions
|
||||||
Please use `black` formatter.
|
Please use `black` formatter.
|
||||||
|
|
92
n-ssl.py
Normal file
92
n-ssl.py
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import re
|
||||||
|
|
||||||
|
# NGINX_DIR="/etc/nginx"
|
||||||
|
# DOMAINS_TXT = "/etc/autossl/domains.txt"
|
||||||
|
# DEHYDRATED_LOC = "/etc/autossl/dehydrated.sh"
|
||||||
|
|
||||||
|
NGINX_DIR = "./nginx"
|
||||||
|
DOMAINS_TXT = "./autossl/domains.txt"
|
||||||
|
DEHYDRATED_LOC = "./autossl/dehydrated.sh"
|
||||||
|
|
||||||
|
REMOTE = "10.55.55.55" # make a .env variable or something like that. It will be different on each server
|
||||||
|
|
||||||
|
|
||||||
|
def create_domfile():
|
||||||
|
# Get nginx config files with "# AUTOSSL" tag, parse IDs and domains and create domains.txt file for Dehydrated
|
||||||
|
sites_path = NGINX_DIR + "/sites"
|
||||||
|
# It's probably not the best to use grep here, but it's really fast unlike reading files in Python directly. But what can go wrong? (lol)
|
||||||
|
grep_out = subprocess.run(
|
||||||
|
["grep", "-Rh", "AUTOSSL", sites_path], capture_output=True, text=True
|
||||||
|
)
|
||||||
|
if grep_out.returncode == 0:
|
||||||
|
DOMAIN_LINES = []
|
||||||
|
for line in grep_out.stdout.splitlines():
|
||||||
|
id = re.findall(r"\d+", line)[-1]
|
||||||
|
domains = re.findall(r"(?<=server_name )(.*)(?=;)", line)[0]
|
||||||
|
DOMAIN_LINES.append(domains + " > " + str(id))
|
||||||
|
|
||||||
|
if len(DOMAIN_LINES) > 0:
|
||||||
|
with open(DOMAINS_TXT, "w") as fp:
|
||||||
|
for line in DOMAIN_LINES:
|
||||||
|
# write each item on a new line
|
||||||
|
fp.write("%s\n" % line)
|
||||||
|
else:
|
||||||
|
print("No data to write to domains.txt. \n Aborting")
|
||||||
|
exit()
|
||||||
|
else:
|
||||||
|
print("Finding #AUTOSSL comments in nginx configs failed.")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
def request_cert():
|
||||||
|
print("Requesting certificate")
|
||||||
|
dehydrated_run = subprocess.run(
|
||||||
|
[DEHYDRATED_LOC, "-c"], capture_output=True, text=True
|
||||||
|
)
|
||||||
|
if dehydrated_run.returncode != 0:
|
||||||
|
print("Something went wrong with dehydrated.sh")
|
||||||
|
print(dehydrated_run.stdout)
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
"Certificates are successfully dehydrated. (It went OK and cert is now generated)"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def reload_local_nginx():
|
||||||
|
nginx_check = subprocess.run(["nginx", "-t"], capture_output=True, text=True)
|
||||||
|
if nginx_check.returncode != 0:
|
||||||
|
print("nginx config is not valid! Aborting")
|
||||||
|
print(nginx_check.stdout)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
nginx_reload = subprocess.run(
|
||||||
|
["systemctl", "reload", "nginx.service"], capture_output=True, text=True
|
||||||
|
)
|
||||||
|
if nginx_reload.returncode != 0:
|
||||||
|
print("Nginx reload returned non-zero status code")
|
||||||
|
print(nginx_reload.stdout)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
def remote_replication(remote):
|
||||||
|
# Do RSYNC to second server
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def remote_reload(remote):
|
||||||
|
# Check and reload nginx on second server
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# create_domfile()
|
||||||
|
request_cert()
|
||||||
|
reload_local_nginx()
|
||||||
|
remote_replication(REMOTE)
|
||||||
|
remote_reload(REMOTE)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -10,11 +10,11 @@ upstream up_{{ id }} {
|
||||||
server {
|
server {
|
||||||
server_name{% for domain in domains %} {{ domain }}{% endfor %}; # AUTOSSL > {{ id }}
|
server_name{% for domain in domains %} {{ domain }}{% endfor %}; # AUTOSSL > {{ id }}
|
||||||
|
|
||||||
listen 80;
|
listen 443 ssl http2;
|
||||||
listen [::]:80;
|
listen [::]:443 ssl http2;
|
||||||
|
|
||||||
# ssl
|
# ssl
|
||||||
include /etc/autossl/gen/{{ id }}.conf;
|
include /etc/nginx/ssl/{{ id }}.conf;
|
||||||
|
|
||||||
# logging
|
# logging
|
||||||
include include/logging-nolog.conf; # Change to "logging-debug" if needed
|
include include/logging-nolog.conf; # Change to "logging-debug" if needed
|
||||||
|
@ -31,4 +31,19 @@ server {
|
||||||
include include/proxy-headers.conf;
|
include include/proxy-headers.conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
listen [::]:80;
|
||||||
|
|
||||||
|
server_name{% for domain in domains %} {{ domain }}{% endfor %};
|
||||||
|
|
||||||
|
location ^~ /.well-known/acme-challenge {
|
||||||
|
alias /var/www/dehydrated;
|
||||||
|
}
|
||||||
|
|
||||||
|
location / {
|
||||||
|
return 301 https://$host$request_uri;
|
||||||
|
}
|
||||||
}
|
}
|
4
templates/ssl.conf
Normal file
4
templates/ssl.conf
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
ssl_certificate /etc/autossl/certs/{{ id }}/fullchain.pem;
|
||||||
|
ssl_certificate_key /etc/autossl/certs/{{ id }}/privkey.pem;
|
||||||
|
include include/ssl_defaults.conf;
|
||||||
|
ssl_dhparam /etc/autossl/ssl-dhparams.pem;
|
Loading…
Reference in a new issue