From 41a52a732ad4c595bd35420d4ea082bb9887410f Mon Sep 17 00:00:00 2001 From: James Olds <12104969+oldsj@users.noreply.github.com> Date: Wed, 7 Oct 2026 19:59:36 +0000 Subject: [PATCH] fix: allow exact in-cluster HTTP MCP credential origins Add controller.auth.serviceToken.cleartextMCPOrigins (default empty). Each entry must be an exact lowercase http origin on a .svc.cluster.local host, optional port, with no path, query, fragment, userinfo or wildcard; duplicates are rejected at startup. Only approved MCP-purpose credential tuples may use a listed origin; everything else still requires HTTPS, and the control API transport is unchanged. Signed-off-by: James Olds <12104969+oldsj@users.noreply.github.com> --- .../service-token-authentication.md | 29 ++++++++- .../internal/service/controlauth/policy.go | 35 ++++++++++- .../service/controlauth/policy_test.go | 61 +++++++++++++++++++ .../templates/controller-configmap.yaml | 2 +- .../tests/controller-configmap_test.yaml | 20 ++++++ helm/kagent/values.yaml | 3 + 6 files changed, 147 insertions(+), 3 deletions(-) diff --git a/docs/architecture/service-token-authentication.md b/docs/architecture/service-token-authentication.md index 0ad5bef4a6..1d3c4c38bb 100644 --- a/docs/architecture/service-token-authentication.md +++ b/docs/architecture/service-token-authentication.md @@ -63,7 +63,7 @@ controller: ``` The tuple uses the Agent namespace, a shell-style Secret name pattern, exact key -and HTTPS origin, and a case-insensitive authorization header. Only MCP-purpose +and exact origin, and a case-insensitive authorization header. Only MCP-purpose credentials are supported. Duplicate origin/header pairs and unapproved extra headers deny before Session reservation or Secret resolution. Existing checkout validation against the prepared Agent revision still applies. @@ -78,3 +78,30 @@ or a Secret label lookup. No label alone confers authority. Control bearers are stripped from outbound HTTP headers and A2A service metadata. Runtime identity headers continue to follow the existing callback contract; this slice does not claim verified runtime identity or listener separation. + +Credential origins require HTTPS by default. To allow an in-cluster MCP service +using HTTP, explicitly configure the separate allowlist: + +```yaml +controller: + auth: + serviceToken: + cleartextMCPOrigins: + - http://mainloop-mcp.mainloop.svc.cluster.local +``` + +The corresponding approved credential tuple must use that exact origin and +`purpose: mcp`. Entries must be plain HTTP origins with a lowercase DNS hostname +ending in `.svc.cluster.local` and an optional numeric port from 1 to 65535. +Paths (including `/`), wildcards, userinfo, queries, fragments, and non-cluster +hosts are rejected at startup, even when no credential tuple uses the entry. +The empty default denies HTTP tuples. Host and port differences never match; +the allowlist does not itself grant access to credentials. This exception only +controls MCP credential tuples and does not change control API transport or TLS. + +This policy setting only admits the credential tuple; it does not enable gateway +injection or prove routing. The actor egress gateway must separately be configured +to inject credentials for that exact cleartext host, and actor traffic must route +through it. The injected Authorization header travels unencrypted from the gateway +to the in-cluster service, relying on the cluster network as the trust boundary. +Use HTTPS where that trust boundary is not acceptable. diff --git a/go/core/internal/service/controlauth/policy.go b/go/core/internal/service/controlauth/policy.go index 68a6fa216e..818ce954bd 100644 --- a/go/core/internal/service/controlauth/policy.go +++ b/go/core/internal/service/controlauth/policy.go @@ -6,6 +6,8 @@ import ( "errors" "net/url" "path" + "slices" + "strconv" "strings" "buf.build/go/protovalidate" @@ -31,6 +33,7 @@ type Config struct { Namespace string `json:"namespace"` Agents []string `json:"agents"` Credentials []CredentialRule `json:"credentials"` + CleartextMCPOrigins []string `json:"cleartextMCPOrigins"` DevelopmentEnvironmentRegistries []string `json:"developmentEnvironmentRegistries"` } @@ -50,15 +53,24 @@ func New(config Config) (*Policy, error) { return nil, errors.New("invalid service Agent allowlist") } } + for i, origin := range config.CleartextMCPOrigins { + if !validCleartextMCPOrigin(origin) { + return nil, errors.New("cleartext MCP allowlist requires exact HTTP origins on svc.cluster.local hosts") + } + if slices.Contains(config.CleartextMCPOrigins[:i], origin) { + return nil, errors.New("cleartext MCP allowlist contains a duplicate origin") + } + } for _, rule := range config.Credentials { origin, err := url.Parse(rule.Origin) _, patternErr := path.Match(rule.SecretNamePattern, "probe") - if rule.Namespace != config.Namespace || rule.SecretNamePattern == "" || patternErr != nil || rule.Key == "" || rule.Purpose != "mcp" || rule.Header != "authorization" || err != nil || origin.Scheme != "https" || origin.Host == "" || origin.User != nil || origin.RawQuery != "" || origin.ForceQuery || origin.Fragment != "" || origin.Path != "" { + if rule.Namespace != config.Namespace || rule.SecretNamePattern == "" || patternErr != nil || rule.Key == "" || rule.Purpose != "mcp" || rule.Header != "authorization" || err != nil || (origin.Scheme != "https" && !(origin.Scheme == "http" && slices.Contains(config.CleartextMCPOrigins, rule.Origin))) || origin.Host == "" || origin.User != nil || origin.RawQuery != "" || origin.ForceQuery || origin.Fragment != "" || origin.Path != "" { return nil, errors.New("invalid approved credential tuple") } } config.Agents = append([]string(nil), config.Agents...) config.Credentials = append([]CredentialRule(nil), config.Credentials...) + config.CleartextMCPOrigins = append([]string(nil), config.CleartextMCPOrigins...) for _, registry := range config.DevelopmentEnvironmentRegistries { parsed, err := url.Parse("https://" + registry) if err != nil || registry == "" || parsed.Host != registry || parsed.Hostname() == "" || parsed.User != nil || parsed.Path != "" || parsed.RawQuery != "" || parsed.ForceQuery || parsed.Fragment != "" || registry != strings.ToLower(registry) { @@ -69,6 +81,27 @@ func New(config Config) (*Policy, error) { return &Policy{config: config}, nil } +// validCleartextMCPOrigin accepts only canonical, exact in-cluster HTTP origins. +func validCleartextMCPOrigin(origin string) bool { + parsed, err := url.Parse(origin) + if err != nil || parsed.Scheme != "http" || parsed.User != nil || parsed.Path != "" || parsed.RawQuery != "" || parsed.ForceQuery || parsed.Fragment != "" || parsed.RawFragment != "" { + return false + } + host := parsed.Hostname() + if !strings.HasSuffix(host, ".svc.cluster.local") || len(validation.IsDNS1123Subdomain(host)) != 0 { + return false + } + authority := host + if port := parsed.Port(); port != "" { + number, err := strconv.Atoi(port) + if err != nil || number < 1 || number > 65535 { + return false + } + authority += ":" + port + } + return origin == "http://"+authority +} + // WithRuntimePlatforms binds selection to the operator's current payload catalog. // It returns a copy, keeping the authenticator's original policy immutable. func (p *Policy) WithRuntimePlatforms(platforms []string) *Policy { diff --git a/go/core/internal/service/controlauth/policy_test.go b/go/core/internal/service/controlauth/policy_test.go index fe7ed9288d..3910c9ebf1 100644 --- a/go/core/internal/service/controlauth/policy_test.go +++ b/go/core/internal/service/controlauth/policy_test.go @@ -52,3 +52,64 @@ func TestServicePolicy(t *testing.T) { require.Error(t, p.CheckSession(t.Context(), &api.Session{Creator: "mainloop", Agent: &api.ResourceReference{Namespace: "kagent", Name: "other"}})) require.Error(t, CheckMethod("/future.Service/Method")) } + +func TestCleartextMCPOrigins(t *testing.T) { + const origin = "http://mainloop-mcp.mainloop.svc.cluster.local" + for _, tc := range []struct { + name string + allowed []string + origin, purpose string + wantErr bool + }{ + {"exact", []string{origin}, origin, "mcp", false}, + {"exact port", []string{origin + ":8080"}, origin + ":8080", "mcp", false}, + {"empty default", nil, origin, "mcp", true}, + {"other host", []string{origin}, "http://other.mainloop.svc.cluster.local", "mcp", true}, + {"other port", []string{origin}, origin + ":8080", "mcp", true}, + {"other purpose", []string{origin}, origin, "model", true}, + {"tuple path", []string{origin}, origin + "/mcp", "mcp", true}, + {"HTTPS default", nil, "https://example.com", "mcp", false}, + } { + t.Run(tc.name, func(t *testing.T) { + p, err := New(Config{Namespace: "kagent", Agents: []string{"mainloop-main"}, CleartextMCPOrigins: tc.allowed, Credentials: []CredentialRule{{Namespace: "kagent", SecretNamePattern: "binding", Key: "authorization", Origin: tc.origin, Header: "authorization", Purpose: tc.purpose}}}) + if tc.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + agent := &api.ResourceReference{Namespace: "kagent", Name: "mainloop-main"} + credential := &api.SessionCredential{Origin: tc.origin, Header: "Authorization", SecretRef: &api.SecretKeyReference{Name: "binding", Key: "authorization"}} + require.NoError(t, p.CheckCreateSession(t.Context(), agent, []*api.SessionCredential{credential})) + for _, denied := range []string{origin + "/mcp", "http://other.mainloop.svc.cluster.local", origin + ":9090"} { + credential.Origin = denied + require.Error(t, p.CheckCreateSession(t.Context(), agent, []*api.SessionCredential{credential})) + } + }) + } +} + +func TestMalformedCleartextMCPOrigins(t *testing.T) { + const origin = "http://mainloop-mcp.mainloop.svc.cluster.local" + for _, invalid := range []string{ + "", "https://mainloop-mcp.mainloop.svc.cluster.local", "http://example.com", + "http://*.svc.cluster.local", "http://svc.cluster.local", "http://MAINLOOP.mainloop.svc.cluster.local", + origin + "/", origin + "/mcp", origin + "?", origin + "?key=value", origin + "#", origin + "#fragment", + "http://user@mainloop-mcp.mainloop.svc.cluster.local", origin + ":", origin + ":abc", origin + ":0", origin + ":65536", + "http://bad_name.mainloop.svc.cluster.local", "http://127.0.0.1", origin + ".", " " + origin, + } { + t.Run(invalid, func(t *testing.T) { + _, err := New(Config{Namespace: "kagent", Agents: []string{"mainloop-main"}, CleartextMCPOrigins: []string{invalid}}) + require.Error(t, err) + }) + } +} + +func TestDuplicateCleartextMCPOrigins(t *testing.T) { + const origin = "http://mainloop-mcp.mainloop.svc.cluster.local" + _, err := New(Config{ + Namespace: "kagent", + Agents: []string{"mainloop-main"}, + CleartextMCPOrigins: []string{origin, origin}, + }) + require.Error(t, err) +} diff --git a/helm/kagent/templates/controller-configmap.yaml b/helm/kagent/templates/controller-configmap.yaml index 9d176c16b2..a96e62682e 100644 --- a/helm/kagent/templates/controller-configmap.yaml +++ b/helm/kagent/templates/controller-configmap.yaml @@ -10,7 +10,7 @@ data: {{- if eq .Values.controller.auth.mode "service-token" }} KAGENT_AUTH_SERVICE_TOKEN_CURRENT_FILE: /run/kagent-control-auth/current KAGENT_AUTH_SERVICE_TOKEN_NEXT_FILE: {{ ternary "/run/kagent-control-auth/next" "" (ne .Values.controller.auth.serviceToken.nextKey "") | quote }} - KAGENT_AUTH_SERVICE_POLICY: {{ dict "namespace" .Values.controller.auth.serviceToken.namespace "agents" .Values.controller.auth.serviceToken.agents "credentials" .Values.controller.auth.serviceToken.credentials "developmentEnvironmentRegistries" .Values.controller.auth.serviceToken.developmentEnvironmentRegistries | toJson | quote }} + KAGENT_AUTH_SERVICE_POLICY: {{ dict "namespace" .Values.controller.auth.serviceToken.namespace "agents" .Values.controller.auth.serviceToken.agents "credentials" .Values.controller.auth.serviceToken.credentials "cleartextMCPOrigins" .Values.controller.auth.serviceToken.cleartextMCPOrigins "developmentEnvironmentRegistries" .Values.controller.auth.serviceToken.developmentEnvironmentRegistries | toJson | quote }} {{- end }} KAGENT_AUTH_USER_ID_CLAIM: {{ .Values.controller.auth.userIdClaim | quote }} KAGENT_GATEWAY_URL: {{ include "kagent.a2aGatewayUrl" . | quote }} diff --git a/helm/kagent/tests/controller-configmap_test.yaml b/helm/kagent/tests/controller-configmap_test.yaml index 7824e687c6..c3e2b4d429 100644 --- a/helm/kagent/tests/controller-configmap_test.yaml +++ b/helm/kagent/tests/controller-configmap_test.yaml @@ -145,3 +145,23 @@ tests: - matchRegex: path: data.KAGENT_AUTH_SERVICE_POLICY pattern: '"developmentEnvironmentRegistries":\["registry.example:5000"\]' + + - it: denies cleartext MCP origins by default + set: + controller.auth.mode: service-token + controller.auth.serviceToken.secretName: mainloop-control + asserts: + - matchRegex: + path: data.KAGENT_AUTH_SERVICE_POLICY + pattern: '"cleartextMCPOrigins":\[\]' + + - it: renders exact cleartext MCP origins + set: + controller.auth.mode: service-token + controller.auth.serviceToken.secretName: mainloop-control + controller.auth.serviceToken.cleartextMCPOrigins: + - http://mainloop-mcp.mainloop.svc.cluster.local + asserts: + - matchRegex: + path: data.KAGENT_AUTH_SERVICE_POLICY + pattern: '"cleartextMCPOrigins":\["http://mainloop-mcp.mainloop.svc.cluster.local"\]' diff --git a/helm/kagent/values.yaml b/helm/kagent/values.yaml index 59c7dd57ca..ba7c4a0cb3 100644 --- a/helm/kagent/values.yaml +++ b/helm/kagent/values.yaml @@ -251,6 +251,9 @@ controller: # header: authorization # purpose: mcp credentials: [] + # Exact HTTP origins on *.svc.cluster.local hosts, only for MCP tuples. + # Empty requires HTTPS for every credential tuple. + cleartextMCPOrigins: [] # Exact registry authorities (optionally host:port), never repository prefixes. # Empty denies development environment selection, including for Mainloop. developmentEnvironmentRegistries: []