使用 Cloud Service Mesh 安全政策限制条件

Policy Controller 附带默认的限制条件模板库,您可以将该库与 Cloud Service Mesh 安全包搭配使用,以审核网格安全漏洞的合规性并了解是否符合最佳实践。

本页面包含有关手动应用政策包的说明。或者,您也可以直接应用政策包。

此限制条件软件包在以下网域中处理并强制执行政策:

  • Cloud Service Mesh 强制执行 mTLS 流量
  • Cloud Service Mesh AuthorizationPolicy 最佳实践
  • Cloud Service Mesh 工作负载安全强制执行

Cloud 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 强制要求 istio 代理 Sidecar 始终注入工作负载 pod 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 标准化 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

软件包配置文件

在 Cloud Service Mesh 安全政策包中,您可以根据严格程度使用两个配置文件。低严格程度所应用的限制条件较少,因此具有更大的灵活性。高严格程度所应用的限制条件较多,因此可以提供更安全的政策控制。

严格程度低

低严格程度的配置文件包含以下政策限制条件:

  • 标签 istio:ingressgateway 只能由 Istio 入站流量网关上的 Pod 使用。

  • 在 AuthorizationPolicy 中,只能通过选择具有标签 istio:ingressgateway 的 Istio 入站流量网关来使用 hosts 或 notHosts 字段。

  • 在 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 setter 说明
严格程度 Cloud Service Mesh 软件包严格程度配置文件,选项包括“低”或“高”(默认)

准备工作

  1. 安装并初始化 Google Cloud CLI,它提供了以下说明中使用的 gcloud 和 kubectl 命令。如果您使用 Cloud Shell,则系统会预安装 Google Cloud CLI。
  2. 在集群上安装 Policy Controller v1.11.2 或更高版本以及默认的限制条件模板库。您还必须启用对参照限制条件的支持,因为此包包含参照限制条件。
  3. 确认您的集群上已安装 Cloud Service Mesh。

为参照限制条件配置政策控制器

  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"
    

    此清单将政策控制器配置为监视特定种类的对象。

  2. 应用 policycontroller-config.yaml 清单:

    kubectl apply -f policycontroller-config.yaml
    

审核 Cloud Service Mesh 政策包

政策控制器允许您为 Kubernetes 集群强制执行政策。为了帮助测试您的工作负载及其对上表中列出的 Cloud Service Mesh 安全政策的合规性,您可以在“审核”模式下部署这些限制条件以发现违规行为,更重要的是让您有机会在 Kubernetes 集群上强制执行之前处理这些违规行为。

您可以在 spec.enforcementAction 设置为 dryrun 的情况下,使用 kubectl、kpt 或 Config Sync 应用这些政策。

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 下载 Cloud 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 ghcr.io/kptdev/krm-functions-catalog/set-enforcement-action:v0.1 \
      -- enforcementAction=dryrun
    
  4. 运行 kpt setter 函数以设置 Cloud Service Mesh 安全政策特定的字段:

    kpt fn eval asm-policy-v0.0.1 --image ghcr.io/kptdev/krm-functions-catalog/apply-setters:v0.2 -- \
    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 下载 Cloud 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 ghcr.io/kptdev/krm-functions-catalog/set-enforcement-action:v0.1 -- enforcementAction=dryrun
    
  5. 运行 kpt setter 函数以设置 Cloud Service Mesh 安全政策特定的字段:

    kpt fn eval policies/asm-policy-v0.0.1 --image ghcr.io/kptdev/krm-functions-catalog/apply-setters:v0.2 -- \
    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,请将 policies/asm-policy-v0.0.1 添加到根 kustomization.yaml。 否则,请移除 policies/asm-policy-v0.0.1/kustomization.yaml 文件:

    rm SYNC_ROOT_DIR/policies/asm-policy-v0.0.1/kustomization.yaml
    
  8. 将更改推送到 Config Sync 代码库:

    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 用于确认政策的安装情况。

查看违规问题

在审核模式下安装政策限制条件后,您可以使用 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[]?]'

更改 Cloud 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 ghcr.io/kptdev/krm-functions-catalog/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 ghcr.io/kptdev/krm-functions-catalog/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 将更改推送到 Config Sync 代码库:

    git add SYNC_ROOT_DIR/policies/asm-policy-v0.0.1
    git commit -m 'Adding ASM security policy bundle warn enforcement'
    git push
    
  4. 验证安装状态:

    nomos status
    

    集群应显示状态为 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

移除 Cloud Service Mesh 政策包

如果需要,您可以从集群中移除 Cloud 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 代码库:

    git rm -r SYNC_ROOT_DIR/policies/asm-policy-v0.0.1
    git commit -m 'Removing Cloud Service Mesh  policies'
    git push
    
  2. 验证状态:

    nomos status
    

    集群应显示状态为 SYNCED,且资源已移除。