Skip to content

Commit 3578671

Browse files
committed
Add support for running rootless and with readonly filesystem
* Use supervisord's built in support for reading env vars * Move all log files to /etc/bitwarden/logs * Move all pid files and sockets to /tmp/bitwarden * Move nginx temp files to /tmp/bitwarden * Clean up some shellcheck errors in the entrypoint * Only chown files that need it (when running root; report the list otherwise) * Change the default PUID/PGID variables to 911 (using 1000 is a security risk as it's the default user for many distros and often isn't unprivileged) * Use softlinks in the image to point to files generated in the entrypoint * Update docker compose and settings examples to be more secure * Add EXPOSE to Dockerfile
1 parent 6aaa41a commit 3578671

File tree

17 files changed

+86
-78
lines changed

17 files changed

+86
-78
lines changed

docker-unified/Dockerfile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,16 @@ RUN apt-get update && apt-get install -y \
222222
RUN mkdir -p /etc/bitwarden/attachments/send
223223
RUN mkdir -p /etc/bitwarden/data-protection
224224
RUN mkdir -p /etc/bitwarden/licenses
225-
RUN mkdir -p /etc/bitwarden/logs
226225
RUN mkdir -p /etc/supervisor
227226
RUN mkdir -p /etc/supervisor.d
228-
RUN mkdir -p /var/log/bitwarden
229-
RUN mkdir -p /var/log/nginx/logs
230-
RUN mkdir -p /etc/nginx/http.d
231-
RUN mkdir -p /var/run/nginx
232-
RUN mkdir -p /var/lib/nginx/tmp
233-
RUN touch /var/run/nginx/nginx.pid
234-
RUN mkdir -p /app
227+
RUN mkdir -p /app/Identity
228+
RUN mkdir -p /app/Sso
229+
RUN mkdir -p /app/Web
230+
231+
# Create soft links for files generated in the entrypoint
232+
RUN ln -s /etc/bitwarden/identity.pfx /app/Identity/identity.pfx
233+
RUN ln -s /etc/bitwarden/identity.pfx /app/Sso/identity.pfx
234+
RUN ln -s /etc/bitwarden/app-id.json /app/Web/app-id.json
235235

236236
# Copy all apps from dotnet-build stage
237237
WORKDIR /app
@@ -273,6 +273,7 @@ RUN chmod +x /usr/local/bin/hbs
273273
COPY docker-unified/entrypoint.sh /entrypoint.sh
274274
RUN chmod +x /entrypoint.sh
275275

276+
EXPOSE 8080 8443
276277
VOLUME ["/etc/bitwarden"]
277278

278279
WORKDIR /app

docker-unified/docker-compose.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ services:
77
env_file:
88
- settings.env
99
image: ${REGISTRY:-ghcr.io/bitwarden}/self-host:${TAG:-beta}
10+
user: 999:999
11+
tmpfs: /tmp
12+
read_only: true
13+
security_opt:
14+
- no-new-privileges
1015
restart: always
1116
ports:
1217
- "80:8080"
1318
- "443:8443"
1419
volumes:
15-
- bitwarden:/etc/bitwarden
16-
- logs:/var/log/bitwarden
20+
# The user specified above must have permissions
21+
# for the bind mount on the host
22+
- ./bitwarden:/etc/bitwarden
1723

1824
# MariaDB Example
1925
db:
@@ -50,6 +56,4 @@ services:
5056
# - data:/var/opt/mssql
5157

5258
volumes:
53-
bitwarden:
54-
logs:
5559
data:

docker-unified/entrypoint.sh

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
22

3-
# Set up user group
4-
PGID="${PGID:-1000}"
5-
addgroup --gid $PGID bitwarden
3+
if [[ "$(id -u)" == "0" ]]; then
4+
# Set up user group
5+
PGID="${PGID:-911}"
6+
addgroup --gid "$PGID" bitwarden
67

7-
# Set up user
8-
PUID="${PUID:-1000}"
9-
adduser --no-create-home --shell /bin/bash --disabled-password --uid $PUID --gid $PGID --gecos "" bitwarden
8+
# Set up user
9+
PUID="${PUID:-911}"
10+
adduser --no-create-home --shell /bin/bash --disabled-password --uid "$PUID" --gid "$PGID" --gecos "" bitwarden
11+
fi
1012

1113
# Translate environment variables for application settings
1214
VAULT_SERVICE_URI=https://$BW_DOMAIN
@@ -49,60 +51,55 @@ if [ ! -f /etc/bitwarden/identity.pfx ]; then
4951
-subj "/CN=Bitwarden IdentityServer" \
5052
-days 36500
5153

54+
# identity.pfx is soft linked to the necessary locations in the Dockerfile
5255
openssl pkcs12 \
5356
-export \
5457
-out /etc/bitwarden/identity.pfx \
5558
-inkey /etc/bitwarden/identity.key \
5659
-in /etc/bitwarden/identity.crt \
57-
-passout pass:$globalSettings__identityServer__certificatePassword
60+
-passout "pass:$globalSettings__identityServer__certificatePassword"
5861

5962
rm /etc/bitwarden/identity.crt
6063
rm /etc/bitwarden/identity.key
6164
fi
6265

63-
cp /etc/bitwarden/identity.pfx /app/Identity/identity.pfx
64-
cp /etc/bitwarden/identity.pfx /app/Sso/identity.pfx
65-
6666
# Generate SSL certificates
67-
if [ "$BW_ENABLE_SSL" = "true" -a ! -f /etc/bitwarden/${BW_SSL_KEY:-ssl.key} ]; then
67+
if [ "$BW_ENABLE_SSL" = "true" ] && [ ! -f "/etc/bitwarden/${BW_SSL_KEY:-ssl.key}" ]; then
6868
openssl req \
6969
-x509 \
7070
-newkey rsa:4096 \
7171
-sha256 \
7272
-nodes \
7373
-days 36500 \
74-
-keyout /etc/bitwarden/${BW_SSL_KEY:-ssl.key} \
75-
-out /etc/bitwarden/${BW_SSL_CERT:-ssl.crt} \
74+
-keyout /etc/bitwarden/"${BW_SSL_KEY:-ssl.key}" \
75+
-out /etc/bitwarden/"${BW_SSL_CERT:-ssl.crt}" \
7676
-reqexts SAN \
7777
-extensions SAN \
78-
-config <(cat /usr/lib/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:${BW_DOMAIN:-localhost}\nbasicConstraints=CA:true")) \
78+
-config <(cat /usr/lib/ssl/openssl.cnf <(printf '[SAN]\nsubjectAltName=DNS:%s\nbasicConstraints=CA:true' "${BW_DOMAIN:-localhost}")) \
7979
-subj "/C=US/ST=California/L=Santa Barbara/O=Bitwarden Inc./OU=Bitwarden/CN=${BW_DOMAIN:-localhost}"
8080
fi
8181

8282
# Launch a loop to rotate nginx logs on a daily basis
8383
/bin/sh -c "/logrotate.sh loop >/dev/null 2>&1 &"
8484

85-
/usr/local/bin/hbs
85+
# Create necessary directories
86+
mkdir -p /etc/bitwarden/logs/supervisord
87+
mkdir -p /etc/bitwarden/logs/nginx
88+
mkdir -p /etc/bitwarden/nginx
89+
mkdir -p /tmp/bitwarden
8690

87-
# Enable/Disable services
88-
sed -i "s/autostart=true/autostart=${BW_ENABLE_ADMIN}/" /etc/supervisor.d/admin.ini
89-
sed -i "s/autostart=true/autostart=${BW_ENABLE_API}/" /etc/supervisor.d/api.ini
90-
sed -i "s/autostart=true/autostart=${BW_ENABLE_EVENTS}/" /etc/supervisor.d/events.ini
91-
sed -i "s/autostart=true/autostart=${BW_ENABLE_ICONS}/" /etc/supervisor.d/icons.ini
92-
sed -i "s/autostart=true/autostart=${BW_ENABLE_IDENTITY}/" /etc/supervisor.d/identity.ini
93-
sed -i "s/autostart=true/autostart=${BW_ENABLE_NOTIFICATIONS}/" /etc/supervisor.d/notifications.ini
94-
sed -i "s/autostart=true/autostart=${BW_ENABLE_SCIM}/" /etc/supervisor.d/scim.ini
95-
sed -i "s/autostart=true/autostart=${BW_ENABLE_SSO}/" /etc/supervisor.d/sso.ini
91+
/usr/local/bin/hbs
9692

97-
chown -R $PUID:$PGID \
98-
/app \
99-
/etc/bitwarden \
100-
/etc/nginx/http.d \
101-
/etc/supervisor \
102-
/etc/supervisor.d \
103-
/var/lib/nginx \
104-
/var/log \
105-
/var/run/nginx \
106-
/run
93+
if [[ "$(id -u)" == 0 ]]; then
94+
find /etc/bitwarden ! -xtype l \( ! -gid "$PGID" -o ! -uid "$PUID" \) -exec chown "${PUID}:${PGID}" {} +
95+
96+
exec setpriv --reuid="$PUID" --regid="$PGID" --init-groups /usr/bin/supervisord
97+
else
98+
FILES="$(find /etc/bitwarden ! -xtype l \( ! -gid "$(id -g)" -o ! -uid "$(id -u)" \))"
99+
if [[ -n "$FILES" ]]; then
100+
echo "The following files are not owned by the running user and may cause errors:" >&2
101+
echo "$FILES" >&2
102+
fi
107103

108-
exec setpriv --reuid=$PUID --regid=$PGID --init-groups /usr/bin/supervisord
104+
exec /usr/bin/supervisord
105+
fi

docker-unified/hbs/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ helper_categories:
22
- String
33
templates:
44
- src: /etc/hbs/app-id.hbs
5-
dest: /app/Web/app-id.json
5+
dest: /etc/bitwarden/app-id.json
66
- src: /etc/hbs/nginx-config.hbs
7-
dest: /etc/nginx/http.d/bitwarden.conf
7+
dest: /etc/bitwarden/nginx/bitwarden.conf

docker-unified/hbs/nginx-config.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# THIS FILE IS AUTOMATICALLY GENERATED BY BITWARDEN!
2+
# CHANGES WILL BE OVERWRITTEN ON CONTAINER STARTUP!
13
server {
24
listen {{{String.Coalesce env.BW_PORT_HTTP "8080"}}} default_server;
35
#listen [::]:{{{String.Coalesce env.BW_PORT_HTTP "8080"}}} default_server;

docker-unified/nginx/nginx.conf

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ events {
2828

2929
# Default error log file
3030
# (this is only used when you don't override error_log on a server{} level)
31-
error_log /var/log/nginx/error.log warn;
32-
pid /var/run/nginx/nginx.pid;
31+
error_log /etc/bitwarden/logs/nginx/error.log warn;
32+
pid /tmp/bitwarden/nginx.pid;
3333

3434
http {
3535
# Include proxy and server configuration.
3636
include /etc/nginx/proxy.conf;
37-
include /etc/nginx/http.d/bitwarden.conf;
37+
include /etc/bitwarden/nginx/bitwarden.conf;
38+
39+
# Use /tmp/bitwarden for (or disable) nginx temp files
40+
client_body_temp_path /tmp/bitwarden/nginx 1 2;
41+
fastcgi_temp_path /tmp/bitwarden/nginx-fastcgi;
42+
proxy_temp_path /tmp/bitwarden/nginx-proxy;
43+
uwsgi_temp_path /tmp/bitwarden/nginx-uwsgi;
44+
scgi_temp_path /tmp/bitwarden/nginx-scgi;
3845

3946
# Hide nginx version information.
4047
server_tokens off;
@@ -62,7 +69,7 @@ http {
6269

6370
# Default log file
6471
# (this is only used when you don't override access_log on a server{} level)
65-
access_log /var/log/nginx/access.log main;
72+
access_log /etc/bitwarden/logs/nginx/access.log main;
6673

6774
# How long to allow each connection to stay idle; longer values are better
6875
# for each individual client, particularly for SSL, but means that worker

docker-unified/settings.env

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ BW_INSTALLATION_KEY=xxxxxxxxxxxx
2323
#####################
2424
# Learn more here: https://bitwarden.com/help/environment-variables/
2525

26-
# Container user ID/group ID
27-
#PUID=1000
28-
#PGID=1000
29-
3026
# Webserver ports
3127
#BW_PORT_HTTP=8080
3228
#BW_PORT_HTTPS=8443
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[program:admin]
2-
autostart=true
2+
autostart=%(ENV_BW_ENABLE_ADMIN)s
33
autorestart=true
44
command=/usr/bin/dotnet "Admin.dll"
55
directory=/app/Admin
66
environment=ASPNETCORE_URLS="http://+:5000"
77
redirect_stderr=true
88
startsecs=15
9-
stdout_logfile=/var/log/bitwarden/admin.log
9+
stdout_logfile=/etc/bitwarden/logs/supervisord/admin.log

docker-unified/supervisord/api.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[program:api]
2-
autostart=true
2+
autostart=%(ENV_BW_ENABLE_API)s
33
autorestart=true
44
command=/usr/bin/dotnet "Api.dll"
55
directory=/app/Api
66
environment=ASPNETCORE_URLS="http://+:5001"
77
redirect_stderr=true
88
startsecs=15
9-
stdout_logfile=/var/log/bitwarden/api.log
9+
stdout_logfile=/etc/bitwarden/logs/supervisord/api.log
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[program:events]
2-
autostart=true
2+
autostart=%(ENV_BW_ENABLE_EVENTS)s
33
autorestart=true
44
command=/usr/bin/dotnet "Events.dll"
55
directory=/app/Events
66
environment=ASPNETCORE_URLS="http://+:5003"
77
redirect_stderr=true
88
startsecs=15
9-
stdout_logfile=/var/log/bitwarden/events.log
9+
stdout_logfile=/etc/bitwarden/logs/supervisord/events.log

0 commit comments

Comments
 (0)