Skip to content

Commit 2e3feca

Browse files
pjbgfbrendan-kellam
authored andcommitted
security: run Docker container as non-root user
Running containers as a non-root user is a long standing security practice. The changes ensure that the sourcebot user is created and has the correct level of permissions to run all its dependencies (postgres, redis and node). Please note that as a side effect, existing mounted volumes would need to have their ownership reviewed or it may not be able to access the files. This is specially the case for previous versions that would create said files as 0:0. To fix that, users can run chown -R 1500:1500 /path/.sourcebot. The chmod may also need to be a bit more strict in such cases, so changing that is advised: chown -R 0750 /path/.sourcebot. Signed-off-by: Paulo Gomes <pjbgf@linux.com>
1 parent 33c7328 commit 2e3feca

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,26 @@ RUN mkdir -p /run/postgresql && \
225225
chown -R postgres:postgres /run/postgresql && \
226226
chmod 775 /run/postgresql
227227

228+
# To run as non-root, the user must be part of postgres, redis and node groups
229+
RUN addgroup -g 1500 sourcebot && \
230+
adduser -D -u 1500 -h /app -S sourcebot && \
231+
adduser sourcebot postgres && \
232+
adduser sourcebot redis && \
233+
adduser sourcebot node && \
234+
chown -R sourcebot /data && \
235+
chown -R sourcebot /app && \
236+
mkdir /var/log/sourcebot && \
237+
chown sourcebot /var/log/sourcebot
238+
228239
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
229240
COPY prefix-output.sh ./prefix-output.sh
230241
RUN chmod +x ./prefix-output.sh
231242
COPY entrypoint.sh ./entrypoint.sh
232243
RUN chmod +x ./entrypoint.sh
233244

245+
246+
USER sourcebot
247+
234248
EXPOSE 3000
235249
ENV PORT=3000
236250
ENV HOSTNAME="0.0.0.0"

entrypoint.sh

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,20 @@ fi
7777

7878
# Check if DATA_CACHE_DIR exists, if not create it
7979
if [ ! -d "$DATA_CACHE_DIR" ]; then
80-
mkdir -p "$DATA_CACHE_DIR"
80+
mkdir -m 0750 -p "$DATA_CACHE_DIR"
8181
fi
8282

8383
# Check if DATABASE_DATA_DIR exists, if not initialize it
8484
if [ "$DATABASE_EMBEDDED" = "true" ] && [ ! -d "$DATABASE_DATA_DIR" ]; then
85-
echo -e "\e[34m[Info] Initializing database at $DATABASE_DATA_DIR...\e[0m"
86-
mkdir -p $DATABASE_DATA_DIR && chown -R postgres:postgres "$DATABASE_DATA_DIR"
87-
su postgres -c "initdb -D $DATABASE_DATA_DIR"
85+
echo -e "\e[34m[Info] Initializing database at $DATABASE_D\ATA_DIR...\e[0m"
86+
mkdir -m 0750 -p $DATABASE_DATA_DIR
87+
88+
initdb -D "$DATABASE_DATA_DIR"
8889
fi
8990

9091
# Create the redis data directory if it doesn't exist
9192
if [ "$REDIS_EMBEDDED" = "true" ] && [ ! -d "$REDIS_DATA_DIR" ]; then
92-
mkdir -p $REDIS_DATA_DIR
93+
mkdir -m 0750 -p $REDIS_DATA_DIR
9394
fi
9495

9596
if [ -z "$SOURCEBOT_ENCRYPTION_KEY" ]; then
@@ -180,13 +181,25 @@ echo "{\"version\": \"$NEXT_PUBLIC_SOURCEBOT_VERSION\", \"install_id\": \"$SOURC
180181

181182
# Start the database and wait for it to be ready before starting any other service
182183
if [ "$DATABASE_EMBEDDED" = "true" ]; then
183-
su postgres -c "postgres -D $DATABASE_DATA_DIR" &
184-
until pg_isready -h localhost -p 5432 -U postgres; do
184+
postgres -D "$DATABASE_DATA_DIR" &
185+
until pg_isready -h localhost -p 5432 -d sourcebot -U postgres; do
185186
echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m"
186187
sleep 1
188+
189+
# As postgres runs in the background, we must check if it is still
190+
# running, otherwise the "until" loop will be running indefinitely.
191+
if ! pgrep -x "postgres" > /dev/null; then
192+
echo "postgres failed to run"
193+
exit 1
194+
break
195+
fi
187196
done
188197

189-
# Check if the database already exists, and create it if it dne
198+
# Running as non-root we need to ensure the postgres account is created.
199+
psql -U postgres -tc "SELECT 1 FROM pg_roles WHERE rolname='postgres'" | grep -q 1 \
200+
|| createuser postgres -s
201+
202+
# Check if the database already exists, and create it if it doesn't
190203
EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = 'sourcebot'")
191204

192205
if [ "$EXISTING_DB" = "1" ]; then
@@ -205,4 +218,4 @@ DATABASE_URL="$DATABASE_URL" yarn workspace @sourcebot/db prisma:migrate:prod
205218
mkdir -p /var/log/sourcebot
206219

207220
# Run supervisord
208-
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf
221+
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf

0 commit comments

Comments
 (0)