コンテンツにスキップ

polaris@ベストプラクティス違反

はじめに

本サイトにつきまして、以下をご認識のほど宜しくお願いいたします。


01. polarisの仕組み

検出項目

一般に知られているベストプラクティス項目に基づいて、マニフェストのベストプラクティス違反 (例:脆弱性、効率性、信頼性、など) を検証する。

Helmチャートのまま検査できず、一度マニフェストとして渡す必要がある。


セットアップ

▼ GUI

$ helm repo add <チャートリポジトリ名> https://charts.fairwinds.com/stable

$ kubectl create namespace polaris

$ helm install <Helmリリース名> <チャートリポジトリ名>/polaris --namespace polaris

# ダッシュボードにリクエストを送信する
$ kubectl port-forward --namespace polaris svc/polaris-dashboard 8080:80

▼ CLI

$ brew tap FairwindsOps/tap

$ brew install FairwindsOps/tap/polaris

▼ Admission Controller

Admission Controllerとして、実際にデプロイされたマニフェストに対して静的解析を実行する。

$ helm repo add <チャートリポジトリ名> https://charts.fairwinds.com/stable

$ helm install <Helmリリース名> <チャートリポジトリ名>/polaris --namespace polaris --set webhook.enable=true --set dashboard.enable=false


02. config.yamlファイル

checks

▼ checksとは

検査項目ごとに重要度レベル (ignore、warning、danger) を設定する。

checks:

  # 信頼性
  deploymentMissingReplicas: ignore
  priorityClassNotSet: warning
  tagNotSpecified: danger

  ...

  # ハードウェアリソース効率性
  cpuRequestsMissing: ignore
  cpuLimitsMissing: warning
  memoryRequestsMissing: danger

  ...

  # 安全性
  automountServiceAccountToken: ignore
  hostIPCSet: warning
  hostPIDSet: danger

  ...

▼ 重要度レベルの変更

polarisの実行時に、重要度がdanger以上のルールを検証するようにしたとする。

重要度がデフォルトでwarningのルールは検証しなくなるため、検証したいルールはdangerに格上げする

checks:
  # Podのcpuの設定し忘れ
  cpuLimitsMissing: danger
  cpuRequestsMissing: danger

  # Deploymentのreplicasが1個である
  deploymentMissingReplicas: danger

  # PodのlivenessProbeの設定し忘れ
  livenessProbeMissing: danger

  # Podのメモリの設定し忘れ
  memoryLimitsMissing: danger
  memoryRequestsMissing: danger

  # PodDisruptionBudgetの作成し忘れ
  # PodDisruptionBudgetで指定したラベル値をDeploymentが持たない場合でも、Missingとみなす‍♂️
  missingPodDisruptionBudget: danger

  # PodのpriorityClassの設定し忘れ
  priorityClassNotSet: danger

  # PodのreadinessProbeの設定し忘れ
  readinessProbeMissing: danger


customChecks

▼ customChecksとは

カスタムルールを定義する。

▼ 設定し忘れの検証

DaemonSet配下のPodでは、.spec.priorityClassNameキーや.spec.affinityキーを設定しておく方が良いが、これを設定し忘れてしまう可能性がある。

こういった場合に、カスタムルールが役立つ。

checks:
  # 重要度を設定する
  daemonSetPriorityClassMissing: danger

customChecks:
  # DaemonSetのpriorityClassの設定し忘れを検証する
  # ビルトインのpriorityClassNotSetルールではWorkload全体を検証してしまうため、DaemonSet限定のルールを定義した
  daemonSetPriorityClassMissing:
    successMessage: In DaemonSet, priority class is set
    failureMessage: In DaemonSet, priority class should be set
    category: Reliability
    target: apps/DaemonSet
    schema:
      "$schema": http://json-schema.org/draft-07/schema
      type: object
      required:
        - spec
      properties:
        # .specキーに関するルール
        spec:
          type: object
          required:
            - template
          properties:
            # .spec.templateキーに関するルール
            template:
              type: object
              required:
                - spec
              properties:
                # .spec.template.specキーに関するルール
                spec:
                  type: object
                  required:
                    - priorityClassName
                  properties:
                    # .spec.template.spec.priorityClassNameキーに関するルール
                    priorityClassName:
                      type: string
                      not:
                        # ostring型が空値であった場合にエラーとする
                        const: ""
checks:
  # 重要度を設定する
  deploymentAffinityMissing: danger

customChecks:
  # Deploymentのaffinityの設定し忘れを検証する
  deploymentAffinityMissing:
    successMessage: In Deployment, affinity is set
    failureMessage: In Deployment, affinity should be set
    category: Reliability
    target: apps/Deployment
    schema:
      "$schema": http://json-schema.org/draft-07/schema
      type: object
      required:
        - spec
      properties:
        # .specキーに関するルール
        spec:
          type: object
          required:
            - template
          properties:
            # .spec.templateキーに関するルール
            template:
              type: object
              required:
                - spec
              properties:
                # .spec.template.specキーに関するルール
                spec:
                  type: object
                  required:
                    - affinity
                  properties:
                    # .spec.template.spec.affinityキーに関するルール
                    affinity:
                      type: object
                      not:
                        # object型が空値であった場合にエラーとする
                        const: {}
checks:
  # 重要度を設定する
  deploymentNodeSelectorMissing: danger

customChecks:
  # DeploymentのnodeSelectorの設定し忘れを検証する
  deploymentNodeSelectorMissing:
    successMessage: In Deployment, nodeSelector is set
    failureMessage: In Deployment, nodeSelector should be set
    category: Reliability
    target: apps/Deployment
    schema:
      '\$schema': http://json-schema.org/draft-07/schema
      type: object
      required:
        - spec
      properties:
        spec:
          type: object
          required:
            - template
          properties:
            template:
              type: object
              required:
                - spec
              properties:
                spec:
                  type: object
                  required:
                    - nodeSelector
                  properties:
                    nodeSelector:
                      type: object
                      not:
                        const: {}

▼ 作成し忘れ

HorizontalPodAutoscalerは、Deploymentと合わせて作る必要があるが、作成し忘れてしまう可能性がある。

こういった場合に、カスタムルールが役立つ。

checks:
  # 重要度を設定する
  missingHorizontalPodAutoscalerWithDeployment: danger

customChecks:
  # Deploymentを作成している場合に、HorizontalPodAutoscalerも作成していることを検証する
  missingHorizontalPodAutoscalerWithDeployment:
    successMessage: HorizontalPodAutoscaler exists
    failureMessage: HorizontalPodAutoscaler should exist
    category: Reliability
    target: apps/Deployment
    schema: {}
    # Deploymentがある場合に合わせて必要なKubernetesリソースを定義する
    additionalSchemas:
      autoscaling/HorizontalPodAutoscaler: {}


mutations

記入中...

mutations:
  - pullPolicyNotAlways


exemptions

脆弱性検出の項目から除外するKubernetesリソースを設定する。

一部のKubernetesリソース (例:kube-system) は、root権限を持つ実行ユーザーで実行しなければならないため、除外設定が必要である。

exemptions:
  # Namespace名
  - namespace: kube-system
    # Controller名 (Workload名)
    controllerNames:
      - dns-controller
      - ebs-csi-controller
      - ebs-csi-node
      - kindnet
      - kops-controller
      - kube-dns
      - kube-flannel-ds
      - kube-proxy
      - kube-scheduler
      - vpa-recommender
    # 脆弱性検出の項目名
    rules:
      - automountServiceAccountToken
      - linuxHardening
      - missingNetworkPolicy

  ...


03. コマンド

audit

▼ auditとは

マニフェストがルールに違反しているかどうかを検証する。

# 結果を読みやすく出力する。
$ polaris audit --audit-path manifest.yaml

# 該当するKubernetesリソースのルールのみを検証する
INFO[0000] 1 danger items found in audit

▼ --format

結果の形式を指定する。

# 結果を読みやすく出力する。
$ polaris audit --audit-path manifest.yaml --format pretty

▼ --only-show-failed-tests

失敗した結果のみを出力する。

$ polaris audit --audit-path manifest.yaml --only-show-failed-tests

カスタムルールに違反があった場合に、--only-show-failed-testsオプションを有効化していると、ルール違反が無視されて結果に表示されない不具合がある可能性がある。

$ polaris audit --audit-path manifest.yaml

Polaris audited Path manifest.yaml at 2023-09-13T03:27:57+09:00
    Nodes: 0 | Namespaces: 0 | Controllers: 1
    Final score: 85


DaemonSet fluentd
    daemonSetPriorityClassMissing         Danger # <----------- このカスタムルールが無視されてしまう
        Reliability - In DaemonSet, priority class should be set
  Container fluentd
    memoryRequestsMissing                🎉 Success
        Efficiency - Memory requests are set
    readinessProbeMissing                🎉 Success
        Reliability - Readiness probe is configured
    cpuLimitsMissing                     🎉 Success
        Efficiency - CPU limits are set
    cpuRequestsMissing                   🎉 Success
        Efficiency - CPU requests are set
    livenessProbeMissing                 🎉 Success
        Reliability - Liveness probe is configured
    memoryLimitsMissing                  🎉 Success
        Efficiency - Memory limits are set
$ polaris audit --audit-path manifest.yaml --only-show-failed-tests

Polaris audited Path manifest.yaml at 2023-09-13T03:27:57+09:00
    Nodes: 0 | Namespaces: 0 | Controllers: 1
    Final score: 85

▼ --helm-chart、--helm-values

Helmチャートを指定する。

$ polaris audit --helm-chart ./chart --helm-values ./chart/values.yaml

▼ --set-exit-code-on-danger

dangerレベルのルール違反が検出された場合に、終了コード3を出力する。

$ polaris audit --audit-path manifest.yaml --severity danger --set-exit-code-on-danger

▼ --severity

検出する下限の重要度レベル (warning、danger) を設定する。

$ polaris audit --audit-path manifest.yaml --severity danger