-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_cert.py
69 lines (62 loc) · 2.19 KB
/
generate_cert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import subprocess
import os
certs = ["auth-api.com"]
IP = "192.168.1.228"
# generate nginx and ssl conf files as well
os.makedirs("generated")
for cert in certs:
with open(f"generated/{cert}.cnf", "w") as f:
f.writelines(
[
"[ req ]\n",
"prompt = no\n",
"days = 365\n",
"distinguished_name = req_distinguished_name\n\n",
"[ req_distinguished_name ]\n"
"countryName = AB\n"
"stateOrProvinceName = CD\n"
"localityName = EFG_HIJ\n"
"organizationName = MyOrg\n"
"organizationalUnitName = MyOrgUnit\n"
f"commonName = {cert}\n"
f"subjectAltName = 2{cert}\n"
"emailAddress = [email protected]",
]
)
with open(f"generated/{cert}.nginx", "w") as f:
f.writelines(
[
"server {\n\n",
" listen 443 ssl;\n",
f" ssl_certificate /server/generated/{cert}.pem;\n",
f" ssl_certificate_key /server/generated/{cert}.key;\n",
f" server_name {cert};\n\n",
" location / {\n",
f" proxy_pass http://{IP}:5000;\n",
" proxy_set_header X-Real_IP $remote_addr;\n",
" }\n",
"}\n\n",
"server {\n",
" listen 80;\n",
f" server_name {cert};\n",
"return 302 https://$server_name$request_uri;\n",
"}",
]
)
subprocess.check_call(
[
"openssl",
"req",
"-x509",
"-newkey",
"rsa:2048",
"-keyout",
f"generated/{cert}.key",
"-out",
f"generated/{cert}.pem",
"-nodes",
"-config",
f"generated/{cert}.cnf",
]
)
# openssl req -x509 -newkey rsa:2048 -keyout /server/keylocalhost.pem -out /server/localhost.pem -nodes -config server.cnf