mcpField
K
k8s-security-policies

k8s-security-policies

github.com/oimiragieoセキュリティ4452 回インストール17 週間前に更新
リポジトリ

k8s-security-policies について

k8s-security-policiesは、Kubernetesクラスターのセキュリティ設定を支援するガイド型スキルです。ネットワーク分離、Pod Security Standards、RBACを扱い、ポリシー例とテンプレートを含みます。

主な機能

  1. 01Pod Security Standardsの設定
  2. 02NetworkPolicyルールの作成
  3. 03最小権限RBACの設定
  4. 04Gatekeeperによるポリシー適用
  5. 05IstioのmTLSと認可の設定

活用シーン

  1. 01クラスターのネットワーク分離
  2. 02NamespaceのPodセキュリティレベル設定
  3. 03サービスアカウントへの最小権限付与
  4. 04コンプライアンス向けポリシー策定

インストール

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

$npx skills add https://github.com/oimiragieo/agent-studio --skill k8s-security-policies

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

同梱ファイル

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

assets/
network-policy-template.yaml
pod-security-template.yaml
commands/
k8s-security-policies.md
hooks/
post-execute.cjs
pre-execute.cjs
references/
rbac-patterns.md
research-requirements.md
rules/
k8s-security-policies.md
schemas/
input.schema.json
output.schema.json
scripts/
main.cjs
templates/
implementation-template.md

詳細

Kubernetes セキュリティポリシー

Kubernetes で NetworkPolicy、PodSecurityPolicy、RBAC、Pod Security Standards を実装するための包括的なガイドです。

目的

ネットワークポリシー、Pod Security Standards、RBAC を使用して、Kubernetes クラスターに多層防御のセキュリティを実装します。

このスキルを使用する場面

  • ネットワークセグメンテーションを実装する
  • Pod Security Standards を設定する
  • 最小権限アクセスのために RBAC を設定する
  • コンプライアンス対応のセキュリティポリシーを作成する
  • アドミッション制御を実装する
  • マルチテナントクラスターを保護する

Pod Security Standards

1. Privileged(制限なし)
apiVersion: v1
kind: Namespace
metadata:
  name: privileged-ns
  labels:
    pod-security.kubernetes.io/enforce: privileged
    pod-security.kubernetes.io/audit: privileged
    pod-security.kubernetes.io/warn: privileged
2. Baseline(最小限の制限)
apiVersion: v1
kind: Namespace
metadata:
  name: baseline-ns
  labels:
    pod-security.kubernetes.io/enforce: baseline
    pod-security.kubernetes.io/audit: baseline
    pod-security.kubernetes.io/warn: baseline
3. Restricted(最も厳しい制限)
apiVersion: v1
kind: Namespace
metadata:
  name: restricted-ns
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/warn: restricted

ネットワークポリシー

すべてをデフォルトで拒否
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: production
spec:
  podSelector: {}
  policyTypes:
    - Ingress
    - Egress
フロントエンドからバックエンドへの通信を許可
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend-to-backend
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 8080
DNS を許可
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: production
spec:
  podSelector: {}
  policyTypes:
    - Egress
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              name: kube-system
      ports:
        - protocol: UDP
          port: 53

参照: assets/network-policy-template.yaml を参照してください。

RBAC の設定

Role(Namespace スコープ)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
  namespace: production
rules:
  - apiGroups: ['']
    resources: ['pods']
    verbs: ['get', 'watch', 'list']
ClusterRole(クラスター全体)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: secret-reader
rules:
  - apiGroups: ['']
    resources: ['secrets']
    verbs: ['get', 'watch', 'list']
RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: production
subjects:
  - kind: User
    name: jane
    apiGroup: rbac.authorization.k8s.io
  - kind: ServiceAccount
    name: default
    namespace: production
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

参照: references/rbac-patterns.md を参照してください。

Pod のセキュリティコンテキスト

制限付き Pod
apiVersion: v1
kind: Pod
metadata:
  name: secure-pod
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    fsGroup: 1000
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: app
      image: myapp:1.0
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true
        capabilities:
          drop:
            - ALL

OPA Gatekeeper によるポリシー適用

ConstraintTemplate
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
  name: k8srequiredlabels
spec:
  crd:
    spec:
      names:
        kind: K8sRequiredLabels
      validation:
        openAPIV3Schema:
          type: object
          properties:
            labels:
              type: array
              items:
                type: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8srequiredlabels
        violation[{"msg": msg, "details": {"missing_labels": missing}}] {
          provided := {label | input.review.object.metadata.labels[label]}
          required := {label | label := input.parameters.labels[_]}
          missing := required - provided
          count(missing) > 0
          msg := sprintf("missing required labels: %v", [missing])
        }
Constraint
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
  name: require-app-label
spec:
  match:
    kinds:
      - apiGroups: ['apps']
        kinds: ['Deployment']
  parameters:
    labels: ['app', 'environment']

サービスメッシュのセキュリティ(Istio)

PeerAuthentication(mTLS)
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: production
spec:
  mtls:
    mode: STRICT
AuthorizationPolicy
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: allow-frontend
  namespace: production
spec:
  selector:
    matchLabels:
      app: backend
  action: ALLOW
  rules:
    - from:
        - source:
            principals: ['cluster.local/ns/production/sa/frontend']

ベストプラクティス

  1. Namespace レベルで Pod Security Standards を実装する
  2. ネットワークセグメンテーションに Network Policies を使用する
  3. すべてのサービスアカウントに 最小権限の RBAC を適用する
  4. アドミッション制御を有効化する(OPA Gatekeeper/Kyverno)
  5. コンテナーを root 以外のユーザーとして実行する
  6. ルートファイルシステムを読み取り専用にする
  7. 必要な場合を除き、すべてのケイパビリティを削除する
  8. リソースクォータと制限範囲を実装する
  9. セキュリティイベントの 監査ログを有効化する
  10. イメージを 定期的にセキュリティスキャンする

コンプライアンスフレームワーク

CIS Kubernetes Benchmark
  • RBAC 認可を使用する
  • 監査ログを有効化する
  • Pod Security Standards を使用する
  • ネットワークポリシーを設定する
  • 保存時のシークレット暗号化を実装する
  • ノード認証を有効化する
NIST Cybersecurity Framework
  • 多層防御を実装する
  • ネットワークセグメンテーションを使用する
  • セキュリティ監視を設定する
  • アクセス制御を実装する
  • ログ記録と監視を有効化する

トラブルシューティング

NetworkPolicy が機能しない場合:

# Check if CNI supports NetworkPolicy
kubectl get nodes -o wide
kubectl describe networkpolicy <name>

RBAC の権限が拒否された場合:

# Check effective permissions
kubectl auth can-i list pods --as system:serviceaccount:default:my-sa
kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa

参照ファイル

  • assets/network-policy-template.yaml - ネットワークポリシーの例
  • assets/pod-security-template.yaml - Pod セキュリティポリシー
  • references/rbac-patterns.md - RBAC 設定パターン

関連スキル

  • k8s-manifest-generator - セキュアなマニフェストの作成に使用
  • gitops-workflow - ポリシーのデプロイ自動化に使用

メモリプロトコル(必須)

開始前:

cat C:\dev\projects\agent-studio\.claude\context\memory\learnings.md

完了後:

  • 新しいパターン → C:\dev\projects\agent-studio\.claude\context\memory\learnings.md
  • 見つかった問題 → C:\dev\projects\agent-studio\.claude\context\memory\issues.md
  • 決定事項 → C:\dev\projects\agent-studio\.claude\context\memory\decisions.md

中断されることを想定してください。メモリに記録されていなければ、実行されていないものとみなされます。

コメント