Skip to content

Commit

Permalink
Switch to appuser to avoid root (#43)
Browse files Browse the repository at this point in the history
* Switch to appuser for pip install

---------

Signed-off-by: Benoit Donneaux <[email protected]>
  • Loading branch information
btlogy authored Jun 15, 2023
1 parent e220921 commit 7540d75
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ jobs:
MW_RELAY_PORT: 4001
MW_RELAY_WS_PORT: 4200
run: |
# Start the back-end with the current user ID
echo "MW_MAILBOX_UID=$(id -u)" >> .env
echo "MW_RELAY_UID=$(id -u)" >> .env
mkdir mailbox_database relay_database
docker-compose -f docker-compose-back.yml up --detach
# Wait for the server
# Wait for the services
for try in {1..3}; do
{ nc -w 3 127.0.0.1 ${MW_MAILBOX_PORT} > /dev/null 2>&1 && \
nc -w 3 127.0.0.1 ${MW_RELAY_PORT} > /dev/null 2>&1 && \
Expand Down
2 changes: 2 additions & 0 deletions docker-compose-back.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
context: ./mailbox
environment:
- MW_MAILBOX_PORT=${MW_MAILBOX_PORT:-4000}
user: ${MW_MAILBOX_UID:-1000}
volumes:
- "./mailbox_database:/db"
ports:
Expand All @@ -17,6 +18,7 @@ services:
environment:
- MW_RELAY_PORT=${MW_RELAY_PORT:-4001}
- MW_RELAY_WS_PORT=${MW_RELAY_WS_PORT:-4200}
user: ${MW_RELAY_UID:-1000}
volumes:
- "./relay_database:/db"
ports:
Expand Down
25 changes: 25 additions & 0 deletions mailbox/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ RUN pip install \
--upgrade \
-r /app/requirements.txt

# Parameters for default user:group
ARG uid=1000
ARG user=appuser
ARG gid=1000
ARG group=appgroup

# Add group and user so the command above and its
# output will be owned by the specified uid:gid
# These steps will fail explicitely on conflict
RUN getent group "${gid}" > /dev/null \
|| groupadd -g "${gid}" "${group}"; \
test "$(getent group "${gid}")" = "$(getent group "${group}")" \
|| { echo "Group name/id conflict!"; exit 1; }
RUN id "${uid}" > /dev/null 2>&1 \
|| useradd -md "/home/${user}" -s /bin/bash -g "${group}" -u "${uid}" "${user}"; \
test "$(id "${uid}")" = "$(id "${user}")" \
|| { echo "User name/id conflict!"; exit 1; }

# Prepare directories with ownership
RUN { test -d /db || mkdir /db; } && chown -R ${user}:${group} /db

# Switch to non-root user
USER ${user}
WORKDIR /app

# Copy welcome message as file to load with newlines symbols in parameters
# FIXME: This motd should not be shipped in this image (configured downstream)
COPY welcome.motd /app/welcome.motd
Expand Down
25 changes: 25 additions & 0 deletions relay/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ RUN pip install \
--upgrade \
-r /app/requirements.txt

# Parameters for default user:group
ARG uid=1000
ARG user=appuser
ARG gid=1000
ARG group=appgroup

# Add group and user so the command above and its
# output will be owned by the specified uid:gid
# These steps will fail explicitely on conflict
RUN getent group "${gid}" > /dev/null \
|| groupadd -g "${gid}" "${group}"; \
test "$(getent group "${gid}")" = "$(getent group "${group}")" \
|| { echo "Group name/id conflict!"; exit 1; }
RUN id "${uid}" > /dev/null 2>&1 \
|| useradd -md "/home/${user}" -s /bin/bash -g "${group}" -u "${uid}" "${user}"; \
test "$(id "${uid}")" = "$(id "${user}")" \
|| { echo "User name/id conflict!"; exit 1; }

# Prepare directories with ownership
RUN { test -d /db || mkdir /db; } && chown -R ${user}:${group} /db

# Switch to non-root user
USER ${user}
WORKDIR /app

# Default parameters and command to start
ENV MW_RELAY_BLUR_USAGE="3600"
ENV MW_RELAY_PROTO="tcp"
Expand Down
24 changes: 22 additions & 2 deletions wormhole/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,27 @@ RUN pip install \
--upgrade \
-r /app/requirements.txt

# Default command to start the application
CMD wormhole
# Parameters for default user:group
ARG uid=1000
ARG user=appuser
ARG gid=1000
ARG group=appgroup

# Add group and user so the command above and its
# output will be owned by the specified uid:gid
# These steps will fail explicitely on conflict
RUN getent group "${gid}" > /dev/null \
|| groupadd -g "${gid}" "${group}"; \
test "$(getent group "${gid}")" = "$(getent group "${group}")" \
|| { echo "Group name/id conflict!"; exit 1; }
RUN id "${uid}" > /dev/null 2>&1 \
|| useradd -md "/home/${user}" -s /bin/bash -g "${group}" -u "${uid}" "${user}"; \
test "$(id "${uid}")" = "$(id "${user}")" \
|| { echo "User name/id conflict!"; exit 1; }

# Switch to non-root user
USER ${user}
WORKDIR /app

# Default command to start the application
CMD wormhole

0 comments on commit 7540d75

Please sign in to comment.