Skip to content

Commit

Permalink
fix(security): Restore ADD_PROXY_ROUTE functionality for docker NGINX
Browse files Browse the repository at this point in the history
Signed-off-by: Bryon Nevis <bryon.nevis@intel.com>
  • Loading branch information
bnevis-i committed Mar 6, 2023
1 parent b9da6ae commit 00e1079
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ echo "$(date) Executing waitFor with waiting on tcp://${STAGEGATE_BOOTSTRAPPER_H

echo "$(date) Generating default config ..."

# Ensure this file exists since reference below; proxy-setup will regenerate it
touch /etc/nginx/templates/generated-routes.inc.template

# This file can be modified by the user; deleted when docker volumes are pruned;
# but preserved across start/up and stop/down actions
if test -f /etc/nginx/templates/edgex-custom-rewrites.inc.template; then
echo "Using existing custom-rewrites."
else
Expand Down Expand Up @@ -197,6 +202,7 @@ server {
proxy_set_header Host $host;
}
include /etc/nginx/conf.d/generated-routes.inc;
include /etc/nginx/conf.d/edgex-custom-rewrites.inc;
}
Expand Down
35 changes: 34 additions & 1 deletion cmd/security-proxy-setup/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ certfile=nginx.crt

# Check for default TLS certificate for reverse proxy, create if missing
# Normally we would run the below command in the nginx container itself,
# but nginx:alpine-slim does not container openssl, thus run it here instead.
# but nginx:alpine-slim does not include openssl, thus run it here instead.
if test -d /etc/ssl/nginx ; then
cd /etc/ssl/nginx
if test ! -f "${keyfile}" ; then
Expand All @@ -44,6 +44,39 @@ if test -d /etc/ssl/nginx ; then
fi
fi

#
# Generate custom forwarders based on ADD_PROXY_ROUTE
#

# Truncate the template file before we start appending
: >/etc/nginx/templates/generated-routes.inc.template

IFS=', '
for service in ${ADD_PROXY_ROUTE}; do
prefix=$(echo -n "${service}" | sed -n -e 's/\([-0-9a-zA-Z]*\)\..*/\1/p')
host=$(echo -n "${service}" | sed -n -e 's/.*\/\/\([-0-9a-zA-Z]*\):.*/\1/p')
port=$(echo -n "${service}" | sed -n -e 's/.*:\(\d*\)/\1/p')
varname=$(echo -n "${prefix}" | tr '-' '_')
echo $service $prefix $host $port
cat >> /etc/nginx/templates/generated-routes.inc.template <<EOH
set \$upstream_$varname $host;
location /$prefix {
rewrite /$prefix/(.*) /\$1 break;
resolver 127.0.0.11 valid=30s;
proxy_pass http://\$upstream_$varname:$port;
proxy_redirect off;
proxy_set_header Host \$host;
auth_request /auth;
auth_request_set \$auth_status \$upstream_status;
}
EOH

done
unset IFS



# Hang the container now that initialization is done.
cd /
exec su nobody -s /bin/sh -c "exec tail -f /dev/null"

0 comments on commit 00e1079

Please sign in to comment.