11#! /bin/bash
22
3+ set -euo pipefail
4+
5+ SCRIPT_DIR=" $( cd " $( dirname " $0 " ) " && pwd) "
6+ REPO_ROOT=" $( cd " ${SCRIPT_DIR} /.." && pwd) "
7+ ENV_FILE=" $REPO_ROOT /.env"
8+ CERTSTORE=" $REPO_ROOT /data/certs"
9+ TMP_COMPOSE=" $( mktemp " ${TMPDIR:-/ tmp} /dvrtc-certbot.XXXXXX.yml" ) "
10+
11+ cleanup () {
12+ rm -f " $TMP_COMPOSE "
13+ }
14+ trap cleanup EXIT INT TERM
15+
316# Load public IPs from .env if it exists. Treat the file as shell syntax
417# instead of trying to parse it with grep/xargs.
5- if [ -f .env ]; then
18+ if [ -f " $ENV_FILE " ]; then
619 set -a
7- . ./.env
20+ . " $ENV_FILE "
821 set +a
922fi
1023
24+ PUBLIC_IPV4=" ${PUBLIC_IPV4:- } "
25+ PUBLIC_IPV6=" ${PUBLIC_IPV6:- } "
26+
1127SAN_ENTRIES=" DNS:localhost,IP:127.0.0.1"
1228if [ -n " $PUBLIC_IPV4 " ]; then
1329 SAN_ENTRIES=" $SAN_ENTRIES ,IP:${PUBLIC_IPV4} "
@@ -16,46 +32,56 @@ if [ -n "$PUBLIC_IPV6" ]; then
1632 SAN_ENTRIES=" $SAN_ENTRIES ,IP:${PUBLIC_IPV6} "
1733fi
1834
19- certstore=" data/certs"
20- mkdir -p " $certstore "
35+ mkdir -p " $CERTSTORE "
2136
2237host_uid=$( id -u)
2338host_gid=$( id -g)
2439
40+ cat > " $TMP_COMPOSE " << EOF
41+ services:
42+ certbot:
43+ image: certbot/certbot:v5.4.0
44+ volumes:
45+ - type: bind
46+ source: $CERTSTORE
47+ target: /etc/certstore
48+ EOF
49+
2550set_runtime_cert_permissions () {
2651 # Some scenario services (notably FreeSWITCH and OpenSIPS in pbx2) run as
2752 # non-root users and read the bind-mounted cert files directly. Keep the
2853 # generated PEM files readable on these dedicated lab hosts so all runtime
2954 # services can start consistently.
30- chmod 644 " $certstore /fullchain.pem" " $certstore /privkey.pem"
31- if [ -f " $certstore /ssl-dhparams.pem" ]; then
32- chmod 644 " $certstore /ssl-dhparams.pem"
55+ chmod 644 " $CERTSTORE /fullchain.pem" " $CERTSTORE /privkey.pem"
56+ if [ -f " $CERTSTORE /ssl-dhparams.pem" ]; then
57+ chmod 644 " $CERTSTORE /ssl-dhparams.pem"
3358 fi
3459}
3560
61+ compose_cmd=(docker compose -f " $TMP_COMPOSE " --project-directory " $REPO_ROOT " )
62+
3663echo " ### Creating self-signed certificate for localhost${PUBLIC_IPV4: +, ${PUBLIC_IPV4} }${PUBLIC_IPV6: +, ${PUBLIC_IPV6} } "
37- path=" /etc/certstore"
3864
39- # Create certificate with both localhost and IP address as Subject Alternative Names
40- docker compose run --rm --entrypoint " \
65+ # Create certificate with both localhost and IP address as Subject Alternative Names.
66+ " ${compose_cmd[@]} " run --rm --entrypoint " \
4167 /bin/sh -ec '
4268 openssl req -x509 -nodes -newkey rsa:4096 -days 365 \
43- -keyout \" $path / privkey.pem\" \
44- -out \" $path / fullchain.pem\" \
69+ -keyout /etc/certstore/ privkey.pem \
70+ -out /etc/certstore/ fullchain.pem \
4571 -subj \" /CN=localhost\" \
4672 -addext \" subjectAltName=${SAN_ENTRIES} \"
47- chown ${host_uid} :${host_gid} \" $path / privkey.pem\" \" $path / fullchain.pem\"
73+ chown ${host_uid} :${host_gid} /etc/certstore/ privkey.pem /etc/certstore/ fullchain.pem
4874 '" certbot
4975set_runtime_cert_permissions
5076
51- # Generate DH parameters for nginx TLS if not present
52- dhparams=" $certstore /ssl-dhparams.pem"
77+ # Generate DH parameters for nginx TLS if not present.
78+ dhparams=" $CERTSTORE /ssl-dhparams.pem"
5379if [ ! -f " $dhparams " ]; then
5480 echo " ### Generating DH parameters ..."
55- docker compose run --rm --entrypoint " \
81+ " ${compose_cmd[@]} " run --rm --entrypoint " \
5682 /bin/sh -ec '
57- openssl dhparam -out \" $path / ssl-dhparams.pem\" 2048
58- chown ${host_uid} :${host_gid} \" $path / ssl-dhparams.pem\"
83+ openssl dhparam -out /etc/certstore/ ssl-dhparams.pem 2048
84+ chown ${host_uid} :${host_gid} /etc/certstore/ ssl-dhparams.pem
5985 '" certbot
6086else
6187 echo " ### DH parameters already exist"
0 commit comments