Skip to content

[] Add helm/templates/networkpolicy.yaml (DB/Redis/MinIO traffic restriction) and helm/templates/servicemonitor.yaml (Prometheus scraping)#306

Merged
zoetaka38 merged 2 commits into
mainfrom
feature/019f0d78fbdd-019f0d78fbdd
Jun 29, 2026
Merged

[] Add helm/templates/networkpolicy.yaml (DB/Redis/MinIO traffic restriction) and helm/templates/servicemonitor.yaml (Prometheus scraping)#306
zoetaka38 merged 2 commits into
mainfrom
feature/019f0d78fbdd-019f0d78fbdd

Conversation

@red-codens

@red-codens red-codens Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Type of Change

  • feat — new feature
  • fix — bug fix
  • chore — tooling / maintenance
  • docs — documentation only
  • refactor — code change that neither fixes a bug nor adds a feature
  • test — adding or updating tests
  • ci — CI/CD configuration

Testing

Checklist

  • Tests pass locally
  • Lint clean (task lint)
  • Commit messages follow Conventional Commits

Task Goal

Target Repository

Corevice/open-git
All work for this task belongs in this repository. Do the implementation inside this repository's directory in the workspace; do not search or edit other repositories unless this task explicitly requires a cross-repo change.

Context

NetworkPolicy limits lateral movement: only backend pods can reach PostgreSQL/Redis/MinIO; all other ingress to DB pods is denied. ServiceMonitor enables Prometheus Operator scraping of the backend metrics endpoint, both optional via values flags.

Steps

  1. Create helm/templates/networkpolicy.yaml{{- if .Values.networkPolicy.enabled }}; create one NetworkPolicy per protected component: (a) backend-egress policy on backend pods allowing egress to port 5432, 6379, 9000, and DNS 53; (b) database-ingress policy on postgresql pods allowing ingress only from backend and worker pods; use podSelector.matchLabels with component labels; set policyTypes: [Ingress, Egress] on backend; policyTypes: [Ingress] on db policy
  2. Create helm/templates/servicemonitor.yaml{{- if .Values.serviceMonitor.enabled }}; apiVersion: monitoring.coreos.com/v1; kind: ServiceMonitor; spec.selector.matchLabels: { app.kubernetes.io/name: {{ include "open-git.name" . }}, app.kubernetes.io/component: backend }; spec.endpoints: [{port: http, path: /metrics, interval: 30s}]; spec.namespaceSelector.matchNames: [{{ .Release.Namespace }}]

Verification Checklist

  • helm/templates/networkpolicy.yaml exists
  • NetworkPolicy conditional on .Values.networkPolicy.enabled
  • NetworkPolicy allows egress to port 5432 (PostgreSQL)
  • NetworkPolicy allows egress to port 6379 (Redis)
  • NetworkPolicy allows egress to port 9000 (MinIO)
  • NetworkPolicy allows DNS egress (port 53)
  • helm/templates/servicemonitor.yaml exists
  • ServiceMonitor conditional on .Values.serviceMonitor.enabled
  • ServiceMonitor uses monitoring.coreos.com/v1 apiVersion
  • ServiceMonitor endpoint

Changes Made

Hel
m chart の既
存テンプレ
ートとラベ
ル規
約を確認し
、Network
Policy と ServiceMonitor

追加
します

Network
Policy と
ServiceMonitor のテ
ンプレートを
作成
します

helm /templates/networkpolicy.yaml
helm/templates/s ervicemonitor .yaml を追加
しました。

**
NetworkPolicy** ( networkPolicy .enabled
true のとき):

  • backend-
    egress

    — backend Pod
    向け。
    Postgre
    SQL (
    5432)、Redis
    (6379)、
    MinIO (900
    0)、DNS (
    53/T
    CP+
    UDP) への
    egress を
    許可。 policyTypes: [ Ingress, Egress ]
  • **
    postgresql-ingress**
    — Postgre
    SQL Pod
    向け。backend
    / worker Pod
    からの
    5432 の
    み ingress を許
    可。policy Types: [Ingress ]

**
Service
Monitor** ( serviceMonitor.enabled
が true のとき
):

  • backend Service

    / metrics (
    30s interval
    ) でスク
    レイプ

  • monitoring .coreos.com/v 1
    同一
    namespace の

    対象


存 chart
のラ
ベル規
約(open -git.selectorLabels open -git.full name
)に
合わせてい
ます。

[P
URPLE-M
ODIFIED-FILES
]
helm/templates
/networkpolicy.yaml

helm/templates/serv
icemonitor.yaml

[/PUR
PLE-MOD
IFIED-FILES]

Task ID: 019f0d78-fbdd-75fc-9e85-e1bc10115ee5
Generated by Purple Codens

…restriction) and helm/templates/servicemonitor.yaml (Prometheus scraping)

@orange-codens orange-codens Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍊 Orange Codens レビュー

重大な問題あり(マージ前に修正必須): networkpolicy.yaml の backend-egress ポリシーで policyTypes: [Ingress, Egress] を宣言しながら ingress: ルールを省略しており、Kubernetes の仕様上 backend Pod へのすべての受信トラフィックが遮断されます(即座のサービス停止)。また、PR タイトルに「Redis/MinIO traffic restriction」と謳っているにもかかわらず、Redis と MinIO への ingress 制限ポリシーが未実装です。ServiceMonitor 自体は概ね問題ありませんが、values.yaml のデフォルト値追加が diff に含まれていない点も確認が必要です。

🔒 セキュリティ: 今回の diff はセキュリティ上の致命的な脆弱性(認証 bypass・SQLi 等)は含まれていませんが、NetworkPolicy の設計に複数の懸念点があります。最も影響度が高いのは「backend NetworkPolicy で policyTypes: Ingress を宣言しつつ ingress ルールを省略している点」と「DNS egress に宛先制限がない点」で、前者はサービス断・後者は侵害時の横移動リスクにつながります。また Redis/MinIO への対応する Ingress 制限ポリシーが欠如しており、データストア保護が不完全です。ServiceMonitor の /metrics 認証はアプリ側の実装次第ですが、明示的な設定を推奨します。

🔴 critical: 1 / 🟠 high: 2 / 🟡 medium: 4 / 🔵 low: 4

その他の指摘 (9 件)
  • 🟠 [high] helm/templates/networkpolicy.yaml:17 — egress to.podSelectornamespaceSelector が指定されておらず、同一 namespace 限定にならない可能性がある (confidence: 0.65, code.kubernetes.networkpolicy_namespace_isolation)
  • 🟡 [medium] helm/templates/networkpolicy.yaml:40 — DNS egress ルールに宛先 podSelector/ipBlock がなく任意ホストへの DNS 問い合わせを許可 (confidence: 0.85, sec.sast.authz.networkpolicy_dns_unrestricted_destination)
  • 🟡 [medium] helm/templates/networkpolicy.yaml:13 — backend Pod への Ingress ルールが未定義のまま policyTypes に Ingress を宣言 (confidence: 0.82, sec.sast.authz.networkpolicy_ingress_missing)
  • 🟡 [medium] helm/templates/networkpolicy.yaml:60from リスト内の複数 podSelector は OR 条件だが、意図が AND 条件と混同されやすい (confidence: 0.72, code.kubernetes.networkpolicy_ingress_or_operator)
  • 🟡 [medium] helm/templates/networkpolicy.yaml:1.Values.networkPolicy.enabled.Values.serviceMonitor.enabled のデフォルト値が diff に存在しない (confidence: 0.55, maintainability.helm.missing_values_defaults)
  • 🔵 [low] helm/templates/networkpolicy.yaml:46 — Redis および MinIO Pod への Ingress 制限が NetworkPolicy に存在しない (confidence: 0.78, sec.sast.authz.networkpolicy_redis_ingress_missing)
  • 🔵 [low] helm/templates/servicemonitor.yaml:13 — ServiceMonitor の scrapeTimeout が未指定でデフォルト依存になっている (confidence: 0.70, maintainability.helm.servicemonitor_no_scrape_timeout)
  • 🔵 [low] helm/templates/networkpolicy.yaml:60 — postgresql-ingress の from ルールが AND ではなく OR セマンティクスになっている (confidence: 0.65, sec.sast.authz.networkpolicy_ingress_or_semantics)
  • 🔵 [low] helm/templates/servicemonitor.yaml:13 — /metrics エンドポイントへの認証設定が ServiceMonitor に存在しない (confidence: 0.60, sec.sast.authz.servicemonitor_metrics_unauthenticated)

head: 956e110 | Orange Codens (P1)

app.kubernetes.io/component: backend
policyTypes:
- Ingress
- Egress

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 [critical] backend-egress ポリシーに ingress ルールが未定義のまま policyTypes: Ingress を宣言している

該当箇所:

  policyTypes:
    - Ingress
    - Egress

policyTypesIngress を含めているにもかかわらず、spec.ingress フィールドが一切定義されていません。Kubernetes の NetworkPolicy の仕様では、policyTypesIngress を列挙して ingress ルールを省略した場合、その Pod へのすべての ingress トラフィックが拒否 されます(空リスト扱い)。

backend Pod 宛ての受信トラフィック(外部からの HTTP リクエスト、Ingress Controller からの通信など)が全断するため、本番では即座にサービス停止になります。

意図が「egress のみ制限する」なら policyTypes: [Egress] のみにすべきです。もし「ingress も制限したい」なら対応する ingress: ルールを明示的に追加してください。

# 修正案1: egress のみ制御する場合
  policyTypes:
    - Egress

# 修正案2: ingress も制御する場合 (例: Ingress Controller からの 8080 を許可)
  policyTypes:
    - Ingress
    - Egress
  ingress:
    - ports:
        - protocol: TCP
          port: 8080
@@ -13,6 +13,5 @@
   policyTypes:
-    - Ingress
     - Egress

code.kubernetes.networkpolicy_ingress_missing | confidence: 0.97

ports:
- protocol: TCP
port: 5432
{{- end }}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 [high] Redis・MinIO の ingress 制限 NetworkPolicy が存在しない

該当箇所:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: {{ include "open-git.fullname" . }}-postgresql-ingress

タスク要件および PR 説明には「DB/Redis/MinIO traffic restriction」と明記されていますが、実装では PostgreSQL (-postgresql-ingress) の NetworkPolicy のみ作成されており、Redis (6379) および MinIO (9000) への ingress 制限ポリシーが存在しません

backend からの egress は許可されていますが、Redis/MinIO Pod 側で ingress を制限しないと、他の Pod (攻撃者が掌握した Pod 等) からも直接アクセス可能になります。

Redis・MinIO に対しても同様の ingress ポリシーを追加することを推奨します:

---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: {{ include "open-git.fullname" . }}-redis-ingress
spec:
  podSelector:
    matchLabels:
      app.kubernetes.io/name: redis
      app.kubernetes.io/instance: {{ .Release.Name }}
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              {{- include "open-git.selectorLabels" . | nindent 14 }}
              app.kubernetes.io/component: backend
      ports:
        - protocol: TCP
          port: 6379

code.kubernetes.networkpolicy_redis_ingress_missing | confidence: 0.92

@zoetaka38
zoetaka38 merged commit 3317080 into main Jun 29, 2026
12 of 16 checks passed

@orange-codens orange-codens Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍊 Orange Codens レビュー

critical: backend NetworkPolicy が policyTypesIngress を含む一方で ingress ルール未定義のため、backend への全 Ingress が拒否されます。修正 (Ingress ルール追加、または egress のみに policyTypes を限定) がマージ前に必須です。加えて Task Context 対比では Redis/MinIO の ingress 制限が未実装です。ServiceMonitor は Task Goal の指定どおりに見えます。

🔒 セキュリティ: 本 PR は PostgreSQL 向け ingress と backend 向け egress によるネットワーク分離を追加する方向性は妥当ですが、Redis / MinIO の ingress 制御欠如と worker egress 未制限により分離が不完全です。backend NetworkPolicy の Ingress 宣言と ingress ルール不足は Kubernetes セマンティクス上、意図しない全 ingress 拒否を招く可能性があります。ServiceMonitor は一般的な構成ですが、metrics の認証・暗号化強化余地があります。

🔴 critical: 1 / 🟡 medium: 3 / 🔵 low: 2 / ⚪ info: 1

その他の指摘 (6 件)
  • 🟡 [medium] helm/templates/networkpolicy.yaml:48 — Redis/MinIO の ingress 制限が未実装 (confidence: 0.82, code.networkpolicy.incomplete-coverage)
  • 🟡 [medium] helm/templates/networkpolicy.yaml:26 — Redis/MinIO に ingress NetworkPolicy がなく横方向移動を完全には防げない (confidence: 0.82, sec.sast.authz.network_segmentation_gap)
  • 🟡 [medium] helm/templates/networkpolicy.yaml:13 — backend NetworkPolicy が Ingress を policyTypes に含むが allow ルールがなく全 ingress を拒否する (confidence: 0.78, sec.sast.authz.networkpolicy_ingress_deny)
  • 🔵 [low] helm/templates/networkpolicy.yaml:67 — worker を PostgreSQL ingress に許可しているが worker 向け egress 制限がない (confidence: 0.72, sec.sast.authz.worker_egress_unrestricted)
  • 🔵 [low] helm/templates/servicemonitor.yaml:13 — ServiceMonitor が /metrics を平文 HTTP で認証なしにスクレイプする構成 (confidence: 0.58, sec.sast.info_disclosure.metrics_exposure)
  • [info] helm/templates/networkpolicy.yaml:1 — Helm template / values の検証が diff に含まれない (confidence: 0.55, maintainability.test.missing-helm-validation)

head: 4837fc4 | Orange Codens (P1)

policyTypes:
- Ingress
- Egress
egress:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 [critical] backend NetworkPolicy が Ingress を全拒否する

policyTypesIngress を含めたうえで ingress ルールを定義していないため、Kubernetes NetworkPolicy の仕様上、backend Pod への すべての Ingress トラフィックが拒否 されます。Ingress Controller・Service・liveness/readiness probe 等から backend に到達できなくなり、本番障害につながります。

Task Goal では policyTypes: [Ingress, Egress] が指定されていますが、Ingress ルール未指定のままでは default-deny になります。

該当箇所:

  policyTypes:
    - Ingress
    - Egress
  egress:
@@ -13,4 +13,3 @@
   policyTypes:
-    - Ingress
     - Egress
   egress:

code.networkpolicy.default-deny-ingress | confidence: 0.95

@zoetaka38
zoetaka38 deleted the feature/019f0d78fbdd-019f0d78fbdd branch July 2, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant