制約を使用した監査

Policy Controller の制約オブジェクトを使用すると、Kubernetes クラスタにポリシーを適用できます。ポリシーをテストするために、制約に適用アクションを追加できます。その後、制約オブジェクトとログで違反を表示できるようになります。

適用アクションの種類

適用アクションには、denydryrunwarn の 3 つがあります。

deny がデフォルトの適用アクションです。制約に適用アクションを追加しない場合でも、自動的に有効になります。deny を使用して、違反が発生したときに特定のクラスタ オペレーションが発生しないようにします。

dryrun を使用すると、トランザクションを積極的にブロックすることなく、ルール違反を監視できます。deny アクションを使用してアクティブな適用を有効にする前に、この制約が意図したとおりに機能しているかどうかをテストできます。この方法で制約をテストすると、不適切に構成された制約によって引き起こされる中断を防ぐことができます。

warndryrun に似ていますが、アドミッション時に発生する違反についての即時メッセージも示します。

新しい制約をテストしたり、プラットフォームのアップグレードなどの移行アクションを実行する場合は、適用アクションを deny から warn または dryrun に切り替えて、ポリシーが想定どおりに機能することをテストできるようにすることをおすすめします。

適用アクションの追加

enforcementAction: deny または enforcementAction: dryrun を制約に追加できます。

次の制約の例では、audit.yaml という名前で dryrun アクションを追加します。

#audit.yaml
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAllowedUsers
metadata:
  name: user-must-be-3333
spec:
  enforcementAction: dryrun
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    runAsUser:
      rule: MustRunAs
      ranges:
        - min: 3333
          max: 3333

制約を作成します。たとえば、kubectl apply -f を使用して適用します。

kubectl apply -f audit.yaml

監査結果の表示

監査された違反は Constraint オブジェクトに追加され、ログにも書き込まれます。アドミッション コントローラで拒否された違反は、ログに表示されません。

制約オブジェクトでの監査結果の表示

特定の制約の違反を確認するには、次のコマンドを実行して spec.status フィールドを表示します。

kubectl get constraint-kind constraint-name -o yaml

audit.yaml からの制約の出力を表示するには、次のコマンドを実行します。

kubectl get K8sPSPAllowedUsers user-must-be-3333 -o yaml

表示される出力は次のようになります。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAllowedUsers
metadata:
  creationTimestamp: "2020-05-22T01:34:22Z"
  generation: 1
  name: user-must-be-3333
  resourceVersion: "13351707"
  selfLink: /apis/constraints.gatekeeper.sh/v1beta1/k8spspallowedusers/user-must-be-3333
  uid: 5d0b39a8-9bcc-11ea-bb38-42010a80000c
spec:
  enforcementAction: dryrun
  match:
    kinds:
    - apiGroups:
      - ""
      kinds:
      - Pod
  parameters:
    runAsUser:
      ranges:
      - max: 3333
        min: 3333
      rule: MustRunAs
 status:
  auditTimestamp: "2020-05-22T01:39:05Z"
  byPod:
  - enforced: true
    id: gatekeeper-controller-manager-6b665d4c4d-lwnz5
    observedGeneration: 1
 totalViolations: 5
  violations:
  - enforcementAction: dryrun
    kind: Pod
    message: Container git-sync is attempting to run as disallowed user 65533
    name: git-importer-86564db8cb-5r4gs
    namespace: config-management-system
  - enforcementAction: dryrun
    kind: Pod
    message: Container manager is attempting to run as disallowed user 1000
    name: gatekeeper-controller-manager-6b665d4c4d-lwnz5
    namespace: gatekeeper-system
  - enforcementAction: dryrun
    kind: Pod
    message: Container kube-proxy is attempting to run without a required securityContext/runAsUser
    name: kube-proxy-gke-fishy131-default-pool-7369b17c-cckf
    namespace: kube-system
  - enforcementAction: dryrun
    kind: Pod
    message: Container kube-proxy is attempting to run without a required securityContext/runAsUser
    name: kube-proxy-gke-fishy131-default-pool-7369b17c-jnhb
    namespace: kube-system
  - enforcementAction: dryrun
    kind: Pod
    message: Container kube-proxy is attempting to run without a required securityContext/runAsUser
    name: kube-proxy-gke-fishy131-default-pool-7369b17c-xrd8
    namespace: kube-system

ログでの監査結果の表示

ログで監査レポートを表示できます。ログの詳細については、監査ログの使用をご覧ください。

すべての Policy Controller ログを取得するには、次のコマンドを実行します。

kubectl logs -n gatekeeper-system -l gatekeeper.sh/system=yes

監査結果のログ行に "process":"audit" が含まれているため、出力を別のコマンドにパイプして、これらの行でフィルタリングできます。たとえば、jq を使用して JSON ファイルを解析し、特定のログタイプにフィルタを設定できます。

ロギングから取得した監査結果の例:

{
"level":"info",
"ts":1590111401.9769812,
"logger":"controller",
"msg":"Container kube-proxy is attempting to run without a required securityContext/runAsUser",
"process":"audit",
"audit_id":"2020-05-22T01:36:24Z",
"event_type":"violation_audited",
"constraint_kind":"K8sPSPAllowedUsers",
"constraint_name":"user-must-be-3333",
"constraint_namespace":"",
"constraint_action":"dryrun",
"resource_kind":"Pod",
"resource_namespace":"kube-system",
"resource_name":"kube-proxy-gke-fishy131-default-pool-7369b17c-xrd8"
}

次のステップ