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/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 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 1e05396..7ee5773 100644 --- a/scripts/generate-nginx-config.ts +++ b/scripts/generate-nginx-config.ts @@ -131,10 +131,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;