Skip to content

Commit cef0726

Browse files
authored
build: containers will now run as the non-root rafiki user (#3277)
* build: containers will now run as the non-root rafiki user * build: frontend build image will now also have read-only access to packages folder
1 parent b02616e commit cef0726

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

packages/auth/Dockerfile.dev

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
FROM node:20-alpine3.20
22

3+
RUN adduser -D rafiki
34
WORKDIR /home/rafiki
45

5-
RUN corepack enable
6+
# Install Corepack and pnpm as the Rafiki user
7+
USER rafiki
8+
RUN mkdir -p /home/rafiki/.local/bin
9+
ENV PATH="/home/rafiki/.local/bin:$PATH"
10+
RUN corepack enable --install-directory ~/.local/bin
611
RUN corepack prepare [email protected] --activate
712

813
COPY pnpm-lock.yaml package.json pnpm-workspace.yaml .npmrc tsconfig.json tsconfig.build.json ./
914

15+
# Fetch the pnpm dependencies, but use a local cache.
16+
USER rafiki
1017
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
1118
pnpm fetch \
1219
| grep -v "cross-device link not permitted\|Falling back to copying packages from store"
1320

21+
# Copy the source code and chown the relevant folders back to the Rafiki user
22+
USER root
1423
COPY . ./
24+
RUN chown -v -R rafiki:rafiki /home/rafiki/localenv
25+
RUN chown -v -R rafiki:rafiki /home/rafiki/packages
26+
RUN chown -v -R rafiki:rafiki /home/rafiki/test
1527

28+
# As the Rafiki user, install the rest of the dependencies and build the source code
29+
USER rafiki
1630
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
1731
pnpm install \
1832
--recursive \
1933
--offline \
2034
--frozen-lockfile
21-
2235
RUN pnpm --filter auth build:deps
2336

2437
CMD pnpm --filter auth dev

packages/auth/Dockerfile.prod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ RUN pnpm --filter auth build
4545

4646
FROM node:20-alpine3.20 AS runner
4747

48+
RUN adduser -D rafiki
49+
4850
WORKDIR /home/rafiki
4951

5052
COPY --from=prod-deps /home/rafiki/node_modules ./node_modules
@@ -58,4 +60,11 @@ COPY --from=builder /home/rafiki/packages/auth/migrations/ ./packages/auth/migra
5860
COPY --from=builder /home/rafiki/packages/auth/dist ./packages/auth/dist
5961
COPY --from=builder /home/rafiki/packages/token-introspection/dist ./packages/token-introspection/dist
6062

63+
USER root
64+
65+
# For additional paranoia, we make it so that the Rafiki user has no write access to the packages
66+
RUN chown -R :rafiki /home/rafiki/packages
67+
RUN chmod -R 750 /home/rafiki/packages
68+
69+
USER rafiki
6170
CMD ["node", "/home/rafiki/packages/auth/dist/index.js"]

packages/backend/Dockerfile.dev

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
FROM node:20-alpine3.20
22

3+
RUN adduser -D rafiki
34
WORKDIR /home/rafiki
45

5-
RUN corepack enable
6+
# Install Corepack and pnpm as the Rafiki user
7+
USER rafiki
8+
RUN mkdir -p /home/rafiki/.local/bin
9+
ENV PATH="/home/rafiki/.local/bin:$PATH"
10+
RUN corepack enable --install-directory ~/.local/bin
611
RUN corepack prepare [email protected] --activate
7-
812
COPY pnpm-lock.yaml package.json pnpm-workspace.yaml .npmrc tsconfig.json tsconfig.build.json ./
913

14+
# Fetch the pnpm dependencies, but use a local cache.
1015
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
1116
pnpm fetch \
1217
| grep -v "cross-device link not permitted\|Falling back to copying packages from store"
1318

19+
# Copy the source code and chown the relevant folders back to the Rafiki user
20+
USER root
1421
COPY . ./
22+
RUN chown -v -R rafiki:rafiki /home/rafiki/localenv
23+
RUN chown -v -R rafiki:rafiki /home/rafiki/packages
24+
RUN chown -v -R rafiki:rafiki /home/rafiki/test
1525

26+
# As the Rafiki user, install the rest of the dependencies and build the source code
27+
USER rafiki
1628
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
1729
pnpm install \
1830
--recursive \
1931
--offline \
2032
--frozen-lockfile
21-
2233
RUN pnpm --filter backend build:deps
2334

2435
CMD pnpm --filter backend dev

packages/backend/Dockerfile.prod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ RUN pnpm --filter backend build
4545

4646
FROM node:20-alpine3.20 AS runner
4747

48+
# Since this is from a fresh image, we need to first create the Rafiki user
49+
RUN adduser -D rafiki
4850
WORKDIR /home/rafiki
4951

5052
COPY --from=prod-deps /home/rafiki/node_modules ./node_modules
@@ -59,4 +61,11 @@ COPY --from=builder /home/rafiki/packages/backend/dist ./packages/backend/dist
5961
COPY --from=builder /home/rafiki/packages/token-introspection/dist ./packages/token-introspection/dist
6062
COPY --from=builder /home/rafiki/packages/backend/knexfile.js ./packages/backend/knexfile.js
6163

64+
USER root
65+
66+
# For additional paranoia, we make it so that the Rafiki user has no write access to the packages
67+
RUN chown -R :rafiki /home/rafiki/packages
68+
RUN chmod -R 750 /home/rafiki/packages
69+
70+
USER rafiki
6271
CMD ["node", "-r", "/home/rafiki/packages/backend/dist/telemetry/index.js", "/home/rafiki/packages/backend/dist/index.js"]

packages/frontend/Dockerfile.dev

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
FROM node:20-alpine3.20 AS base
22

3+
RUN adduser -D rafiki
34
WORKDIR /home/rafiki
45

5-
ENV PNPM_HOME="/pnpm"
6-
ENV PATH="$PNPM_HOME:$PATH"
7-
8-
RUN corepack enable
6+
# Install Corepack and pnpm as the Rafiki user
7+
USER rafiki
8+
RUN mkdir -p /home/rafiki/.local/bin
9+
ENV PATH="/home/rafiki/.local/bin:$PATH"
10+
RUN corepack enable --install-directory ~/.local/bin
911
RUN corepack prepare [email protected] --activate
1012

1113
COPY pnpm-lock.yaml package.json pnpm-workspace.yaml .npmrc tsconfig.json tsconfig.build.json ./
1214
COPY packages/frontend ./packages/frontend
1315

16+
# Chown the copied packages folder back to the Rafiki user
17+
USER root
18+
RUN chown -v -R rafiki:rafiki /home/rafiki/packages
19+
20+
# Fetch the pnpm dependencies, but use a local cache.
21+
USER rafiki
1422
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
1523
pnpm fetch \
1624
| grep -v "cross-device link not permitted\|Falling back to copying packages from store"

packages/frontend/Dockerfile.prod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
4141
RUN pnpm --filter frontend build
4242

4343
FROM node:20-alpine3.20 AS runner
44-
44+
RUN adduser -D rafiki
4545
WORKDIR /home/rafiki
4646

4747
COPY --from=prod-deps /home/rafiki/node_modules ./node_modules
@@ -51,5 +51,11 @@ COPY --from=prod-deps /home/rafiki/packages/frontend/package.json ./packages/fro
5151
COPY --from=builder /home/rafiki/packages/frontend/build ./packages/frontend/build
5252
COPY --from=builder /home/rafiki/packages/frontend/public ./packages/frontend/public
5353

54+
USER root
55+
RUN chown -R :rafiki /home/rafiki/packages
56+
RUN chmod -R 750 /home/rafiki/packages
57+
58+
USER rafiki
59+
5460
WORKDIR /home/rafiki/packages/frontend
5561
CMD ["sh", "./node_modules/.bin/remix-serve", "./build/index.js"]

0 commit comments

Comments
 (0)