Repository navigation
feat(authn): harden OIDC HTTPS and validate Kubernetes tokens - #2218
Merged
Merged
Conversation
The broker authenticates workloads with Kubernetes service-account tokens. The API server's JWKS endpoint requires a bearer token, and authn/oidc had no way to send one. Its private-HTTPS mode also approved more network destinations than it used. NewKubernetesValidator (new) - Reads the "iss" claim from the broker's projected token without verifying it, and accepts it only if the configured discovery URL returns the same issuer. Tokens are then verified with keys from the configured JWKS URL. Neither the token nor the discovery response chooses where requests go; the discovery document is read only for its issuer. - Discovery and JWKS URLs must be HTTPS on the same host and port, are reached over private addresses, and are verified only against the CA file (TrustedCAFile). There is no per-host address pin; TLS verification against the cluster CA is what authenticates the API server. - One client built with networking.HttpClientBuilder (private addresses, the CA, the token file, a 15s timeout, redirects refused) serves the discovery request, toolhive-core's key fetches, and Ready. The builder re-reads the token file on every request, so rotation works; this relies on toolhive-core v0.0.51, where v0.0.50 read the file once. TokenSource supplies the token's contents once, for the issuer claim, because authn/oidc may not read files. - A dedicated audience is required. An empty audience or AllowAnyAudience is rejected, because every pod in the cluster can mint tokens from this issuer. cfg.Issuer and cfg.JWKSURI must be empty (both come from the bootstrap), and a caller-supplied HTTPClient or another private-address mode is rejected. Private-HTTPS hardening - With JWKSURI set, only the JWKS host is approved; the issuer host no longer is. toolhive-core never fetches the issuer when a JWKS URL is pinned, so nothing used that destination. A request to the issuer now fails with "not an approved endpoint" instead of being allowed. - scopedhttps caps every response body at 4 MiB using http.MaxBytesReader. A body of exactly 4 MiB succeeds; one more byte is an error. This applies to every NewSingleIssuerClient caller (cliconfig native endpoint, clientauth issuer client). - scopedhttps rejects two different CA bundles for the same host:port instead of merging them into one pool. Validator.Ready (new) - Fetches the JWKS URL and requires at least one key that parses as an RSA or EC public key, the key types toolhive-core accepts (it excludes EdDSA). The set is parsed with jwx v4's jwk package, non-strictly as core does, so one malformed key does not hide its siblings; a kid is optional. It returns nil without checking anything when no JWKSURI is configured (discovery mode). - authn/oidc takes jwx/v4 as a direct dependency (already in the module graph through toolhive-core), and depguard allows its jwk package. - Ready probes through the validator's own client: the one this package built for private-HTTPS mode, or the Kubernetes client above, so a probe uses the same transport, CA, token and timeout as the key fetches. Otherwise Ready applies the policy toolhive-core uses for its JWKS fetches: a caller-supplied client is copied and gets redirect refusal and a 15s timeout if it set none, and with no client one is built with networking.HttpClientBuilder. - The key check approximates toolhive-core's policy (it does not enforce the RSA size floor, use, key_ops, or algorithm match), as noted on usableJWKS. - depguard: authn-oidc may now import toolhive-core/networking. Tests - Kubernetes: the issuer is derived from the token, which reaches both endpoints, and tokens from another issuer are rejected; Ready sends a rotated token; a discovery issuer that differs from the token's, unusable tokens (none exposed in an error), bad discovery responses, a different origin, a JWKS URL that redirects to a valid key set, and 14 invalid configurations are all refused. - Ready: a table that makes every case reach Ready (usable keys, 500, redirect, not JSON, empty set, key without material), and a table for usableJWKS built from generated RSA, EC and Ed25519 keys, including fields that are present but not a key. - scopedhttps: exact-limit and over-limit bodies through the client, conflicting CA bundles. Checked with gofmt, go vet, golangci-lint and go test -race in authn/oidc, plus the tests of its direct consumers (internal/cliconfig, clientauth, oidcclient). The full lint and race gates are left to CI. Co-Authored-By: mecatl <[email protected]>
Merged
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The broker authenticates workloads with Kubernetes service-account tokens.
The API server's JWKS endpoint requires a bearer token, and authn/oidc had no
way to send one. Its private-HTTPS mode also approved more network
destinations than it used.
NewKubernetesValidator (new)
it, and accepts it only if the configured discovery URL returns the same
issuer. Tokens are then verified with keys from the configured JWKS URL.
Neither the token nor the discovery response chooses where requests go;
the discovery document is read only for its issuer.
over private addresses, and are verified only against the CA file
(TrustedCAFile). There is no per-host address pin; TLS verification against
the cluster CA is what authenticates the API server.
CA, the token file, a 15s timeout, redirects refused) serves the discovery
request, toolhive-core's key fetches, and Ready. The builder re-reads the
token file on every request, so rotation works; this relies on toolhive-core
v0.0.51, where v0.0.50 read the file once. TokenSource supplies the token's
contents once, for the issuer claim, because authn/oidc may not read files.
rejected, because every pod in the cluster can mint tokens from this issuer.
cfg.Issuer and cfg.JWKSURI must be empty (both come from the bootstrap), and
a caller-supplied HTTPClient or another private-address mode is rejected.
Private-HTTPS hardening
is. toolhive-core never fetches the issuer when a JWKS URL is pinned, so
nothing used that destination. A request to the issuer now fails with
"not an approved endpoint" instead of being allowed.
body of exactly 4 MiB succeeds; one more byte is an error. This applies to
every NewSingleIssuerClient caller (cliconfig native endpoint, clientauth
issuer client).
of merging them into one pool.
Validator.Ready (new)
EC public key, the key types toolhive-core accepts (it excludes EdDSA). The
set is parsed with jwx v4's jwk package, non-strictly as core does, so one
malformed key does not hide its siblings; a kid is optional. It returns nil
without checking anything when no JWKSURI is configured (discovery mode).
graph through toolhive-core), and depguard allows its jwk package.
built for private-HTTPS mode, or the Kubernetes client above, so a probe
uses the same transport, CA, token and timeout as the key fetches.
Otherwise Ready applies the policy toolhive-core uses for its JWKS fetches:
a caller-supplied client is copied and gets redirect refusal and a 15s
timeout if it set none, and with no client one is built with
networking.HttpClientBuilder.
RSA size floor, use, key_ops, or algorithm match), as noted on usableJWKS.
Tests
endpoints, and tokens from another issuer are rejected; Ready sends a
rotated token; a discovery issuer that differs from the token's, unusable
tokens (none exposed in an error), bad discovery responses, a different
origin, a JWKS URL that redirects to a valid key set, and 14 invalid
configurations are all refused.
redirect, not JSON, empty set, key without material), and a table for
usableJWKS built from generated RSA, EC and Ed25519 keys, including fields
that are present but not a key.
conflicting CA bundles.
Checked with gofmt, go vet, golangci-lint and go test -race in authn/oidc, plus
the tests of its direct consumers (internal/cliconfig, clientauth,
oidcclient). The full lint and race gates are left to CI.
Co-Authored-By: mecatl [email protected]
Summary
Issue relationship
Relates to #
Type of change
Test plan
Baseline checks
task lint)task test)task test:race)go run ./cmd/mecademo)task docs)task site:build)task api:check)task api:update+engine/CHANGELOG.mdChanges
User-facing change
Special notes for reviewers