From dd10e273350c8e232c4b1235dc1ec32f18cb96ff Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 08:21:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat(runtime):=20frontend=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=EB=A5=BC=20=EB=B9=84=ED=8A=B9=EA=B6=8C=20ngi?= =?UTF-8?q?nx=EB=A1=9C=208080=EC=97=90=EC=84=9C=20=EB=8F=8C=EB=A6=B0?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit frontend 이미지는 nginx 공식 이미지라 root로 시작해 80번을 열고 worker를 nginx 사용자로 내린다. 그래서 Kubernetes에서 runAsNonRoot와 drop ALL을 둘 수 없다(k3s 성숙도 점검 2026-11-11, 7.5절). 운영 매니페스트는 allowPrivilegeEscalation: false만 두고 PSA restricted 경고를 낸다. runtime base를 nginxinc/nginx-unprivileged:1.27.5-alpine(digest 고정)으로 바꾼다. 예전 base와 같은 NGINX 1.27.5·Alpine이고 wget도 있어 HEALTHCHECK가 그대로 돈다. 파일을 고치는 동안만 root로 있다가 USER 101로 돌아간다. 생성되는 server 블록은 listen 8080이고, EXPOSE·HEALTHCHECK·dev-stack 포트 매핑도 8080으로 맞춘다. 버린 대안: Debian판 nginx-unprivileged. wget이 없어 HEALTHCHECK가 늘 실패한다. 80번을 그대로 두고 NET_BIND_SERVICE만 남기는 안도 버렸다. drop ALL을 못 하고 PSA restricted도 통과하지 못한다. 위험·한계: 이 이미지는 8080을 열므로 운영 매니페스트의 포트를 같이 바꿔야 한다. deploy/production/frontend-nonroot가 그 일을 하고, 기본 경로에는 넣지 않는다. dev-stack의 443·/etc/nginx/tls 부분은 이미지에 없는 파일을 쓰는 예전 상태 그대로다. 이 repo의 pnpm 게이트와 전체 docker build는 돌리지 않았다(heavy lock 대기가 길어 취소). 검증: desktop에서 이 Dockerfile의 runtime 단계를 그대로 쓰고, 빌드 단계만 운영 이미지 7a4e201의 dist와 conf(listen 8080으로 바꿈)로 대신해 이미지를 만들었다. --user 101 --cap-drop ALL --security-opt no-new-privileges --sysctl net.ipv4.ip_unprivileged_port_start=1024로 띄우면 GET /config.json 200, GET / 200, CapEff 0, NoNewPrivs 1이고, Docker health는 healthy다. --- Dockerfile | 17 +++++++++++++---- docker-compose.dev-stack.yml | 2 +- scripts/generate-nginx-config.ts | 5 +++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 67e39ce..5851aef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -68,7 +68,14 @@ RUN corepack pnpm build \ # --------------------------------------------------------------------------- # runtime # --------------------------------------------------------------------------- -FROM nginx@sha256:65645c7bb6a0661892a8b03b89d0743208a18dd2f3f17a54ef4b76fb8e2f2a10 AS runtime +# Unprivileged NGINX 1.27.5 on Alpine, the same release and distribution as the +# previous nginx base: the master runs as uid 101 and listens on 8080, so the +# container needs no root and no capability. Kubernetes runs it with +# runAsNonRoot and capabilities drop ALL (deploy/production/frontend-nonroot). +FROM nginxinc/nginx-unprivileged@sha256:65e3e85dbaed8ba248841d9d58a899b6197106c23cb0ff1a132b7bfe0547e4c0 AS runtime + +# The file edits below need root; the image drops back to uid 101 at the end. +USER root # Replaces the packaged default server block; the generated file is the whole # server definition, including the BFF proxy locations. @@ -81,16 +88,18 @@ COPY --from=build /src/dist/ /usr/share/nginx/html/ # not a page either. RUN rm -f /usr/share/nginx/html/nginx.conf /usr/share/nginx/html/server.mjs \ && rm -rf /usr/share/nginx/html/.vite \ - # The build writes config.json 0600, which nginx (running as `nginx`) cannot + # The build writes config.json 0600, which nginx (running as uid 101) cannot # read — the container came up healthy and answered 403 for the one file the # SPA needs before it can boot. Normalise what is served to world-readable. && chmod -R a+rX /usr/share/nginx/html -EXPOSE 80 +USER 101 + +EXPOSE 8080 # No `nginx -t` here: proxy_pass names are resolved when the config loads, and # `backend`/`keycloak` only exist on the compose network. The container's own # startup is the check, and it fails loudly. HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ - CMD wget --no-verbose --tries=1 --spider http://127.0.0.1${VITE_ROUTER_BASE_PATH:-/}config.json || exit 1 + CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:8080${VITE_ROUTER_BASE_PATH:-/}config.json || exit 1 diff --git a/docker-compose.dev-stack.yml b/docker-compose.dev-stack.yml index 9e11040..780dd53 100644 --- a/docker-compose.dev-stack.yml +++ b/docker-compose.dev-stack.yml @@ -163,7 +163,7 @@ services: - "cp /etc/nginx/tls/server.crt /tls-public/server.crt && exec nginx -g 'daemon off;'" ports: - - "${PUBLIC_HTTP_PORT:-8088}:80" + - "${PUBLIC_HTTP_PORT:-8088}:8080" - "${PUBLIC_PORT:-8443}:443" depends_on: backend: { condition: service_started } diff --git a/scripts/generate-nginx-config.ts b/scripts/generate-nginx-config.ts index f17cd89..3b27e2d 100644 --- a/scripts/generate-nginx-config.ts +++ b/scripts/generate-nginx-config.ts @@ -136,10 +136,11 @@ ${secure} # config/hosting/cache-policy.json # # Plain HTTP on purpose: the edge terminates TLS and this container is only ever -# reached from inside the deployment network. +# reached from inside the deployment network. 8080, not 80: the runtime image is +# unprivileged NGINX, which cannot bind a port below 1024 without a capability. server { - listen 80; + listen 8080; server_name _; root /usr/share/nginx/html; index index.html; From 8c93cb05bc1c5f9dfa2a090b4a980a6e57ebd0dd Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 08:21:49 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat(deploy):=20frontend=EB=A5=BC=20non-roo?= =?UTF-8?q?t=EB=A1=9C=20=EB=8F=8C=EB=A6=AC=EB=8A=94=20component=EB=A5=BC?= =?UTF-8?q?=20=EB=91=94=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 비특권 nginx 이미지는 uid 101로 8080을 연다. 지금 운영 매니페스트는 pod 포트 80, probe 80, root를 전제로 한다. deploy/production/frontend-nonroot에 Kustomize component를 둔다. 켜면 frontend Deployment가 containerPort·probe 8080, runAsNonRoot·uid/gid 101, capabilities drop ALL이 되고, Service targetPort가 8080, Traefik → frontend NetworkPolicy 포트가 8080이 된다. Service port는 80 그대로라 Ingress는 바뀌지 않는다. ports 목록은 containerPort로 합쳐져 80이 남으므로 $patch: replace로 바꾼다. 기본 경로에는 넣지 않는다. 지금 운영 이미지 7a4e201은 root로 80을 열어, 켜면 probe가 모두 실패한다. README는 이미지를 비특권 Dockerfile이 들어간 commit에서 다시 만들고, 운영에만 있는 로컬 4커밋도 그 commit에 들어가 있어야 한다고 적는다. 그 이미지로 태그를 바꾸는 같은 커밋에서 켠다. 위험·한계: 운영 frontend의 source(7a4e201)가 Gitea에 없어서, 지금 main에서 이미지를 만들면 운영에만 있는 4커밋이 빠진다. 아직 적용하지 않았다. 검증: component를 켠 렌더에서 pod 포트·probe가 8080, Service targetPort 8080, NetworkPolicy 8080, securityContext가 위와 같다. kubectl apply --dry-run=server는 통과하고, frontend의 PSA restricted 경고가 사라졌다. --- deploy/production/README.md | 37 +++++++++++++++++-- .../frontend-deployment-patch.yaml | 35 ++++++++++++++++++ .../frontend-network-policy-patch.yaml | 20 ++++++++++ .../frontend-service-patch.yaml | 12 ++++++ .../frontend-nonroot/kustomization.yaml | 15 ++++++++ 5 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 deploy/production/frontend-nonroot/frontend-deployment-patch.yaml create mode 100644 deploy/production/frontend-nonroot/frontend-network-policy-patch.yaml create mode 100644 deploy/production/frontend-nonroot/frontend-service-patch.yaml create mode 100644 deploy/production/frontend-nonroot/kustomization.yaml diff --git a/deploy/production/README.md b/deploy/production/README.md index 29b5bbf..4935f60 100644 --- a/deploy/production/README.md +++ b/deploy/production/README.md @@ -26,9 +26,8 @@ Git 밖에서 만든다. 레지스트리에서 다시 받는다. - 세 파드 모두 `priorityClassName: platform-critical`(platform-core `services/priority-classes`)이고 seccomp는 `RuntimeDefault`다. -- **frontend non-root는 남은 일이다.** nginx 공식 이미지는 root로 시작해 80번을 열고 worker를 - `nginx`로 내린다. 권한을 버리거나 non-root로 돌리려면 이미지를 비특권 nginx(8080)로 바꾸고 Service - `targetPort`와 probe를 같이 바꿔야 한다. +- frontend non-root는 `frontend-nonroot/` component로 둔다(아래). 지금 운영 이미지 `7a4e201`은 root로 + 80번을 열기 때문에 아직 켜지 않는다. ## 적용 @@ -124,3 +123,35 @@ curl -s -o /dev/null -w '%{http_code}\n' https://hyeonworks.com/api/v1/public/ho 되돌리기: `kubectl -n hyeonworks-prod rollout undo deploy/techlog-backend`. 그다음 `components`를 빼는 커밋을 만든다. ConfigMap `techlog-backend-prod-env`는 남아 있어도 해가 없다. + +## frontend non-root (`frontend-nonroot/`, 아직 켜지 않음) + +이 repo의 `Dockerfile`은 runtime을 `nginxinc/nginx-unprivileged:1.27.5-alpine`(예전 base와 같은 NGINX 판·Alpine)으로 +만든다. master가 uid 101로 돌고 8080을 연다(`scripts/generate-nginx-config.ts`). `frontend-nonroot/` +component는 그 이미지에 맞춰 다음을 바꾼다. + +| 객체 | 바뀌는 것 | +| --- | --- | +| Deployment `techlog-frontend` | `containerPort` 8080, liveness·readiness probe 8080, `runAsNonRoot`·uid/gid 101, `drop: [ALL]` | +| Service `techlog-frontend` | `targetPort` 80 → 8080(`port`는 80 그대로라 Ingress는 안 바뀐다) | +| NetworkPolicy `techlog-frontend-ingress` | Traefik → pod 포트 80 → 8080 | + +### 켜는 순서 + +1. 이 Dockerfile이 들어간 commit에서 frontend 이미지를 만든다. **운영 `7a4e201`에만 있는 커밋(로컬 + 브랜치 `chore/template-sync-v0.2.0`, main보다 4개 앞)이 그 commit에 들어 있어야 한다.** 빠지면 그 + 4개(다크 모드 글씨, 2KB 넘는 본문 저장, 제목 문구, 템플릿 동기화)가 운영에서 되돌아간다. +2. 그 이미지를 레지스트리에 push한다(위 「적용」의 전제 1과 같은 방법). +3. 한 커밋에서 `frontend-deployment.yaml`의 이미지 태그를 새 태그로 바꾸고, `kustomization.yaml`에 + `components: [frontend-nonroot]`를 더한다(`prod-profile`이 켜져 있으면 목록에 같이 둔다). +4. 적용과 확인: + + ```bash + kubectl diff -k deploy/production + kubectl apply -k deploy/production + kubectl -n hyeonworks-prod rollout status deploy/techlog-frontend + curl -s -o /dev/null -w '%{http_code}\n' https://hyeonworks.com/config.json + ``` + +되돌리기: `kubectl -n hyeonworks-prod rollout undo deploy/techlog-frontend`. 그다음 3의 커밋을 되돌리는 +커밋을 만들고 다시 적용한다(Service와 NetworkPolicy 포트도 80으로 돌아가야 한다). diff --git a/deploy/production/frontend-nonroot/frontend-deployment-patch.yaml b/deploy/production/frontend-nonroot/frontend-deployment-patch.yaml new file mode 100644 index 0000000..9f10c17 --- /dev/null +++ b/deploy/production/frontend-nonroot/frontend-deployment-patch.yaml @@ -0,0 +1,35 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: techlog-frontend +spec: + template: + spec: + securityContext: + runAsNonRoot: true + runAsUser: 101 + runAsGroup: 101 + seccompProfile: + type: RuntimeDefault + containers: + - name: frontend + # replace, not merge: ports merge on containerPort, which would + # keep the old 80 entry next to 8080 under the same name. + ports: + - $patch: replace + - name: http + containerPort: 8080 + protocol: TCP + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + livenessProbe: + httpGet: + path: /config.json + port: 8080 + readinessProbe: + httpGet: + path: /config.json + port: 8080 diff --git a/deploy/production/frontend-nonroot/frontend-network-policy-patch.yaml b/deploy/production/frontend-nonroot/frontend-network-policy-patch.yaml new file mode 100644 index 0000000..08111e0 --- /dev/null +++ b/deploy/production/frontend-nonroot/frontend-network-policy-patch.yaml @@ -0,0 +1,20 @@ +# NetworkPolicy ports are pod ports: Traefik now reaches the pod on 8080. +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: techlog-frontend-ingress +spec: + podSelector: + matchLabels: + app: techlog-frontend + ingress: + - from: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: kube-system + podSelector: + matchLabels: + app.kubernetes.io/name: traefik + ports: + - protocol: TCP + port: 8080 diff --git a/deploy/production/frontend-nonroot/frontend-service-patch.yaml b/deploy/production/frontend-nonroot/frontend-service-patch.yaml new file mode 100644 index 0000000..ab3c0b2 --- /dev/null +++ b/deploy/production/frontend-nonroot/frontend-service-patch.yaml @@ -0,0 +1,12 @@ +# The Service keeps port 80, so the Ingress and anything that calls +# techlog-frontend:80 do not change; only the pod side moves to 8080. +apiVersion: v1 +kind: Service +metadata: + name: techlog-frontend +spec: + ports: + - name: http + port: 80 + protocol: TCP + targetPort: 8080 diff --git a/deploy/production/frontend-nonroot/kustomization.yaml b/deploy/production/frontend-nonroot/kustomization.yaml new file mode 100644 index 0000000..80e3408 --- /dev/null +++ b/deploy/production/frontend-nonroot/kustomization.yaml @@ -0,0 +1,15 @@ +apiVersion: kustomize.config.k8s.io/v1alpha1 +kind: Component + +# Opt-in: runs techlog-frontend as uid 101 on port 8080 with every capability +# dropped. Needs an image built from a commit that has the unprivileged +# Dockerfile; the 7a4e201 image listens on 80 as root and would fail every +# probe. Enable it in the same commit that moves the frontend image tag, by +# adding +# components: +# - frontend-nonroot +# to ../kustomization.yaml. See ../README.md ("frontend non-root"). +patches: + - path: frontend-deployment-patch.yaml + - path: frontend-service-patch.yaml + - path: frontend-network-policy-patch.yaml