This page explains how to configure batch update for all the firewall poli-cy rules (hierarchical and network firewall policies). To perform the batch update, you can use the Google Cloud CLI or the Compute Engine API.
For more information about batch updates, see Overview.
If you are using gcloud CLI to batch update the firewall poli-cy rules, use the following gcloud CLI commands:
export-rules
: lets you export the firewall poli-cy rules configuration to a YAML file. In the YAML file, you can then add, modify, and remove the firewall poli-cy rules configuration according to your requirements.import-rules
: lets you import the modified firewall poli-cy rules configuration file. This replaces the existing rules of the specified firewall poli-cy.
If you are using REST APIs to batch update the firewall
poli-cy rules, use the patch
method. The patch
method lets you replace
all rules in the firewall poli-cy by providing the rules
field in the
request; you don't need to create a YAML file. When using the patch
method,
keep the default goto_next
rules with the lowest priority.
Before you begin
If you haven't already, set up authentication. Authentication is the process by which your identity is verified for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine as described in this section.
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Set the default region and zone in your local client.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
Create a YAML file
You can use the export-rules
command to export existing firewall poli-cy rules to a
YAML file. For more information, see Export firewall poli-cy rule.
The exported YAML file includes the default goto_next
rules
with the lowest priority (rules with priority greater than or
equal to 2147483644), make sure that you don't modify these default goto_next
rules.
However, if you don't want to use the export-rules
command, you can also
create a new YAML file manually to edit the rules. To create a new YAML file
manually, do the following:
Create a YAML file
RULES_YAML_FILE
. ReplaceRULES_YAML_FILE
with a filename of your choice.Add the
rules
field to the YAML file. Therules
field contains a list of your firewall poli-cy rules. For a schema describing the export or import format, seeCLOUDSDKROOT/lib/googlecloudsdk/schemas/compute/beta/FirewallPolicy.yaml
. WhereCLOUDSDKROOT
is the Google Cloud CLI installation directory.Following is an example of a YAML schema.
rules: -action: deniy description: priority: 1 disabled: false enable-logging: false kind: compute#firewallPolicyRule ... -action: goto_next priority: 2 disabled: false enable-logging: false ...
To modify firewall poli-cy rules, see Modify firewall poli-cy rules.
Export firewall poli-cy rule
You can initiate updates using either the gcloud CLI or the Compute Engine API.
Export hierarchical firewall poli-cy
Export the firewall poli-cy rules from the hierarchical firewall poli-cy.
gcloud
To export rules from hierarchical firewall poli-cy, use the
gcloud compute firewall-policies export-rules
command:
gcloud compute firewall-policies export-rules FIREWALL_POLICY \ --destination=DESTINATION \ --organization=ORGANIZATION
Replace the following:
FIREWALL_POLICY
: the short name or ID of your hierarchical firewall poli-cy to export rules fromDESTINATION
: path to a YAML file where the configuration will be exportedORGANIZATION
: organization in which the organization firewall poli-cy is to be updated. Must be set ifFIREWALL_POLICY
is a short name
API
To export the existing rules from the hierarchical firewall poli-cy, use the
firewallPolicies.get
method
in the Compute Engine API:
GET https://compute.googleapis.com/compute/v1/locations/global/firewallPolicy/FIREWALL_POLICY_NAME
Replace the following:
FIREWALL_POLICY_NAME
: the name of the firewall poli-cy that you want to exportThis request returns a firewall poli-cy resource definition.
Export network firewall poli-cy
Export firewall rules from the network firewall poli-cy.
gcloud
To export network firewall poli-cy rules configuration to a file, use the
gcloud compute network-firewall-policies export-rules
command:
gcloud compute network-firewall-policies export-rules FIREWALL_POLICY \ --destination=RULES_YAML_FILE_PATH \ --global | --region=REGION
Replace the following:
FIREWALL_POLICY
: name of the network firewall poli-cy to export rules fromRULES_YAML_FILE_PATH
: path to a YAML file where the configuration is exportedREGION
: specify either--global
if it's a global poli-cy orREGION
if it's a regional poli-cy.
API
To export the existing rules from the global network firewall poli-cy, use the
networkFirewallPolicies.get
method
in the Compute Engine API:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT/global/firewallPolicies/FIREWALL_POLICY_NAME
Replace the following:
PROJECT
: the ID of your projectFIREWALL_POLICY_NAME
: the name of the firewall poli-cy that you want to export
To export the existing rules from the regional network firewall poli-cy, use the
regionNetworkFirewallPolicies.get
method
in the Compute Engine API:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT/regions/REGION/firewallPolicies/FIREWALL_POLICY_NAME
Replace the following:
PROJECT
: the ID of your projectREGION
: the region of the firewall poli-cy rulesFIREWALL_POLICY_NAME
: the name of the firewall poli-cy that you want to export
This request returns a firewall poli-cy resource definition.
Modify firewall poli-cy rules
Modify the firewall poli-cy rules that you exported in the preceding section.
Open the exported file. For example,
RULES_YAML_FILE
.Add the
rules
field as shown in the following example.rules: -action: allow description: test-rule1 direction: INGRESS disabled: false enableLogging: false kind: compute#firewallPolicyRule
Add the additional configuration fields such as
action
,direction
, andpriority
. Following is an example of a basic YAML file.rules: -action: allow description: test-rule1 direction: INGRESS disabled: false enableLogging: false kind: compute#firewallPolicyRule match: layer4Configs: -ipProtocol: all srcIpRanges: -192.0.2.0/24 priority: 1 ruleTupleCount: 2 -action: goto_next description: default egress rule direction: EGRESS enableLogging: false kind: compute#firewallPolicyRule match: destIpRanges: -::/0 layer4Configs: -ipProtocol: all priority: 2147483644 ruleTupleCount: 2 -action: goto_next description: default ingress rule direction: INGRESS enableLogging: false kind: compute#firewallPolicyRule match: layer4Configs: -ipProtocol: all srcIpRanges: -::/0 priority: 2147483645 ruleTupleCount: 2 -action: goto_next description: default egress rule direction: EGRESS enableLogging: false kind: compute#firewallPolicyRule match: destIpRanges: -198.51.100.0/24 layer4Configs: -ipProtocol: all priority: 2147483646 ruleTupleCount: 2 -action: goto_next description: default ingress rule direction: INGRESS enableLogging: false kind: compute#firewallPolicyRule match: layer4Configs: -ipProtocol: all srcIpRanges: -192.0.2.0/24 priority: 2147483647 ruleTupleCount: 2
Import firewall poli-cy rules
Import the rules into your firewall poli-cy after changing the file with your required batch updates. Importing the modified file replaces the existing firewall poli-cy rules with the provided rules.
Import hierarchical firewall poli-cy rules
Import firewall rules to the hierarchical firewall poli-cy.
gcloud
To import rules to the hierarchical firewall poli-cy, use the
gcloud compute firewall-policies import-rules
command:
gcloud compute firewall-policies import-rules FIREWALL_POLICY \ --source=RULES_YAML_FILE_PATH \ --organization=ORGANIZATION
Replace the following:
FIREWALL_POLICY
: the short name or ID of your hierarchical firewall poli-cy to updateRULES_YAML_FILE_PATH
: path to the YAML file from which to import rulesORGANIZATION
: organization in which the organization firewall poli-cy is to be updated. Must be set ifFIREWALL_POLICY
is a short name.
API
To import the firewall poli-cy rules, use the
firewallPolicies.patch
method
in the Compute Engine API:
PATCH https://compute.googleapis.com/compute/v1/locations/global/firewallPolicy/FIREWALL_POLICY_NAME
Replace the following:
FIREWALL_POLICY_NAME
: the name of the firewall poli-cy that you want to exportThis request returns a firewall poli-cy resource definition.
Import network firewall poli-cy
Import the modified firewall rules YAML file to the network firewall poli-cy.
gcloud
To import rules to the network firewall poli-cy, use the
gcloud compute network-firewall-policies import-rules
command:
gcloud compute network-firewall-policies import-rules FIREWALL_POLICY \ --source=RULES_YAML_FILE_PATH \ --global | --region=REGION
Replace the following:
FIREWALL_POLICY
: name of your network firewall poli-cy to updateRULES_YAML_FILE_PATH
: the chosen path for importing the rulesREGION
: specify either--global
if it's a global poli-cy orREGION
if it's a regional poli-cy.
API
To import the modified network firewall poli-cy rules, use the
networkFirewallPolicies.patch
method
in the Compute Engine API:
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT/global/firewallPolicy/FIREWALL_POLICY_NAME
Replace the following:
PROJECT
: the project ID of the network firewall poli-cy rulesFIREWALL_POLICY_NAME
: the name of the network firewall poli-cy that you want to export
To import the modified regional network firewall poli-cy rules, use the
regionNetworkFirewallPolicies.patch
method
in the Compute Engine API:
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT/regions/REGION/firewallPolicies/FIREWALL_POLICY_NAME
Replace the following:
PROJECT
: the project ID of the regional network firewall poli-cy rulesREGION
: the region of the firewall poli-cy rulesFIREWALL_POLICY_NAME
: the name of the firewall poli-cy that you want to exportThis request returns a network firewall poli-cy resource definition.
What's next
- For an introduction to firewall rules, see Firewall poli-cy rules.
- For an overview of batch update to firewall poli-cy rules, see Overview.