Skip to content

WithTokenFromFile / AuthTokenFile read the token once; rotated tokens are never picked up #319

Closed

Description

@jhrozek

Summary

networking.HttpClientBuilder.WithTokenFromFile (and therefore authn.Config.AuthTokenFile) reads the token file once, when Build() is called. A token that is rotated on disk afterwards is never picked up, so the client keeps sending the old bearer.

The doc comment on authn.Config.AuthTokenFile says the opposite: "The networking package re-reads the file per request, so a rotated token is picked up without restarting the validator."

Where

networking/http_client.go, createTokenSourceFromFile: it calls os.ReadFile once and returns oauth2.StaticTokenSource(token). Build() wraps that in an oauth2.Transport, so every later request reuses the same string.

This is still the code on main.

Reproduction

f := filepath.Join(t.TempDir(), "token")
os.WriteFile(f, []byte("old"), 0o600)
c, _ := networking.NewHttpClientBuilder().
    WithPrivateIPs(true).WithInsecureAllowHTTP(true).
    WithTokenFromFile(f).Build()

for _, tok := range []string{"old", "new"} {
    os.WriteFile(f, []byte(tok), 0o600)
    resp, _ := c.Get(srv.URL) // srv records the Authorization header
    resp.Body.Close()
}
// server saw: ["Bearer old", "Bearer old"]
// expected:   ["Bearer old", "Bearer new"]

Run against toolhive-core v0.0.50.

Why it matters

The ToolHive operator's KubernetesServiceAccount OIDC type sets JWKSAuthTokenPath to /var/run/secrets/kubernetes.io/serviceaccount/token (cmd/thv-operator/pkg/oidc/resolver.go). Kubelet rewrites that file as the projected token rotates. A validator built from that path keeps presenting the token it read at startup. I have not tested this on a live cluster; the reproduction above is the file-level behaviour.

Suggested fix

Read the file on each request. Either:

  • an oauth2.TokenSource whose Token() re-reads and trims the file (not wrapped in oauth2.ReuseTokenSource, since there is no expiry to key on), or
  • a small RoundTripper that does the same.

Keep the current checks: an empty file is an error, and Build() can still read the file once to fail fast on a bad path. The existing SameHostRedirectPolicy handling does not need to change.

A regression test along the lines of the reproduction above would cover it.

Activity

  1. added
    needs-triage
    on Oct 8, 2026
  2. added a commit that references this issue on Oct 8, 2026
    f54dead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions