# Setup authorization policies on sidecars on GKE

> [!WARNING]
>
> **Preview
> --- Authorization Policies on Sidecars**
>
>
> This feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the
> [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

This page provides instructions for setting up different kinds of authorization
policies on Cloud Service Mesh sidecars on GKE.

## Before you begin

You should be familiar with
[Gateway API](https://docs.cloud.google.com/service-mesh/docs/gateway/prepare-gateway.md.txt)
and [Authorization Extensions](https://docs.cloud.google.com/service-extensions/docs/configure-authorization-extensions.md.txt).

Before you create authorization policy, you must perform the following steps:

<br />

> [!NOTE]
> **Note:** After you create or change an authorization policy, it can take up to 15 minutes for the sidecars to start enforcing it.

## Namespace-wide AuthzPolicy

If you want to deny an expected traffic pattern for the whole namespace,
configure an authorization policy to deny any incoming HTTP requests to all the
workloads in a namespace:

```bash
cat >ns-authz-policy-deny.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: ns-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector: {}
  rules:
  - to:
      operations:
      - paths:
        - type: Prefix
          value: "/bad_token"
  action: DENY
  enforcementLevel: L7
EOF
kubectl apply -f ns-authz-policy-deny.yaml
```

The empty `selector: {}` targets all Pods in the namespace.

If you want to allow an expected traffic pattern for the whole namespace,
configure an authorization policy to allow any incoming HTTP requests to all
the workloads in a namespace:

```bash
cat >ns-authz-policy-allow.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: ns-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector: {}
  rules:
  - to:
      operations:
      - paths:
        - type: Prefix
          value: "/headers"
        methods: ["GET"]
  action: ALLOW
  enforcementLevel: L7
EOF
kubectl apply -f ns-authz-policy-allow.yaml
```

The empty `selector: {}` targets all Pods in the namespace.

## Deny inbound requests to a workload

If you have a workload that is supposed to make only outbound calls, like a cron
job, configure an authorization policy to deny any incoming HTTP requests to the
workload:

```bash
cat >deny-path-authz-policy.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: my-workload-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector:
      matchLabels:
        app: EXAMPLE_APP
  rules:
  - to:
      operations:
      - paths:
        - type: Prefix
          value: "/deny_path"
  action: DENY
  enforcementLevel: L7
EOF
kubectl apply -f deny-path-authz-policy.yaml
```

The output is similar to:

    gcpauthzpolicy.networking.gke.io/my-workload-authz created

After this policy is applied, any incoming HTTP request to the path /deny_path
on the Pods matching app: <var translate="no">EXAMPLE_APP</var> will be rejected and the caller
will receive an HTTP 403 Forbidden response code.

## Allow specific inbound requests to a workload

You can also configure an `ALLOW` policy that allows only requests that match a
specific criteria while rejecting the rest.

The following example configures an
authorization policy on the example-app Deployment to allow only mTLS requests
from Pods with identity `spiffe://cluster.local/NAMESPACE/pod1`.

```bash
cat >allow-authz-policy.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: my-workload-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector:
      matchLabels:
        app: EXAMPLE_APP
  rules:
  - from:
      sources:
      - principals:
        - principal:
            type: Exact
            value: "spiffe://cluster.local/NAMESPACE/pod1"
  action: ALLOW
  enforcementLevel: L7
EOF
kubectl apply -f allow-authz-policy.yaml
```

The output is similar to:

    gcpauthzpolicy.networking.gke.io/my-workload-authz created

Only incoming requests authenticated using mTLS presenting the exact SPIFFE
identity `spiffe://cluster.local/NAMESPACE/pod1` will receive an HTTP
200 OK. Any other request will be rejected by the proxy with an HTTP 403
Forbidden.

## Delegate to an external authorization engine

You can bring your own authorization engine and configure Cloud Service Mesh
to delegate all authorization decisions for a workload to the configured engine.

1. Install the `GCPAuthzExtension` CRD if it's not already installed:

   ```bash
   kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/gke-gateway-api/main/config/crd/networking.gke.io_gcpauthzextensions.yaml
   ```
2. Deploy a callout-based authorization extension workload in Kubernetes
   supporting the `ext_proc` gRPC protocol and configure an authorization
   extension resource:

   ```bash
   cat >authz-extension.yaml <<EOF
   apiVersion: networking.gke.io/v1
   kind: GCPAuthzExtension
   metadata:
     name: my-authz-ext
     namespace: NAMESPACE
   spec:
     backendRef:
       group: ""
       kind: Service
       name: authz-service
       port: PORT
     authority: "authz-service.NAMESPACE.svc.cluster.local"
     forwardHeaders:
     - Authorization
     failOpen: false
     timeout: "100ms"
     wireFormat: ExtProcGRPC
   EOF
   kubectl apply -f authz-extension.yaml
   ```

   Replace the following:
   - `NAMESPACE`: the namespace of the `authz-service` Service and the workloads.
   - `PORT`: the port of the `authz-service` Service that serves the `ext_proc` gRPC API.

   This command creates a callout-based authorization extension that runs as a
   Kubernetes service `authz-service`.

   The output is similar to:

       gcpauthzextension.networking.gke.io/my-authz-ext created

3. Set up the authorization policy to delegate authorization decisions for the
   workload to the authorization extension previously configured:

   ```bash
   cat >authz-policy.yaml <<EOF
   apiVersion: networking.gke.io/v1
   kind: GCPAuthzPolicy
   metadata:
     name: my-workload-authz
     namespace: NAMESPACE
   spec:
     targetRefs:
     - group: ""
       kind: Pod
       selector:
         matchLabels:
           app: EXAMPLE_APP
     rules:
     - from:
         sources:
         - principals:
           - principal:
               type: Exact
               value: "spiffe://cluster.local/NAMESPACE/pod1"
       to:
         operations:
         - paths:
           - type: Exact
             value: "/api/payments-schedule"
     action: CUSTOM
     enforcementLevel: L7
     customProviders:
       extensionRefs:
       - group: networking.gke.io
         kind: GCPAuthzExtension
         name: my-authz-ext
   EOF
   kubectl apply -f authz-policy.yaml
   ```

   The output is similar to:

       gcpauthzpolicy.networking.gke.io/my-workload-authz created

   The proxy waits for the authorization extension to respond before it
   forwards the request to the application. If the extension rejects the
   request by ending the call with an error, or if the call fails or times out
   while `failOpen` is `false`, the caller receives an HTTP 500 response. If the
   extension sends an immediate response, the caller receives the status code
   from that response.