Skip to content

Commit 99dea49

Browse files
committed
Run nginx as unprevileged user
1 parent dba4344 commit 99dea49

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/bci_build/package/appcontainers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def _generate_prometheus_family_healthcheck(port: int) -> str:
251251
"LICENSE",
252252
"20-envsubst-on-templates.sh",
253253
"30-tune-worker-processes.sh",
254+
"40-unprivileged-mode.sh",
254255
"index.html",
255256
):
256257
_NGINX_FILES[filename] = (Path(__file__).parent / "nginx" / filename).read_bytes()
@@ -278,6 +279,8 @@ def _get_nginx_kwargs(os_version: OsVersion):
278279
"nginx",
279280
"findutils",
280281
_envsubst_pkg_name(os_version),
282+
"sed",
283+
"grep",
281284
]
282285
)
283286
+ (["libcurl-mini4"] if os_version.is_sl16 else []),
@@ -293,13 +296,14 @@ def _get_nginx_kwargs(os_version: OsVersion):
293296
),
294297
"custom_end": textwrap.dedent(f"""
295298
{DOCKERFILE_RUN} mkdir /docker-entrypoint.d
296-
COPY [1-3]0-*.sh /docker-entrypoint.d/
299+
COPY [1-4]0-*.sh /docker-entrypoint.d/
297300
COPY docker-entrypoint.sh /usr/local/bin
298301
COPY index.html /srv/www/htdocs/
299302
{DOCKERFILE_RUN} chmod +x /docker-entrypoint.d/*.sh /usr/local/bin/docker-entrypoint.sh
300-
{DOCKERFILE_RUN} install -d -o nginx -g nginx -m 750 /var/log/nginx; \
301-
ln -sf /dev/stdout /var/log/nginx/access.log; \
302-
ln -sf /dev/stderr /var/log/nginx/error.log
303+
{DOCKERFILE_RUN} set -euo pipefail; mkdir -p /var/cache/nginx /var/run/nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp;\
304+
ln -sf /dev/stdout /var/log/nginx/access.log;\
305+
ln -sf /dev/stderr /var/log/nginx/error.log;\
306+
chmod -R 777 /var/cache/nginx /etc/nginx /var/run/nginx /var/log/nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp;
303307
STOPSIGNAL SIGQUIT"""),
304308
}
305309

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
CURRENT_UID=$(id -u)
6+
if [ "$CURRENT_UID" -gt "0" ]; then
7+
echo "$0: Running as unprivileged user (UID: $CURRENT_UID). Configuring for unprivileged mode (Port 8080)."
8+
9+
CONF_FILES="/etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf"
10+
11+
for FILE in $CONF_FILES; do
12+
if [ -w "$FILE" ]; then
13+
if grep -q "listen .*80;" "$FILE"; then
14+
echo "Changing port 80 to 8080 in $FILE"
15+
sed 's/listen\s*80;/listen 8080;/g' "$FILE" > /tmp/client_temp/nginx_swap.conf && \
16+
cat /tmp/client_temp/nginx_swap.conf > "$FILE" && \
17+
rm -f /tmp/client_temp/nginx_swap.conf
18+
fi
19+
20+
if [ "$FILE" = "/etc/nginx/nginx.conf" ]; then
21+
echo "Redirecting NGINX temp paths and setting PID to /tmp in $FILE"
22+
sed -e '/^user/d' \
23+
-e 's,^#\?\s*pid\s\+.*;$,pid /var/run/nginx/nginx.pid;,' \
24+
-e '/http {/a \ client_body_temp_path /tmp/client_temp;\n proxy_temp_path /tmp/proxy_temp;\n fastcgi_temp_path /tmp/fastcgi_temp;\n uwsgi_temp_path /tmp/uwsgi_temp;\n scgi_temp_path /tmp/scgi_temp;' \
25+
"$FILE" > /tmp/client_temp/nginx_ultra.conf && \
26+
cat /tmp/client_temp/nginx_ultra.conf > "$FILE" && \
27+
rm -f /tmp/client_temp/nginx_ultra.conf
28+
echo "$0: Removed 'user' directive and updated PID path."
29+
fi
30+
fi
31+
done
32+
33+
echo "$0: Listening on port 8080."
34+
fi

src/bci_build/package/nginx/README.md.j2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ The template above is then rendered to `/etc/nginx/conf.d/default.conf` as follo
4141
```nginx
4242
listen 80;
4343
```
44+
## Running nginx as a non-root user
45+
To run the image as a less privileged user using the `nginx` user, do the following:
46+
```ShellSession
47+
$ podman run -it --user nginx --rm -p 8080:8080 -v /path/to/html/:/srv/www/htdocs/:Z {{ image.pretty_reference }}
48+
```
49+
**Note:** When running as the `nginx` user the default port is 8080.
4450

4551
## Environment variables
4652

0 commit comments

Comments
 (0)