Skip to content

Commit de8c787

Browse files
feat: implement server url environment variable (#15)
* feat: implement server url environment variable * Update entrypoint.sh to include additional environment variables for validation * Fix formatting * Enhance entrypoint.sh to check for existing Cloudflare credentials file before creating a new one, improving efficiency and preventing unnecessary overwrites. --------- Co-authored-by: Jay Rogers <jay@521dimensions.com>
1 parent 73e2306 commit de8c787

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ The following environment variables can be used to customize the Certbot contain
5959
| `CERTBOT_DOMAINS` | Comma-separated list of domains for which to obtain the certificate | - |
6060
| `CERTBOT_EMAIL` | Email address for Let's Encrypt notifications | - |
6161
| `CERTBOT_KEY_TYPE` | Type of private key to generate | `ecdsa` |
62+
| `CERTBOT_SERVER` | The ACME server URL | `https://acme-v02.api.letsencrypt.org/directory` |
6263
| `CLOUDFLARE_API_TOKEN` | Cloudflare API token for DNS authentication (see below how to create one) | - |
6364
| `CLOUDFLARE_CREDENTIALS_FILE` | Path to the Cloudflare credentials file. | `/cloudflare.ini` |
6465
| `CLOUDFLARE_PROPAGATION_SECONDS` | Wait time (in seconds) after setting DNS TXT records before validation. Useful if DNS propagation is slow. | `10` |

src/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ARG CERTBOT_GID=9999
1010
ENV CERTBOT_DOMAINS="" \
1111
CERTBOT_EMAIL="" \
1212
CERTBOT_KEY_TYPE="ecdsa" \
13+
CERTBOT_SERVER="https://acme-v02.api.letsencrypt.org/directory" \
1314
CLOUDFLARE_API_TOKEN="" \
1415
CLOUDFLARE_CREDENTIALS_FILE="/cloudflare.ini" \
1516
CLOUDFLARE_PROPAGATION_SECONDS="10" \

src/entrypoint.sh

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ debug_print() {
2626

2727
configure_uid_and_gid() {
2828
debug_print "Preparing environment for $PUID:$PGID..."
29-
29+
3030
# Handle existing user with the same UID
3131
if id -u "${PUID}" >/dev/null 2>&1; then
3232
old_user=$(id -nu "${PUID}")
@@ -112,6 +112,7 @@ run_certbot() {
112112
-d "$CERTBOT_DOMAINS" \
113113
--key-type "$CERTBOT_KEY_TYPE" \
114114
--email "$CERTBOT_EMAIL" \
115+
--server "$CERTBOT_SERVER" \
115116
--agree-tos \
116117
--non-interactive \
117118
--strict-permissions
@@ -122,13 +123,13 @@ run_certbot() {
122123
fi
123124

124125
if [ "$REPLACE_SYMLINKS" = "true" ]; then
125-
replace_symlinks "/etc/letsencrypt/live";
126+
replace_symlinks "/etc/letsencrypt/live"
126127
fi
127128
}
128129

129130
validate_environment_variables() {
130131
# Validate required environment variables
131-
for var in CLOUDFLARE_API_TOKEN CERTBOT_DOMAINS CERTBOT_EMAIL CERTBOT_KEY_TYPE; do
132+
for var in CLOUDFLARE_API_TOKEN CERTBOT_DOMAINS CERTBOT_EMAIL CERTBOT_KEY_TYPE CERTBOT_SERVER CLOUDFLARE_CREDENTIALS_FILE CLOUDFLARE_PROPAGATION_SECONDS; do
132133
if [ -z "$(eval echo \$$var)" ]; then
133134
echo "Error: $var environment variable is not set"
134135
exit 1
@@ -144,7 +145,7 @@ trap cleanup TERM INT
144145

145146
# Ensure backwards compatibility with the old CERTBOT_DOMAIN environment variable
146147
if [ -n "$CERTBOT_DOMAIN" ] && [ -z "$CERTBOT_DOMAINS" ]; then
147-
CERTBOT_DOMAINS=$CERTBOT_DOMAIN
148+
CERTBOT_DOMAINS=$CERTBOT_DOMAIN
148149
fi
149150

150151
validate_environment_variables
@@ -157,7 +158,7 @@ if [ "$REPLACE_SYMLINKS" = "true" ]; then
157158
configure_windows_file_permissions
158159
fi
159160

160-
cat << "EOF"
161+
cat <<"EOF"
161162
____________________
162163
< Certbot, activate! >
163164
--------------------
@@ -171,17 +172,22 @@ EOF
171172
echo "🚀 Let's Get Encrypted! 🚀"
172173
echo "🌐 Domain(s): $CERTBOT_DOMAINS"
173174
echo "📧 Email: $CERTBOT_EMAIL"
175+
echo "🌐 Certbot Server: $CERTBOT_SERVER"
174176
echo "🔑 Key Type: $CERTBOT_KEY_TYPE"
175177
echo "⏰ Renewal Interval: $RENEWAL_INTERVAL seconds"
176178
echo "🕒 DNS Propagation Wait: $CLOUDFLARE_PROPAGATION_SECONDS seconds"
177179
echo "Let's Encrypt, shall we?"
178180
echo "-----------------------------------------------------------"
179181

180182
# Create Cloudflare configuration file
181-
echo "dns_cloudflare_api_token = $CLOUDFLARE_API_TOKEN" > "$CLOUDFLARE_CREDENTIALS_FILE"
182-
chmod 600 "$CLOUDFLARE_CREDENTIALS_FILE"
183-
if ! is_default_privileges; then
184-
chown "${PUID}:${PGID}" "$CLOUDFLARE_CREDENTIALS_FILE"
183+
if [ -f "$CLOUDFLARE_CREDENTIALS_FILE" ]; then
184+
echo "Using existing Cloudflare credentials file: $CLOUDFLARE_CREDENTIALS_FILE"
185+
else
186+
echo "dns_cloudflare_api_token = $CLOUDFLARE_API_TOKEN" > "$CLOUDFLARE_CREDENTIALS_FILE"
187+
chmod 600 "$CLOUDFLARE_CREDENTIALS_FILE"
188+
if ! is_default_privileges; then
189+
chown "${PUID}:${PGID}" "$CLOUDFLARE_CREDENTIALS_FILE"
190+
fi
185191
fi
186192

187193
# Check if a command was passed to the container
@@ -210,20 +216,20 @@ else
210216
echo "Next certificate renewal check will be at ${next_run}"
211217

212218
# Store PID of sleep process and wait for it
213-
sleep "$RENEWAL_INTERVAL" &
219+
sleep "$RENEWAL_INTERVAL" &
214220
sleep_pid=$!
215221
wait $sleep_pid
216222
wait_status=$?
217223

218224
# Check if we received a signal (more portable check)
219225
case $wait_status in
220-
0) : ;; # Normal exit
221-
*) cleanup ;;
226+
0) : ;; # Normal exit
227+
*) cleanup ;;
222228
esac
223229

224230
if ! run_certbot; then
225231
echo "Error: Certificate renewal failed. Exiting."
226232
exit 1
227233
fi
228234
done
229-
fi
235+
fi

0 commit comments

Comments
 (0)