The gateway
The gateway is a proxy that agents send model API calls and MCP traffic through.
It enforces the models and mcp sections of the agent’s AgentPolicy:
- Model allowlist: only the listed models (with
*globs) can be called. - Daily token budget: shared by every agent the policy selects. Once it is
used up, calls get
429until midnight UTC, so agents are paused, not killed. - Provider keys: with
authconfigured, the gateway adds the API key itself, so agents never hold it. - MCP tool rules: per server,
denyTools,requireApprovalandallowTools. Denied tools are also removed fromtools/list, so the agent never sees them. - Human approval: a call to a
requireApprovaltool creates aToolApproval, posts to Slack, and waits for someone to approve or deny it. - Kill switch: frozen policies are refused here too.
Why a central gateway, not a sidecar
A sidecar shares the agent pod’s network identity, so network policy can’t tell
the two apart: the agent could skip the sidecar and call the provider directly.
With a separate gateway, the agent’s AgentPolicy allows egress to the gateway
only, and only the gateway can reach the provider.
When a policy sets models or mcp, Leash automatically allows egress to the
gateway. To make the gateway mandatory, don’t also list the provider in
egress.fqdns.
apiVersion: leash.simplyzee.dev/v1alpha1kind: AgentPolicymetadata: name: claude-agent namespace: agentsspec: agentSelector: matchLabels: app: claude-agent # No egress.fqdns for api.anthropic.com: model calls must go through the gateway. models: allow: ["claude-sonnet-*", "claude-haiku-*"] dailyTokenBudget: 2000000 mcp: approvalTimeout: 10m servers: - name: github denyTools: ["delete_*"] requireApproval: ["merge_pull_request", "create_release"] allowTools: ["get_*", "list_*", "search_*", "create_issue", "create_pull_request"]$ kubectl get agp -n agentsNAME PHASE BACKEND FROZEN DEGRADED TOKENS TODAY AGEclaude-agent Enforced cilium false False 48211 3hPointing agents at the gateway
| Upstream | Agent setting |
|---|---|
| Anthropic | ANTHROPIC_BASE_URL=http://leash-gateway.leash-system.svc:8080/anthropic |
| OpenAI | OPENAI_BASE_URL=http://leash-gateway.leash-system.svc:8080/openai/v1 |
MCP server github |
http://leash-gateway.leash-system.svc:8080/mcp/github (Streamable HTTP) |
If the gateway holds the provider key, give the agent a placeholder key: SDKs require one, and the gateway replaces it.
Configuring providers and MCP servers
The registry is config/gateway/gateway.yaml, deployed as the
leash-gateway-config ConfigMap:
providers: anthropic: url: https://api.anthropic.com auth: header: x-api-key valueFromEnv: ANTHROPIC_API_KEY openai: url: https://api.openai.com auth: header: Authorization prefix: "Bearer " valueFromEnv: OPENAI_API_KEYmcpServers: github: url: http://github-mcp.mcp.svc:8080/mcpCredentials, and the optional Slack webhook, come from the
leash-gateway-credentials Secret in leash-system:
kubectl -n leash-system create secret generic leash-gateway-credentials \ --from-literal=ANTHROPIC_API_KEY=sk-ant-... \ --from-literal=SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...The gateway itself needs egress to the providers, the MCP servers and Slack.
Approvals
When an agent calls a requireApproval tool, the gateway holds the call and
creates a ToolApproval in the agent’s namespace:
$ kubectl get tap -n agentsNAME DECISION SERVER TOOL POD EXPIRES AGEmerge-pull-request-x7k2p github merge_pull_request claude-agent-0 4m 12s
$ kubectl -n agents patch tap merge-pull-request-x7k2p --type merge -p '{"spec":{"decision":"Approved"}}'Set "decision":"Denied" (and optionally "reason":"...") to refuse it. The
decision is final, and the request fields can’t be edited. If nobody decides
before approvalTimeout (default 5m), the call is refused. kubectl leash approvals, approve and deny do the same with less typing.
Approving from Slack
With SLACK_WEBHOOK_URL set, every request is posted to a Slack channel. Add
SLACK_SIGNING_SECRET and the post gets Approve and Deny buttons;
once someone clicks one, the message is replaced by the outcome and who
decided.
-
Create a Slack app at https://api.slack.com/apps → Create New App → From a manifest, with your own host:
display_information:name: Leashdescription: Approve or deny AI agents' tool callsfeatures:bot_user:display_name: Leashoauth_config:scopes:bot: [incoming-webhook]settings:interactivity:is_enabled: truerequest_url: https://leash.example.com/slack/interactions -
Install it to your workspace and pick the approvals channel. Copy the webhook URL (Incoming Webhooks) and the signing secret (Basic Information).
-
Give them to the gateway:
Terminal window kubectl -n leash-system create secret generic leash-gateway-credentials \--from-literal=ANTHROPIC_API_KEY=sk-ant-... \--from-literal=SLACK_WEBHOOK_URL=https://hooks.slack.com/services/... \--from-literal=SLACK_SIGNING_SECRET=... \--dry-run=client -o yaml | kubectl apply -f - -
Let Slack reach the gateway. Button clicks arrive on a separate port (8082), never the one agents use. With the chart:
gateway:credentials:existingSecret: leash-gateway-credentialsslack:interactive: trueapprovers: [U012AB3CD, U045EF6GH] # Slack member IDs; empty = anyone in the channelingress:enabled: trueclassName: nginxhost: leash.example.comtls:- hosts: [leash.example.com]secretName: leash-slack-tlsThe Ingress routes only
/slack/interactions, to aleash-gateway-slackService. With kustomize, add a Service for port 8082 and your own Ingress, and pass--slack-approversto the gateway.
How it’s kept safe:
- Every click is checked against Slack’s signature (HMAC with the signing secret) and must be under 5 minutes old, so nobody else can decide by calling the endpoint. Unsigned requests get 401.
approverslimits who can decide; anyone else gets a private “you’re not on the approvers list” reply.- The Approve button asks for confirmation. Decisions are final: a later click, or one after the approval expired, just shows what happened.
- The decision is recorded on the
ToolApproval(reason: approved by @zee (U012AB3CD) in Slack), and the gateway needspatchontoolapprovalsfor it. - A decision made with kubectl doesn’t update the Slack message (webhooks can’t edit their posts); clicking it afterwards shows the outcome.
Who can approve? Anyone who can update
toolapprovals. Agents need no Kubernetes RBAC for the gateway to work, so don’t give their ServiceAccounts any ontoolapprovals, or an agent could approve its own calls.
The agent’s MCP client must wait while the call is held. Raise its request
timeout above approvalTimeout.
How callers are identified
The gateway matches the request’s source IP to a running pod and the one
AgentPolicy that selects it. It refuses the request if:
- no running pod has that IP, or it is a host-network pod;
- no
AgentPolicyselects the pod; - more than one
AgentPolicyselects the pod. The gateway needs exactly one; split overlapping selectors.
Source IPs of pod-to-Service traffic are preserved by kube-proxy and Cilium. If your cluster masquerades in-cluster traffic, the gateway can’t identify callers.
Metrics
On :9090/metrics:
leash_gateway_requests_total{namespace,policy,kind,outcome}, wherekindismodelormcp, andoutcomeisallowed,denied,budget_exhausted,approved,approval_deniedorapproval_errorleash_gateway_tokens_total{namespace,policy,provider,model,direction}
Running several replicas
The gateway runs 2 replicas by default (chart value gateway.replicas), with a
PodDisruptionBudget, a rolling update that never drops below the current count,
and a soft spread across nodes. Any replica can serve any agent:
- Token budgets are shared. Each replica adds the tokens it metered to the
policy’s
status.usageevery--usage-flush-interval(15s). Writes are guarded byresourceVersionand retried on conflict, so no count is lost. Between flushes a replica checks the budget against the latest stored total plus what it hasn’t written yet. - AgentRuns are shared the same way: each replica adds its counts to the pod’s run.
- Approvals wait on the replica that holds the call; any replica, the plugin or Slack can be where the decision comes from.
Limits
- Budgets are soft. A call in flight when the budget runs out still
finishes, and with several replicas a budget can be overshot by roughly what
the other replicas spend in one flush interval. Lower
--usage-flush-intervalfor a tighter limit, at the cost of more API writes. - Cache tokens count in full. Anthropic cache reads and writes are counted as input tokens.
- Globs use Go
path.Matchrules.*doesn’t match/, so a model name such asorg/modelneedsorg/*. - Batches. A batched JSON-RPC request with a tool call that needs approval is refused as a whole.