Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: feat: Run as non-root by default to allow easier deployment on the restricted environments like OpenShift, Kubernetes and any corporate (or just secure) environments where root privileges are not allowed #308

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
14 changes: 10 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ jobs:
- name: Spin up services
run: docker-compose -f docker-compose.yml up -d
- name: Wait
run: sleep 60
run: sleep 70
- name: Check status
run: docker ps -a
- name: Test
run: curl http://localhost:8080/ -L --fail -s | grep 'Sign in</button>' -q
run: curl http://localhost:8080/ -L --fail -vvv | grep 'Sign in</button>' -q
- name: Test Email
run: docker-compose exec -T humhub php /var/www/localhost/htdocs/protected/yii test/email '[email protected]' | grep 'Message successfully sent!' -q
- name: Collect logs
if: ${{ always() }}
run: docker-compose -f docker-compose.prod.yml logs
test_prod:
needs: build_variants
runs-on: ubuntu-latest
Expand Down Expand Up @@ -140,13 +143,16 @@ jobs:
- name: Spin up services
run: docker-compose -f docker-compose.prod.yml up -d
- name: Wait
run: sleep 60
run: sleep 70
- name: Check status
run: docker ps -a
- name: Test
run: curl http://localhost:8080/ -L --fail -s | grep 'Sign in</button>' -q
run: curl http://localhost:8080/ -L --fail -vvv | grep 'Sign in</button>' -q
- name: Test Email
run: docker-compose exec -T humhub php /var/www/localhost/htdocs/protected/yii test/email '[email protected]' | grep 'Message successfully sent!' -q
- name: Collect logs
if: ${{ always() }}
run: docker-compose -f docker-compose.prod.yml logs
push_ghcr:
needs: ["test_aio", "test_prod"]
runs-on: ubuntu-latest
Expand Down
48 changes: 41 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ARG RUNTIME_DEPS="\
php7-xmlwriter \
php7-zip \
sqlite \
supervisor \
multirun \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if HumHub has been fully tested with multirun, you should probably run some tests before switching.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case crond is used instead of supervisor.

tzdata \
"

Expand Down Expand Up @@ -112,17 +112,15 @@ RUN apk add --no-cache --update $RUNTIME_DEPS && \
apk add --no-cache --virtual temp_pkgs gettext && \
cp /usr/bin/envsubst /usr/local/bin/envsubst && \
apk del temp_pkgs && \
rm -rf /var/cache/apk/*
rm -rf /var/cache/apk/* && \
chmod +x /usr/bin/multirun

ENV PHP_POST_MAX_SIZE=16M
ENV PHP_UPLOAD_MAX_FILESIZE=10M
ENV PHP_MAX_EXECUTION_TIME=60
ENV PHP_MEMORY_LIMIT=1G
ENV PHP_TIMEZONE=UTC

RUN touch /var/run/supervisor.sock && \
chmod 777 /var/run/supervisor.sock

# 100=nginx 101=nginx (group)
COPY --from=builder --chown=100:101 /usr/src/humhub /var/www/localhost/htdocs/
COPY --chown=100:101 humhub/ /var/www/localhost/htdocs/
Expand All @@ -132,23 +130,40 @@ RUN mkdir -p /usr/src/humhub/protected/config/ && \
rm -f var/www/localhost/htdocs/protected/config/common.php /usr/src/humhub/protected/config/common.php && \
echo "v${HUMHUB_VERSION}" > /usr/src/humhub/.version

# Non-root directories
RUN mkdir -p /var/www/localhost/htdocs/protected/runtime/logs && \
mkdir -p /var/www/localhost/htdocs/uploads /var/www/localhost/htdocs/assets /var/www/localhost/htdocs/protected/modules /var/www/localhost/htdocs/themes /var/www/localhost/htdocs/protected/config && \
mkdir -p /run/nginx /run/php-fpm && \
touch /var/www/localhost/htdocs/protected/runtime/logs/app.log && \
chown 100:101 -R /var/www/localhost/htdocs/protected/runtime/logs /run/nginx /run/php-fpm \
Copy link
Owner

@mriedmann mriedmann Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for an openshift setup using the group 0 might be better than 101 because openshift gives the user a "random" high-id and the group 0. If we just allow the group 0 to read/write most parts of the files this should work in most scenarios.

see https://catalog.redhat.com/software/containers/ubi9/nginx-120/61a609f2bfd4a5234d596287?q=nginx&architecture=amd64&image=654d19137095b1690e733af3&container-tabs=dockerfile

/var/www/localhost/htdocs/uploads /var/www/localhost/htdocs/assets \
/var/www/localhost/htdocs/protected/modules /var/www/localhost/htdocs/themes \
/var/www/localhost/htdocs/protected/config


COPY base/ /
COPY docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod 600 /etc/crontabs/nginx && \
chown 100:101 /etc/crontabs/nginx && \
rm /etc/crontabs/root && \
chmod +x /docker-entrypoint.sh

VOLUME /var/www/localhost/htdocs/uploads
VOLUME /var/www/localhost/htdocs/protected/config
VOLUME /var/www/localhost/htdocs/protected/modules

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["supervisord", "-n", "-c", "/etc/supervisord.conf"]


#+-----------
# HumHub PHP
#+-----------
FROM base as humhub_phponly

LABEL variant="phponly"

USER root
RUN apk add --no-cache fcgi

COPY phponly/ /
Expand All @@ -159,7 +174,13 @@ RUN chmod +x /usr/local/bin/php-fpm-healthcheck \
&& adduser --uid 100 -g 101 -S nginx

EXPOSE 9000
USER nginx
CMD ["multirun", "/usr/sbin/php-fpm7 --fpm-config /etc/php-fpm.d/pool.conf -O -F", "tail -f /var/www/localhost/htdocs/protected/runtime/logs/app.log", "crond -f -L /proc/self/fd/2"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tailing on this file is not the best idea ... it still writes to the top-layer potentially filling up the nodes local storage. We should use a symlink to /proc/self/fd/2 for that (if possible).



#+-------------
# HumHub NGINX
#+-------------
FROM docker.io/library/nginx:1.23.3-alpine as humhub_nginx

LABEL variant="nginx"
Expand All @@ -168,16 +189,29 @@ ENV NGINX_CLIENT_MAX_BODY_SIZE=10m \
NGINX_KEEPALIVE_TIMEOUT=65 \
NGINX_UPSTREAM=humhub:9000

USER root
COPY nginx/ /
COPY --from=builder --chown=nginx:nginx /usr/src/humhub/ /var/www/localhost/htdocs/
RUN mkdir -p /var/cache/nginx /run/nginx && \
chown nginx:nginx -R /etc/nginx /var/cache/nginx /run/nginx

USER nginx

#+------------------
# HumHub All-in-one
#+------------------
FROM base as humhub_allinone

LABEL variant="allinone"

USER root
RUN apk add --no-cache nginx && \
chown -R nginx:nginx /var/lib/nginx/
mkdir -p /etc/nginx && \
chown -R nginx:nginx /var/lib/nginx/ /etc/nginx

COPY nginx/ /
RUN chown nginx:nginx -R /etc/nginx

EXPOSE 80
USER nginx
CMD ["/usr/bin/multirun", "/usr/sbin/php-fpm7 --fpm-config /etc/php-fpm.d/pool.conf -O -F", "tail -f /var/www/localhost/htdocs/protected/runtime/logs/app.log", "crond -f -L /proc/self/fd/2", "/usr/sbin/nginx -g 'daemon off;'"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

> :warning: **Image Removal**: We have purged all registries from End-Of-Life images (1.4,1.5,1.6,1.7,1.8). These images were not maintained anymore and contained major security flaws. To protect the public we removed them. If you really want to use these images, you have to build them from source.

> :warning: **Default privileges change:** This container is no longer starting as root and then dropping the privileges. Supervisord (running previously as root) was changed to lighter alternative "multirun" that runs as "nginx" user directly. This change opens possibility to easily run on OpenShift and Kubernetes.

[HumHub](https://github.com/humhub/humhub) is a feature rich and highly flexible OpenSource Social Network Kit written in PHP.
This container provides a quick, flexible and lightweight way to set up a proof-of-concept for detailed evaluation.
Using this in production is possible, but please note that there is currently no official support available for this kind of setup.
Expand Down
2 changes: 1 addition & 1 deletion base/etc/php-fpm.d/listen.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
listen = /run/php-fpm.sock
listen = /run/nginx/php-fpm.sock
listen.owner = nginx
listen.group = nginx
17 changes: 0 additions & 17 deletions base/etc/supervisord.conf

This file was deleted.

5 changes: 0 additions & 5 deletions base/etc/supervisord.conf.d/cron.conf

This file was deleted.

7 changes: 0 additions & 7 deletions base/etc/supervisord.conf.d/humhub-log.conf

This file was deleted.

8 changes: 0 additions & 8 deletions base/etc/supervisord.conf.d/php-fpm.conf

This file was deleted.

9 changes: 0 additions & 9 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ else
mkdir -p /var/www/localhost/htdocs/protected/runtime/logs/
touch /var/www/localhost/htdocs/protected/runtime/logs/app.log

echo >&3 "$0: Setting permissions..."
chown -R nginx:nginx /var/www/localhost/htdocs/uploads
chown -R nginx:nginx /var/www/localhost/htdocs/protected/modules
chown -R nginx:nginx /var/www/localhost/htdocs/protected/config
chown -R nginx:nginx /var/www/localhost/htdocs/protected/runtime

wait_for_db

echo >&3 "$0: Creating database..."
Expand Down Expand Up @@ -191,9 +185,6 @@ else
php yii 'settings/set' 'base' 'mailer.encryption' "${HUMHUB_MAILER_ENCRYPTION}"
php yii 'settings/set' 'base' 'mailer.allowSelfSignedCerts' "${HUMHUB_MAILER_ALLOW_SELF_SIGNED_CERTS}"
fi

chown -R nginx:nginx /var/www/localhost/htdocs/protected/runtime
chown nginx:nginx /var/www/localhost/htdocs/protected/config/dynamic.php
fi
fi

Expand Down
2 changes: 1 addition & 1 deletion nginx/docker-entrypoint.d/60-nginx-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

export NGINX_UPSTREAM="${NGINX_UPSTREAM:-unix:/run/php-fpm.sock}"
export NGINX_UPSTREAM="${NGINX_UPSTREAM:-unix:/run/nginx/php-fpm.sock}"
export NGINX_CLIENT_MAX_BODY_SIZE="${NGINX_CLIENT_MAX_BODY_SIZE:-10m}"
export NGINX_KEEPALIVE_TIMEOUT="${NGINX_KEEPALIVE_TIMEOUT:-65}"

Expand Down
2 changes: 1 addition & 1 deletion nginx/etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ worker_processes 1;
error_log /dev/stderr notice;
#error_log logs/error.log info;

pid /run/nginx.pid;
pid /run/nginx/nginx.pid;


events {
Expand Down
8 changes: 0 additions & 8 deletions nginx/etc/supervisord.conf.d/nginx.conf

This file was deleted.