implementing-kubernetes-pod-security-standards について
ワークロードに適用する Pod Security Standards の選定と実装を支援する Kubernetes セキュリティスキルです。ポリシーレベルの評価、違反の特定、準拠した Pod 仕様の作成、段階的な適用や PodSecurityPolicy 移行の計画を扱います。
主な機能
- 01Privileged、Baseline、Restricted の比較
- 02既存ワークロードの違反評価
- 03準拠した Pod 仕様の作成
- 04段階的なポリシー移行の計画
- 05サーバー側ドライランによる適用テスト
活用シーン
- 01各 Namespace のセキュリティレベル選定
- 02Restricted 適用で失敗する可能性があるワークロードの特定
- 03Restricted 準拠に向けた Pod セキュリティコンテキストの変更
- 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 と一緒にインストールされる参考資料やスクリプト。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.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 マウントを使用する必要があります |
| Seccomp | RuntimeDefault または Localhost | seccomp プロファイルを指定する必要があります(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
参考資料
コメント
Markdown 対応