# Egress policy

## The egress proxy
Figure 1: The proxy decides by the host that the pod names. It connects to a listed host by name, and skips an address that only the node can reach: loopback, the node's own addresses, link-local such as the cloud's metadata service, multicast and broadcast. It also skips the cluster's own addresses, its pods and its Services, which NetworkPolicy governs. A host with no other address gets no connection. (drawn at https://runkite.io/docs/egress-policy/)

When an EgressPolicy selects a pod, the node agent sends the pod's traffic to
hosts outside the cluster through a proxy on the node. The proxy allows only
the hosts that the policy lists. A selected pod starts only after its node
redirects the pod's address to the proxy.

The policy does not affect traffic to other pods and to Services, which
[NetworkPolicy](/docs/networking/#networkpolicy) controls.

An `inject` rule adds a header from a Secret to the pod's HTTPS requests to a
host, so code in the pod never has the token.

## Limits

- The proxy uses HTTP/1.1 with the pod and with the host.
- SSH and database protocols cannot use an `inject` rule.
- A client that pins a certificate cannot use an `inject` rule.

A client that cannot use an `inject` rule keeps using a mounted Secret.

## Write a policy

```yaml
apiVersion: kiteplane.dev/v1alpha1
kind: EgressPolicy
metadata:
  name: ci
  namespace: builds
spec:
  podSelector:
    matchLabels: { app: ci }
  allowedHosts: ["github.com", "*.githubusercontent.com"]
  inject:
    - host: ci.example.com
      path: /api/cache/
      prefix: "Bearer "
      secretKeyRef: { name: ci-tokens, key: cache }
    - host: github.com
      basicAuth: { username: x-access-token }
      secretKeyRef: { name: ci-tokens, key: git }
```

## Trust the egress CA

For an `inject` rule, the proxy ends the pod's TLS with a certificate from the
cluster's egress certificate authority (CA). It sets the header in place of
any header of that name that the pod sent. It then sends the request on over
TLS, and checks the host's certificate against public roots.

So a pod with an `inject` rule must trust the egress CA:

1. Mount the CA in the pod spec. The cluster publishes it in the
   ClusterTrustBundle `kiteplane.dev:egress:ca` when a node first needs a
   certificate.

   ```yaml
   volumes:
     - name: egress-ca
       projected:
         sources:
           - clusterTrustBundle:
               name: kiteplane.dev:egress:ca
               path: ca.crt
   ```

2. Add the projected `ca.crt` to the bundle your client reads, beside the
   public roots that a host without a rule needs.

## Hosts

| Hosts and ports              | Effect                                                                                                            |
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `github.com`                 | Ports 80 and 443                                                                                                  |
| `registry.example:5000`      | Port 5000 only                                                                                                    |
| `*.example.com`              | Every name under `example.com`, and not `example.com` itself                                                      |
| `*`                          | Every host and port. Only TCP 80 and 443 go through the proxy, which needs the host's name                        |
| Several policies             | A pod that several policies select reaches the hosts of all of them                                               |
| DNS                          | The node's DNS server resolves cluster names and listed names, and returns REFUSED for others                     |
| A host with an `inject` rule | Allowed whether `allowedHosts` lists it or not. `ci.example.com:8443` applies to port 8443, and no port means 443 |

## Inject rules

| Field          | Rule                                                                                                                                               |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `path`         | Matches whole segments: `/api/cache` covers `/api/cache/x`, not `/api/cachex`. The longest match wins. No `path` covers every request to the host. |
| `header`       | `Authorization` unless the rule sets another                                                                                                       |
| `prefix`       | Written before the Secret's value, such as `Bearer `                                                                                               |
| `basicAuth`    | Sets `Authorization: Basic` from its `username` and the Secret's value, and allows no `header` or `prefix`                                         |
| `secretKeyRef` | The Secret and key. Spaces and newlines at either end of the value are removed. Whoever writes the policy needs `get` on the Secret.               |

## Refusals and errors

| Case                                                                       | Result                                                                                          |
| -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `Host` is not the TLS connection's server name                             | 421                                                                                             |
| A path with a `.` or `..` segment                                          | 400                                                                                             |
| A WebSocket or another upgrade                                             | The header goes on the upgrade request. After 101, bytes pass through unchanged.                |
| A selected pod on the node's network, or privileged outside gVisor or Kata | The node refuses to start it, because either one can change the node's rules.                   |
| A node whose agent does not enforce EgressPolicy                           | The scheduler skips it: `node(s) didn't enforce Pod's EgressPolicy`. A pod already there waits. |
| A missing Secret or key, or no node certificate yet                        | The pod waits, and its status says what for.                                                    |

## Next

- [Networking](/docs/networking/): pod ranges, ports, Services and NetworkPolicy.
- [Troubleshoot](/docs/troubleshoot/#network-failures): steps for a pod that cannot reach another.
