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

Make Docker image immutable on runtime #248

Closed
wants to merge 2 commits into from
Closed
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
35 changes: 32 additions & 3 deletions docker-unified/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ RUN apt-get update && apt-get install -y \
npm \
&& rm -rf /var/lib/apt/lists/*

RUN npm install -g gulp

# Copy csproj files as distinct layers
WORKDIR /source
Expand Down Expand Up @@ -137,7 +136,7 @@ COPY server/.git/. ./.git/
# Build Admin app
WORKDIR /source/src/Admin
RUN npm install
RUN gulp --gulpfile "gulpfile.js" build
RUN npm run build
RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Admin --no-restore --no-self-contained -r $RID

# Build Api app
Expand All @@ -163,7 +162,7 @@ RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Notifications --no-resto
# Build Sso app
WORKDIR /source/bitwarden_license/src/Sso
RUN npm install
RUN gulp --gulpfile "gulpfile.js" build
RUN npm run build
RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Sso --no-restore --no-self-contained -r $RID

# Build Scim app
Expand Down Expand Up @@ -274,6 +273,36 @@ RUN chmod +x /usr/local/bin/hbs
COPY docker-unified/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

#Set up user and group
RUN addgroup --gid 1000 bitwarden
Comment on lines +276 to +277
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#Set up user and group
RUN addgroup --gid 1000 bitwarden
# Set up user and group
RUN addgroup --gid $PGID bitwarden

Copy link
Contributor

Choose a reason for hiding this comment

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

We'll also need to set the following in the final stage:

ENV PUID=1000
ENV PGID=1000

Copy link
Contributor

@tangowithfoxtrot tangowithfoxtrot Nov 5, 2024

Choose a reason for hiding this comment

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

We also need to consider the impact of hard-coding the user and group ID in the image. Since they're currently set at runtime (in the entrypoint script), it's possible to provide custom IDs. Setting them in the Dockerfile removes this capability, and could potentially impact other deployments.

RUN adduser --no-create-home --shell /bin/bash --disabled-password --uid $PUID --gid $PGID --gecos "" bitwarden

#Create symlincs for the identity files:
RUN ln -s /app/Identity/identity.pfx /etc/bitwarden/identity.pfx
RUN ln -s /app/Sso/identity.pfx /etc/bitwarden/identity.pfx
Comment on lines +280 to +282
Copy link
Contributor

@tangowithfoxtrot tangowithfoxtrot Nov 5, 2024

Choose a reason for hiding this comment

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

Suggested change
#Create symlincs for the identity files:
RUN ln -s /app/Identity/identity.pfx /etc/bitwarden/identity.pfx
RUN ln -s /app/Sso/identity.pfx /etc/bitwarden/identity.pfx
# Create symlinks for the identity files:
RUN ln -sf /app/Identity/identity.pfx /etc/bitwarden/identity.pfx
RUN ln -sf /app/Sso/identity.pfx /etc/bitwarden/identity.pfx

I ran into some issues when building this locally, were the existing files where getting cached in the docker build, so I had to force-create the symlinks.


# Enable/Disable services:
RUN sed -i "s/autostart=true/autostart=${BW_ENABLE_ADMIN}/" /etc/supervisor.d/admin.ini && \
sed -i "s/autostart=true/autostart=${BW_ENABLE_API}/" /etc/supervisor.d/api.ini && \
sed -i "s/autostart=true/autostart=${BW_ENABLE_EVENTS}/" /etc/supervisor.d/events.ini && \
sed -i "s/autostart=true/autostart=${BW_ENABLE_ICONS}/" /etc/supervisor.d/icons.ini && \
sed -i "s/autostart=true/autostart=${BW_ENABLE_IDENTITY}/" /etc/supervisor.d/identity.ini && \
sed -i "s/autostart=true/autostart=${BW_ENABLE_NOTIFICATIONS}/" /etc/supervisor.d/notifications.ini && \
sed -i "s/autostart=true/autostart=${BW_ENABLE_SCIM}/" /etc/supervisor.d/scim.ini && \
sed -i "s/autostart=true/autostart=${BW_ENABLE_SSO}/" /etc/supervisor.d/sso.ini

# Set desired ownership:
RUN chown -R 1000:1000 \
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
RUN chown -R 1000:1000 \
RUN chown -R $PUID:$PGID \

/app \
/etc/bitwarden \
/etc/nginx/http.d \
/etc/supervisor \
/etc/supervisor.d \
/var/lib/nginx \
/var/log \
/var/run/nginx \
/run

VOLUME ["/etc/bitwarden"]

WORKDIR /app
Expand Down
34 changes: 1 addition & 33 deletions docker-unified/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#!/bin/bash

# Set up user group
PGID="${PGID:-1000}"
addgroup --gid $PGID bitwarden

# Set up user
PUID="${PUID:-1000}"
shaman007 marked this conversation as resolved.
Show resolved Hide resolved
adduser --no-create-home --shell /bin/bash --disabled-password --uid $PUID --gid $PGID --gecos "" bitwarden

# Translate environment variables for application settings
VAULT_SERVICE_URI=https://$BW_DOMAIN
MYSQL_CONNECTION_STRING="server=$BW_DB_SERVER;port=${BW_DB_PORT:-3306};database=$BW_DB_DATABASE;user=$BW_DB_USERNAME;password=$BW_DB_PASSWORD"
Expand Down Expand Up @@ -60,9 +52,6 @@ if [ ! -f /etc/bitwarden/identity.pfx ]; then
rm /etc/bitwarden/identity.key
fi

cp /etc/bitwarden/identity.pfx /app/Identity/identity.pfx
cp /etc/bitwarden/identity.pfx /app/Sso/identity.pfx

# Generate SSL certificates
if [ "$BW_ENABLE_SSL" = "true" -a ! -f /etc/bitwarden/${BW_SSL_KEY:-ssl.key} ]; then
openssl req \
Expand All @@ -84,25 +73,4 @@ fi

/usr/local/bin/hbs

# Enable/Disable services
sed -i "s/autostart=true/autostart=${BW_ENABLE_ADMIN}/" /etc/supervisor.d/admin.ini
sed -i "s/autostart=true/autostart=${BW_ENABLE_API}/" /etc/supervisor.d/api.ini
sed -i "s/autostart=true/autostart=${BW_ENABLE_EVENTS}/" /etc/supervisor.d/events.ini
sed -i "s/autostart=true/autostart=${BW_ENABLE_ICONS}/" /etc/supervisor.d/icons.ini
sed -i "s/autostart=true/autostart=${BW_ENABLE_IDENTITY}/" /etc/supervisor.d/identity.ini
sed -i "s/autostart=true/autostart=${BW_ENABLE_NOTIFICATIONS}/" /etc/supervisor.d/notifications.ini
sed -i "s/autostart=true/autostart=${BW_ENABLE_SCIM}/" /etc/supervisor.d/scim.ini
sed -i "s/autostart=true/autostart=${BW_ENABLE_SSO}/" /etc/supervisor.d/sso.ini

chown -R $PUID:$PGID \
/app \
/etc/bitwarden \
/etc/nginx/http.d \
/etc/supervisor \
/etc/supervisor.d \
/var/lib/nginx \
/var/log \
/var/run/nginx \
/run

exec setpriv --reuid=$PUID --regid=$PGID --init-groups /usr/bin/supervisord
exec setpriv --reuid=1000--regid=1000 --init-groups /usr/bin/supervisord
Loading