Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .test/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ imageTests+=(
ipv6
static
templates
templates-mail
templates-resolver
templates-resolver-ipv6
workers
Expand Down
46 changes: 46 additions & 0 deletions .test/tests/templates-mail/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

[ "$DEBUG" ] && set -x

set -eo pipefail

dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"

image="$1"

clientImage='alpine:latest'
# ensure the clientImage is ready and available
if ! docker image inspect "$clientImage" &> /dev/null; then
docker pull "$clientImage" > /dev/null
fi

# Create an instance of the container-under-test
serverImage="$("$HOME/oi/test/tests/image-name.sh" librarytest/nginx-template "$image")"
"$HOME/oi/test/tests/docker-build.sh" "$dir" "$serverImage" <<EOD
FROM $image

RUN rm -f /etc/nginx/conf.d/default.conf

COPY dir/smtp.conf.mail-template /etc/nginx/templates/smtp.conf.mail-template
COPY dir/smtp_auth_http.conf.template /etc/nginx/templates/smtp_auth_http.conf.template
EOD
cid="$(docker run -d -e NGINX_MY_SERVER_NAME=example.com "$serverImage")"
trap "docker rm -vf $cid > /dev/null" EXIT

_request() {
if [ "$(docker inspect -f '{{.State.Running}}' "$cid" 2>/dev/null)" != 'true' ]; then
echo >&2 "$image stopped unexpectedly!"
( set -x && docker logs "$cid" ) >&2 || true
false
fi

docker run --rm \
--link "$cid":nginx \
"$clientImage" \
nc -w 1 nginx 25
}

. "$HOME/oi/test/retry.sh" '[ "$(_request --output /dev/null || echo $?)" != 7 ]'

# Check that we can open an SMTP connection
_request | grep 'ESMTP ready'
6 changes: 6 additions & 0 deletions .test/tests/templates-mail/smtp.conf.mail-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
server_name server-name;
auth_http http://127.0.0.1:9000/;
server {
listen 25;
protocol smtp;
}
9 changes: 9 additions & 0 deletions .test/tests/templates-mail/smtp_auth_http.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server {
listen 127.0.0.1:9000;
location / {
default_type text/plain;
add_header Auth-Status "Login not supported since this is a dummy nginx smtp handler";
add_header Auth-Error-Code "550 5.3.5";
return 200;
}
}
80 changes: 45 additions & 35 deletions entrypoint/20-envsubst-on-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,76 @@ entrypoint_log() {
fi
}

add_stream_block() {
ensure_output_writable() {
local test_dir=$1
if [ ! -w "$test_dir" ]; then
entrypoint_log "$ME: ERROR: $template_dir exists, but $test_dir is not writable"
exit 0
fi
}

add_extra_block() {
local extra=$1
local extra_output_dir=$2
local conffile="/etc/nginx/nginx.conf"

if grep -q -E "\s*stream\s*\{" "$conffile"; then
entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates"
if grep -q -E "\s*$extra\s*\{" "$conffile"; then
entrypoint_log "$ME: $conffile contains a $extra block; include $extra_output_dir/*.conf to enable $extra templates"
else
# check if the file can be modified, e.g. not on a r/o filesystem
touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; }
entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf"
entrypoint_log "$ME: Appending $extra block to $conffile to include $extra_output_dir/*.conf"
cat << END >> "$conffile"
# added by "$ME" on "$(date)"
stream {
include $stream_output_dir/*.conf;
$extra {
include $extra_output_dir/*.conf;
}
END
fi
}

write_template_conf() {
local select_suffix=$1
local conf_output_dir=$2
local template relative_path output_path subdir
find "$template_dir" -follow -type f -name "*$select_suffix" -print | while read -r template; do
relative_path="${template#"$template_dir/"}"
output_path="$conf_output_dir/${relative_path%"$select_suffix"}"
subdir=$(dirname "$relative_path")
# create a subdirectory where the template file exists
mkdir -p "$conf_output_dir/$subdir"
entrypoint_log "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
}

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 mail_suffix="${NGINX_ENVSUBST_MAIL_TEMPLATE_SUFFIX:-.mail-template}"
local mail_output_dir="${NGINX_ENVSUBST_MAIL_OUTPUT_DIR:-/etc/nginx/mail-conf.d}"
local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}"
local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}"
local filter="${NGINX_ENVSUBST_FILTER:-}"

local template defined_envs relative_path output_path subdir
defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null ))
local defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null ))
[ -d "$template_dir" ] || return 0
if [ ! -w "$output_dir" ]; then
entrypoint_log "$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"
entrypoint_log "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
ensure_output_writable "$output_dir"
write_template_conf "$suffix" "$output_dir"

# Print the first file with the stream suffix, this will be false if there are none
if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then
mkdir -p "$stream_output_dir"
if [ ! -w "$stream_output_dir" ]; then
entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable"
return 0
fi
add_stream_block
find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do
relative_path="${template#"$template_dir/"}"
output_path="$stream_output_dir/${relative_path%"$stream_suffix"}"
subdir=$(dirname "$relative_path")
# create a subdirectory where the template file exists
mkdir -p "$stream_output_dir/$subdir"
entrypoint_log "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
ensure_output_writable "$stream_output_dir"
add_extra_block "stream" "$stream_output_dir"
write_template_conf "$stream_suffix" "$stream_output_dir"
fi
if test -n "$(find "$template_dir" -name "*$mail_suffix" -print -quit)"; then
mkdir -p "$mail_output_dir"
ensure_output_writable "$mail_output_dir"
add_extra_block "mail" "$mail_output_dir"
write_template_conf "$mail_suffix" "$mail_output_dir"
fi
}

Expand Down
80 changes: 45 additions & 35 deletions mainline/alpine-slim/20-envsubst-on-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,76 @@ entrypoint_log() {
fi
}

add_stream_block() {
ensure_output_writable() {
local test_dir=$1
if [ ! -w "$test_dir" ]; then
entrypoint_log "$ME: ERROR: $template_dir exists, but $test_dir is not writable"
exit 0
fi
}

add_extra_block() {
local extra=$1
local extra_output_dir=$2
local conffile="/etc/nginx/nginx.conf"

if grep -q -E "\s*stream\s*\{" "$conffile"; then
entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates"
if grep -q -E "\s*$extra\s*\{" "$conffile"; then
entrypoint_log "$ME: $conffile contains a $extra block; include $extra_output_dir/*.conf to enable $extra templates"
else
# check if the file can be modified, e.g. not on a r/o filesystem
touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; }
entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf"
entrypoint_log "$ME: Appending $extra block to $conffile to include $extra_output_dir/*.conf"
cat << END >> "$conffile"
# added by "$ME" on "$(date)"
stream {
include $stream_output_dir/*.conf;
$extra {
include $extra_output_dir/*.conf;
}
END
fi
}

write_template_conf() {
local select_suffix=$1
local conf_output_dir=$2
local template relative_path output_path subdir
find "$template_dir" -follow -type f -name "*$select_suffix" -print | while read -r template; do
relative_path="${template#"$template_dir/"}"
output_path="$conf_output_dir/${relative_path%"$select_suffix"}"
subdir=$(dirname "$relative_path")
# create a subdirectory where the template file exists
mkdir -p "$conf_output_dir/$subdir"
entrypoint_log "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
}

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 mail_suffix="${NGINX_ENVSUBST_MAIL_TEMPLATE_SUFFIX:-.mail-template}"
local mail_output_dir="${NGINX_ENVSUBST_MAIL_OUTPUT_DIR:-/etc/nginx/mail-conf.d}"
local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}"
local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}"
local filter="${NGINX_ENVSUBST_FILTER:-}"

local template defined_envs relative_path output_path subdir
defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null ))
local defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null ))
[ -d "$template_dir" ] || return 0
if [ ! -w "$output_dir" ]; then
entrypoint_log "$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"
entrypoint_log "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
ensure_output_writable "$output_dir"
write_template_conf "$suffix" "$output_dir"

# Print the first file with the stream suffix, this will be false if there are none
if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then
mkdir -p "$stream_output_dir"
if [ ! -w "$stream_output_dir" ]; then
entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable"
return 0
fi
add_stream_block
find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do
relative_path="${template#"$template_dir/"}"
output_path="$stream_output_dir/${relative_path%"$stream_suffix"}"
subdir=$(dirname "$relative_path")
# create a subdirectory where the template file exists
mkdir -p "$stream_output_dir/$subdir"
entrypoint_log "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
ensure_output_writable "$stream_output_dir"
add_extra_block "stream" "$stream_output_dir"
write_template_conf "$stream_suffix" "$stream_output_dir"
fi
if test -n "$(find "$template_dir" -name "*$mail_suffix" -print -quit)"; then
mkdir -p "$mail_output_dir"
ensure_output_writable "$mail_output_dir"
add_extra_block "mail" "$mail_output_dir"
write_template_conf "$mail_suffix" "$mail_output_dir"
fi
}

Expand Down
Loading
Loading