mcpField
I
implementing-kubernetes-pod-security-standards

implementing-kubernetes-pod-security-standards

github.com/mukul975セキュリティ35.2k81 回インストール8 週間前に更新
リポジトリ

implementing-kubernetes-pod-security-standards について

ワークロードに適用する Pod Security Standards の選定と実装を支援する Kubernetes セキュリティスキルです。ポリシーレベルの評価、違反の特定、準拠した Pod 仕様の作成、段階的な適用や PodSecurityPolicy 移行の計画を扱います。

主な機能

  1. 01Privileged、Baseline、Restricted の比較
  2. 02既存ワークロードの違反評価
  3. 03準拠した Pod 仕様の作成
  4. 04段階的なポリシー移行の計画
  5. 05サーバー側ドライランによる適用テスト

活用シーン

  1. 01各 Namespace のセキュリティレベル選定
  2. 02Restricted 適用で失敗する可能性があるワークロードの特定
  3. 03Restricted 準拠に向けた Pod セキュリティコンテキストの変更
  4. 04PodSecurityPolicy 移行の準備

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill implementing-kubernetes-pod-security-standards

インストール後、Claude Code で/implementing-kubernetes-pod-security-standardsを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

LICENSE
assets/
template.md
references/
api-reference.md
standards.md
workflows.md
scripts/
agent.py
process.py

詳細

Kubernetes Pod Security Standards の実装

概要

Pod Security Standards(PSS)は、Privileged、Baseline、Restricted の 3 つのセキュリティポリシーレベルを定義します。これらは、Kubernetes 1.25 以降に組み込まれている Pod Security Admission(PSA)コントローラーによって適用されます。PSA は非推奨となった PodSecurityPolicy に代わるもので、namespace レベルで enforce、audit、warn の 3 つのモードによる適用を提供します。

使用する場面

  • namespace またはワークロードを Privileged、Baseline、Restricted のどのレベルにするか判断するとき
  • Restricted を現在適用した場合に拒否される既存のワークロードを監査するとき
  • 「Restricted の要件を満たす」ための要件を、具体的な securityContext の変更に置き換えるとき
  • PodSecurityPolicy の移行を計画し、問題が起きる前に影響を予測するとき
  • Pod のセキュリティ態勢をコンプライアンス管理策(NIST PR.PS-01、CIS Kubernetes)に対応付けるとき

このスキルの対象外: これらのプロファイルを適用するコントローラーの設定(namespace ラベル、AdmissionConfiguration、例外設定、PSA によって拒否された Pod のデバッグ)。その場合は implementing-pod-security-admission-controller を使用してください。

前提条件

  • Kubernetes クラスター 1.25 以降(PSA GA)
  • cluster-admin アクセスで設定された kubectl
  • Linux のケイパビリティとセキュリティコンテキストに関する知識

基本概念

3 つのセキュリティプロファイル
プロファイル目的制限
Privileged制限なし、システムワークロード向けなし
Baseline既知の権限昇格を防止hostNetwork、hostPID、hostIPC、特権コンテナー、危険なケイパビリティは禁止
Restricted強化されたベストプラクティス非 root、すべてのケイパビリティを削除、seccomp 必須、読み取り専用 rootfs を推奨
3 つの適用モード
モード動作
enforceポリシーに違反する Pod を拒否します
audit違反を監査ログに記録しますが、Pod は許可します
warnユーザーに警告を返しますが、Pod は許可します

ワークフロー

ステップ 1:PSA 用の namespace ラベルを設定する
# Restricted namespace - production workloads
apiVersion: v1
kind: Namespace
metadata:
  name: production
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/enforce-version: latest
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/audit-version: latest
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/warn-version: latest
# Baseline namespace - general workloads
apiVersion: v1
kind: Namespace
metadata:
  name: staging
  labels:
    pod-security.kubernetes.io/enforce: baseline
    pod-security.kubernetes.io/enforce-version: latest
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/audit-version: latest
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/warn-version: latest
# Privileged namespace - system components only
apiVersion: v1
kind: Namespace
metadata:
  name: kube-system
  labels:
    pod-security.kubernetes.io/enforce: privileged
    pod-security.kubernetes.io/enforce-version: latest
ステップ 2:既存の namespace にラベルを適用する
# Apply restricted enforcement to production
kubectl label namespace production \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/audit=restricted \
  pod-security.kubernetes.io/warn=restricted \
  --overwrite

# Apply baseline to staging with restricted warnings
kubectl label namespace staging \
  pod-security.kubernetes.io/enforce=baseline \
  pod-security.kubernetes.io/audit=restricted \
  pod-security.kubernetes.io/warn=restricted \
  --overwrite

# Check labels on all namespaces
kubectl get namespaces -L pod-security.kubernetes.io/enforce
ステップ 3:準拠した Pod 仕様を作成する
# Restricted-compliant deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: secure-app
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: secure-app
  template:
    metadata:
      labels:
        app: secure-app
    spec:
      automountServiceAccountToken: false
      securityContext:
        runAsNonRoot: true
        runAsUser: 65534
        runAsGroup: 65534
        fsGroup: 65534
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: app
          image: myregistry.com/myapp:v1.0.0@sha256:abc123
          ports:
            - containerPort: 8080
              protocol: TCP
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop:
                - ALL
            runAsNonRoot: true
            runAsUser: 65534
          resources:
            requests:
              memory: "64Mi"
              cpu: "100m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          volumeMounts:
            - name: tmp
              mountPath: /tmp
            - name: cache
              mountPath: /var/cache
      volumes:
        - name: tmp
          emptyDir:
            sizeLimit: 100Mi
        - name: cache
          emptyDir:
            sizeLimit: 50Mi
ステップ 4:段階的な移行戦略
# Phase 1: Audit mode - discover violations without blocking
kubectl label namespace my-namespace \
  pod-security.kubernetes.io/audit=restricted \
  pod-security.kubernetes.io/warn=restricted

# Check audit logs for violations
kubectl logs -n kube-system -l component=kube-apiserver | grep "pod-security"

# Phase 2: Enforce baseline, warn on restricted
kubectl label namespace my-namespace \
  pod-security.kubernetes.io/enforce=baseline \
  pod-security.kubernetes.io/warn=restricted \
  --overwrite

# Phase 3: Full restricted enforcement
kubectl label namespace my-namespace \
  pod-security.kubernetes.io/enforce=restricted \
  --overwrite
ステップ 5:ドライランで適用をテストする
# Test what would happen with restricted enforcement
kubectl label --dry-run=server --overwrite namespace my-namespace \
  pod-security.kubernetes.io/enforce=restricted

# Example output:
# Warning: existing pods in namespace "my-namespace" violate the new
# PodSecurity enforce level "restricted:latest"
# Warning: nginx-xxx: allowPrivilegeEscalation != false,
#   unrestricted capabilities, runAsNonRoot != true, seccompProfile

Baseline プロファイルの制限

制御項目Restricted要件
HostProcess設定してはならないPod で Windows HostProcess を使用できません
Host 名前空間設定してはならないhostNetwork、hostPID、hostIPC は使用できません
特権設定してはならないprivileged: true は使用できません
ケイパビリティBaseline のリストのみNET_BIND_SERVICE のみ。Restricted ではすべて削除
HostPath ボリューム使用してはならないhostPath ボリュームのマウントは使用できません
ホストポート使用してはならないコンテナー仕様で hostPort は使用できません
AppArmorデフォルト/ランタイムunconfined に設定できません
SELinux制限されたタイプcontainer_t、container_init_t、container_kvm_t のみ
/proc マウントタイプデフォルトのみDefault の proc マウントを使用する必要があります
SeccompRuntimeDefault または Localhostseccomp プロファイルを指定する必要があります(Restricted)
Sysctls安全なもののみ安全な sysctls に限定

検証コマンド

# Verify namespace labels
kubectl get ns --show-labels | grep pod-security

# Test pod creation against policy
kubectl run test-pod --image=nginx --namespace=production --dry-run=server

# Check for violations in audit logs
kubectl get events --field-selector reason=FailedCreate -A

# Scan with Kubescape for PSS compliance
kubescape scan framework nsa --namespace production

参考資料

コメント