Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion docs/architecture/service-token-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
35 changes: 34 additions & 1 deletion go/core/internal/service/controlauth/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"net/url"
"path"
"slices"
"strconv"
"strings"

"buf.build/go/protovalidate"
Expand All @@ -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"`
}

Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
61 changes: 61 additions & 0 deletions go/core/internal/service/controlauth/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion helm/kagent/templates/controller-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
20 changes: 20 additions & 0 deletions helm/kagent/tests/controller-configmap_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"\]'
3 changes: 3 additions & 0 deletions helm/kagent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
Expand Down
Loading