# Generating authorization policies for an existing application

Buoyant Enterprise for Linkerd provides powerful and expressive [authorization
policies](https://linkerd.io/2.15/features/server-policy/)
that allow you to control the types of traffic that are allowed in your cluster,
even at the level of individual HTTP routes or gRPC methods.

The BEL CLI includes a `linkerd policy generate` command to automatically
generate policy based on existing traffic on your Kubernetes cluster.

## Prerequisites

- [Buoyant Enterprise for Linkerd](/buoyant-enterprise-linkerd/2.15/installation/) installed on a Kubernetes
  cluster
- The `BUOYANT_LICENSE` environment variable set, with functioning BEL CLI

## Step 1: Install sample app

If you do not already have meshed workloads on your cluster, you can install
this sample app to demonstrate policy generation:

```bash
curl https://run.linkerd.io/emojivoto.yml |
  linkerd inject - |
  kubectl apply -f -

linkerd check --proxy
```

## Step 2: Generate policy

To generate policy for all meshed workloads on your cluster, run:

```bash
linkerd policy generate
```

Depending on the number of Linkerd proxies running on your cluster, this command
may take several minutes. It will examine traffic patterns for all running
proxies. For each meshed server and port combination found, the output will
include the following resources:

| Resource                                                                                                                    | Name                  |
| --------------------------------------------------------------------------------------------------------------------------- | --------------------- |
| [Server](https://linkerd.io/2.15/reference/authorization-policy/#server)                               | [server]-[port]       |
| [MeshTLSAuthentication](https://linkerd.io/2.15/reference/authorization-policy/#meshtlsauthentication) | [server]-[port]       |
| [AuthorizationPolicy](https://linkerd.io/2.15/reference/authorization-policy/#authorizationpolicy)     | [server]-[port]       |
| [NetworkAuthentication](https://linkerd.io/2.15/reference/authorization-policy/#networkauthentication) | [server]-[port]-allow |
| [AuthorizationPolicy](https://linkerd.io/2.15/reference/authorization-policy/#authorizationpolicy)     | [server]-[port]-allow |

The `Server` defines the meshed workload and port that is receiving traffic. The
subsequent `MeshTLSAuthentication` and `AuthorizationPolicy` define which meshed
workloads are authorized to connect to the `Server`. The `NetworkAuthentication`
and `AuthorizationPolicy` named `[server]-[port]-allow` act as a catch-all,
allowing all traffic, meshed and unmeshed, to connect to the `Server`. This
catch-all is in place as a starting point, to ensure that you can apply the
generated policy to your cluster without inadvertently denying traffic. Once you
are confident that all authorized traffic is captured via policy, you may remove
these catch-all resources.

To apply the policy, run:

```bash
linkerd policy generate | kubectl apply -f -
```

## Learning more

For more information about using `linkerd policy generate`, see the
[BEL CLI reference](/buoyant-enterprise-linkerd/2.15/reference/bel-cli/#policy-generate) page.

## Walkthrough

[Video](https://vimeo.com/936785424)
