機構政策服務可讓您透過程式以集中方式控管機構資源。身為機構政策管理員,您可以定義機構政策,也就是一組稱為限制的限制,適用於Google Cloud 資源和Google Cloud 資源階層中這些資源的子系。您可以在機構、資料夾或專案層級強制執行組織政策。
機構政策提供各種Google Cloud 服務的預先定義限制。不過,如要進一步控管機構政策中受限制的特定欄位,您也可以建立自訂限制,並在自訂機構政策中強制執行這些限制。
對於 Cloud Next Generation Firewall,您可以針對下列防火牆政策建立及強制執行自訂限制:
自訂限制會套用至防火牆政策中的所有規則,包括建立防火牆政策時新增的預先定義規則。如要進一步瞭解預先定義的防火牆政策規則,請參閱預先定義的規則。
政策繼承
根據預設,機構政策會由您強制執行政策的資源子系繼承。舉例來說,如果您對資料夾強制執行政策, Google Cloud 系統會對該資料夾中的所有專案強制執行政策。如要進一步瞭解這項行為及如何變更,請參閱「階層評估規則」。
Cloud NGFW 支援的資源
對於防火牆政策,您可以針對下列資源和欄位設定自訂限制。
- 防火牆政策:
compute.googleapis.com/FirewallPolicy
- 規則名稱:
resource.rules[].ruleName
- 說明:
resource.rules[].description
- 優先順序:
resource.rules[].priority
- 動作:
resource.rules[].action
- 方向:
resource.rules[].direction
- 是否已啟用記錄功能:
resource.rules[].enableLogging
- 已停用:
resource.rules[].disabled
- 安全性設定檔群組:
resource.rules[].securityProfileGroup
- 是否已啟用 TLS 檢查:
resource.rules[].tlsInspect
- 目標服務帳戶:
resource.rules[].targetServiceAccounts[]
- 目標安全標記:
resource.rules[].targetSecureTags[]
- 名稱:
resource.rules[].targetSecureTags[].name
- 名稱:
- 目標資源:
resource.rules[].targetResources
- 來源 IP 範圍:
resource.rules[].match.srcIpRanges[]
- 目的地 IP 範圍:
resource.rules[].match.destIpRanges[]
- Layer4Config:
resource.rules[].match.layer4Configs[]
- IP 通訊協定:
match.layer4Configs[].ipProtocol
- 通訊埠:
resource.rules[].match.layer4Configs[].ports[]
- IP 通訊協定:
- 來源安全標記:
resource.rules[].match.srcSecureTags[]
- 名稱:
resource.rules[].match.srcSecureTags[].name
- 名稱:
- 來源位址群組:
resource.rules[].match.srcAddressGroups[]
- 目的地地址群組:
resource.rules[].match.destAddressGroups[]
- 來源 FQDN:
resource.rules[].match.srcFqdns[]
- 目的地 FQDN:
resource.rules[].match.destFqdns[]
- 來源區碼:
resource.rules[].match.srcReigonCodes[]
- 目的地地區代碼:
resource.rules[].match.destReigonCodes[]
- 來源網路威脅情報清單:
resource.rules[].match.srcThreatIntelligences[]
- 目的地網路威脅情報清單:
resource.rules[].match.destThreatIntelligences[]
- 規則名稱:
事前準備
-
如果尚未設定驗證,請先完成設定。
驗證可確認您的身分,以便存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:
gcloud init
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
- Set a default region and zone.
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:
gcloud init
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
- 請確認您知道機構 ID。
必要的角色
如要取得管理 Cloud NGFW 資源組織政策所需的權限,請要求管理員授予下列 IAM 角色:
-
機構政策管理員 (
roles/orgpolicy.policyAdmin
) 機構資源 -
如要測試限制條件,請執行下列操作:
-
Compute 網路管理員 (
roles/compute.networkAdmin
) 專案 -
專案的服務帳戶使用者 (
roles/iam.serviceAccountUser
)
-
Compute 網路管理員 (
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
這些預先定義的角色具備管理 Cloud NGFW 資源組織政策所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要管理 Cloud NGFW 資源的組織政策,必須具備下列權限:
-
orgpolicy.constraints.list
-
orgpolicy.policies.create
-
orgpolicy.policies.delete
-
orgpolicy.policies.list
-
orgpolicy.policies.update
-
orgpolicy.policy.get
-
orgpolicy.policy.set
設定自訂限制
您可以使用 Google Cloud 控制台或 Google Cloud CLI 建立自訂限制,並設定在機構政策中使用。
控制台
前往 Google Cloud 控制台的「Organization policies」(機構政策) 頁面。
選取頁面頂端的「專案挑選器」。
在專案選擇工具中,選取要設定機構政策的資源。
按一下「自訂限制」
。在「顯示名稱」方塊中,輸入容易理解的限制名稱。這個欄位的長度上限為 200 個字元。 請勿在限制名稱中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。
在「Constraint ID」(限制條件 ID) 方塊中,輸入新自訂限制條件的名稱。自訂限制條件開頭須為
custom.
,且只能包含大寫英文字母、小寫英文字母或數字,例如custom.createFirewallPolicy
。這個欄位的長度上限為 70 個字元,不含前置字元,例如organizations/123456789/customConstraints/custom.
。在「Description」方塊中,輸入限制的易讀說明,違反政策時會以錯誤訊息形式顯示。這個欄位的長度上限為 2,000 個字元。
在「Resource type」方塊中,選取包含要限制物件和欄位的 Google Cloud REST 資源名稱。例如:
compute.googleapis.com/FirewallPolicy
。在「強制執行方式」下方,選取要僅對 REST
CREATE
方法強制執行限制,還是對 RESTCREATE
和UPDATE
方法都強制執行限制。如要定義條件,請按一下「編輯條件」
。在「Add condition」(新增條件) 面板中,建立參照支援服務資源的 CEL 條件。這個欄位的長度上限為 1,000 個字元。
按一下「儲存」。
在「動作」下方,選取是否要在符合上述條件時允許或拒絕評估方法。
按一下「建立限制」。
在每個欄位中輸入值後,右側會顯示這個自訂限制的對等 YAML 設定。
gcloud
如要使用 Google Cloud CLI 建立自訂限制,請為自訂限制建立 YAML 檔案:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME resource_types: compute.googleapis.com/RESOURCE_NAME method_types: – METHOD1 – METHOD2 condition: "CONDITION" action_type: ACTION display_name: DISPLAY_NAME description: DESCRIPTION
更改下列內容:
ORGANIZATION_ID
:您的機構 ID,例如123456789
。CONSTRAINT_NAME
:新自訂限制的名稱。自訂限制條件開頭須為custom.
,且只能包含大寫英文字母、小寫英文字母或數字,例如custom.createFirewallPolicy
。這個欄位的長度上限為 70 個字元,不含前置字元,例如organizations/123456789/customConstraints/custom
。RESOURCE_NAME
:包含要限制物件和欄位的 Compute Engine API REST 資源名稱 (而非 URI)。例如:FirewallPolicy
。METHOD1,METHOD2,...
:要強制執行限制的 RESTful 方法清單。可以是CREATE
或CREATE
和UPDATE
。CONDITION
:針對支援服務資源的代表項目編寫的 CEL 條件。這個欄位的長度上限為 1,000 個字元。如要進一步瞭解可編寫條件的資源,請參閱「支援的資源」。ACTION
:如果符合condition
,則要採取的動作。可以是ALLOW
或DENY
。DISPLAY_NAME
:限制條件的易記名稱。這個欄位的長度上限為 200 個字元。DESCRIPTION
:違反政策時,要以錯誤訊息形式顯示的限制說明。這個欄位的長度上限為 2,000 個字元。
如要進一步瞭解如何建立自訂限制,請參閱「定義自訂限制」。
為新的自訂限制建立 YAML 檔案後,您必須進行設定,才能在貴機構的機構政策中使用該檔案。如要設定自訂限制,請使用gcloud org-policies set-custom-constraint
指令: 將gcloud org-policies set-custom-constraint CONSTRAINT_PATH
CONSTRAINT_PATH
替換為自訂限制檔案的完整路徑。例如:/home/user/customconstraint.yaml
。 完成後,自訂限制就會顯示在 Google Cloud 機構政策清單中,做為機構政策使用。如要確認自訂限制條件是否存在,請使用gcloud org-policies list-custom-constraints
指令: 將gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
ORGANIZATION_ID
替換為機構資源的 ID。 詳情請參閱「查看組織政策」。強制執行自訂限制
如要強制執行限制,請建立參照該限制的機構政策,然後將該政策套用至 Google Cloud 資源。控制台
- 前往 Google Cloud 控制台的「Organization policies」(機構政策) 頁面。
- 在專案選擇工具中,選取要設定機構政策的專案。
- 在「Organization policies」(機構政策) 頁面上的清單中選取限制條件,即可查看該限制條件的「Policy details」(政策詳情) 頁面。
- 如要設定資源的機構政策,請按一下「管理政策」。
- 在「編輯政策」頁面中,選取「覆寫上層政策」。
- 按一下「新增規則」。
- 在「Enforcement」(強制執行) 區段中,選取是否要強制執行這項機構政策。
- 選用:如要根據標記設定機構政策條件,請按一下「新增條件」。請注意,如果為組織政策新增條件式規則,您必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「使用標記設定組織政策」。
- 按一下「測試變更」,模擬機構政策的影響。舊版受管理限制不支援政策模擬。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
- 如要完成並套用機構政策,請按一下「設定政策」。這項政策最多需要 15 分鐘才會生效。
gcloud
如要建立含有布林值規則的機構政策,請建立參照限制的政策 YAML 檔案:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
取代下列項目:
-
PROJECT_ID
:您要強制執行限制的專案。 -
CONSTRAINT_NAME
:您為自訂限制定義的名稱。例如:
。custom.createFirewallPolicy
如要強制執行包含限制的機構政策,請執行下列指令:
gcloud org-policies set-policy POLICY_PATH
將
POLICY_PATH
替換為機構政策 YAML 檔案的完整路徑。這項政策最多需要 15 分鐘才會生效。範例:建立限制,強制所有防火牆規則啟用記錄功能
這項限制會禁止建立未啟用記錄功能的防火牆政策規則。含有
goto_next
動作的防火牆政策規則會遭到排除,因為這類規則不支援記錄功能。gcloud
建立包含下列資訊的
enforceLoggingEnabled.yaml
限制檔案。name: organizations/ORGANIZATION_ID/customConstraints/custom.enforceLoggingEnabled resource_types: compute.googleapis.com/FirewallPolicy condition: "resource.rules.exists(rule, rule.action != 'goto_next' && rule.enableLogging == false)" action_type: DENY method_types: [CREATE, UPDATE] display_name: Enforce that all rules have logging enabled description: Firewall policy rules with action other than goto_next can only be created when firewall rules logging is enabled.
將
ORGANIZATION_ID
替換為機構 ID。設定自訂限制。
gcloud org-policies set-custom-constraint enforceLoggingEnabled.yaml
使用下列範例提供的資訊建立
enforceLoggingEnabled-policy.yaml
政策檔案,並在專案層級強制執行這項限制。您也可以在機構或資料夾層級設定這項功能。name: projects/PROJECT_ID/policies/custom.enforceLoggingEnabled spec: rules: – enforce: true
將
PROJECT_ID
替換為您的專案 ID。強制執行政策。
gcloud org-policies set-policy enforceLoggingEnabled-policy.yaml
如要測試限制,請建立防火牆政策規則,允許通訊埠
22
上的輸入 TCP 流量,並停用記錄功能。gcloud compute network-firewall-policies create test-fw-policy --global
gcloud compute network-firewall-policies rules create 1000 \ --action ALLOW \ --direction INGRESS \ --firewall-policy test-fw-policy \ --src-ip-ranges 0.0.0.0/0 \ --layer4-configs tcp:22 \ --no-enable-logging \ --global-firewall-policy
輸出結果會與下列內容相似:
ERROR: (gcloud.compute.network-firewall-policies.create) Could not fetch resource: - Operation denied by custom org policy: [customConstraints/custom.enforceLoggingEnabled] :Firewall policy rules with action other than goto_next can only be created when firewall rules logging is enabled.
刪除上一個步驟中建立的防火牆政策。
gcloud compute network-firewall-policies delete test-fw-policy --global
範例:建立限制,強制所有輸入 SSH 防火牆規則都必須有特定來源範圍
這項限制會強制執行防火牆政策規則,允許輸入的 SSH 流量必須具有以
192.168.
區塊開頭的來源 IP 範圍。gcloud
建立
restrictFirewallPolicyRulesSshRanges.yaml
限制檔案,並加入下列資訊。name: organizations/$ORGANIZATION_ID/customConstraints/custom.restrictFirewallPolicyRulesSshRanges resource_types: compute.googleapis.com/FirewallPolicy condition: "resource.rules.exists(rule, rule.priority < 2147483644 && (rule.direction == 'INGRESS') && !rule.match.srcIpRanges.all(ipRange, ipRange.startsWith('192.168.')) && rule.match.layer4Configs.all(l4config, l4config.ipProtocol == 'tcp' && l4config.ports.all(port, port == '22')) )" action_type: DENY method_types: [CREATE, UPDATE] display_name: Limit firewall policy rules that allow ingress SSH traffic description: Firewall Policy rules that allow ingress SSH traffic can only be created with allowed source ranges.
將
ORGANIZATION_ID
替換為機構 ID。設定自訂限制。
gcloud org-policies set-custom-constraint restrictFirewallPolicyRulesSshRanges.yaml
建立
restrictFirewallPolicyRulesSshRanges-policy.yaml
政策檔案,其中包含下列範例提供的資訊,並在專案層級強制執行限制。您也可以在機構或資料夾層級設定這項限制。name: projects/PROJECT_ID/policies/custom.restrictFirewallPolicyRulesSshRanges spec: rules: – enforce: true
將
PROJECT_ID
替換為您的專案 ID。強制執行政策。
gcloud org-policies set-policy restrictFirewallPolicyRulesSshRanges-policy.yaml
如要測試限制,請建立防火牆政策規則,允許通訊埠
22
上的 SSH 輸入 TCP 流量,來源 IP 範圍為10.0.0.0/0
。gcloud compute network-firewall-policies create test-fw-policy --global
gcloud compute network-firewall-policies rules create 1000 \ --action ALLOW \ --direction INGRESS \ --firewall-policy test-fw-policy \ --src-ip-ranges 10.0.0.0/8 \ --layer4-configs tcp:22 \ --global-firewall-policy
輸出結果會與下列內容相似:
ERROR: (gcloud.compute.network-firewall-policies.create) Could not fetch resource: - Operation denied by custom org policy: [customConstraints/custom.restrictFirewallPolicyRulesSshRanges]: Firewall Policy rules that allow ingress SSH traffic can only be created with allowed source ranges.
刪除上一個步驟中建立的防火牆政策。
gcloud compute network-firewall-policies delete test-fw-policy --global
定價
機構政策服務 (包括預先定義和自訂機構政策) 免費提供。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
-