-
Notifications
You must be signed in to change notification settings - Fork 0
/
certginx.sh
executable file
·182 lines (152 loc) · 4.64 KB
/
certginx.sh
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
#
# DO NOT CHANGE THE CODE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
#
source .env
DOMAINS=""
MAIL=""
STAGING=false
CERTIFICATES_PATH=${CERTIFICATES_PATH:-.certificates}
RSA_KEY_SIZE=${RSA_KEY_SIZE:-4096}
function ERROR() {
echo -e "\033[0;31m\033[1m!! ${1} !! \033[0m\n"
exit 1
}
function PRINT() {
echo -e "\n > $1"
}
function stop_docker() {
PRINT "Stopping docker compose ..."
docker compose down
}
function update_tls() {
PRINT "Downloading recommended TLS parameters ..."
mkdir -p "${CERTIFICATES_PATH}/conf" &&
curl -f -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf >"$CERTIFICATES_PATH/conf/options-ssl-nginx.conf" &&
curl -f -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem >"$CERTIFICATES_PATH/conf/ssl-dhparams.pem" ||
ERROR "Unable to download recommended TLS parameters"
PRINT "TLS parameters updated successfully"
}
function create_certificate() {
stop_docker
update_tls
create_self_signed_certificate
PRINT "Starting nginx ..."
docker compose up --force-recreate -d nginx
[[ $(docker container inspect -f '{{.State.Running}}' "certginx_nginx") = "true" ]] ||
ERROR "Unbale to start nginx (check your configuration)"
remove_certificates
PRINT "Requesting Let's Encrypt certificate for ${DOMAINS} ..."
local domain_args=""
for domain in ${DOMAINS}; do
domain_args="${domain_args} -d ${domain}"
done
[[ ${EMAIL} = "" ]] &&
email_arg="--register-unsafely-without-email" ||
email_arg="--email $EMAIL"
[[ "$STAGING" != false ]] && staging_arg="--staging"
docker compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
$staging_arg \
$email_arg \
$domain_args \
--rsa-key-size $RSA_KEY_SIZE \
--agree-tos \
--force-renewal" certbot ||
ERROR "Unable to request certificates"
stop_docker
}
function create_self_signed_certificate() {
PRINT "Creating self signed certificate for ${DOMAINS%% *} ..."
mkdir -p "${CERTIFICATES_PATH}/conf/live/${DOMAINS%% *}"
docker compose run --rm --entrypoint "\
openssl req -x509 -nodes -newkey rsa:${RSA_KEY_SIZE} -days 3650 \
-keyout '/etc/letsencrypt/live/${DOMAINS%% *}/privkey.pem' \
-out '/etc/letsencrypt/live/${DOMAINS%% *}/fullchain.pem' \
-subj '/CN=localhost'" certbot ||
echo "\033[0;31m\033[1m!! Unable to create dummy certificates !! \033[0m\n"
PRINT "Certificate for ${DOMAINS%% *} created successfully"
}
function remove_certificates() {
PRINT "Removing Let's Encrypt certificate for ${DOMAINS%% *} ..."
local output=$(docker compose run --rm --entrypoint "certbot certificates" certbot 2>&1 | grep -E "Certificate Name:.*${DOMAINS%% *}")
if [[ ! -z "${output}" ]]; then
docker compose run --rm --entrypoint "\
certbot delete --cert-name ${DOMAINS%% *}" certbot ||
ERROR "Unable to remove ${DOMAINS%% *}"
fi
docker compose run --rm --entrypoint "\
rm -Rf /etc/letsencrypt/live/${DOMAINS%% *} && \
rm -Rf /etc/letsencrypt/archive/${DOMAINS%% *} && \
rm -Rf /etc/letsencrypt/renewal/${DOMAINS%% *}.conf" certbot ||
ERROR "Unable to remove ${DOMAINS%% *}"
PRINT "Certificate for ${DOMAINS%% *} removed successfully"
}
function command_list_domains() {
PRINT "List of certificates names:"
docker compose run --rm --entrypoint "\
certbot certificates" certbot ||
ERROR "Unable list domains"
}
function usage() {
cat <<EOF
Usage: $0 <command> [options]
Commands:
add -d <domains> -e <email> Add domains with email (-e not required)
self-signed -d <name> Create self signed certificate
remove -d <domains> Remove domains
list List domains (not self signed)
update-tls Update TLS parameters
EOF
exit 1
}
function main() {
local COMMAND="${1}"
shift
# Parse options
while [[ $# -gt 0 ]]; do
case "${1}" in
-d | --domains)
shift
DOMAINS="${1}"
;;
-e | --email)
shift
EMAIL="${1}"
;;
--staging)
STAGING=true
;;
*)
[[ ! -z "${1}" ]] && echo "Invalid option: ${1}" || echo "Missing arguments"
usage
;;
esac
shift
done
# Parse commands
case ${COMMAND} in
add)
[[ -z ${DOMAINS} ]] && usage
create_certificate
;;
self-signed)
[[ -z ${DOMAINS} ]] && usage
create_self_signed_certificate
;;
remove)
[[ -z ${DOMAINS} ]] && usage
remove_certificates
;;
list)
command_list_domains
;;
update-tls)
update_tls
;;
*)
usage
;;
esac
}
main "$@"