Anthos Service Mesh セキュリティ ポリシーの制約を使用する

Policy Controller には、メッシュの脆弱性とベスト プラクティスのコンプライアンスを監査するために、Anthos Service Mesh のセキュリティ バンドルと併用できる制約テンプレートのデフォルト ライブラリが付属しています。

この制約のバンドルは、次のドメインに対応し、ポリシーを適用します。

  • Anthos Service Mesh の mTLS トラフィックの適用
  • Anthos Service Mesh AuthorizationPolicy のベスト プラクティス
  • Anthos Service Mesh ワークロードのセキュリティの適用

Anthos Service Mesh ポリシー バンドルの制約

制約名 制約の説明 コントロール ID
asm-policy-v0.0.1-asm-ingressgateway-label ingressgateway Pod にのみ Istio ingressgateway ラベルの使用を適用する 1.1.1
asm-policy-v0.0.1-asm-sidecar-injection ワークロード Pod に常に Istio プロキシ サイドカーが挿入されるようにする 1.1.2
asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny メッシュレベルのデフォルトの拒否 AuthorizationPolicy を適用する 1.2.1
asm-policy-v0.0.1-asm-authz-policy-normalization AuthorizationPolicy normalization を適用する 1.2.2
asm-policy-v0.0.1-asm-authz-policy-safe-pattern AuthorizationPolicy 安全なパターンを適用する 1.2.3
asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls メッシュレベルの厳格な mtls PeerAuthentication を適用する 1.3.1
asm-policy-v0.0.1-asm-peer-authn-strict-mtls すべての PeerAuthentication が厳格な mtls を上書きできないようにする 1.3.2
asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers jwtRules outputPayloadToHeader を適用して、既知の HTTP リクエスト ヘッダーが含まれないようにします。 1.4.1

バンドル プロファイル

Anthos Service Mesh セキュリティ ポリシー バンドルでは、厳格性のレベルに応じて 2 つのプロファイルを使用できます。厳格性のレベルが低いほど制約は適用されず、柔軟性が高くなります。厳格性のレベルが高いほど、より多くの制約が適用され、より安全なポリシー制御が提供されます。

厳格性のレベルが低い

厳格性のレベルが低いプロファイルには、次のポリシー制約があります。

  • istio:ingressgateway ラベルは Istio Ingress ゲートウェイ Pod でのみ使用できます。

  • AuthorizationPolicy では、hosts フィールドまたは notHosts フィールドは、istio:ingressgateway というラベルを持つ Istio Ingress ゲートウェイを選択した場合にのみ使用できます。

  • AuthorizationPolicy で、methods フィールドまたは notMethods フィールドが使用される場合、値は大文字にする必要があります。

  • AuthorizationPolicy で、request.headers フィールドを使用する場合、値に空白を含めることはできません。

  • AuthorizationPolicy で、paths フィールドまたは notPaths フィールドが使用される場合、値は正規化される必要があります。

厳格性のレベルが高い

厳格性のレベルが高い場合は、厳格性のレベルが低い場合の制約と次の制約がすべて適用されます。

  • すべてのワークロード Pod で、プロキシ インジェクションをバイパスするためにアノテーション sidecar.istio.io/inject: false は適用できません。

  • デフォルトの拒否ルールを定義するメッシュレベル AuthorizationPolicy が適用されます。

  • AuthorizationPolicy は、ALLOW-with-positive-matching または DENY-with-negative-match のいずれかに従う必要があります。

  • AuthorizationPolicy で、hosts フィールドまたは notHosts フィールドが使用される場合、値は <host-name><host-name>:* のペアでなければなりません。

  • 厳格な mTLS を定義するメッシュレベル PeerAuthentication が適用されます。

  • メッシュ内のすべての PeerAuthentication で、mTLS モードにできるのは UNSET または STRICT のいずれかで、厳格な mTLS に従います。

バンドル設定

KPT セッター 説明
strictness-level Anthos Service Mesh バンドルの厳格性のレベル プロファイル。オプションは Low または High(デフォルト)

準備

  1. この手順で使用する gcloud コマンドと kubectl コマンドを含む Google Cloud CLI をインストールして初期化します。Cloud Shell を使用する場合、Google Cloud CLI がプリインストールされています。
  2. 制約テンプレートのデフォルト ライブラリを使用して、クラスタに Policy Controller v.1.11.2 以降をインストールします。このバンドルには参照制約が含まれているため、参照制約のサポートを有効にする必要があります。
  3. Anthos Service Mesh がクラスタにインストールされていることを確認します。

参照制約用に Policy Controller を構成する

  1. 次の YAML マニフェストを policycontroller-config.yaml として保存します。

    apiVersion: config.gatekeeper.sh/v1alpha1
    kind: Config
    metadata:
      name: config
      namespace: "gatekeeper-system"
    spec:
      sync:
        syncOnly:
          - group: ""
            version: "v1"
            kind: "Namespace"
          - group: "security.istio.io"
            version: "v1beta1"
            kind: "AuthorizationPolicy"
          - group: "security.istio.io"
            version: "v1beta1"
            kind: "PeerAuthentication"
    

    このマニフェストでは、特定の種類のオブジェクトを監視するように Policy Controller を構成します。

  2. policycontroller-config.yaml マニフェストを適用します。

    kubectl apply -f policycontroller-config.yaml
    

Anthos Service Mesh ポリシー バンドルの監査

Policy Controller を使用すると、Kubernetes クラスタにポリシーを適用できます。前の表にある Anthos Service Mesh セキュリティ ポリシーに関するワークロードとそのコンプライアンスをテストする場合、これらの制約を監査モードでデプロイできます。これにより、違反がより明確になり、Kubernetes クラスタに適用する前に修正することが可能になります。

kubectl、kpt、または Config Sync を使用して spec.enforcementActiondryrun に設定し、これらのポリシーを適用できます。

kubectl

  1. (省略可)kubectl でポリシーの制約をプレビューします。

    kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
    
  2. kubectl でポリシーの制約を適用します。

    kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
    

    次のような出力が表示されます。

    asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny created
    asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-normalization created
    asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-safe-pattern created
    asmingressgatewaylabel.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-ingressgateway-label created
    asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls created
    asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-strict-mtls created
    asmrequestauthnprohibitedoutputheaders.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers created
    asmsidecarinjection.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-sidecar-injection created
    
  3. ポリシーの制約がインストールされていることを確認し、クラスタ全体で違反の存在を確認します。

    kubectl get -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
    

    出力は次のようになります。

    NAME                                                                                                       ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny   dryrun               0
    
    NAME                                                                                                     ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-normalization   dryrun               0
    
    NAME                                                                                                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-safe-pattern   dryrun               0
    
    NAME                                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmingressgatewaylabel.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-ingressgateway-label   dryrun               0
    
    NAME                                                                                                     ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls   dryrun               0
    
    NAME                                                                                            ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-strict-mtls   dryrun               0
    
    NAME                                                                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmrequestauthnprohibitedoutputheaders.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers   dryrun               0
    
    NAME                                                                                    ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    asmsidecarinjection.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-sidecar-injection   dryrun               0
    

kpt

  1. kpt をインストールして設定します。これらの手順では、kpt を使用して Kubernetes リソースのカスタマイズとデプロイを行います。

  2. kpt を使用して GitHub から Anthos Service Mesh セキュリティ ポリシー バンドルをダウンロードします。

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
    
  3. set-enforcement-action kpt 関数を実行して、ポリシーの適用アクションを dryrun に設定します。

    kpt fn eval asm-policy-v0.0.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \
      -- enforcementAction=dryrun
    
  4. kpt のセッター関数を実行して、Anthos Service Mesh のセキュリティ ポリシー固有のフィールドを設定します。

    kpt fn eval asm-policy-v0.0.1 --image gcr.io/kpt-fn/apply-setters:v0.2.0 -- \
    strictness-level="Low"
    
  5. kpt を使用して作業ディレクトリを初期化します。これにより、変更を追跡するためのリソースが作成されます。

    cd asm-policy-v0.0.1
    kpt live init
    
  6. kpt を使用してポリシーの制約を適用します。

    kpt live apply
    

    次のような出力が表示されます。

    asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-authz-policy-mesh-default-deny created
    asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-authz-policy-normalization created
    asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-authz-policy-safe-pattern created
    asmingressgatewaylabel.constraints.gatekeeper.sh/asm-ingressgateway-label created
    asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-peer-authn-mesh-strict-mtls created
    asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-peer-authn-strict-mtls created
    asmsidecarinjection.constraints.gatekeeper.sh/asm-sidecar-injection created
    7 resource(s) applied. 7 created, 0 unchanged, 0 configured, 0 failed
    
  7. ポリシーの制約がインストールされていることを確認し、クラスタ全体で違反の存在を確認します。

    kpt live status --output table --poll-until current
    

    ステータスが CURRENT の場合、制約が正常にインストールされています。

Config Sync

  1. kpt をインストールして設定します。これらの手順では、kpt を使用して Kubernetes リソースのカスタマイズとデプロイを行います。

Config Sync を使用してクラスタにポリシーをデプロイするオペレータは、次の手順を使用できます。

  1. Config Sync の同期ディレクトリに移動します。

    cd SYNC_ROOT_DIR
    

    resourcegroup.yaml を使用して .gitignore を作成または追加するには:

    echo resourcegroup.yaml >> .gitignore
    

  2. 専用の policies ディレクトリを作成します。

    mkdir -p policies
    
  3. kpt を使用して GitHub から Anthos Service Mesh セキュリティ ポリシー バンドルをダウンロードします。

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1 policies/asm-policy-v0.0.1
    
  4. set-enforcement-action kpt 関数を実行して、ポリシーの適用アクションを dryrun に設定します。

    kpt fn eval policies/asm-policy-v0.0.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
    
  5. kpt のセッター関数を実行して、Anthos Service Mesh のセキュリティ ポリシー固有のフィールドを設定します。

    kpt fn eval policies/asm-policy-v0.0.1 --image gcr.io/kpt-fn/apply-setters:v0.2.0 -- \
    strictness-level="Low"
    
  6. (省略可)作成されるポリシー制約をプレビューします。

    kpt live init policies/asm-policy-v0.0.1
    kpt live apply --dry-run policies/asm-policy-v0.0.1
    

    次のような出力が表示されます。

    Dry-run strategy: client
    inventory update started
    inventory update finished
    apply phase started
    asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny apply successful
    asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-normalization apply successful
    asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-safe-pattern apply successful
    asmingressgatewaylabel.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-ingressgateway-label apply successful
    asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls apply successful
    asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-strict-mtls apply successful
    asmrequestauthnprohibitedoutputheaders.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers apply successful
    asmsidecarinjection.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-sidecar-injection apply successful
    apply phase finished
    inventory update started
    inventory update finished
    apply result: 8 attempted, 8 successful, 0 skipped, 0 failed
    
  7. Config Sync の同期ディレクトリで Kustomize を使用している場合は、ルート kustomization.yamlpolicies/asm-policy-v0.0.1 を追加します。 それ以外の場合は、policies/asm-policy-v0.0.1/kustomization.yaml ファイルを削除します。

    rm SYNC_ROOT_DIR/policies/asm-policy-v0.0.1/kustomization.yaml
    
  8. 変更を Config Sync リポジトリに push します。

    git add SYNC_ROOT_DIR/policies/asm-policy-v0.0.1
    git commit -m 'Adding ASM security policy audit enforcement'
    git push
    
  9. インストールのステータスを確認します。

    watch gcloud beta container fleet config-management status --project PROJECT_ID
    

    ステータスが SYNCED の場合、ポリシーがインストールされています。

ポリシー違反を確認する

ポリシーの制約が監査モードでインストールされると、UI の Policy Controller ダッシュボードにクラスタに対する違反を表示できます。

また、次のコマンドを実行すると、kubectl を使用してクラスタに対する違反を表示できます。

kubectl get constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'

違反がある場合は、次のコマンドを実行すると、制約ごとに違反メッセージのリストが表示されます。

kubectl get constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'

Anthos Service Mesh ポリシー バンドルの適用アクションを変更する

クラスタのポリシー違反を確認したら、アドミッション コントローラが warn または deny のいずれかでクラスタへの非遵守リソースの適用をブロックするように、強制適用モードを変更することを検討できます。

kubectl

  1. kubectl を使用して、ポリシーの適用アクションを warn に設定します。

    kubectl get constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]'
    
  2. ポリシー制約の適用アクションが更新されていることを確認します。

    kubectl get constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1
    

kpt

  1. set-enforcement-action kpt 関数を実行して、ポリシーの適用アクションを warn に設定します。

    kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  2. ポリシー制約を適用します。

    kpt live apply
    

Config Sync

Config Sync を使用してクラスタにポリシーをデプロイするオペレータは、次の手順を使用できます。

  1. Config Sync の同期ディレクトリに移動します。

    cd SYNC_ROOT_DIR
    
  2. set-enforcement-action kpt 関数を実行して、ポリシーの適用アクションを warn に設定します。

    kpt fn eval policies/asm-policy-v0.0.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 変更を Config Sync リポジトリに push します。

    git add SYNC_ROOT_DIR/policies/asm-policy-v0.0.1
    git commit -m 'Adding ASM security policy bundle warn enforcement'
    git push
    
  4. インストールのステータスを確認します。

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

    SYNCED 列に表示されたリポジトリには、ポリシーがインストールされています。

ポリシーの適用をテストする

次のコマンドを使用して、クラスタに非遵守リソースを作成します。

cat <<EOF | kubectl apply -f -
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: non-compliant-authz-policy
spec:
  action: ALLOW
  rules:
  - to:
    - operation:
        methods: ["get"]
EOF

次の例のように、アドミッション コントローラにより、このリソースが違反しているポリシー違反の警告の一覧が生成されるはずです。

Warning: [asm-policy-v0.0.1-asm-authz-policy-normalization] in rules-to-operation, methods or notMethods must be uppercase
authorizationpolicy.security.istio.io/non-compliant-authz-policy created

Anthos Service Mesh ポリシー バンドルを削除する

必要であれば、Anthos Service Mesh のポリシー バンドルをクラスタから削除できます。

kubectl

  • kubectl を使用してポリシーを削除します。

    kubectl delete constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1
    

kpt

  • ポリシーを削除します。

    kpt live destroy
    

Config Sync

Config Sync を使用してクラスタにポリシーをデプロイするオペレータは、次の手順を使用できます。

  1. 変更を Config Sync リポジトリに push します。

    git rm -r SYNC_ROOT_DIR/policies/asm-policy-v0.0.1
    git commit -m 'Removing Anthos Service Mesh  policies'
    git push
    
  2. ステータスを確認します。

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

    SYNCED 列に表示されたリポジトリで、ポリシーが削除されていることを確認します。