
# Network and egress

On a private target, app traffic outside its namespace goes through the node's egress proxy, apart from cluster DNS. It forwards only to the app's database, Agent Cloud's sign-in and email, granted connections and owner-approved hosts. A working connection logs an `"egress":"closed"` line with a `via` and byte counts.

Read the log with `on_node "k3s kubectl -n agent-cloud logs deploy/egress --tail=50"`. By default the node reaches Agent Cloud and the relay on 443, plus the VPC for SSM. A shared-VPC install can open internet HTTPS for SSM. Any other public host needs `[production] egress_allow` and an owner's approval for that app (**Outbound hosts**, on the target), and reaches production pods only.

## Ask your infra agent

```text
An app on our Agent Cloud target can't reach a host. Over SSM, read the last 50 lines of the egress
proxy's log (k3s kubectl -n agent-cloud logs deploy/egress) and the runner's coredns-custom key
agent-cloud-connections.override in kube-system. Tell me whether the host is routed to the proxy and,
if a connection was refused, the exact reason. Don't change anything.
```

You'll get the refusal reason and whether the host is in the DNS list; fixing it is usually a grant in the console's Connections area.

## Steps

Grant the connection in the console (**Connections**, then the app). Within a heartbeat (a minute) the runner adds the host to its CoreDNS key, and CoreDNS reloads about two minutes later. Confirm on the node:

```bash
on_node "k3s kubectl -n kube-system get configmap coredns-custom -o jsonpath='{.data.agent-cloud-connections\.override}'"
```

Each granted host is one line, `rewrite stop name exact <host>. egress.agent-cloud.svc.cluster.local. answer auto`. The target's row in the console also says "routes N connection hosts to the relay".

## How it works

```mermaid
flowchart LR
  P[app pod] -->|DNS| D[CoreDNS rewrite]
  D -->|"Postgres, Agent Cloud, granted hosts"| E[egress proxy]
  E -->|"Agent Cloud's own host"| AC[Agent Cloud]
  E -->|"via: relay"| RL[relay.agent.cloud]
  E -->|"via: inside"| IN[host in your VPC]
  RL --> X[Postgres or a public connection host]
```

Cluster DNS sends allowed names to the egress proxy, which goes direct to Agent Cloud, through the relay, or straight to a host inside your network.

DNS is the only routing mechanism, which is what keeps TLS end to end. The bundle's `coredns-custom` key `agent-cloud.override` rewrites the database provider's host suffix and Agent Cloud's own host to the egress Service. The runner owns a second key, `agent-cloud-connections.override`, built from the `dns_hosts` list in each heartbeat answer. That list holds every host of every connection granted to, and every outbound host approved for, any app on the target, less the database provider's and Agent Cloud's. The runner patches only that key and removes it when the list is empty; a `kubectl apply` of the manifests leaves it alone.

The proxy reads the destination from the TLS ClientHello's SNI. For Postgres it first answers the client's SSLRequest itself. It maps the source pod IP to its `app-<name>` namespace and `agent.cloud/app` label. Agent Cloud's own host (`DIRECT_HOSTS`, exact names, so a subdomain of it doesn't qualify) is dialled directly. For everything else it asks Agent Cloud, `POST /runner/v1/egress/check`, which answers `via: relay`, `via: inside` or a refusal, cached 10 seconds per app, host and port. If Agent Cloud is unreachable, the connection takes the relay, which decides on its own. Neither the proxy nor the relay can read the bytes, and `sslmode=verify-full` still passes.

DNS routes but doesn't authorize. A host granted to app A resolves to the proxy for every pod on the node, and app B's connection to it is still refused per app.

**The relay.** For each app connection the proxy opens `https://relay.agent.cloud/v1/tunnel` with `Upgrade: agc-relay`, the runner token, `X-Agc-App` and `X-Agc-Dest: host:port`. The relay asks control (`/internal/relay/check`) and answers `101` and carries bytes, or `403` with the reason. It allows the app's production database endpoint, the endpoint of any rehearsal fork in flight, and the `host:port` pairs of connections granted to it. It refuses names that resolve to loopback, private, link-local, carrier-NAT or local IPv6 addresses, dials the address it checked, and refuses everything when control is unreachable.

**Hosts inside your network.** The relay runs outside your account and can't reach `10.x`. A Flight SQL connection can be marked inside one of your targets. Only that target's apps may be granted it (`connection.not_reachable` otherwise), the egress check answers `via: inside`, and the proxy dials the host from the node with the node's DNS. The relay never carries an inside host, and the security group's 443-to-VPC rule already allows it.

## Reference

### Node security group egress (all TCP 443)

| Destination | Why |
|---|---|
| Each address of `agent.cloud` and `relay.agent.cloud`, as `/32` | Tasks, heartbeats, image pulls, the relay. The addresses those names resolved to when the runbook was downloaded. |
| The VPC CIDR | SSM endpoints; in a shared VPC, connections inside it |
| `0.0.0.0/0` | Only in a shared-VPC template with `InternetEgress=true`, for SSM through the NAT |

There's no rule for UDP 53 or NTP: AWS exempts the VPC resolver, IMDS and the Amazon time service. A VPC with a custom resolver needs one more rule.

### Egress log fields

| Field | Meaning |
|---|---|
| `egress` | `closed` or `refused` |
| `kind` | `pg` or `https` |
| `pod`, `app` | Source pod IP and its app |
| `host`, `port`, `via` | Destination and path: `direct`, `relay` or `inside` |
| `bytes_in`, `bytes_out`, `ms` | Traffic and duration |
| `reason` | Why it was refused |
| `alert` | The TLS alert sent before closing: `access_denied` (49) for a refusal by policy, `internal_error` (80) when the upstream couldn't be reached |
| `environment` | `production` or `rehearsal`, for an app's pod |

## Errors

The first five are egress-log refusals: the app gets a TLS alert (if it sent its hello) and a closed connection.

| code | exit | cause | fix |
|---|---|---|---|
| `not an SSLRequest: plain Postgres is not carried` | n/a | The client didn't ask for TLS | Use `sslmode=verify-full` |
| `no SNI in the client hello` | n/a | The client sent no server name | Connect by host name, not IP |
| `<host>:<port> isn't on <app>'s allowlist` | n/a | Not the app's database, a granted connection or an approved host | Grant the connection, or name the host in `[production] egress_allow` and have an owner approve it |
| `<host> is approved for <app>'s production pods, not its rehearsals` | n/a | A rehearsal called an approved host | Skip the call when `AGC_RUN` is `rehearsal` |
| `relay answered 403: <reason>` | n/a | The relay refused | Read the reason; check the grant and the runner |
| `connection.not_reachable` | 1 | An inside connection used by an app on another target or on Agent Cloud | Grant it only to apps on its target |
| `egress.bad_entry` | 1 | `agc ship`: an entry isn't one public DNS name on 443 (a wildcard, an IP, another port, an internal name) | Name each host exactly, like `"api.example.com:443"` |
| `egress.not_approved` | 1 | `agc ship`: a host isn't approved for this app on this target; it's now a pending request | An owner approves it under **Outbound hosts**; ship again |
| `egress.too_many` | 1 | `agc ship`: the app would hold more than 20 hosts, approved and pending | An owner revokes or declines some, or name fewer |
| `egress.off` | 1 | `agc ship`: an owner turned outbound hosts off for the target | Remove `[production] egress_allow`, or an owner turns them back on |

A failing example, an app reaching a host nobody granted it:

```json
{"egress":"refused","kind":"https","pod":"10.42.0.31","reason":"api.example.test:443: api.example.test:443 isn't on camp-bookings's allowlist","alert":"access_denied"}
```

Hosts that aren't in the CoreDNS list never reach the proxy; the app sees a connect timeout and the log has no line.

## Limits

- Ports 443 and 5432 only; TLS with SNI only. Approved outbound hosts: 443 only, at most 20 per app, approved and pending together.
- Verdict caches: 10 seconds at the proxy and at the relay. Checks happen when a connection opens; revoking a grant does not close existing tunnels.
- DNS change after a grant: about two minutes. The console flags a runner whose list trails Agent Cloud's for more than 5 minutes.

## Agent notes

- `agc exec` and mirrors don't run on the target, so a host that works on a mirror can still be refused in production. Check the grant, not the code.
- A timeout to a public API from a target app usually means no grant or approval, not a network fault.
- Name each outbound host exactly, in advance, for one app; egress is TLS only.
- App pods reach granted connection hosts from [manifests version 3](/docs/targets/manifests-and-updates.md#the-version-matrix); a target below it shows the update line.
- The security group's `/32` rules hold the addresses `agent.cloud` and `relay.agent.cloud` had at download; if those change, update the stack from a fresh runbook.

## Related

- [Architecture of a private target](/docs/targets/architecture-of-a-private-target.md)
- [Isolation guarantees](/docs/security/tenant-isolation.md#isolation-on-private-targets)
- [Your IdP at the edge](/docs/targets/your-idp-at-the-edge.md)
- [Connections](/docs/owners/connections.md)
- [The version matrix](/docs/targets/manifests-and-updates.md#the-version-matrix)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "targets/network-and-egress: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
