This page describes how to use authorization policies to designate
Identity-Aware Proxy (IAP) as the custom authorization engine in an
authorization policy for Application Load Balancers.

To delegate authorization to IAP and
Identity and Access Management (IAM), you authorize traffic to a forwarding rule based on
IAM or end-user identities for managed load balancers. This lets
you enforce IAP-based access control for your backend services.

## Before you begin

- Understand [authorization policies](https://docs.cloud.google.com/load-balancing/docs/auth-policy/auth-policy-overview.md.txt)
  concepts.

- Ensure that the backend services of the
  forwarding rules don't have IAP enabled.
  If IAP is enabled, you receive an error when submitting your
  configuration.

- [Enable the Network Services API](https://docs.cloud.google.com/endpoints/docs/openapi/enable-api.md.txt#enabling_an_api).

## Create the authorization policy and enable IAP

To create an authorization policy, you create a policy file that defines the
target and rules, and then enable IAP on the forwarding rule. You
can choose to apply IAP only to specific requests by defining
conditions in the `httpRules` section of your policy file. If the `httpRules`
section is omitted, IAP is enforced on all requests.

### gcloud

1. Run the following command to prepare a `policy.yaml` file.
   The policy allows clients with an IP address range of `10.0.0.0/24` to
   enable IAP on a forwarding rule.

   ```yaml
   $ cat << EOF > policy.yaml
   action: CUSTOM
   description: authz policy with Cloud IAP
   name: AUTHZ_POLICY_NAME
   httpRules:
     - from:
         sources:
         - ipBlocks:
           - prefix: "10.0.0.0"
             length: 24
   customProvider:
     cloudIap: {}
   target:
     loadBalancingScheme: EXTERNAL_MANAGED
     resources:
     - https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_ID
   EOF
   ```
2. Run the following command to enable IAP on a forwarding rule.

   ```
   gcloud network-security authz-policies import AUTHZ_POLICY_NAME \
   --source=policy.yaml \
   --location=LOCATION \
   --project=PROJECT_ID
   ```

   Replace the following:
   - <var translate="no">PROJECT_ID</var>: The Google Cloud project ID.

   - <var translate="no">LOCATION</var>: The region that the resource is located in.

   - <var translate="no">FORWARDING_RULE_ID</var>: The ID of the forwarding rule resource.

   - <var translate="no">AUTHZ_POLICY_NAME</var>: The name of the authorization policy.

### API

1. Run the following command to prepare a `policy.json` file.

   ```
   cat << EOF > policy.json
   {
    "name": "AUTHZ_POLICY_NAME",
    "target": {
        "loadBalancingScheme": "INTERNAL_MANAGED",
        "resources": [
          "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_ID"
        ],
    },
    "action": "CUSTOM",
    "httpRules": [
        {
          "from": {
            "sources": {
              "ipBlocks": [
                {
                  "prefix": "10.0.0.0",
                  "length": 24
                }
              ]
            }
          }
        }
    ],
    "customProvider": {
        "cloudIap": {}
    }
   }
   EOF
   ```
2. Run the following command to enable IAP on a forwarding rule.

   ```
   curl -X PATCH \\
   -H "Authorization: Bearer $(gcloud auth print-access-token)" \\
   -H "Accept: application/json" \\
   -H "Content-Type: application/json" \\
   -d @policy.json \\
   "https://networksecurity.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/authzPolicies"
   ```

   Replace the following:
   - <var translate="no">PROJECT_ID</var>: The Google Cloud project ID.

   - <var translate="no">LOCATION</var>: The region that the resource is located in.

   - <var translate="no">FORWARDING_RULE_ID</var>: The ID of the forwarding rule resource.

   - <var translate="no">AUTHZ_POLICY_NAME</var>: The name of the authorization policy.

### Apply permissions to resources

You can use the IAP API to apply IAM
permissions to individual resources in an IAP-secured project.
IAM permissions granted at a certain level apply to all of the
levels underneath it. For example, a permission granted at the project level
applies to all of the Google Cloud resources in the project.

Following are some example commands for configuring permissions. For more
information, see [`gcloud iap web set-iam-policy`](https://docs.cloud.google.com/sdk/gcloud/reference/iam/policies/get).

### gcloud

In the example commands, replace <var translate="no">POLICY_FILE</var> with the path to a YAML
formatted file that contains a valid policy. Following is an example YAML
file:

```yaml
bindings:
  members: user:example@example.com
  role: roles/iap.httpsResourceAccessor
```

#### Update the permission for who can access the forwarding rule resource

```
gcloud iap web set-iam-policy POLICY_FILE [--organization=ORGANIZATION \
--folder=FOLDER --project=PROJECT_ID \
--resource-type=RESOURCE_TYPE --service=SERVICE]
```

#### Configure permission at the organization level

```
gcloud iap web set-iam-policy POLICY_FILE --organization=ORGANIZATION
```

#### Configure permission at the folder level

```
gcloud iap web set-iam-policy POLICY_FILE --folder=FOLDER
```

#### Configure permission at the project level

```
gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID --resource-type=iap_web
```

#### Configure permission for all global forwarding rules in a project

```
gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID --resource-type=forwarding_rule
```

#### Configure permission for all forwarding rules in a project in a region

```
gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID \
--resource-type=forwarding_rule --region=REGION
```

#### Configure permission for a specific global forwarding rule in a project

```
gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID \
--resource-type=forwarding_rule --service=SERVICE
```

#### Configure permission for a specific forwarding rule in a project in a region

```
gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID \
--resource-type=forwarding_rule --service=SERVICE --region=REGION
```

Replace the following:

- <var translate="no">POLICY_FILE</var>: The path to a YAML file that contains a
  valid policy.

- <var translate="no">ORGANIZATION</var>: The ID of your organization.

- <var translate="no">FOLDER</var>: The folder that contains your application.

- <var translate="no">PROJECT_ID</var>: The Google Cloud project ID.

- <var translate="no">RESOURCE_TYPE</var>: The IAP resource type. The resource
  type must be `app-engine`, `iap_web`, `compute`, `organization`, or `folder`.

- <var translate="no">SERVICE</var>: The forwarding rule ID or name.

- <var translate="no">REGION</var>: The region that the resource is located in.

### API

#### Configure permission at the organization level

```
organization/ORGANIZATION
```

#### Configure permission at the folder level

```
folders/FOLDER
```

#### Configure permission at the project level

```
projects/PROJECT_ID
```

#### Configure permission for all global forwarding rules in a project

```
projects/PROJECT_ID/forwarding_rule
```

#### Configure permission for all forwarding rules in a project in a region

```
projects/PROJECT_ID/iap_web/forwarding_rule-REGION
```

#### Configure permission for a specific global forwarding rule in a project

```
projects/PROJECT_ID/iap_web/forwarding_rule/services/SERVICE
```

#### Configure permission for a specific forwarding rule in a project in a region

```
projects/PROJECT_ID/iap_web/forwarding_rule-REGION/services/SERVICE
```

#### Set the authorization policy

```
curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d @policy.json \
"https://iap.googleapis.com/v1/RESOURCE_NAME:setIamPolicy"
```

For `policy.json`, you can create the JSON file by running the following
command. Update the values as needed.

```
cat << EOF > policy.json
{
  "bindings": {
     "members": "user:example@example.com",
     "role": "roles/iap.httpsResourceAccessor",
   }
}
EOF
```

Replace the following:

- <var translate="no">ORGANIZATION</var>: The ID of your organization.

- <var translate="no">FOLDER</var>: The folder that contains your application.

- <var translate="no">PROJECT_ID</var>: The Google Cloud project ID.

- <var translate="no">REGION</var>: The region that the resource is located in.

- <var translate="no">SERVICE</var>: The forwarding rule ID or name.