
# Tenant isolation

Every app runs in its own Kubernetes namespace, `app-<name>`, under a NetworkPolicy named `tenant-isolation`. App pods mount no service account token and can't reach the cloud metadata service or the cluster's private ranges. A nightly probe app checks this from inside a live pod; success is every `critical` check passing.

## How it works

### Namespace and pod settings

The runner creates the namespace labelled `agent.cloud/app: <name>`, applies `tenant-isolation` before writing any workload (`prepare()` then `isolate()`), and writes a short-lived pull secret. On start it reapplies the policy to every namespace carrying that label, so a namespace made under an older policy is updated without waiting for its next release.

Every web, worker and rehearsal pod gets:

- `automountServiceAccountToken: false` and `enableServiceLinks: false`;
- `allowPrivilegeEscalation: false` and `capabilities: { drop: ['ALL'] }` on the container.

Rehearsal and checker pods run in the app's namespace under the same policy. The smoke-flow checker pod is stricter: `runAsNonRoot`, user 1000, `RuntimeDefault` seccomp and a read-only root filesystem.

### The network policy

| Direction | Allowed |
|---|---|
| Ingress | `kube-system` (Traefik), the runner's namespace, pods in the same namespace, the kubelet's probe addresses |
| Egress | Pods in the same namespace; `kube-dns` on port 53; and the cluster-type rule below |

| Cluster | Egress rule | Pod Security admission |
|---|---|---|
| Agent Cloud's own GCP node | `0.0.0.0/0` except `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`, `100.64.0.0/10`, `169.254.0.0/16` and `PRIVATE_RANGES` (default `10.42.0.0/16`, `10.43.0.0/16`, `10.64.0.0/24`) | Not enforced |
| Platform target (`MODE=spike`, AWS or GCP) | `0.0.0.0/0` except `PRIVATE_RANGES` (default `10.42.0.0/16`, `10.43.0.0/16`, `169.254.0.0/16`); a bundle adds the target's VPC CIDR | `pod-security.kubernetes.io/enforce: restricted` on every app namespace, and pods carry `runAsNonRoot` and `RuntimeDefault` seccomp |
| Customer AWS target | Only the egress proxy pods in the runner's namespace (TCP 5432 and 8443) and the API gateway in `agc-egress` (TCP 8080) | `restricted`, as on a platform target, on a high-availability target (`CUSTOMER_HA=true`); not enforced on a single-node one |

Other tenants are unreachable because no app namespace admits ingress from another, and the cluster API and services are inside the excluded private ranges.

### Metadata and cloud credentials

On GCP the NetworkPolicy's exclusion of `169.254.169.254` (and, on Agent Cloud's own node, all of `169.254.0.0/16`) is the only thing between an app and the node's service account. On AWS targets the instance also requires IMDSv2 with `HttpPutResponseHopLimit: 1`, so a token answer can't cross the extra hop a pod sits behind (`bundle.ts`).

### The runner's own limits

The runner's `agc-runner` ClusterRole lets it manage namespaces, secrets, services, pods, deployments, network policies and Traefik routes cluster-wide, because Kubernetes can't scope a role by name prefix. A `ValidatingAdmissionPolicy`, `agc-runner-namespaces`, with `failurePolicy: Fail`, refuses any namespace `DELETE` by the runner's service account unless the name starts with `app-`. The same policy ships in GCP's `runner.yaml` and in every target bundle. The runner never changes its own permissions; a hand edit to its role shows as drift in the console.

### Verification: the probe app

`e2e/security/isolation/probe-app` is a real app deployed through `agc` like any other. From inside its pod it attempts each access it must not have and reports `pass` when the attempt was blocked.

| Check | Fold | Severity | Passes when |
|---|---|---|---|
| `gcp-metadata`, `aws-imds`, `pod-identity` | A, breakout | critical | `169.254.169.254` and `169.254.170.23` don't answer |
| `rfc1918-10`, `rfc1918-172`, `rfc1918-192`, `kube-api`, `datadog-cluster-agent` | A, breakout | critical | TCP connects are refused or time out |
| `no-mount:*` | A, breakout | critical | No SA token, docker socket, host path or platform secret path exists in the pod |
| `cross-app:*` | B, app to app | critical | An attacker app can't open TCP to a victim app's in-cluster names on 80, 8080 or 443 |
| `cross-app-canary:*` | B, app to app | critical | The attacker can't read the victim's `/canary` token |
| `kube-dns-egress`, `non-root`, `read-only-root`, `caps-dropped`, `no-new-privs`, `seccomp` | A | hardening | Reported, never gating; DNS is reachable by design |

The `e2e-security` workflow (`security-staging.yml`) runs daily at 06:00 UTC and on dispatch, one job per target and fold: `aws-us-west-2-c1` and `gcp-us-west1-c1`, each with `breakout` (app to infrastructure) and `cross-app` (app to app). Jobs run one at a time because they share one e2e organization's default target. Results go to the run summary and, on failure, to Slack.

## Isolation on private targets

On a customer's private target, the network and credential boundaries are the same `tenant-isolation` policy, narrowed to the egress proxy, and a high-availability target also enforces Pod Security `restricted`. Your check is the doctor: `on_node "/opt/agent-cloud/doctor.sh"` should print `"pod_metadata_denied": true`.

An app pod accepts traffic only from Traefik, the runner and the kubelet's probes, and sends it only to the egress proxy, the API gateway and DNS:

| `tenant-isolation` on a customer target | Allowed |
|---|---|
| Ingress | Namespaces `kube-system` (Traefik) and `agent-cloud` (runner); the app's own pods; the kubelet probe source `10.42.0.1/32` (each node's bridge address on a multi-node target) |
| Egress | Pods labelled `app: egress` in `agent-cloud`, TCP 5432 and 8443; pods labelled `app: gateway` in `agc-egress`, TCP 8080 (credentialed calls to an `http-api` connection); the app's own pods; `kube-dns` in `kube-system`, UDP and TCP 53 |

IMDS is closed to apps twice: the hop limit of 1 stops a token being fetched from a pod's network namespace, and the policy has no route to `169.254.169.254` anyway. The runner, on the host network, still reaches it to prove its identity, and the heartbeat reports `runner_metadata_ok`. Besides the runner, only the egress proxy reads the kube API, with `get`, `list` and `watch` on pods to map a source IP to an app. Import restore and gateway pods (`role: restore`, `role: import-gateway`) are excluded from this policy and have narrower ones of their own.

The probe app runs on Agent Cloud's staging targets; on your own target, the doctor is the check.

## Errors

| code | exit | cause | fix |
|---|---|---|---|
| `"pod_metadata_denied": false` in the doctor | n/a | An app pod reached the metadata service | Check the instance's `HttpPutResponseHopLimit` is 1 and that `tenant-isolation` exists in each `app-*` namespace; tell Agent Cloud |
| "Isolation checks failed: runner_metadata_ok." on the target row | n/a | The runner can't reach its own instance role | Check the instance profile and IMDS settings |

On that failure the target row's badge is **Unhealthy**.

## Limits

| Item | Value |
|---|---|
| Probe connect timeout | 3,000 ms per check |
| Nightly matrix | 2 targets x 2 folds, staging only |

## Agent notes

- On a platform target, an image that runs as root (`USER root`, or no `USER` in an image whose default is root) is refused by the kubelet. A numeric user (`USER 1000`) works, and so does a named one (`USER node`): Agent Cloud reads its uid and gid from the image's own `/etc/passwd` at build time. Don't add `USER root` or capabilities because another target allows it.
- Pods in one app's namespace reach each other, so a rehearsal reaches that app's live production pods. Don't rely on the namespace boundary inside your own app.
- Apps reach each other's public `https://<name>.ac0.app` addresses like any internet client. Isolation is about the in-cluster network, not public endpoints.
- On a private target, an app's outbound traffic goes through the egress proxy, and credentialed calls to an `http-api` connection go through the API gateway. For a new destination, ask the owner to set up a connection.

## Related

- [Network and egress (targets)](/docs/targets/network-and-egress.md)
- [The doctor](/docs/targets/the-doctor.md)
- [Architecture of a private target](/docs/targets/architecture-of-a-private-target.md)
- [Security overview and threat model](/docs/security/security-overview-and-threat-model.md)
- [How Agent Cloud protects production](/docs/security/known-limits-stated-honestly.md)

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