Repository navigation
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·166 lines (140 loc) · 5.12 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·166 lines (140 loc) · 5.12 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
#/bin/sh
## Required vars
# CPAD_MAIN_DOMAIN
# CPAD_SANDBOX_DOMAIN
#
## Optional vars
# CPAD_API_DOMAIN
# CPAD_FILES_DOMAIN
#
## Both these vars must be set in order for nginx to terminate TLS
# CPAD_TLS_CERT
# CPAD_TLS_KEY
## If dhparam.pem file is absent it will be generated
# CPAD_TLS_DHPARAM
#
# CPAD_TRUSTED_PROXY
# CPAD_REALIP_HEADER
# CPAD_REALIP_RECURSIVE
#
## Testing vars, may be removed later
# CPAD_HTTP2_DISABLE="true"
#
set -e
CPAD_HOME="/cryptpad"
# Since Nginx configuration is copied from the official image we need to
# correct some stuff
# Fix log path
sed -i -e "s@\(error_log\) */.* \(.*;\)@\1 /dev/stderr \2@" \
-e "s@\(access_log\) */.* \(.*;\)@\1 /dev/stdout \2@" \
${CPAD_NGINX_CONF:=/etc/nginx/nginx.conf}
# Remove nginx default enabled conf
if [ -f /etc/nginx/conf.d/default.conf ]; then
rm /etc/nginx/conf.d/default.conf
fi
# Test if nginx config file already exists (eg: docker swarm config)
# if absent, copy example and apply corrections
if [ ! -f "${CPAD_NGINX_CPAD_CONF:=/etc/nginx/conf.d/cryptpad.conf}" ]; then
# Copy nginx config example from Cryptpad
cp $CPAD_HOME/docs/example.nginx.conf $CPAD_NGINX_CPAD_CONF
# Set domains
if [ -z "${CPAD_MAIN_DOMAIN:-}" ]; then
echo "Error: No main domain specified"
exit 1
else
sed -i "s@your-main-domain.com@$CPAD_MAIN_DOMAIN@g" $CPAD_NGINX_CPAD_CONF
fi
if [ -z "${CPAD_SANDBOX_DOMAIN:-}" ]; then
echo "Error: No sandbox domain specified"
exit 1
else
sed -i "s@your-sandbox-domain.com@$CPAD_SANDBOX_DOMAIN@g" $CPAD_NGINX_CPAD_CONF
fi
if [ -z "${CPAD_API_DOMAIN}" ]; then
sed -i "s@api.$CPAD_MAIN_DOMAIN@\$\{main_domain\}@" $CPAD_NGINX_CPAD_CONF
fi
if [ -z "${CPAD_FILES_DOMAIN}" ]; then
sed -i "s@files.$CPAD_MAIN_DOMAIN@\$\{main_domain\}@" $CPAD_NGINX_CPAD_CONF
fi
# Change nginx document root
sed -i "s@\(root\) */.*\([^;]\)@\1 $CPAD_HOME@" $CPAD_NGINX_CPAD_CONF
# Whether or not Nginx should terminate TLS (defaults to true)
if [ -n "$CPAD_TLS_CERT" \
-a -n "$CPAD_TLS_KEY" ]; then
# If cert is present, set path. If not, exit with error
if [ -f "$CPAD_TLS_CERT" ]; then
sed -i "s@\( *ssl_certificate[^_key] *\).*[^;]@\1$CPAD_TLS_CERT@" $CPAD_NGINX_CPAD_CONF
else
echo "Error: Missing TLS certificate file"
exit 1
fi
# If key is present, set path. If not, exit with error
if [ -f "$CPAD_TLS_KEY" ]; then
sed -i "s@\( *ssl_certificate_key *\).*[^;]@\1$CPAD_TLS_KEY@" $CPAD_NGINX_CPAD_CONF
else
echo "Error: Missing TLS key file"
exit 1
fi
# This option is only useful for OCSP stapling which Cryptpad doesn't use
# so we'll comment it to avoid errors.
sed -i "s@\(ssl_trusted_certificate\)@#\1@" $CPAD_NGINX_CPAD_CONF
# If no DH parameters are provided, generate them
if [ ! -f "${CPAD_TLS_DHPARAM:=/etc/nginx/dhparam.pem}" ]; then
# Generate DH parameters
openssl dhparam -out $CPAD_TLS_DHPARAM 4096
fi
# If no TLS termination
else
# Make Nginx listen on 80 in plaintext
# and comment out all ssl related options
sed -i -e "s@\(^.*\) \+443 ssl\(.*$\)@\1 80\2@" \
-e "s@[^#]ssl_@ #ssl_@g" $CPAD_NGINX_CPAD_CONF
fi
# Should Nginx use http_realip_module
if [ -n "${CPAD_TRUSTED_PROXY:=}" ]; then
# Set trusted proxy
sed -i -e "/listen/ G" \
-e "/listen/ a \ \ # Set trusted proxy and header containing real client IP" \
-e "/listen/ a \ \ set_real_ip_from $CPAD_TRUSTED_PROXY;" $CPAD_NGINX_CPAD_CONF
# Set header to get real client IP from
if [ -n "${CPAD_REALIP_HEADER:-}" ]; then
sed -i "/set_real_ip_from/ a \ \ real_ip_header $CPAD_REALIP_HEADER;" $CPAD_NGINX_CPAD_CONF
fi
# Should Nginx perform a recursive search to get real client IP
if [ -n "${CPAD_REALIP_RECURSIVE:-}" ]; then
sed -i "/set_real_ip_from/ a \ \ real_ip_recursive $CPAD_REALIP_RECURSIVE;" $CPAD_NGINX_CPAD_CONF
fi
fi
# Should nginx use HTTP2 (defaults to false)
if [ "${CPAD_HTTP2_DISABLE:-false}" = "true" ]; then
sed -i -e "s@\(^.*\) \+http2\(.*$\)@\1\2@" $CPAD_NGINX_CPAD_CONF
fi
## WIP
# If cryptad conf isn't provided
# if [ ! -f "$CPAD_CONF" ]; then
# echo -e "\n\
# ############################################### \n\
# Warning: No config file provided for cryptpad \n\
# We will create a basic one for now but you should rerun this service \n\
# by providing a file with your settings \n\
# eg: docker run -v /path/to/config.js:/cryptpad/config/config.js \n\
# ############################################### \n"
#
# cp $CPAD_HOME/config/config.example.js $CPAD_CONF
#
# # Set domains
# sed -i -e "s@\(httpUnsafeOrigin:\).*[^,]@\1 'https://$CPAD_MAIN_DOMAIN'@" \
# -e "s@\(^ *\).*\(httpSafeOrigin:\).*[^,]@\1\2 'https://$CPAD_SANDBOX_DOMAIN'@" $CPAD_CONF
#
# # Set admin email
# if [ -z "$CPAD_ADMIN_EMAIL" ]; then
# echo "Error: Missing admin email (Did you read the config?)"
# exit 1
# else
# sed -i "s@\(adminEmail:\).*[^,]@\1 '$CPAD_ADMIN_EMAIL'@" $CPAD_CONF
# fi
# fi
fi
cd $CPAD_HOME
npm run build
exec "$@"