管理 Cloud Armor 安全政策规则

本页面介绍了如何管理 Google Cloud Armor 安全政策中的规则,包括更新单个规则、以原子方式更新多个规则以及删除规则。

如需在安全政策中创建新规则或查看现有规则,请参阅创建和查看 Cloud Armor 安全政策规则。如需管理安全政策容器本身,请参阅管理 Cloud Armor 安全政策。

管理安全政策规则

以下部分介绍了如何更新或删除安全政策规则。

更新安全政策中的单个规则

本部分介绍如何更新 Cloud Armor 安全政策中的单条规则。如需以原子方式更新多条规则,请参阅以原子方式更新安全政策中的多条规则。

控制台

  1. 在 Google Cloud 控制台中,前往 Cloud Armor 政策页面。

    前往“Cloud Armor 政策”页面

  2. 点击安全政策的名称,打开政策详情页面。

  3. 点击规则标签页。

  4. 在要更新的规则旁边,点击修改。随即会显示修改规则页面。

  5. 进行更改,然后点击更新。

gcloud

使用以下命令更新规则。您一次只能更新一项安全政策:

gcloud compute security-policies rules update PRIORITY [ \
    --security-policy POLICY_NAME  \
    --description DESCRIPTION  \
    --src-ip-ranges IP_RANGES  | --expression EXPRESSION \
    --action=[ allow | deny-403 | deny-404 | deny-502 ]  \
    --preview
  ]
  

例如,以下命令会更新优先级为 1111 的规则,以允许来自 IP 地址范围 192.0.2.0/24 的流量:

gcloud compute security-policies rules update 1111 \
    --security-policy my-policy \
    --description "allow traffic from 192.0.2.0/24" \
    --src-ip-ranges "192.0.2.0/24" \
    --action "allow"

如需详细了解上述命令,请参阅 gcloud compute security-policies rules update。

如需更新规则的优先级,请使用 REST API。请参阅 securityPolicies.patchRule。

以原子方式更新安全政策中的多条规则

您可以使用原子更新功能,在单次更新中对多条规则应用更改。单规则更新可能会导致意外行为(例如错误),因为新旧规则可能会在短时间内重叠。

要以原子方式更新多个规则,请将当前安全政策导出到 JSON 或 YAML 文件中,然后对其进行修改。使用修改后的文件创建安全政策,然后切换后端服务的安全政策。

gcloud

  1. 导出政策以进行更新,如以下示例所示:

    gcloud compute security-policies export my-policy \
        --file-name my-file \
        --file-format yaml
    

    导出的政策类似于以下示例:

        description: my description
        fingerprint: PWfLGDWQDLY=
        id: '123'
        name: my-policy
        rules:
        - action: deny(404)
          description: my-rule-1
          match:
            expr:
              expression: evaluatePreconfiguredWaf('xss-v422-stable')
            versionedExpr: SRC_IPS_V1
          preview: false
          priority: 1
        - action: allow
          description: my-rule-2
          match:
            config:
              srcIpRanges:
              - '1.2.3.4'
            versionedExpr: SRC_IPS_V1
          preview: false
          priority: 2
        - action: deny
          description: default rule
          kind: compute#securityPolicyRule
          match:
            config:
              srcIpRanges:
              - '*'
            versionedExpr: SRC_IPS_V1
          preview: false
          priority: 2147483647
        selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/securityPolicies/my-policy
    
  2. 使用文本编辑器修改政策。例如,您可以修改现有规则的优先级并添加规则:

        description: my description
        fingerprint: PWfLGDWQDLY=
        id: '123'
        name: my-policy
        rules:
        - action: deny(404)
          description: my-rule-1
          match:
            expr:
              expression: evaluatePreconfiguredWaf('xss-v422-stable')
            versionedExpr: SRC_IPS_V1
          preview: false
          priority: 1
        - action: allow
          description: my-new-rule
          match:
            config:
              srcIpRanges:
              - '1.2.3.1'
            versionedExpr: SRC_IPS_V1
          preview: false
          priority: 10
        - action: allow
          description: my-rule-2
          match:
            config:
              srcIpRanges:
              - '1.2.3.4'
            versionedExpr: SRC_IPS_V1
          preview: false
          priority: 11
        - action: deny
          description: default rule
          kind: compute#securityPolicyRule
          match:
            config:
              srcIpRanges:
              - '*'
            versionedExpr: SRC_IPS_V1
          preview: false
          priority: 2147483647
        selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/securityPolicies/my-policy
    
  3. 创建一个新的 Cloud Armor 安全政策并指定修改后的文件名和格式,如以下示例所示:

    gcloud compute security-policies create new-policy \
        --file-name modified-policy \
        --file-format yaml
    
  4. 从相关后端服务中移除旧安全政策,如以下示例所示:

    gcloud compute backend-services update my-backend \
        --security-policy ""
    
  5. 将新安全政策添加到后端服务,如以下示例所示:

    gcloud compute backend-services update my-backend \
        --security-policy new-policy
    
  6. 如果未使用旧政策,请将其删除:

    gcloud compute security-policies delete my-policy
    

从安全政策中删除规则

按照本部分中的步骤从 Cloud Armor 安全政策中删除规则。

控制台

  1. 在 Google Cloud 控制台中,前往 Cloud Armor 政策页面。

    前往“Cloud Armor 政策”页面

  2. 点击安全政策的名称,打开政策详情页面。

  3. 在规则标签页中,选中要删除的规则旁边的复选框。

  4. 点击 删除。

gcloud

使用以下命令移除规则。您一次只能修改一项安全政策,但可以一次删除多条规则:

gcloud compute security-policies rules delete PRIORITY [...] [
    --security-policy POLICY_NAME \
  ]

例如:

gcloud compute security-policies rules delete 1000 \
    --security-policy my-policy

后续步骤