# Copyright 2024 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
name: 'AuthzPolicy'
description: |
  AuthzPolicy is a resource that allows to forward traffic to a callout backend designed to scan the traffic for security purposes.
references:
  guides:
  api: 'https://cloud.google.com/load-balancing/docs/reference/network-security/rest/v1beta1/projects.locations.authzPolicies'
docs:
base_url: 'projects/{{project}}/locations/{{location}}/authzPolicies'
self_link: 'projects/{{project}}/locations/{{location}}/authzPolicies/{{name}}'
create_url: 'projects/{{project}}/locations/{{location}}/authzPolicies?authzPolicyId={{name}}'
update_verb: 'PATCH'
update_mask: true
import_format:
  - 'projects/{{project}}/locations/{{location}}/authzPolicies/{{name}}'
  - '{{name}}'
timeouts:
  insert_minutes: 30
  update_minutes: 30
  delete_minutes: 30
autogen_async: true
async:
  actions: ['create', 'delete', 'update']
  type: 'OpAsync'
  operation:
    base_url: '{{op_id}}'
    timeouts:
      insert_minutes: 30
      update_minutes: 30
      delete_minutes: 30
  result:
    resource_inside_response: false
custom_code:
sweeper:
  url_substitutions:
    - region: "us-west1"
examples:
  - name: 'network_services_authz_policy_advanced'
    primary_resource_id: 'default'
    vars:
      resource_name: 'my-authz-policy'
      network_name: 'lb-network'
      subnet_name: 'backend-subnet'
      proxy_subnet_name: 'proxy-only-subnet'
      address_name: 'l7-ilb-ip-address'
      health_check_name: 'l7-ilb-basic-check'
      backend_url_name: 'l7-ilb-backend-service'
      url_name: 'l7-ilb-map'
      target_proxy_name: 'l7-ilb-proxy'
      forwarding_rule_name: 'l7-ilb-forwarding-rule'
      callouts_instance_name: 'l7-ilb-callouts-ins'
      callouts_instance_group_name: 'l7-ilb-callouts-ins-group'
      callouts_health_check_name: 'l7-ilb-callouts-healthcheck'
      backend_authz_name: 'authz-service'
      authz_extension_name: 'my-authz-ext'
    test_env_vars:
      project: 'PROJECT_NAME'
parameters:
  - name: 'name'
    type: String
    description: |
      Identifier. Name of the AuthzPolicy resource.
    required: true
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'
    custom_expand: 'templates/terraform/custom_expand/network_services_authz_policies.tmpl'
  - name: 'location'
    type: String
    description: |
      The location of the resource.
    url_param_only: true
    required: true
properties:
  - name: 'createTime'
    type: Time
    description: |
      The timestamp when the resource was created.
    output: true
  - name: 'updateTime'
    type: Time
    description: |
      The timestamp when the resource was updated.
    output: true
  - name: 'description'
    type: String
    description: |
      A human-readable description of the resource.
  - name: 'labels'
    type: KeyValueLabels
    description: |
      Set of labels associated with the AuthzExtension resource.
  - name: 'target'
    type: NestedObject
    description: |
      Specifies the set of resources to which this policy should be applied to.
    required: true
    properties:
      - name: 'loadBalancingScheme'
        type: Enum
        description: |
          All gateways and forwarding rules referenced by this policy and extensions must share the same load balancing scheme.
          For more information, refer to [Backend services overview](https://cloud.google.com/load-balancing/docs/backend-service).
        required: true
        enum_values:
          - 'INTERNAL_MANAGED'
          - 'EXTERNAL_MANAGED'
          - 'INTERNAL_SELF_MANAGED'
      - name: 'resources'
        type: Array
        description: |
          A list of references to the Forwarding Rules on which this policy will be applied.
        diff_suppress_func: 'tpgresource.ProjectNumberDiffSuppress'
        item_type:
          type: String
  - name: 'httpRules'
    type: Array
    description: |
      A list of authorization HTTP rules to match against the incoming request.A policy match occurs when at least one HTTP rule matches the request or when no HTTP rules are specified in the policy. At least one HTTP Rule is required for Allow or Deny Action.
      Limited to 5 rules.
    item_type:
      type: NestedObject
      properties:
        - name: 'from'
          type: NestedObject
          description: |
            Describes properties of one or more sources of a request.
          properties:
            - name: 'sources'
              type: Array
              description: |
                Describes the properties of a request's sources. At least one of sources or notSources must be specified. Limited to 5 sources. A match occurs when ANY source (in sources or notSources) matches the request. Within a single source, the match follows AND semantics across fields and OR semantics within a single field, i.e. a match occurs when ANY principal matches AND ANY ipBlocks match.
              item_type:
                type: NestedObject
                properties:
                  - name: 'principals'
                    type: Array
                    description: |
                      A list of identities derived from the client's certificate. This field will not match on a request unless mutual TLS is enabled for the Forwarding rule or Gateway. Each identity is a string whose value is matched against the URI SAN, or DNS SAN or the subject field in the client's certificate. The match can be exact, prefix, suffix or a substring match. One of exact, prefix, suffix or contains must be specified.
                      Limited to 5 principals.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'ignoreCase'
                          type: Boolean
                          description: |
                            If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                        - name: 'exact'
                          type: String
                          description: |
                            The input string must match exactly the string specified here.
                            Examples:
                            * abc only matches the value abc.
                        - name: 'prefix'
                          type: String
                          description: |
                            The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value abc.xyz
                        - name: 'suffix'
                          type: String
                          description: |
                            The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc
                        - name: 'contains'
                          type: String
                          description: |
                            The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc.def
                  - name: 'resources'
                    type: Array
                    description: |
                      A list of resources to match against the resource of the source VM of a request.
                      Limited to 5 resources.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'tagValueIdSet'
                          type: NestedObject
                          description: |
                            A list of resource tag value permanent IDs to match against the resource manager tags value associated with the source VM of a request.
                          properties:
                            - name: 'ids'
                              type: Array
                              description: |
                                A list of resource tag value permanent IDs to match against the resource manager tags value associated with the source VM of a request. The match follows AND semantics which means all the ids must match.
                                Limited to 5 matches.
                              item_type:
                                type: String
                        - name: 'iamServiceAccount'
                          type: NestedObject
                          description: |
                            An IAM service account to match against the source service account of the VM sending the request.
                          properties:
                            - name: 'ignoreCase'
                              type: Boolean
                              description: |
                                If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                            - name: 'exact'
                              type: String
                              description: |
                                The input string must match exactly the string specified here.
                                Examples:
                                * abc only matches the value abc.
                            - name: 'prefix'
                              type: String
                              description: |
                                The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                                Examples:
                                * abc matches the value abc.xyz
                            - name: 'suffix'
                              type: String
                              description: |
                                The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                                Examples:
                                * abc matches the value xyz.abc
                            - name: 'contains'
                              type: String
                              description: |
                                The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                                Examples:
                                * abc matches the value xyz.abc.def
            - name: 'notSources'
              type: Array
              description: |
                Describes the properties of a request's sources. At least one of sources or notSources must be specified. Limited to 5 sources. A match occurs when ANY source (in sources or notSources) matches the request. Within a single source, the match follows AND semantics across fields and OR semantics within a single field, i.e. a match occurs when ANY principal matches AND ANY ipBlocks match.
              item_type:
                type: NestedObject
                properties:
                  - name: 'principals'
                    type: Array
                    description: |
                      A list of identities derived from the client's certificate. This field will not match on a request unless mutual TLS is enabled for the Forwarding rule or Gateway. Each identity is a string whose value is matched against the URI SAN, or DNS SAN or the subject field in the client's certificate. The match can be exact, prefix, suffix or a substring match. One of exact, prefix, suffix or contains must be specified.
                      Limited to 5 principals.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'ignoreCase'
                          type: Boolean
                          description: |
                            If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                        - name: 'exact'
                          type: String
                          description: |
                            The input string must match exactly the string specified here.
                            Examples:
                            * abc only matches the value abc.
                        - name: 'prefix'
                          type: String
                          description: |
                            The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value abc.xyz
                        - name: 'suffix'
                          type: String
                          description: |
                            The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc
                        - name: 'contains'
                          type: String
                          description: |
                            The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc.def
                  - name: 'resources'
                    type: Array
                    description: |
                      A list of resources to match against the resource of the source VM of a request.
                      Limited to 5 resources.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'tagValueIdSet'
                          type: NestedObject
                          description: |
                            A list of resource tag value permanent IDs to match against the resource manager tags value associated with the source VM of a request.
                          properties:
                            - name: 'ids'
                              type: Array
                              description: |
                                A list of resource tag value permanent IDs to match against the resource manager tags value associated with the source VM of a request. The match follows AND semantics which means all the ids must match.
                                Limited to 5 matches.
                              item_type:
                                type: String
                        - name: 'iamServiceAccount'
                          type: NestedObject
                          description: |
                            An IAM service account to match against the source service account of the VM sending the request.
                          properties:
                            - name: 'ignoreCase'
                              type: Boolean
                              description: |
                                If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                            - name: 'exact'
                              type: String
                              description: |
                                The input string must match exactly the string specified here.
                                Examples:
                                * abc only matches the value abc.
                            - name: 'prefix'
                              type: String
                              description: |
                                The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                                Examples:
                                * abc matches the value abc.xyz
                            - name: 'suffix'
                              type: String
                              description: |
                                The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                                Examples:
                                * abc matches the value xyz.abc
                            - name: 'contains'
                              type: String
                              description: |
                                The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                                Examples:
                                * abc matches the value xyz.abc.def
        - name: 'to'
          type: NestedObject
          description: |
            Describes properties of one or more targets of a request
          properties:
            - name: 'operations'
              type: Array
              description: |
                Describes properties of one or more targets of a request. At least one of operations or notOperations must be specified. Limited to 5 operations. A match occurs when ANY operation (in operations or notOperations) matches. Within an operation, the match follows AND semantics across fields and OR semantics within a field, i.e. a match occurs when ANY path matches AND ANY header matches and ANY method matches.
              item_type:
                type: NestedObject
                properties:
                  - name: 'headerSet'
                    type: NestedObject
                    description: |
                      A list of headers to match against in http header.
                    properties:
                      - name: 'headers'
                        type: Array
                        description: |
                          A list of headers to match against in http header. The match can be one of exact, prefix, suffix, or contains (substring match). The match follows AND semantics which means all the headers must match. Matches are always case sensitive unless the ignoreCase is set. Limited to 5 matches.
                        item_type:
                          type: NestedObject
                          properties:
                            - name: 'name'
                              type: String
                              description: |
                                Specifies the name of the header in the request.
                            - name: 'value'
                              type: NestedObject
                              description: |
                                Specifies how the header match will be performed.
                              properties:
                                - name: 'ignoreCase'
                                  type: Boolean
                                  description: |
                                    If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                                - name: 'exact'
                                  type: String
                                  description: |
                                    The input string must match exactly the string specified here.
                                    Examples:
                                    * abc only matches the value abc.
                                - name: 'prefix'
                                  type: String
                                  description: |
                                    The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                                    Examples:
                                    * abc matches the value abc.xyz
                                - name: 'suffix'
                                  type: String
                                  description: |
                                    The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                                    Examples:
                                    * abc matches the value xyz.abc
                                - name: 'contains'
                                  type: String
                                  description: |
                                    The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                                    Examples:
                                    * abc matches the value xyz.abc.def
                  - name: 'hosts'
                    type: Array
                    description: |
                      A list of HTTP Hosts to match against. The match can be one of exact, prefix, suffix, or contains (substring match). Matches are always case sensitive unless the ignoreCase is set.
                      Limited to 5 matches.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'ignoreCase'
                          type: Boolean
                          description: |
                            If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                        - name: 'exact'
                          type: String
                          description: |
                            The input string must match exactly the string specified here.
                            Examples:
                            * abc only matches the value abc.
                        - name: 'prefix'
                          type: String
                          description: |
                            The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value abc.xyz
                        - name: 'suffix'
                          type: String
                          description: |
                            The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc
                        - name: 'contains'
                          type: String
                          description: |
                            The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc.def
                  - name: 'paths'
                    type: Array
                    description: |
                      A list of paths to match against. The match can be one of exact, prefix, suffix, or contains (substring match). Matches are always case sensitive unless the ignoreCase is set.
                      Limited to 5 matches.
                      Note that this path match includes the query parameters. For gRPC services, this should be a fully-qualified name of the form /package.service/method.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'ignoreCase'
                          type: Boolean
                          description: |
                            If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                        - name: 'exact'
                          type: String
                          description: |
                            The input string must match exactly the string specified here.
                            Examples:
                            * abc only matches the value abc.
                        - name: 'prefix'
                          type: String
                          description: |
                            The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value abc.xyz
                        - name: 'suffix'
                          type: String
                          description: |
                            The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc
                        - name: 'contains'
                          type: String
                          description: |
                            The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc.def
                  - name: 'methods'
                    type: Array
                    description: |
                      A list of HTTP methods to match against. Each entry must be a valid HTTP method name (GET, PUT, POST, HEAD, PATCH, DELETE, OPTIONS). It only allows exact match and is always case sensitive.
                    item_type:
                      type: String
            - name: 'notOperations'
              type: Array
              description: |
                Describes the negated properties of the targets of a request. Matches requests for operations that do not match the criteria specified in this field. At least one of operations or notOperations must be specified.
              item_type:
                type: NestedObject
                properties:
                  - name: 'headerSet'
                    type: NestedObject
                    description: |
                      A list of headers to match against in http header.
                    properties:
                      - name: 'headers'
                        type: Array
                        description: |
                          A list of headers to match against in http header. The match can be one of exact, prefix, suffix, or contains (substring match). The match follows AND semantics which means all the headers must match. Matches are always case sensitive unless the ignoreCase is set. Limited to 5 matches.
                        item_type:
                          type: NestedObject
                          properties:
                            - name: 'name'
                              type: String
                              description: |
                                Specifies the name of the header in the request.
                            - name: 'value'
                              type: NestedObject
                              description: |
                                Specifies how the header match will be performed.
                              properties:
                                - name: 'ignoreCase'
                                  type: Boolean
                                  description: |
                                    If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                                - name: 'exact'
                                  type: String
                                  description: |
                                    The input string must match exactly the string specified here.
                                    Examples:
                                    * abc only matches the value abc.
                                - name: 'prefix'
                                  type: String
                                  description: |
                                    The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                                    Examples:
                                    * abc matches the value abc.xyz
                                - name: 'suffix'
                                  type: String
                                  description: |
                                    The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                                    Examples:
                                    * abc matches the value xyz.abc
                                - name: 'contains'
                                  type: String
                                  description: |
                                    The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                                    Examples:
                                    * abc matches the value xyz.abc.def
                  - name: 'hosts'
                    type: Array
                    description: |
                      A list of HTTP Hosts to match against. The match can be one of exact, prefix, suffix, or contains (substring match). Matches are always case sensitive unless the ignoreCase is set.
                      Limited to 5 matches.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'ignoreCase'
                          type: Boolean
                          description: |
                            If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                        - name: 'exact'
                          type: String
                          description: |
                            The input string must match exactly the string specified here.
                            Examples:
                            * abc only matches the value abc.
                        - name: 'prefix'
                          type: String
                          description: |
                            The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value abc.xyz
                        - name: 'suffix'
                          type: String
                          description: |
                            The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc
                        - name: 'contains'
                          type: String
                          description: |
                            The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc.def
                  - name: 'paths'
                    type: Array
                    description: |
                      A list of paths to match against. The match can be one of exact, prefix, suffix, or contains (substring match). Matches are always case sensitive unless the ignoreCase is set.
                      Limited to 5 matches.
                      Note that this path match includes the query parameters. For gRPC services, this should be a fully-qualified name of the form /package.service/method.
                    item_type:
                      type: NestedObject
                      properties:
                        - name: 'ignoreCase'
                          type: Boolean
                          description: |
                            If true, indicates the exact/prefix/suffix/contains matching should be case insensitive. For example, the matcher data will match both input string Data and data if set to true.
                        - name: 'exact'
                          type: String
                          description: |
                            The input string must match exactly the string specified here.
                            Examples:
                            * abc only matches the value abc.
                        - name: 'prefix'
                          type: String
                          description: |
                            The input string must have the prefix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value abc.xyz
                        - name: 'suffix'
                          type: String
                          description: |
                            The input string must have the suffix specified here. Note: empty prefix is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc
                        - name: 'contains'
                          type: String
                          description: |
                            The input string must have the substring specified here. Note: empty contains match is not allowed, please use regex instead.
                            Examples:
                            * abc matches the value xyz.abc.def
                  - name: 'methods'
                    type: Array
                    description: |
                      A list of HTTP methods to match against. Each entry must be a valid HTTP method name (GET, PUT, POST, HEAD, PATCH, DELETE, OPTIONS). It only allows exact match and is always case sensitive.
                    item_type:
                      type: String
        - name: 'when'
          type: String
          description: |
            CEL expression that describes the conditions to be satisfied for the action. The result of the CEL expression is ANDed with the from and to. Refer to the CEL language reference for a list of available attributes.
  - name: 'action'
    type: Enum
    description: |
      When the action is CUSTOM, customProvider must be specified.
      When the action is ALLOW, only requests matching the policy will be allowed.
      When the action is DENY, only requests matching the policy will be denied.

      When a request arrives, the policies are evaluated in the following order:
      1. If there is a CUSTOM policy that matches the request, the CUSTOM policy is evaluated using the custom authorization providers and the request is denied if the provider rejects the request.
      2. If there are any DENY policies that match the request, the request is denied.
      3. If there are no ALLOW policies for the resource or if any of the ALLOW policies match the request, the request is allowed.
      4. Else the request is denied by default if none of the configured AuthzPolicies with ALLOW action match the request.
    required: true
    enum_values:
      - 'ALLOW'
      - 'DENY'
      - 'CUSTOM'
  - name: 'customProvider'
    type: NestedObject
    description: |
      Required if the action is CUSTOM. Allows delegating authorization decisions to Cloud IAP or to Service Extensions. One of cloudIap or authzExtension must be specified.
    properties:
      # Cloud IAP type has no fields according to the API
      # https://cloud.google.com/load-balancing/docs/reference/network-security/rest/v1beta1/projects.locations.authzPolicies#cloudiap
      - name: 'cloudIap'
        type: NestedObject
        description: |
          Delegates authorization decisions to Cloud IAP. Applicable only for managed load balancers. Enabling Cloud IAP at the AuthzPolicy level is not compatible with Cloud IAP settings in the BackendService. Enabling IAP in both places will result in request failure. Ensure that IAP is enabled in either the AuthzPolicy or the BackendService but not in both places.
        send_empty_value: true
        custom_flatten: 'templates/terraform/custom_flatten/cloud_iap.tmpl'
        custom_expand: 'templates/terraform/custom_expand/cloud_iap.tmpl'
        properties:
          - name: 'enabled'
            type: Boolean
            description: |
              Enable Cloud IAP at the AuthzPolicy level.
            required: true
      - name: 'authzExtension'
        type: NestedObject
        description: |
          Delegate authorization decision to user authored Service Extension. Only one of cloudIap or authzExtension can be specified.
        properties:
          - name: 'resources'
            type: Array
            description: |
              A list of references to authorization extensions that will be invoked for requests matching this policy. Limited to 1 custom provider.
            required: true
            diff_suppress_func: 'tpgresource.ProjectNumberDiffSuppress'
            item_type:
              type: String
