-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathentrypoint.sh
More file actions
360 lines (293 loc) · 9.86 KB
/
entrypoint.sh
File metadata and controls
360 lines (293 loc) · 9.86 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
set -e
source "/scripts/functions.sh"
PORT=${PORT:-443}
TXT_PREFIX=${TXT_PREFIX:-"_dstack-app-address"}
if ! PORT=$(sanitize_port "$PORT"); then
exit 1
fi
if ! DOMAIN=$(sanitize_domain "$DOMAIN"); then
exit 1
fi
if ! TARGET_ENDPOINT=$(sanitize_target_endpoint "$TARGET_ENDPOINT"); then
exit 1
fi
if ! CLIENT_MAX_BODY_SIZE=$(sanitize_client_max_body_size "$CLIENT_MAX_BODY_SIZE"); then
exit 1
fi
if ! PROXY_READ_TIMEOUT=$(sanitize_proxy_timeout "$PROXY_READ_TIMEOUT"); then
exit 1
fi
if ! PROXY_SEND_TIMEOUT=$(sanitize_proxy_timeout "$PROXY_SEND_TIMEOUT"); then
exit 1
fi
if ! PROXY_CONNECT_TIMEOUT=$(sanitize_proxy_timeout "$PROXY_CONNECT_TIMEOUT"); then
exit 1
fi
if ! PROXY_BUFFER_SIZE=$(sanitize_proxy_buffer_size "$PROXY_BUFFER_SIZE"); then
exit 1
fi
if ! PROXY_BUFFERS=$(sanitize_proxy_buffers "$PROXY_BUFFERS"); then
exit 1
fi
if ! PROXY_BUSY_BUFFERS_SIZE=$(sanitize_proxy_buffer_size "$PROXY_BUSY_BUFFERS_SIZE"); then
exit 1
fi
if ! TXT_PREFIX=$(sanitize_dns_label "$TXT_PREFIX"); then
exit 1
fi
if ! ALIAS_DOMAIN=$(sanitize_domain "$ALIAS_DOMAIN"); then
exit 1
fi
PROXY_CMD="proxy"
if [[ "${TARGET_ENDPOINT}" == grpc://* ]]; then
PROXY_CMD="grpc"
fi
echo "Setting up certbot environment"
setup_py_env() {
if [ ! -d /opt/app-venv ]; then
echo "Creating application virtual environment"
python3 -m venv --system-site-packages /opt/app-venv
fi
# Activate venv for subsequent steps
# shellcheck disable=SC1091
source /opt/app-venv/bin/activate
if [ ! -f /.venv_bootstrapped ]; then
echo "Bootstrapping certbot dependencies"
pip install --upgrade pip
pip install certbot requests boto3 botocore
touch /.venv_bootstrapped
fi
ln -sf /opt/app-venv/bin/certbot /usr/local/bin/certbot
echo 'source /opt/app-venv/bin/activate' > /etc/profile.d/app-venv.sh
}
setup_certbot_env() {
# Ensure the virtual environment is active for certbot configuration
# shellcheck disable=SC1091
source /opt/app-venv/bin/activate
if [ "${DNS_PROVIDER}" = "route53" ]; then
mkdir -p /root/.aws
cat <<EOF >/root/.aws/config
[profile certbot]
role_arn=${AWS_ROLE_ARN}
source_profile=certbot-source
region=${AWS_REGION:-us-east-1}
EOF
cat <<EOF >/root/.aws/credentials
[certbot-source]
aws_access_key_id=${AWS_ACCESS_KEY_ID}
aws_secret_access_key=${AWS_SECRET_ACCESS_KEY}
EOF
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
export AWS_PROFILE=certbot
fi
# Use the unified certbot manager to install plugins and setup credentials
echo "Installing DNS plugins and setting up credentials"
certman.py setup
if [ $? -ne 0 ]; then
echo "Error: Failed to setup certbot environment"
exit 1
fi
}
setup_py_env
setup_nginx_conf() {
local cert_name
cert_name=$(cert_dir_name "$DOMAIN")
local client_max_body_size_conf=""
if [ -n "$CLIENT_MAX_BODY_SIZE" ]; then
client_max_body_size_conf=" client_max_body_size ${CLIENT_MAX_BODY_SIZE};"
fi
local proxy_read_timeout_conf=""
if [ -n "$PROXY_READ_TIMEOUT" ]; then
proxy_read_timeout_conf=" ${PROXY_CMD}_read_timeout ${PROXY_READ_TIMEOUT};"
fi
local proxy_send_timeout_conf=""
if [ -n "$PROXY_SEND_TIMEOUT" ]; then
proxy_send_timeout_conf=" ${PROXY_CMD}_send_timeout ${PROXY_SEND_TIMEOUT};"
fi
local proxy_connect_timeout_conf=""
if [ -n "$PROXY_CONNECT_TIMEOUT" ]; then
proxy_connect_timeout_conf=" ${PROXY_CMD}_connect_timeout ${PROXY_CONNECT_TIMEOUT};"
fi
local proxy_buffer_size_conf=""
if [ -n "$PROXY_BUFFER_SIZE" ]; then
proxy_buffer_size_conf=" proxy_buffer_size ${PROXY_BUFFER_SIZE};"
fi
local proxy_buffers_conf=""
if [ -n "$PROXY_BUFFERS" ]; then
proxy_buffers_conf=" proxy_buffers ${PROXY_BUFFERS};"
fi
local proxy_busy_buffers_size_conf=""
if [ -n "$PROXY_BUSY_BUFFERS_SIZE" ]; then
proxy_busy_buffers_size_conf=" proxy_busy_buffers_size ${PROXY_BUSY_BUFFERS_SIZE};"
fi
local server_name_value="${DOMAIN}"
if [ -n "$ALIAS_DOMAIN" ]; then
server_name_value="${DOMAIN} ${ALIAS_DOMAIN}"
fi
cat <<EOF >/etc/nginx/conf.d/default.conf
server {
listen ${PORT} ssl;
http2 on;
server_name ${server_name_value};
# SSL certificate configuration
ssl_certificate /etc/letsencrypt/live/${cert_name}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/${cert_name}/privkey.pem;
# Modern SSL configuration - TLS 1.2 and 1.3 only
ssl_protocols TLSv1.2 TLSv1.3;
# Strong cipher suites - Only AES-GCM and ChaCha20-Poly1305
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
# Prefer server cipher suites
ssl_prefer_server_ciphers on;
# ECDH curve for ECDHE ciphers
ssl_ecdh_curve secp384r1;
# Enable OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/${cert_name}/fullchain.pem;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# SSL session configuration
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
# SSL buffer size (optimized for TLS 1.3)
ssl_buffer_size 4k;
${proxy_buffer_size_conf}
${proxy_buffers_conf}
${proxy_busy_buffers_size_conf}
# Disable SSL renegotiation
ssl_early_data off;
${client_max_body_size_conf}
location / {
${PROXY_CMD}_pass ${TARGET_ENDPOINT};
${PROXY_CMD}_set_header Host \$host;
${PROXY_CMD}_set_header X-Real-IP \$remote_addr;
${PROXY_CMD}_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
${PROXY_CMD}_set_header X-Forwarded-Proto \$scheme;
${proxy_read_timeout_conf}
${proxy_send_timeout_conf}
${proxy_connect_timeout_conf}
}
location /evidences/ {
alias /evidences/;
autoindex on;
}
}
EOF
}
set_alias_record() {
local domain="$1"
echo "Setting alias record for $domain"
dnsman.py set_alias \
--domain "$domain" \
--content "$GATEWAY_DOMAIN"
if [ $? -ne 0 ]; then
echo "Error: Failed to set alias record for $domain"
exit 1
fi
echo "Alias record set for $domain"
}
set_txt_record() {
local domain="$1"
local APP_ID
if [[ -S /var/run/dstack.sock ]]; then
DSTACK_APP_ID=$(curl -s --unix-socket /var/run/dstack.sock http://localhost/Info | jq -j .app_id)
export DSTACK_APP_ID
else
DSTACK_APP_ID=$(curl -s --unix-socket /var/run/tappd.sock http://localhost/prpc/Tappd.Info | jq -j .app_id)
export DSTACK_APP_ID
fi
APP_ID=${APP_ID:-"$DSTACK_APP_ID"}
local txt_domain
if [[ "$domain" == \*.* ]]; then
# Wildcard domain: *.myapp.com → _dstack-app-address-wildcard.myapp.com
txt_domain="${TXT_PREFIX}-wildcard.${domain#\*.}"
else
txt_domain="${TXT_PREFIX}.${domain}"
fi
dnsman.py set_txt \
--domain "$txt_domain" \
--content "$APP_ID:$PORT"
if [ $? -ne 0 ]; then
echo "Error: Failed to set TXT record for $domain"
exit 1
fi
}
set_caa_record() {
local domain="$1"
if [ "$SET_CAA" != "true" ]; then
echo "Skipping CAA record setup"
return
fi
local ACCOUNT_URI
local account_file
if ! account_file=$(get_letsencrypt_account_file); then
echo "Warning: Cannot set CAA record - account file not found"
echo "This is not critical - certificates can still be issued without CAA records"
return
fi
local caa_domain caa_tag
if [[ "$domain" == \*.* ]]; then
caa_domain="${domain#\*.}"
caa_tag="issuewild"
else
caa_domain="$domain"
caa_tag="issue"
fi
ACCOUNT_URI=$(jq -j '.uri' "$account_file")
echo "Adding CAA record ($caa_tag) for $caa_domain, accounturi=$ACCOUNT_URI"
dnsman.py set_caa \
--domain "$caa_domain" \
--caa-tag "$caa_tag" \
--caa-value "letsencrypt.org;validationmethods=dns-01;accounturi=$ACCOUNT_URI"
if [ $? -ne 0 ]; then
echo "Warning: Failed to set CAA record for $domain"
echo "This is not critical - certificates can still be issued without CAA records"
echo "Consider disabling CAA records by setting SET_CAA=false if this continues to fail"
fi
}
process_domain() {
local domain="$1"
echo "Processing domain: $domain"
set_alias_record "$domain"
set_txt_record "$domain"
renew-certificate.sh "$domain" || echo "First certificate renewal failed for $domain, will retry after set CAA record"
set_caa_record "$domain"
renew-certificate.sh "$domain"
}
bootstrap() {
echo "Bootstrap: Setting up domains"
local all_domains
all_domains=$(get-all-domains.sh)
if [ -z "$all_domains" ]; then
echo "Error: No domains found. Set either DOMAIN or DOMAINS environment variable"
exit 1
fi
echo "Found domains:"
echo "$all_domains"
while IFS= read -r domain; do
[[ -n "$domain" ]] || continue
process_domain "$domain"
done <<<"$all_domains"
# Generate evidences after all certificates are obtained
echo "Generating evidence files for all domains..."
generate-evidences.sh
touch /.bootstrapped
}
# Credentials are now handled by certman.py setup
# Setup certbot environment (venv is already created in Dockerfile)
setup_certbot_env
# Check if it's the first time the container is started
if [ ! -f "/.bootstrapped" ]; then
bootstrap
else
echo "Certificate for $DOMAIN already exists"
generate-evidences.sh
fi
renewal-daemon.sh &
mkdir -p /var/log/nginx
if [ -n "$DOMAIN" ] && [ -n "$TARGET_ENDPOINT" ]; then
setup_nginx_conf
fi
exec "$@"