From 8c93cb05bc1c5f9dfa2a090b4a980a6e57ebd0dd Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 08:21:49 +0900 Subject: [PATCH] =?UTF-8?q?feat(deploy):=20frontend=EB=A5=BC=20non-root?= =?UTF-8?q?=EB=A1=9C=20=EB=8F=8C=EB=A6=AC=EB=8A=94=20component=EB=A5=BC=20?= =?UTF-8?q?=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