本頁面說明如何管理 Google Cloud Armor 安全性政策中的規則,包括更新個別規則、自動更新多項規則,以及刪除規則。
如要建立新規則或查看安全性政策中的現有規則,請參閱「建立及查看 Cloud Armor 安全性政策規則」。如要管理安全性政策容器本身,請參閱「管理 Cloud Armor 安全性政策」。
管理安全性政策規則
下列各節說明如何更新或刪除安全性政策規則。
更新安全性政策中的單一規則
本節說明如何更新 Cloud Armor 安全性政策中的單一規則。如要自動更新多項規則,請參閱「自動更新安全性政策中的多項規則」。
控制台
前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下安全性政策名稱,開啟「Policy details」(政策詳細資料) 頁面。
點選「規則」分頁標籤。
在要更新的規則旁邊,按一下「 編輯」。系統會顯示「編輯規則」頁面。
進行變更,然後按一下「更新」。
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
匯出要更新的政策,如以下範例所示:
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使用文字編輯器修改政策。例如,您可以修改現有規則的優先順序,並新增規則:
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建立新的 Cloud Armor 安全性政策,並指定修改後的檔案名稱和格式,如以下範例所示:
gcloud compute security-policies create new-policy \ --file-name modified-policy \ --file-format yaml從相關後端服務中移除舊的安全性政策,如以下範例所示:
gcloud compute backend-services update my-backend \ --security-policy ""將新的安全性政策新增至後端服務,如下例所示:
gcloud compute backend-services update my-backend \ --security-policy new-policy如果舊政策已不再使用,請將其刪除:
gcloud compute security-policies delete my-policy
刪除安全性政策中的規則
請按照本節中的步驟,刪除 Cloud Armor 安全性政策中的規則。
控制台
前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下安全性政策名稱,開啟「Policy details」(政策詳細資料) 頁面。
在「Rules」(規則) 分頁中,找出要刪除的規則,然後勾選旁邊的核取方塊。
按一下 「Delete」(刪除)。
gcloud
使用下列指令移除規則。一次只能修改一項安全性政策,但可以一次刪除多項規則:
gcloud compute security-policies rules delete PRIORITY [...] [
--security-policy POLICY_NAME \
]
例如:
gcloud compute security-policies rules delete 1000 \
--security-policy my-policy