forked from nginxinc/nginx-s3-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standalone_ubuntu_oss_install.sh
370 lines (306 loc) · 12.3 KB
/
standalone_ubuntu_oss_install.sh
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
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env bash
set -o errexit # abort on nonzero exit status
set -o pipefail # don't hide errors within pipes
if [ "$EUID" -ne 0 ];then
>&2 echo "This script requires root level access to run"
exit 1
fi
if ! dpkg --status grep 2>/dev/null | grep --quiet Status > /dev/null; then
>&2 echo "This script requires the grep package to be installed in order to run"
exit 1
fi
if ! dpkg --status coreutils 2>/dev/null | grep --quiet Status > /dev/null; then
>&2 echo "This script requires the coreutils package to be installed in order to run"
exit 1
fi
if ! dpkg --status apt 2>/dev/null | grep --quiet Status > /dev/null; then
>&2 echo "This script requires the apt package to be installed in order to run"
exit 1
fi
if ! dpkg --status wget 2>/dev/null | grep --quiet Status > /dev/null; then
>&2 echo "This script requires the wget package to be installed in order to run"
exit 1
fi
failed=0
required=("S3_BUCKET_NAME" "S3_SERVER" "S3_SERVER_PORT" "S3_SERVER_PROTO"
"S3_REGION" "S3_STYLE" "ALLOW_DIRECTORY_LIST" "AWS_SIGS_VERSION")
if [ ! -z ${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI+x} ]; then
echo "Running inside an ECS task, using container credentials"
uses_iam_creds=1
elif curl --output /dev/null --silent --head --fail --connect-timeout 2 "http://169.254.169.254"; then
echo "Running inside an EC2 instance, using IMDS for credentials"
uses_iam_creds=1
else
required+=("S3_ACCESS_KEY_ID" "S3_SECRET_KEY")
uses_iam_creds=0
fi
if [[ -v S3_SESSION_TOKEN ]]; then
echo "S3 Session token present"
fi
for name in ${required[@]}; do
if [ -z ${!name+x} ]; then
>&2 echo "Required ${name} environment variable missing"
failed=1
fi
done
if [ "${S3_SERVER_PROTO}" != "http" ] && [ "${S3_SERVER_PROTO}" != "https" ]; then
>&2 echo "S3_SERVER_PROTO contains an invalid value (${S3_SERVER_PROTO}). Valid values: http, https"
failed=1
fi
if [ "${AWS_SIGS_VERSION}" != "2" ] && [ "${AWS_SIGS_VERSION}" != "4" ]; then
>&2 echo "AWS_SIGS_VERSION contains an invalid value (${AWS_SIGS_VERSION}). Valid values: 2, 4"
failed=1
fi
if [ $failed -gt 0 ]; then
exit 1
fi
if [ "${1}" == "" ]; then
branch="master"
else
branch="${1}"
fi
echo "Installing using github '${branch}' branch"
echo "S3 Backend Environment"
echo "Access Key ID: ${S3_ACCESS_KEY_ID}"
echo "Origin: ${S3_SERVER_PROTO}://${S3_BUCKET_NAME}.${S3_SERVER}:${S3_SERVER_PORT}"
echo "Region: ${S3_REGION}"
echo "Addressing Style: ${S3_STYLE}"
echo "AWS Signatures Version: v${AWS_SIGS_VERSION}"
echo "DNS Resolvers: ${DNS_RESOLVERS}"
echo "Directory Listing Enabled: ${ALLOW_DIRECTORY_LIST}"
echo "Proxy Caching Time for Valid Response: ${PROXY_CACHE_VALID_OK}"
echo "Proxy Caching Time for Not Found Response: ${PROXY_CACHE_VALID_NOTFOUND}"
echo "Proxy Caching Time for Forbidden Response: ${PROXY_CACHE_VALID_FORBIDDEN}"
echo "CORS Enabled: ${CORS_ENABLED}"
set -o nounset # abort on unbound variable
if [ ! -f /etc/apt/sources.list.d/nginx.list ]; then
release="$(grep 'VERSION_CODENAME' /etc/os-release | cut --delimiter='=' --field=2)"
echo "▶ Adding NGINX package repository"
cat > "/etc/apt/sources.list.d/nginx.list" << EOF
deb https://nginx.org/packages/ubuntu/ $release nginx
deb-src https://nginx.org/packages/ubuntu/ $release nginx
EOF
key="ABF5BD827BD9BF62"
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key
apt-get -qq update
fi
to_install=""
if ! dpkg --status nginx 2>/dev/null | grep --quiet Status > /dev/null; then
to_install="nginx"
fi
if ! dpkg --status nginx-module-njs 2>/dev/null | grep --quiet Status > /dev/null; then
# find latest njs version because the package manager gets this wrong
latest_njs_version="$(apt show -a nginx-module-njs 2>/dev/null | grep 'Version:' | cut --delimiter=' ' --field=2 | sort --reverse | head --lines=1)"
to_install="${to_install} nginx-module-njs=${latest_njs_version}"
fi
if ! dpkg --status nginx-module-xslt 2>/dev/null | grep --quiet Status > /dev/null; then
to_install="${to_install} nginx-module-xslt"
fi
if [ "${to_install}" != "" ]; then
apt-get -qq install --yes ${to_install}
echo "▶ Stopping nginx so that it can be configured as a S3 Gateway"
systemctl stop nginx
fi
echo "▶ Adding environment variables to NGINX configuration file: /etc/nginx/environment"
cat > "/etc/nginx/environment" << EOF
# Enables or disables directory listing for the S3 Gateway (true=enabled, false=disabled)
ALLOW_DIRECTORY_LIST=${ALLOW_DIRECTORY_LIST}
# AWS Authentication signature version (2=v2 authentication, 4=v4 authentication)
AWS_SIGS_VERSION=${AWS_SIGS_VERSION}
# Name of S3 bucket to proxy requests to
S3_BUCKET_NAME=${S3_BUCKET_NAME}
# Region associated with API
S3_REGION=${S3_REGION}
# SSL/TLS port to connect to
S3_SERVER_PORT=${S3_SERVER_PORT}
# Protocol to used connect to S3 server - 'http' or 'https'
S3_SERVER_PROTO=${S3_SERVER_PROTO}
# S3 host to connect to
S3_SERVER=${S3_SERVER}
# The S3 host/path method - 'virtual', 'path' or 'default'
S3_STYLE=${S3_STYLE}
# Flag (true/false) enabling AWS signatures debug output (default: false)
S3_DEBUG=${S3_DEBUG}
# Proxy caching time for response code 200 and 302
PROXY_CACHE_VALID_OK=${PROXY_CACHE_VALID_OK}
# Proxy caching time for response code 404
PROXY_CACHE_VALID_NOTFOUND=${PROXY_CACHE_VALID_NOTFOUND}
# Proxy caching time for response code 403
PROXY_CACHE_VALID_FORBIDDEN=${PROXY_CACHE_VALID_FORBIDDEN}
# Enables or disables CORS for the S3 Gateway (true=enabled, false=disabled)
CORS_ENABLED=${CORS_ENABLED}
EOF
# By enabling CORS, we also need to enable the OPTIONS method which
# is not normally used as part of the gateway. The following variable
# defines the set of acceptable headers.
if [ "${CORS_ENABLED}" == "1" ]; then
cat >> "/etc/nginx/environment" << EOF
LIMIT_METHODS_TO="GET HEAD OPTIONS"
LIMIT_METHODS_TO_CSV="GET, HEAD, OPTIONS"
EOF
else
cat >> "/etc/nginx/environment" << EOF
LIMIT_METHODS_TO="GET HEAD"
LIMIT_METHODS_TO_CSV="GET, HEAD"
EOF
fi
if [ -z "${CORS_ALLOWED_ORIGIN+x}" ]; then
CORS_ALLOWED_ORIGIN="*"
fi
cat >> "/etc/nginx/environment" << EOF
CORS_ALLOWED_ORIGIN=${CORS_ALLOWED_ORIGIN}
EOF
# Only include these env vars if we are not using a instance profile credential
# to obtain S3 permissions.
if [ $uses_iam_creds -eq 0 ]; then
cat >> "/etc/nginx/environment" << EOF
# AWS Access key
S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID}
# AWS Secret access key
S3_SECRET_KEY=${S3_SECRET_KEY}
EOF
if [[ -v S3_SESSION_TOKEN ]]; then
cat >> "/etc/nginx/environment" << EOF
# AWS Session Token
S3_SESSION_TOKEN=${S3_SESSION_TOKEN}
EOF
fi
fi
set +o nounset # don't abort on unbound variable
if [ -z ${DNS_RESOLVERS+x} ]; then
cat >> "/etc/default/nginx" << EOF
# DNS resolvers (separated by single spaces) to configure NGINX with
DNS_RESOLVERS=${DNS_RESOLVERS}
EOF
fi
set -o nounset # abort on unbound variable
# Make sure that only the root user can access the environment variables file
chown root:root /etc/nginx/environment
chmod og-rwx /etc/nginx/environment
cat > /usr/local/bin/template_nginx_config.sh << 'EOF'
#!/usr/bin/env bash
ME=$(basename $0)
auto_envsubst() {
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
local template defined_envs relative_path output_path subdir
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
[ -d "$template_dir" ] || return 0
if [ ! -w "$output_dir" ]; then
echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
return 0
fi
find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do
relative_path="${template#$template_dir/}"
output_path="$output_dir/${relative_path%$suffix}"
subdir=$(dirname "$relative_path")
# create a subdirectory where the template file exists
mkdir -p "$output_dir/$subdir"
echo "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
}
# Attempt to read DNS Resolvers from /etc/resolv.conf
if [ -z ${DNS_RESOLVERS+x} ]; then
export DNS_RESOLVERS="$(cat /etc/resolv.conf | grep nameserver | cut -d' ' -f2 | xargs)"
fi
auto_envsubst
EOF
chmod +x /usr/local/bin/template_nginx_config.sh
echo "▶ Reconfiguring systemd for S3 Gateway"
mkdir -p /etc/systemd/system/nginx.service.d
cat > /etc/systemd/system/nginx.service.d/override.conf << 'EOF'
[Service]
EnvironmentFile=/etc/nginx/environment
ExecStartPre=/usr/local/bin/template_nginx_config.sh
EOF
systemctl daemon-reload
echo "▶ Creating NGINX configuration for S3 Gateway"
mkdir -p /etc/nginx/include
mkdir -p /etc/nginx/conf.d/gateway
mkdir -p /etc/nginx/templates/gateway
function download() {
wget --quiet --output-document="$2" "https://raw.githubusercontent.com/nginxinc/nginx-s3-gateway/${branch}/$1"
}
if [ ! -f /etc/nginx/nginx.conf.orig ]; then
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig
fi
if [ ! -f /etc/nginx/conf.d/default.conf.orig ]; then
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.orig
fi
cat > /etc/nginx/nginx.conf << 'EOF'
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
# NJS module used for implementing S3 authentication
load_module modules/ngx_http_js_module.so;
load_module modules/ngx_http_xslt_filter_module.so;
# Preserve S3 environment variables for worker threads
EOF
# Only include these env vars if we are not using a instance profile credential
# to obtain S3 permissions.
if [ $uses_iam_creds -eq 0 ]; then
cat >> "/etc/nginx/environment" << EOF
env S3_ACCESS_KEY_ID;
env S3_SECRET_KEY;
EOF
if [[ -v S3_SESSION_TOKEN ]]; then
cat >> "/etc/nginx/environment" << EOF
env S3_SESSION_TOKEN;
EOF
fi
cat >> /etc/nginx/nginx.conf << 'EOF'
env S3_BUCKET_NAME;
env S3_SERVER;
env S3_SERVER_PORT;
env S3_SERVER_PROTO;
env S3_REGION;
env AWS_SIGS_VERSION;
env S3_DEBUG;
env S3_STYLE;
env ALLOW_DIRECTORY_LIST;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# Settings for S3 cache
proxy_cache_path /var/cache/nginx/s3_proxy
levels=1:2
keys_zone=s3_cache:10m
max_size=10g
inactive=60m
use_temp_path=off;
include /etc/nginx/conf.d/*.conf;
}
EOF
download "common/etc/nginx/include/listing.xsl" "/etc/nginx/include/listing.xsl"
download "common/etc/nginx/include/s3gateway.js" "/etc/nginx/include/s3gateway.js"
download "common/etc/nginx/templates/default.conf.template" "/etc/nginx/templates/default.conf.template"
download "common/etc/nginx/templates/gateway/v2_headers.conf.template" "/etc/nginx/templates/gateway/v2_headers.conf.template"
download "common/etc/nginx/templates/gateway/v2_js_vars.conf.template" "/etc/nginx/templates/gateway/v2_js_vars.conf.template"
download "common/etc/nginx/templates/gateway/v4_headers.conf.template" "/etc/nginx/templates/gateway/v4_headers.conf.template"
download "common/etc/nginx/templates/gateway/v4_js_vars.conf.template" "/etc/nginx/templates/gateway/v4_js_vars.conf.template"
download "common/etc/nginx/templates/gateway/cors.conf.template" "/etc/nginx/templates/gateway/cors.conf.template"
download "common/etc/nginx/templates/gateway/js_fetch_trusted_certificate.conf.template" "/etc/nginx/templates/gateway/js_fetch_trusted_certificate.conf.template"
download "common/etc/nginx/templates/gateway/s3listing_location.conf.template" "/etc/nginx/templates/gateway/s3listing_location.conf.template"
download "common/etc/nginx/templates/gateway/s3_location.conf.template" "/etc/nginx/templates/gateway/s3_location.conf.template"
download "common/etc/nginx/templates/gateway/s3_server.conf.template" "/etc/nginx/templates/gateway/s3_server.conf.template"
download "oss/etc/nginx/templates/upstreams.conf.template" "/etc/nginx/templates/upstreams.conf.template"
download "oss/etc/nginx/conf.d/gateway/server_variables.conf" "/etc/nginx/conf.d/gateway/server_variables.conf"
echo "▶ Creating directory for proxy cache"
mkdir -p /var/cache/nginx/s3_proxy
chown nginx:nginx /var/cache/nginx/s3_proxy
echo "▶ Starting NGINX"
systemctl start nginx