Skip to content

fix(security): pass the passthrough token to helm, istio, argo and cilium, not only kubectl - #114

Open
braghettos wants to merge 1 commit into
kagent-dev:mainfrom
braghettos:fix/helm-token-passthrough
Open

braghettos wants to merge 1 commit into
kagent-dev:mainfrom
braghettos:fix/helm-token-passthrough

Conversation

@braghettos

Copy link
Copy Markdown
Contributor

Problem

With TOKEN_PASSTHROUGH=true, only the kubectl tools in pkg/k8s send the caller's bearer token. helm, istioctl, cilium and kubectl argo rollouts run with the server's configured kubeconfig instead.

In a passthrough deployment that kubeconfig has no usable credentials. On ours, every helm call failed with Error: kubernetes cluster unreachable: the server has asked for the client to provide credentials: 7 of 7 in 30 hours, while 70 of 70 kubectl calls from the same agents succeeded. Where the server's kubeconfig does have credentials, those tools instead act as the server, not the caller, which is what passthrough exists to prevent.

Change

  • One resolver, internal/mcp/passthrough.go (next to mcp.Header). ResolvePassthroughToken(passthrough, headers) keeps the existing kubectl semantics exactly:
    • passthrough on: the Authorization: Bearer token, or Bearer token required when TOKEN_PASSTHROUGH is true;
    • passthrough off: no token.
      pkg/k8s now uses it, with no change in behaviour.
  • helm, istio, argo, cilium resolve the token from the request and pass it to CommandBuilder.WithToken, so the per-call kubeconfig from fix(security): pass the passthrough token via a per-call kubeconfig, never argv #107 carries it. It's never on argv, in logs or in error text. A cluster call without a token is refused before any command runs.
  • Calls that stay local need no token: helm repo add/update, istioctl manifest generate and waypoint generate (they only render YAML; the tool never passes --cluster-specific), and the argo plugin version check.
  • Tests. The per-call kubeconfig helpers move from pkg/k8s to internal/commands/commandstest, with a shared suite that every provider runs:
    1. the token goes through the per-call kubeconfig (file mode, removal, untouched args);
    2. without a token, the call is refused and zero commands run;
    3. local tools run without one;
    4. with passthrough off, the token is ignored.
      There are also table tests for the resolver. Removing WithToken from helm makes all four helm cluster tools fail the suite.

Notes for review

  • Behaviour change under passthrough: helm/istio/argo/cilium calls without a bearer token used to run as the server and are now refused. That's intended, but it may deserve a release note.
  • tools.k8s.tokenPassthrough now governs every CLI provider. I only updated its comment; a top-level name may be preferable.
  • Not covered: kubescape builds a client-go client at startup and still uses the server's identity. cilium clustermesh connect --destination-context can't work under passthrough: one token, one context. It didn't work before either.

Checks

  • go build ./..., go vet ./... and gofmt -l: clean.
  • make lint (golangci-lint v2.15.0): 0 issues.
  • go test -tags=test ./pkg/... ./internal/... ./cmd/...: all pass.
  • e2e (make e2e, which needs Kind) was not run locally.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TLFnUd6g7xhDXDvaho4rDW

…lium, not only kubectl

With TOKEN_PASSTHROUGH=true only the kubectl tools in pkg/k8s sent the
caller's bearer token. helm, istioctl, cilium and kubectl argo rollouts ran
with the server's configured kubeconfig, which in a passthrough deployment
has no usable credentials, so every helm call failed with "the server has
asked for the client to provide credentials" while kubectl succeeded.

The token resolution moves to one place, internal/mcp (next to Header):
ResolvePassthroughToken(passthrough, headers) keeps the kubectl semantics
exactly (passthrough on: the Authorization: Bearer token, or "Bearer token
required when TOKEN_PASSTHROUGH is true"; passthrough off: no token), and
PassthroughToken(headers) reads TOKEN_PASSTHROUGH the way pkg/k8s does.
pkg/k8s now uses it, unchanged in behaviour.

helm, istio, argo and cilium resolve the token from the request and pass it
to CommandBuilder.WithToken, so the per-call kubeconfig from kagent-dev#107 carries it;
it is never on argv, in logs or in error text. A call that reaches the
cluster is refused without a token before any command runs. Calls that stay
local need none: helm repo add/update, istioctl manifest generate and
waypoint generate (YAML only), and the argo plugin version check.

The per-call kubeconfig test helpers from pkg/k8s move to
internal/commands/commandstest, with a shared suite every provider runs:
token through the per-call kubeconfig, refusal without one, local tools
without one, and passthrough off ignoring it.

Kubescape builds a client-go client at startup and still uses the
server's identity; it is not covered here.

Signed-off-by: Diego Braga <diego.braga86@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TLFnUd6g7xhDXDvaho4rDW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant