設定安全性政策

Media CDN 會使用 Google Cloud Armor 安全性政策,防止不當流量影響服務。您可以根據下列條件允許或拒絕要求:

  • IPv4 和 IPv6 位址和範圍 (CIDR)
  • 國家/地區代碼 (地理位置)
  • 第 7 層篩選
  • Google Threat Intelligence (需要 Google Cloud Armor Enterprise 方案)

這些功能可讓您限制特定地區的使用者下載內容 (您在這些地區有內容授權限制)、只允許公司 IP 位址存取測試或暫存端點,以及拒絕存取已知的不良用戶端 IP 位址清單。

您可以插入自訂標頭,並設定名稱和值,藉此修飾 Google Cloud Armor 允許的要求。

Google Cloud Armor 與 Google 威脅情報整合後,您就能控管來自已知惡意 IP 位址和網域的流量,進而獲得進階威脅防護。

Google Cloud Armor 安全性政策適用於 Media CDN 提供的所有內容,包括快取內容和快取失敗。

Google Cloud Armor 安全性政策是針對每個 Media CDN 服務設定,凡是傳送至該服務 IP 位址 (或主機名稱) 的要求,都會一律強制執行安全性政策。不同服務可以套用不同的安全性政策,您也可以視需要為不同地理區域建立多項服務。

如要以使用者為單位,更精細地保護內容,建議您搭配使用已簽署的網址和 Cookie,以及 Google Cloud Armor 政策。

referer 標頭設為下列任一值時,Media CDN 在評估第 7 層標頭篩選邊緣安全政策的規則時,不會考量該標頭:

  • 多個網址
  • 相對網址
  • 含有使用者資訊或片段元件的有效絕對網址

設定安全性政策

請按照下列操作說明設定安全性政策。

事前準備

如要將 Google Cloud Armor 安全性政策附加至 Media CDN 服務,請確認下列事項:

如要授權、建立安全性政策,並將其附加至 Media CDN 服務,您還需要下列身分與存取權管理權限:

  • compute.securityPolicies.addAssociation
  • compute.securityPolicies.create
  • compute.securityPolicies.delete
  • compute.securityPolicies.get
  • compute.securityPolicies.list
  • compute.securityPolicies.update
  • compute.securityPolicies.use

如要將現有憑證附加至 Media CDN 服務,使用者只需要下列 IAM 權限:

  • compute.securityPolicies.get
  • compute.securityPolicies.list
  • compute.securityPolicies.use

roles/networkservices.edgeCacheUser 角色包含以上所有權限。

建立安全性政策

Google Cloud Armor 安全性政策由多項規則組成,每項規則都會為要求定義一組相符條件 (運算式) 和動作。舉例來說,運算式可包含位於印度的用戶比對邏輯,相關聯的動作為 allow。如果要求與規則不符,Google Cloud Armor 會繼續評估下一條規則,直到嘗試過所有規則為止。

安全性政策具有 預設規則,且動作為 allow。預設規則允許不符合先前規則的要求。如要allow只允許符合先前規則的要求,並拒絕所有其他要求,可以將這項規則變更為 deny 規則。

以下範例說明如何建立規則,封鎖所有位於澳洲的用戶端,並傳回 HTTP 403 錯誤,同時允許所有其他要求。

gcloud

如要建立 CLOUD_ARMOR_EDGE 類型的新政策,請使用 gcloud compute security-policies create 指令

gcloud compute security-policies create block-australia \
    --type="CLOUD_ARMOR_EDGE" --project="PROJECT_ID"

這會建立一項政策,並在最低優先順序 (priority: 2147483647) 設有預設允許規則:

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/securityPolicies/block-australia].

接著,您可以新增優先順序較高的規則:

gcloud compute security-policies rules create 1000 \
    --security-policy=block-australia --description "block AU" \
    --expression="origin.region_code == 'AU'" --action="deny-403"

輸出內容如下:

Updated [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/securityPolicies/block-australia].

Terraform

resource "google_compute_security_policy" "default" {
  name        = "block-australia"
  type        = "CLOUD_ARMOR_EDGE"
  description = "block AU"

  rule {
    action      = "deny(403)"
    description = "block AU"
    priority    = "1000"
    match {
      expr {
        expression = "origin.region_code == 'AU'"
      }
    }
  }
  rule {
    action   = "allow"
    priority = "2147483647"
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["*"]
      }
    }
    description = "default rule"
  }
}

檢查政策時,您會看到兩項規則:第一項規則會封鎖來自澳洲的要求 (origin.region_code == 'AU'),第二項規則的優先順序最低,會允許所有不符合較高優先順序規則的流量。

kind: compute#securityPolicy
name: block-australia
rules:
- action: deny(403)
  description: block AU
  kind: compute#securityPolicyRule
  match:
    expr:
      expression: origin.region_code == 'AU'
  preview: false
  priority: 1000
- action: allow
  description: default rule
  kind: compute#securityPolicyRule
  match:
    config:
      srcIpRanges:
      - '*'
    versionedExpr: SRC_IPS_V1
  preview: false
  priority: 2147483647
  ruleNumber: '1'
type: CLOUD_ARMOR_EDGE

在安全性政策中新增規則

Google Cloud Armor 安全性政策是一組規則,可根據第 7 層屬性進行比對,以保護對外應用程式或服務。系統會針對傳入流量評估每項規則。

這些屬性可用於安全政策中的 HTTP 要求:request.headersrequest.methodrequest.pathrequest.schemerequest.query。如要進一步瞭解如何為安全性政策規則編寫運算式,請參閱 Google Cloud Armor 自訂規則語言參考資料

Google Cloud Armor 安全性政策規則包含比對條件,以及符合條件時要採取的動作。

gcloud

如要為安全性政策建立規則,請使用 gcloud compute security-policies rules create PRIORITY 指令。將 PRIORITY 替換為政策中規則的優先順序:

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

將政策附加至服務

gcloud

如要將現有的 Google Cloud Armor 政策附加至 Media CDN 服務,請使用 gcloud edge-cache services update 指令

gcloud edge-cache services update MY_SERVICE \
    --edge-security-policy=SECURITY_POLICY

更新安全性政策中的規則

按照這些操作說明更新 Google Cloud Armor 安全性政策中的單一規則。或者,您也可以以原子方式更新安全性政策中的多項規則

gcloud

使用 gcloud compute security-policies rules update 指令

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"

如要更新規則的優先順序,請使用 REST API。詳情請參閱 securityPolicies.patchRule 方法

查看政策附件

如要查看現有服務附加的政策,請檢查 (說明) 該服務。

gcloud

如要查看附加至 Media CDN 服務的 Google Cloud Armor 政策,請使用 gcloud edge-cache services describe 指令

gcloud edge-cache services describe MY_SERVICE

服務的 edgeSecurityPolicy 欄位會說明附加的政策:

name: "MY_SERVICE"
edgeSecurityPolicy: "SECURITY_POLICY

移除政策

如要移除現有政策,請更新相關聯的服務,並傳遞空白字串做為政策。

gcloud

使用 gcloud edge-cache services update 指令

  gcloud edge-cache services update MY_SERVICE \
      --edge-security-policy=""

edgeSecurityPolicy 欄位現在會從 gcloud edge-cache services describe MY_SERVICE 指令的輸出內容中省略。

範例

請參考下列詳細的應用實例。

範例:找出遭封鎖的要求

您必須啟用記錄功能,系統才會記錄特定 EdgeCache 服務遭封鎖的要求。

系統會將篩選政策允許或拒絕的要求記錄到記錄檔。如要篩選遭拒的要求,prod-video-service 設定的記錄查詢會如下所示:

resource.type="edge_cache_service"
jsonPayload.statusDetails="denied_by_security_policy"

範例:自訂回應代碼

您可以設定 Google Cloud Armor 規則,將特定狀態碼做為與指定規則相關聯的動作傳回。在大多數情況下,最好傳回 HTTP 403 DENY 狀態碼,明確指出用戶端遭到規則封鎖。

支援的狀態碼如下:

  • HTTP 403 Forbidden
  • HTTP 404 Not Found
  • HTTP 502 Bad Gateway

以下範例說明如何設定傳回的狀態碼:

如要將其中一個 [allow | deny-403 | deny-404 | deny-502] 指定為與規則相關聯的動作,請執行下列指令。這個範例會將規則設為傳回 HTTP 502 狀態碼。

gcloud compute security-policies rules create 1000 \
    --security-policy=block-australia --description "block AU" \
    --expression="origin.region_code == 'AU'" --action="deny-502"

安全政策中的每條規則都可以定義不同的狀態碼回應。

範例:拒絕來自特定國家/地區以外的用戶端,但允許特定 IP 位址

在媒體放送中,常見的案例是拒絕來自客戶的連線,因為這些客戶位於您擁有內容授權或付款機制的區域之外。

舉例來說,您可能只想允許位於印度的用戶,以及許可清單中的所有 IP 位址 (包括內容合作夥伴和自家員工的 IP 位址),位於 192.0.2.0/24 範圍內,並拒絕所有其他 IP 位址。

使用 Google Cloud Armor 自訂規則語言,下列運算式可達成此目的:

origin.region_code == "IN" || inIpRange(origin.ip, '192.0.2.0/24')

這個運算式會設定為 allow 規則,並預設設定 deny 規則,以比對所有其他用戶端。安全性政策一律會有預設規則。您通常會將此設定為default deny您未明確允許的流量。在其他情況下,您可能會選擇封鎖部分流量和default allow所有其他流量。

在安全性政策輸出內容中,請注意下列事項:

  • 優先順序最高的規則 (priority: 0) 允許來自印度定義 IP 位址清單的流量。
  • 優先順序最低的規則代表 default deny。如果優先順序較高的規則評估結果為 false,規則引擎就會拒絕所有用戶端。
  • 您可以使用布林值運算子合併多項規則。

這項政策允許來自印度的用戶端流量,允許來自指定 IP 範圍的用戶端,並拒絕所有其他流量

查看政策詳細資料時,輸出內容會類似以下範例:

kind: compute#securityPolicy
name: allow-india-only
type: "CLOUD_ARMOR_EDGE"
rules:
- action: allow
  description: ''
  kind: compute#securityPolicyRule
  match:
    expr:
      expression: origin.region_code == "IN" || inIpRange(origin.ip, '192.0.2.0/24')
  preview: false
  priority: 0
- action: deny(403)
  description: Default rule, higher priority overrides it
  kind: compute#securityPolicyRule
  match:
    config:
      srcIpRanges:
      - '*'
    versionedExpr: SRC_IPS_V1
  preview: false
  priority: 2147483647

您也可以使用 {region_code} 標頭變數設定自訂回應標頭。您可以使用 JavaScript 檢查這個標頭,並反映給用戶端。

範例:封鎖來自已知惡意 IP 的流量

下列 Google Cloud Armor 自訂規則語言運算式會封鎖來自惡意 IP 位址的流量:

evaluateThreatIntelligence('iplist-known-malicious-ips')

這個運算式會指示 Google Cloud Armor 根據 Google 持續更新的已知惡意 IP 位址清單檢查傳入要求,並提供強大的自動防護機制。

如要自動封鎖惡意 IP 位址,您可以透過 Google Threat Intelligence 規則設定邊緣安全政策。

下列 Google Cloud CLI 指令說明如何將新的 Google 威脅情報規則新增至現有政策,例如 my-edge-policy

  gcloud compute security-policies create my-edge-policy \
      --type=CLOUD_ARMOR_EDGE

  gcloud edge-cache services update my-edge-cache-service \
      --edge-security-policy "my-edge-policy"

  gcloud compute security-policies rules create 1000 \
      --security-policy "my-edge-policy" \
      --expression "evaluateThreatIntelligence('iplist-known-malicious-ips')" \
      --action "deny-403"

範例:依 IP 位址和 IP 範圍封鎖惡意用戶端

使用 Google Cloud Armor 自訂規則語言,下列運算式可達成此目的:

inIpRange(origin.ip, '192.0.2.2/32') || inIpRange(origin.ip, '192.0.2.170/32')

您可以在 IPv4 中封鎖最多 /8 遮罩的 IP 範圍,在 IPv6 中則為 /32。串流平台常見的做法是封鎖 Proxy 或 VPN 供應商的輸出 IP 範圍,盡量避免內容授權遭規避:

inIpRange(origin.ip, '192.0.2.0/24') || inIpRange(origin.ip, '198.51.100.0/24') || inIpRange(origin.ip, '203.0.113.0/24') || inIpRange(origin.ip, '2001:DB8::B33F:2002/64')

支援 IPv4 和 IPv6 位址範圍。

範例:僅允許固定地理區域清單

如果您有國家/地區代碼清單,可以使用布林 OR 運算子 || 合併比對條件。

使用 Google Cloud Armor 自訂規則語言,以下運算式可允許來自澳洲或紐西蘭的使用者:

origin.region_code == "AU" || origin.region_code == "NZ"

此外,您也可以將這項設定與 origin.ipinIpRange(origin.ip, '...') 運算式合併,允許測試人員、合作夥伴和公司 IP 範圍存取應用程式,即使他們不在指定地理區域內也沒問題。

具備自訂運算式的各項規則所含子運算式數有記載。如要合併更多子運算式,請在單一政策中定義多項規則。

範例:封鎖來自特定國家/地區的用戶端

較不常見的例子是封鎖特定國家/地區的用戶端,但允許來自其他國家/地區的要求。

如要達成這個目標,請建立封鎖國家/地區和無法判斷地區的用戶端政策,然後針對所有其他要求套用預設允許規則。

以下範例說明一項政策,該政策會封鎖來自加拿大的用戶端和位置不明的用戶端,但允許所有其他流量:

  kind: compute#securityPolicy
  name: block-canada
  type: "CLOUD_ARMOR_EDGE"
  rules:
  - action: deny(403)
    description: ''
    kind: compute#securityPolicyRule
    match:
      expr:
        expression: origin.region_code == "CA" || origin.region_code == "ZZ"
    preview: false
    priority: 0
  - action: allow
    description: Default rule, higher priority overrides it
    kind: compute#securityPolicyRule
    match:
      config:
        srcIpRanges:
        - '*'
      versionedExpr: SRC_IPS_V1
    preview: false
    priority: 2147483647

範例:拒絕含有特定標頭的快取內容要求

邊緣安全性政策會套用至所有以政策附加的任何 Media CDN 服務為目標的要求。這項政策會在任何快取查詢前強制執行。如果要求違反邊緣安全性政策,系統會拒絕要求,並傳回設定的狀態碼。

下列運算式會比對來自 IP 位址 1.2.3.4 的要求,這些要求在 user-agent 標頭中包含字串 user1

inIpRange(origin.ip, '1.2.3.4/32') && request.headers['user-agent'].contains('user1')

下列指令會將篩選規則 105 新增至邊緣安全性政策 my-edge-policy,該政策已附加至 Media CDN 服務:

gcloud compute security-policies rules create 105 \
    --security-policy "my-edge-policy" \
    --expression = "inIpRange(origin.ip, '1.2.3.4/32') && request.headers['user-agent'].contains('charlie')" \
    --action= deny-403 \
    --description="block requests from IP addresses in which the user-agent header contains the string charlie"
    

記錄違規處置

每筆要求記錄都會提供詳細資料,說明套用的安全性政策,以及要求是否獲得允許 (ALLOW) 或遭到拒絕 (DENY)。

如要啟用記錄功能,請確保服務中的 logConfig.enable 設為 true。如果服務未啟用記錄功能,就不會記錄安全性政策事件。

如果用戶端位於美國境外,且強制執行名為 deny-non-us-clients 的安全性政策,拒絕來自美國境外的要求,則遭拒要求的記錄項目如下:

enforcedSecurityPolicy:
  name: deny-non-us-clients
  outcome: DENY

如果服務未附加 Google Cloud Armor 政策,enforcedSecurityPolicy.name 的值會是 no_policy,且 outcome 的值會是 ALLOW。舉例來說,如果服務沒有附加政策,要求記錄項目會包含下列值:

enforcedSecurityPolicy:
  name: no_policy
  outcome: ALLOW

瞭解 GeoIP 分類

Media CDN 會根據 Google 內部 IP 分類資料來源,從 IP 位址衍生出位置 (區域、州、省或城市)。如果您要從多個供應商遷移或在多個供應商之間分配流量,有時可能會發生少數 IP 位址與不同位置建立關聯的情況。

  • Google Cloud Armor 會使用 ISO 3166-1 alpha 2 地區代碼,將用戶端與地理位置建立關聯。
  • 例如美國的 US,或澳洲的 AU
  • 在某些情況下,區域會對應到國家/地區,但並非一律如此。舉例來說,US 代碼包含美國所有州、一個行政區和六個邊遠地區。
  • 詳情請參閱 Unicode 技術標準中的 unicode_region_subtag
  • 如果無法推算用戶端位置,origin.region_code 會設為 ZZ

您可以在回應標頭中加入地理資料至 Media CDN 端點 (使用 routing.routeRules[].headerActions[].responseHeadersToAdd[]),或反映提供給 Cloud Function 的地理資料,以驗證初始整合和測試期間,geoIP 資料來源之間的差異。

此外,Media CDN 請求記錄還包含 clientRegion 和其他用戶端專屬資料,您可以根據現有資料來源驗證這些資料。

後續步驟