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

build: containers will now run as the non-root rafiki user #3277

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions packages/auth/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
FROM node:20-alpine3.20

RUN adduser -D rafiki
WORKDIR /home/rafiki

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

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

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

# Copy the source code and chown the relevant folders back to the Rafiki user
USER root
COPY . ./
RUN chown -v -R rafiki:rafiki /home/rafiki/localenv
RUN chown -v -R rafiki:rafiki /home/rafiki/packages
RUN chown -v -R rafiki:rafiki /home/rafiki/test

# As the Rafiki user, install the rest of the dependencies and build the source code
USER rafiki
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
pnpm install \
--recursive \
--offline \
--frozen-lockfile

RUN pnpm --filter auth build:deps

CMD pnpm --filter auth dev
9 changes: 9 additions & 0 deletions packages/auth/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ RUN pnpm --filter auth build

FROM node:20-alpine3.20 AS runner

RUN adduser -D rafiki

WORKDIR /home/rafiki

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

USER root

# For additional paranoia, we make it so that the Rafiki user has no write access to the packages
RUN chown -R :rafiki /home/rafiki/packages
RUN chmod -R 750 /home/rafiki/packages

USER rafiki
CMD ["node", "/home/rafiki/packages/auth/dist/index.js"]
17 changes: 14 additions & 3 deletions packages/backend/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
FROM node:20-alpine3.20

RUN adduser -D rafiki
WORKDIR /home/rafiki

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

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

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

# Copy the source code and chown the relevant folders back to the Rafiki user
USER root
COPY . ./
RUN chown -v -R rafiki:rafiki /home/rafiki/localenv
RUN chown -v -R rafiki:rafiki /home/rafiki/packages
RUN chown -v -R rafiki:rafiki /home/rafiki/test

# As the Rafiki user, install the rest of the dependencies and build the source code
USER rafiki
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
pnpm install \
--recursive \
--offline \
--frozen-lockfile

RUN pnpm --filter backend build:deps

CMD pnpm --filter backend dev
9 changes: 9 additions & 0 deletions packages/backend/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ RUN pnpm --filter backend build

FROM node:20-alpine3.20 AS runner

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

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

USER root

# For additional paranoia, we make it so that the Rafiki user has no write access to the packages
RUN chown -R :rafiki /home/rafiki/packages
RUN chmod -R 750 /home/rafiki/packages

USER rafiki
CMD ["node", "-r", "/home/rafiki/packages/backend/dist/telemetry/index.js", "/home/rafiki/packages/backend/dist/index.js"]
16 changes: 12 additions & 4 deletions packages/frontend/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
FROM node:20-alpine3.20 AS base

RUN adduser -D rafiki
WORKDIR /home/rafiki

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

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

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

# Chown the copied packages folder back to the Rafiki user
USER root
RUN chown -v -R rafiki:rafiki /home/rafiki/packages

# Fetch the pnpm dependencies, but use a local cache.
USER rafiki
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
pnpm fetch \
| grep -v "cross-device link not permitted\|Falling back to copying packages from store"
Expand Down
8 changes: 7 additions & 1 deletion packages/frontend/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
RUN pnpm --filter frontend build

FROM node:20-alpine3.20 AS runner

RUN adduser -D rafiki
WORKDIR /home/rafiki

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

USER root
RUN chown -R :rafiki /home/rafiki/packages
RUN chmod -R 750 /home/rafiki/packages

USER rafiki

WORKDIR /home/rafiki/packages/frontend
CMD ["sh", "./node_modules/.bin/remix-serve", "./build/index.js"]
Loading