-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathentrypoint.sh
More file actions
180 lines (146 loc) · 4.97 KB
/
Copy pathentrypoint.sh
File metadata and controls
180 lines (146 loc) · 4.97 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
#!/bin/bash
set -e
PORT=${PORT:-443}
if [[ -e /var/run/dstack.sock ]]; then
TXT_PREFIX=${TXT_PREFIX:-"_dstack-app-address"}
else
TXT_PREFIX=${TXT_PREFIX:-"_tapp-address"}
fi
echo "Setting up certbot environment"
setup_certbot_env() {
# Activate the pre-built virtual environment
source /opt/app-venv/bin/activate
# 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
}
PROXY_CMD="proxy"
if [[ "${TARGET_ENDPOINT}" == grpc://* ]]; then
PROXY_CMD="grpc"
fi
setup_nginx_conf() {
cat <<EOF >/etc/nginx/conf.d/default.conf
server {
listen ${PORT} ssl;
http2 on;
server_name ${DOMAIN};
# SSL certificate configuration
ssl_certificate /etc/letsencrypt/live/${DOMAIN}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/${DOMAIN}/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/${DOMAIN}/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;
# Disable SSL renegotiation
ssl_early_data off;
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;
}
location /evidences/ {
alias /evidences/;
autoindex on;
}
}
EOF
mkdir -p /var/log/nginx
}
set_alias_record() {
# Use the unified DNS manager to set the alias record
source /opt/app-venv/bin/activate
echo "Setting alias record for $DOMAIN"
dns_manager.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 APP_ID
# Generate a unique app ID if not provided
if [[ -e /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"}
# Use the unified DNS manager to set the TXT record
source /opt/app-venv/bin/activate
dns_manager.py set_txt \
--domain "${TXT_PREFIX}.${DOMAIN}" \
--content "$APP_ID:$PORT"
if [ $? -ne 0 ]; then
echo "Error: Failed to set TXT record for $DOMAIN"
exit 1
fi
}
set_caa_record() {
if [ "$SET_CAA" != "true" ]; then
echo "Skipping CAA record setup"
return
fi
# Add CAA record for the domain
local ACCOUNT_URI
ACCOUNT_URI=$(jq -j '.uri' /evidences/acme-account.json)
echo "Adding CAA record for $DOMAIN, accounturi=$ACCOUNT_URI"
source /opt/app-venv/bin/activate
dns_manager.py set_caa \
--domain "$DOMAIN" \
--caa-tag "issue" \
--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"
# Don't exit - CAA records are optional for certificate generation
fi
}
bootstrap() {
echo "Bootstrap: Setting up $DOMAIN"
source /opt/app-venv/bin/activate
renew-certificate.sh -n
set_alias_record
set_txt_record
set_caa_record
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"
fi
renewal-daemon.sh &
setup_nginx_conf
exec "$@"