| --- |
| # ---------------------------------------------------------------------------- |
| # |
| # *** AUTO GENERATED CODE *** Type: MMv1 *** |
| # |
| # ---------------------------------------------------------------------------- |
| # |
| # This file is automatically generated by Magic Modules and manual |
| # changes will be clobbered when the file is regenerated. |
| # |
| # Please read more about how to change this file in |
| # .github/CONTRIBUTING.md. |
| # |
| # ---------------------------------------------------------------------------- |
| subcategory: "Compute Engine" |
| description: |- |
| A rule for the SecurityPolicy. |
| --- |
| |
| # google_compute_security_policy_rule |
| |
| A rule for the SecurityPolicy. |
| |
| |
| To get more information about SecurityPolicyRule, see: |
| |
| * [API documentation](https://cloud.google.com/compute/docs/reference/rest/v1/securityPolicies/addRule) |
| * How-to Guides |
| * [Creating global security policy rules](https://cloud.google.com/armor/docs/configure-security-policies) |
| |
| <div class = "oics-button" style="float: right; margin: 0 0 -15px"> |
| <a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=security_policy_rule_basic&open_in_editor=main.tf" target="_blank"> |
| <img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;"> |
| </a> |
| </div> |
| ## Example Usage - Security Policy Rule Basic |
| |
| |
| ```hcl |
| resource "google_compute_security_policy" "default" { |
| name = "policyruletest" |
| description = "basic global security policy" |
| type = "CLOUD_ARMOR" |
| } |
| |
| resource "google_compute_security_policy_rule" "policy_rule" { |
| security_policy = google_compute_security_policy.default.name |
| description = "new rule" |
| priority = 100 |
| match { |
| versioned_expr = "SRC_IPS_V1" |
| config { |
| src_ip_ranges = ["10.10.0.0/16"] |
| } |
| } |
| action = "allow" |
| preview = true |
| } |
| ``` |
| ## Example Usage - Security Policy Rule Default Rule |
| |
| |
| ```hcl |
| resource "google_compute_security_policy" "default" { |
| name = "policyruletest" |
| description = "basic global security policy" |
| type = "CLOUD_ARMOR" |
| } |
| |
| # A default rule is generated when creating the security_policy resource, import is needed to patch it |
| # import { |
| # id = "projects//global/securityPolicies/policyruletest/priority/2147483647" |
| # to = google_compute_security_policy_rule.default_rule |
| # } |
| resource "google_compute_security_policy_rule" "default_rule" { |
| security_policy = google_compute_security_policy.default.name |
| description = "default rule" |
| action = "allow" |
| priority = "2147483647" |
| match { |
| versioned_expr = "SRC_IPS_V1" |
| config { |
| src_ip_ranges = ["*"] |
| } |
| } |
| } |
| |
| resource "google_compute_security_policy_rule" "policy_rule" { |
| security_policy = google_compute_security_policy.default.name |
| description = "new rule" |
| priority = 100 |
| match { |
| versioned_expr = "SRC_IPS_V1" |
| config { |
| src_ip_ranges = ["10.10.0.0/16"] |
| } |
| } |
| action = "allow" |
| preview = true |
| } |
| ``` |
| <div class = "oics-button" style="float: right; margin: 0 0 -15px"> |
| <a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=security_policy_rule_multiple_rules&open_in_editor=main.tf" target="_blank"> |
| <img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;"> |
| </a> |
| </div> |
| ## Example Usage - Security Policy Rule Multiple Rules |
| |
| |
| ```hcl |
| resource "google_compute_security_policy" "default" { |
| name = "policywithmultiplerules" |
| description = "basic global security policy" |
| type = "CLOUD_ARMOR" |
| } |
| |
| resource "google_compute_security_policy_rule" "policy_rule_one" { |
| security_policy = google_compute_security_policy.default.name |
| description = "new rule one" |
| priority = 100 |
| match { |
| versioned_expr = "SRC_IPS_V1" |
| config { |
| src_ip_ranges = ["10.10.0.0/16"] |
| } |
| } |
| action = "allow" |
| preview = true |
| } |
| |
| resource "google_compute_security_policy_rule" "policy_rule_two" { |
| security_policy = google_compute_security_policy.default.name |
| description = "new rule two" |
| priority = 101 |
| match { |
| versioned_expr = "SRC_IPS_V1" |
| config { |
| src_ip_ranges = ["192.168.0.0/16", "10.0.0.0/8"] |
| } |
| } |
| action = "allow" |
| preview = true |
| } |
| ``` |
| |
| ## Argument Reference |
| |
| The following arguments are supported: |
| |
| |
| * `priority` - |
| (Required) |
| An integer indicating the priority of a rule in the list. |
| The priority must be a positive value between 0 and 2147483647. |
| Rules are evaluated from highest to lowest priority where 0 is the highest priority and 2147483647 is the lowest priority. |
| |
| * `action` - |
| (Required) |
| The Action to perform when the rule is matched. The following are the valid actions: |
| * allow: allow access to target. |
| * deny(STATUS): deny access to target, returns the HTTP response code specified. Valid values for STATUS are 403, 404, and 502. |
| * rate_based_ban: limit client traffic to the configured threshold and ban the client if the traffic exceeds the threshold. Configure parameters for this action in RateLimitOptions. Requires rateLimitOptions to be set. |
| * redirect: redirect to a different target. This can either be an internal reCAPTCHA redirect, or an external URL-based redirect via a 302 response. Parameters for this action can be configured via redirectOptions. This action is only supported in Global Security Policies of type CLOUD_ARMOR. |
| * throttle: limit client traffic to the configured threshold. Configure parameters for this action in rateLimitOptions. Requires rateLimitOptions to be set for this. |
| |
| * `security_policy` - |
| (Required) |
| The name of the security policy this rule belongs to. |
| |
| |
| - - - |
| |
| |
| * `description` - |
| (Optional) |
| An optional description of this resource. Provide this property when you create the resource. |
| |
| * `match` - |
| (Optional) |
| A match condition that incoming traffic is evaluated against. |
| If it evaluates to true, the corresponding 'action' is enforced. |
| Structure is [documented below](#nested_match). |
| |
| * `preconfigured_waf_config` - |
| (Optional) |
| Preconfigured WAF configuration to be applied for the rule. |
| If the rule does not evaluate preconfigured WAF rules, i.e., if evaluatePreconfiguredWaf() is not used, this field will have no effect. |
| Structure is [documented below](#nested_preconfigured_waf_config). |
| |
| * `rate_limit_options` - |
| (Optional) |
| Must be specified if the action is "rate_based_ban" or "throttle". Cannot be specified for any other actions. |
| Structure is [documented below](#nested_rate_limit_options). |
| |
| * `preview` - |
| (Optional) |
| If set to true, the specified action is not enforced. |
| |
| * `project` - (Optional) The ID of the project in which the resource belongs. |
| If it is not provided, the provider project is used. |
| |
| |
| <a name="nested_match"></a>The `match` block supports: |
| |
| * `versioned_expr` - |
| (Optional) |
| Preconfigured versioned expression. If this field is specified, config must also be specified. |
| Available preconfigured expressions along with their requirements are: SRC_IPS_V1 - must specify the corresponding srcIpRange field in config. |
| Possible values are: `SRC_IPS_V1`. |
| |
| * `expr` - |
| (Optional) |
| User defined CEVAL expression. A CEVAL expression is used to specify match criteria such as origin.ip, source.region_code and contents in the request header. |
| Structure is [documented below](#nested_expr). |
| |
| * `expr_options` - |
| (Optional) |
| The configuration options available when specifying a user defined CEVAL expression (i.e., 'expr'). |
| Structure is [documented below](#nested_expr_options). |
| |
| * `config` - |
| (Optional) |
| The configuration options available when specifying versionedExpr. |
| This field must be specified if versionedExpr is specified and cannot be specified if versionedExpr is not specified. |
| Structure is [documented below](#nested_config). |
| |
| |
| <a name="nested_expr"></a>The `expr` block supports: |
| |
| * `expression` - |
| (Required) |
| Textual representation of an expression in Common Expression Language syntax. The application context of the containing message determines which well-known feature set of CEL is supported. |
| |
| <a name="nested_expr_options"></a>The `expr_options` block supports: |
| |
| * `recaptcha_options` - |
| (Required) |
| reCAPTCHA configuration options to be applied for the rule. If the rule does not evaluate reCAPTCHA tokens, this field has no effect. |
| Structure is [documented below](#nested_recaptcha_options). |
| |
| |
| <a name="nested_recaptcha_options"></a>The `recaptcha_options` block supports: |
| |
| * `action_token_site_keys` - |
| (Optional) |
| A list of site keys to be used during the validation of reCAPTCHA action-tokens. The provided site keys need to be created from reCAPTCHA API under the same project where the security policy is created. |
| |
| * `session_token_site_keys` - |
| (Optional) |
| A list of site keys to be used during the validation of reCAPTCHA session-tokens. The provided site keys need to be created from reCAPTCHA API under the same project where the security policy is created. |
| |
| <a name="nested_config"></a>The `config` block supports: |
| |
| * `src_ip_ranges` - |
| (Optional) |
| CIDR IP address range. Maximum number of srcIpRanges allowed is 10. |
| |
| <a name="nested_preconfigured_waf_config"></a>The `preconfigured_waf_config` block supports: |
| |
| * `exclusion` - |
| (Optional) |
| An exclusion to apply during preconfigured WAF evaluation. |
| Structure is [documented below](#nested_exclusion). |
| |
| |
| <a name="nested_exclusion"></a>The `exclusion` block supports: |
| |
| * `request_header` - |
| (Optional) |
| Request header whose value will be excluded from inspection during preconfigured WAF evaluation. |
| Structure is [documented below](#nested_request_header). |
| |
| * `request_cookie` - |
| (Optional) |
| Request cookie whose value will be excluded from inspection during preconfigured WAF evaluation. |
| Structure is [documented below](#nested_request_cookie). |
| |
| * `request_uri` - |
| (Optional) |
| Request URI from the request line to be excluded from inspection during preconfigured WAF evaluation. |
| When specifying this field, the query or fragment part should be excluded. |
| Structure is [documented below](#nested_request_uri). |
| |
| * `request_query_param` - |
| (Optional) |
| Request query parameter whose value will be excluded from inspection during preconfigured WAF evaluation. |
| Note that the parameter can be in the query string or in the POST body. |
| Structure is [documented below](#nested_request_query_param). |
| |
| * `target_rule_set` - |
| (Required) |
| Target WAF rule set to apply the preconfigured WAF exclusion. |
| |
| * `target_rule_ids` - |
| (Optional) |
| A list of target rule IDs under the WAF rule set to apply the preconfigured WAF exclusion. |
| If omitted, it refers to all the rule IDs under the WAF rule set. |
| |
| |
| <a name="nested_request_header"></a>The `request_header` block supports: |
| |
| * `operator` - |
| (Required) |
| You can specify an exact match or a partial match by using a field operator and a field value. |
| Available options: |
| EQUALS: The operator matches if the field value equals the specified value. |
| STARTS_WITH: The operator matches if the field value starts with the specified value. |
| ENDS_WITH: The operator matches if the field value ends with the specified value. |
| CONTAINS: The operator matches if the field value contains the specified value. |
| EQUALS_ANY: The operator matches if the field value is any value. |
| |
| * `value` - |
| (Optional) |
| A request field matching the specified value will be excluded from inspection during preconfigured WAF evaluation. |
| The field value must be given if the field operator is not EQUALS_ANY, and cannot be given if the field operator is EQUALS_ANY. |
| |
| <a name="nested_request_cookie"></a>The `request_cookie` block supports: |
| |
| * `operator` - |
| (Required) |
| You can specify an exact match or a partial match by using a field operator and a field value. |
| Available options: |
| EQUALS: The operator matches if the field value equals the specified value. |
| STARTS_WITH: The operator matches if the field value starts with the specified value. |
| ENDS_WITH: The operator matches if the field value ends with the specified value. |
| CONTAINS: The operator matches if the field value contains the specified value. |
| EQUALS_ANY: The operator matches if the field value is any value. |
| |
| * `value` - |
| (Optional) |
| A request field matching the specified value will be excluded from inspection during preconfigured WAF evaluation. |
| The field value must be given if the field operator is not EQUALS_ANY, and cannot be given if the field operator is EQUALS_ANY. |
| |
| <a name="nested_request_uri"></a>The `request_uri` block supports: |
| |
| * `operator` - |
| (Required) |
| You can specify an exact match or a partial match by using a field operator and a field value. |
| Available options: |
| EQUALS: The operator matches if the field value equals the specified value. |
| STARTS_WITH: The operator matches if the field value starts with the specified value. |
| ENDS_WITH: The operator matches if the field value ends with the specified value. |
| CONTAINS: The operator matches if the field value contains the specified value. |
| EQUALS_ANY: The operator matches if the field value is any value. |
| |
| * `value` - |
| (Optional) |
| A request field matching the specified value will be excluded from inspection during preconfigured WAF evaluation. |
| The field value must be given if the field operator is not EQUALS_ANY, and cannot be given if the field operator is EQUALS_ANY. |
| |
| <a name="nested_request_query_param"></a>The `request_query_param` block supports: |
| |
| * `operator` - |
| (Required) |
| You can specify an exact match or a partial match by using a field operator and a field value. |
| Available options: |
| EQUALS: The operator matches if the field value equals the specified value. |
| STARTS_WITH: The operator matches if the field value starts with the specified value. |
| ENDS_WITH: The operator matches if the field value ends with the specified value. |
| CONTAINS: The operator matches if the field value contains the specified value. |
| EQUALS_ANY: The operator matches if the field value is any value. |
| |
| * `value` - |
| (Optional) |
| A request field matching the specified value will be excluded from inspection during preconfigured WAF evaluation. |
| The field value must be given if the field operator is not EQUALS_ANY, and cannot be given if the field operator is EQUALS_ANY. |
| |
| <a name="nested_rate_limit_options"></a>The `rate_limit_options` block supports: |
| |
| * `rate_limit_threshold` - |
| (Optional) |
| Threshold at which to begin ratelimiting. |
| Structure is [documented below](#nested_rate_limit_threshold). |
| |
| * `conform_action` - |
| (Optional) |
| Action to take for requests that are under the configured rate limit threshold. |
| Valid option is "allow" only. |
| |
| * `exceed_redirect_options` - |
| (Optional) |
| Parameters defining the redirect action that is used as the exceed action. Cannot be specified if the exceed action is not redirect. This field is only supported in Global Security Policies of type CLOUD_ARMOR. |
| Structure is [documented below](#nested_exceed_redirect_options). |
| |
| * `exceed_action` - |
| (Optional) |
| Action to take for requests that are above the configured rate limit threshold, to either deny with a specified HTTP response code, or redirect to a different endpoint. |
| Valid options are deny(STATUS), where valid values for STATUS are 403, 404, 429, and 502. |
| |
| * `enforce_on_key` - |
| (Optional) |
| Determines the key to enforce the rateLimitThreshold on. Possible values are: |
| * ALL: A single rate limit threshold is applied to all the requests matching this rule. This is the default value if "enforceOnKey" is not configured. |
| * IP: The source IP address of the request is the key. Each IP has this limit enforced separately. |
| * HTTP_HEADER: The value of the HTTP header whose name is configured under "enforceOnKeyName". The key value is truncated to the first 128 bytes of the header value. If no such header is present in the request, the key type defaults to ALL. |
| * XFF_IP: The first IP address (i.e. the originating client IP address) specified in the list of IPs under X-Forwarded-For HTTP header. If no such header is present or the value is not a valid IP, the key defaults to the source IP address of the request i.e. key type IP. |
| * HTTP_COOKIE: The value of the HTTP cookie whose name is configured under "enforceOnKeyName". The key value is truncated to the first 128 bytes of the cookie value. If no such cookie is present in the request, the key type defaults to ALL. |
| * HTTP_PATH: The URL path of the HTTP request. The key value is truncated to the first 128 bytes. |
| * SNI: Server name indication in the TLS session of the HTTPS request. The key value is truncated to the first 128 bytes. The key type defaults to ALL on a HTTP session. |
| * REGION_CODE: The country/region from which the request originates. |
| * TLS_JA3_FINGERPRINT: JA3 TLS/SSL fingerprint if the client connects using HTTPS, HTTP/2 or HTTP/3. If not available, the key type defaults to ALL. |
| * USER_IP: The IP address of the originating client, which is resolved based on "userIpRequestHeaders" configured with the security policy. If there is no "userIpRequestHeaders" configuration or an IP address cannot be resolved from it, the key type defaults to IP. |
| Possible values are: `ALL`, `IP`, `HTTP_HEADER`, `XFF_IP`, `HTTP_COOKIE`, `HTTP_PATH`, `SNI`, `REGION_CODE`, `TLS_JA3_FINGERPRINT`, `USER_IP`. |
| |
| * `enforce_on_key_name` - |
| (Optional) |
| Rate limit key name applicable only for the following key types: |
| HTTP_HEADER -- Name of the HTTP header whose value is taken as the key value. |
| HTTP_COOKIE -- Name of the HTTP cookie whose value is taken as the key value. |
| |
| * `enforce_on_key_configs` - |
| (Optional) |
| If specified, any combination of values of enforceOnKeyType/enforceOnKeyName is treated as the key on which ratelimit threshold/action is enforced. |
| You can specify up to 3 enforceOnKeyConfigs. |
| If enforceOnKeyConfigs is specified, enforceOnKey must not be specified. |
| Structure is [documented below](#nested_enforce_on_key_configs). |
| |
| * `ban_threshold` - |
| (Optional) |
| Can only be specified if the action for the rule is "rate_based_ban". |
| If specified, the key will be banned for the configured 'banDurationSec' when the number of requests that exceed the 'rateLimitThreshold' also exceed this 'banThreshold'. |
| Structure is [documented below](#nested_ban_threshold). |
| |
| * `ban_duration_sec` - |
| (Optional) |
| Can only be specified if the action for the rule is "rate_based_ban". |
| If specified, determines the time (in seconds) the traffic will continue to be banned by the rate limit after the rate falls below the threshold. |
| |
| |
| <a name="nested_rate_limit_threshold"></a>The `rate_limit_threshold` block supports: |
| |
| * `count` - |
| (Optional) |
| Number of HTTP(S) requests for calculating the threshold. |
| |
| * `interval_sec` - |
| (Optional) |
| Interval over which the threshold is computed. |
| |
| <a name="nested_exceed_redirect_options"></a>The `exceed_redirect_options` block supports: |
| |
| * `type` - |
| (Optional) |
| Type of the redirect action. |
| |
| * `target` - |
| (Optional) |
| Target for the redirect action. This is required if the type is EXTERNAL_302 and cannot be specified for GOOGLE_RECAPTCHA. |
| |
| <a name="nested_enforce_on_key_configs"></a>The `enforce_on_key_configs` block supports: |
| |
| * `enforce_on_key_type` - |
| (Optional) |
| Determines the key to enforce the rateLimitThreshold on. Possible values are: |
| * ALL: A single rate limit threshold is applied to all the requests matching this rule. This is the default value if "enforceOnKeyConfigs" is not configured. |
| * IP: The source IP address of the request is the key. Each IP has this limit enforced separately. |
| * HTTP_HEADER: The value of the HTTP header whose name is configured under "enforceOnKeyName". The key value is truncated to the first 128 bytes of the header value. If no such header is present in the request, the key type defaults to ALL. |
| * XFF_IP: The first IP address (i.e. the originating client IP address) specified in the list of IPs under X-Forwarded-For HTTP header. If no such header is present or the value is not a valid IP, the key defaults to the source IP address of the request i.e. key type IP. |
| * HTTP_COOKIE: The value of the HTTP cookie whose name is configured under "enforceOnKeyName". The key value is truncated to the first 128 bytes of the cookie value. If no such cookie is present in the request, the key type defaults to ALL. |
| * HTTP_PATH: The URL path of the HTTP request. The key value is truncated to the first 128 bytes. |
| * SNI: Server name indication in the TLS session of the HTTPS request. The key value is truncated to the first 128 bytes. The key type defaults to ALL on a HTTP session. |
| * REGION_CODE: The country/region from which the request originates. |
| * TLS_JA3_FINGERPRINT: JA3 TLS/SSL fingerprint if the client connects using HTTPS, HTTP/2 or HTTP/3. If not available, the key type defaults to ALL. |
| * USER_IP: The IP address of the originating client, which is resolved based on "userIpRequestHeaders" configured with the security policy. If there is no "userIpRequestHeaders" configuration or an IP address cannot be resolved from it, the key type defaults to IP. |
| Possible values are: `ALL`, `IP`, `HTTP_HEADER`, `XFF_IP`, `HTTP_COOKIE`, `HTTP_PATH`, `SNI`, `REGION_CODE`, `TLS_JA3_FINGERPRINT`, `USER_IP`. |
| |
| * `enforce_on_key_name` - |
| (Optional) |
| Rate limit key name applicable only for the following key types: |
| HTTP_HEADER -- Name of the HTTP header whose value is taken as the key value. |
| HTTP_COOKIE -- Name of the HTTP cookie whose value is taken as the key value. |
| |
| <a name="nested_ban_threshold"></a>The `ban_threshold` block supports: |
| |
| * `count` - |
| (Optional) |
| Number of HTTP(S) requests for calculating the threshold. |
| |
| * `interval_sec` - |
| (Optional) |
| Interval over which the threshold is computed. |
| |
| ## Attributes Reference |
| |
| In addition to the arguments listed above, the following computed attributes are exported: |
| |
| * `id` - an identifier for the resource with format `projects/{{project}}/global/securityPolicies/{{security_policy}}/priority/{{priority}}` |
| |
| |
| ## Timeouts |
| |
| This resource provides the following |
| [Timeouts](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts) configuration options: |
| |
| - `create` - Default is 20 minutes. |
| - `update` - Default is 20 minutes. |
| - `delete` - Default is 20 minutes. |
| |
| ## Import |
| |
| |
| SecurityPolicyRule can be imported using any of these accepted formats: |
| |
| * `projects/{{project}}/global/securityPolicies/{{security_policy}}/priority/{{priority}}` |
| * `{{project}}/{{security_policy}}/{{priority}}` |
| * `{{security_policy}}/{{priority}}` |
| |
| |
| In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import SecurityPolicyRule using one of the formats above. For example: |
| |
| ```tf |
| import { |
| id = "projects/{{project}}/global/securityPolicies/{{security_policy}}/priority/{{priority}}" |
| to = google_compute_security_policy_rule.default |
| } |
| ``` |
| |
| When using the [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import), SecurityPolicyRule can be imported using one of the formats above. For example: |
| |
| ``` |
| $ terraform import google_compute_security_policy_rule.default projects/{{project}}/global/securityPolicies/{{security_policy}}/priority/{{priority}} |
| $ terraform import google_compute_security_policy_rule.default {{project}}/{{security_policy}}/{{priority}} |
| $ terraform import google_compute_security_policy_rule.default {{security_policy}}/{{priority}} |
| ``` |
| |
| ## User Project Overrides |
| |
| This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override). |