Skip to content
Open
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
64 changes: 53 additions & 11 deletions cmd/mecak8s/identity_broker_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,20 @@ const (
// gatewayClientSecretEnv names the protected route's client secret,
// which ToolHive reads at construction.
gatewayClientSecretEnv = "MECATL_TEST_CALENDAR_CLIENT_SECRET" // #nosec G101 -- environment variable name, not a credential.
// gatewaySigningKeyPlaceholder stands in gatewaySettings for the path of
// the embedded gateway's signing key, which each test writes.
gatewaySigningKeyPlaceholder = "/GATEWAY-SIGNING-KEY.pem"
)

// gatewaySettings declares the protected calendar route the gateway routes,
// in broker mode, the way an operator configures mecak8s's MCP servers. The
// in broker mode, the way an operator configures mecak8s's MCP servers, with
// the embedded gateway's signing key at gatewaySigningKeyPlaceholder. The
// upstream is never dialled: the vMCP holds no upstream token for it.
const gatewaySettings = `mcp:
mode: broker
broker:
callback_url: ` + gatewayCallbackURL + `
signing_key_file: ` + gatewaySigningKeyPlaceholder + `
servers:
- name: calendar
url: https://mcp.example/calendar
Expand Down Expand Up @@ -344,13 +349,13 @@ const gatewaySettings = `mcp:
// TestIdentityBrokerSessionToolCallThroughGateway:
//
// stand-in retired by
// test-only AS-2 trust and gateway seam #2533 and #2538
// (IdentityBrokerAS2Trust,
// IdentityBrokerGatewayServers); AS-2's test
// trust names Alice's AS-1 subject, which
// exists only after her sign-in, so these
// tests run the real AS-1 outside mecak8s
// rather than the one mecak8s hosts
// test-only AS-2 trust (IdentityBrokerAS2Trust), #2533
// which a gateway entry routing servers
// requires; AS-2's test trust names Alice's
// AS-1 subject, which exists only after her
// sign-in, so these tests run the real AS-1
// outside mecak8s rather than the one
// mecak8s hosts
// the embedded vMCP as the gateway, reached #2812
// in process
// no gateway stand-in: the vMCP is real, but #2537
Expand Down Expand Up @@ -646,7 +651,8 @@ func serveGatewayIdentityBroker(t *testing.T, extraSettings string, extraArgs []
as1HTTP.Transport = faults
t.Setenv(gatewayClientSecretEnv, "construction-only-secret")
settings := filepath.Join(t.TempDir(), "settings.yaml")
if err := os.WriteFile(settings, []byte(gatewaySettings+extraSettings), 0o600); err != nil {
signingKey := brokerconfigtest.WriteKey(t, filepath.Join(t.TempDir(), "gateway-signing-key.pem"), brokerconfigtest.NewKey(t))
if err := os.WriteFile(settings, []byte(strings.Replace(gatewaySettings, gatewaySigningKeyPlaceholder, signingKey, 1)+extraSettings), 0o600); err != nil {
t.Fatal(err)
}
caFile := h.WriteCACert(t, t.TempDir())
Expand All @@ -659,9 +665,9 @@ func serveGatewayIdentityBroker(t *testing.T, extraSettings string, extraArgs []
Now: func() time.Time { return time.Now().Add(time.Duration(gw.clockOffset.Load())) },
}, identityBrokerServeOptions{
clientKey: a.Key,
gateways: []identitybroker.GatewayEntry{brokerconfigtest.EmbeddedGateway(gatewayAS2Issuer, "calendar")},
args: append([]string{"--permission-config", settings}, extraArgs...),
edit: func(cfg *app.Config) {
cfg.IdentityBrokerGatewayServers = []string{"calendar"}
cfg.IdentityBrokerAS2Trust = mcpbrokertest.TrustAS1(mcpbrokertest.AS1Trust{
Issuer: a.Issuer(), JWKSURL: a.JWKSURL(), CAFile: caFile,
Subjects: []string{as1servertest.Subject(t, alice)},
Expand All @@ -675,6 +681,40 @@ func serveGatewayIdentityBroker(t *testing.T, extraSettings string, extraArgs []
return gw
}

// TestGatewaySigningKeySettingNeedsAnEmbeddedGatewayEntry: mecak8s reads the
// embedded gateway's signing key from settings.yaml and refuses it without
// the identity broker and an embedded gateway entry in its configuration
// file. It is not parallel: the protected route's client secret is read from
// an environment variable.
func TestGatewaySigningKeySettingNeedsAnEmbeddedGatewayEntry(t *testing.T) {
t.Setenv(gatewayClientSecretEnv, "construction-only-secret")
settings := filepath.Join(t.TempDir(), "settings.yaml")
signingKey := brokerconfigtest.WriteKey(t, filepath.Join(t.TempDir(), "gateway-signing-key.pem"), brokerconfigtest.NewKey(t))
if err := os.WriteFile(settings, []byte(strings.Replace(gatewaySettings, gatewaySigningKeyPlaceholder, signingKey, 1)), 0o600); err != nil {
t.Fatal(err)
}
base := []string{"--mock", "--no-shell", "--permission-mode=yolo", "--guardrails=off", "--session-lease-k8s-namespace", "", "--permission-config", settings}
for name, args := range map[string][]string{
"identity broker off": base,
"no gateway entry": append(slices.Clone(base), "--enable-identity-broker", "--identity-broker-config", brokerconfigtest.Write(t, brokerconfigtest.Options{}).Path),
} {
t.Run(name, func(t *testing.T) {
cfg, err := parseFlags(args)
if err != nil {
t.Fatalf("parseFlags: %v", err)
}
built, err := buildIsolated(t, t.Context(), appConfig(cfg, port.NopDiagnostics{}, observability{}))
if err == nil {
built.Close()
t.Fatal("app.Build accepted the signing key")
}
if !strings.Contains(err.Error(), "mcp.broker.signing_key_file requires the identity broker") {
t.Fatalf("app.Build error = %v, want the signing key refused", err)
}
})
}
}

// gatewayToolScript is a mock model whose first and third turns call the
// gateway route's echo tool, each with its own call ID.
const gatewayToolScript = `{"turns":[
Expand Down Expand Up @@ -1001,6 +1041,8 @@ type identityBrokerServeOptions struct {
// clientKey is the broker client key, the key the AS-1 registered; nil
// generates one.
clientKey *ecdsa.PrivateKey
// gateways are the configuration file's gateway entries.
gateways []identitybroker.GatewayEntry
// args are further flags.
args []string
// edit, when set, changes the application configuration before the
Expand All @@ -1016,7 +1058,7 @@ type identityBrokerServeOptions struct {
// the built application; both stop when t ends.
func serveIdentityBroker(t *testing.T, tokens identityBrokerAS1Tokens, as1 *app.IdentityBrokerAS1, opts identityBrokerServeOptions) (string, *app.Built) {
t.Helper()
brokerFile := brokerconfigtest.Write(t, brokerconfigtest.Options{BrokerClientKey: opts.clientKey}).Path
brokerFile := brokerconfigtest.Write(t, brokerconfigtest.Options{BrokerClientKey: opts.clientKey, Gateways: opts.gateways}).Path
cfg, err := parseFlags(append([]string{
"--mock",
"--enable-identity-broker", "--no-shell", // the identity broker refuses to run beside a shell
Expand Down
194 changes: 194 additions & 0 deletions deploy/helm/mecak8s/gateway_entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package mecak8s_test

import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"

corev1 "k8s.io/api/core/v1"

"github.com/stacklok/mecatl/internal/identitybroker"
"github.com/stacklok/mecatl/internal/identitybroker/adapter/brokerconfig"
"github.com/stacklok/mecatl/internal/identitybroker/adapter/brokerconfig/brokerconfigtest"
)

// gatewayEntryValues adds to the production identity broker profile a
// protected MCP server, an embedded gateway entry for the embedded gateway
// mecak8s derives from its callback URL, and the embedded gateway's signing
// key with a fallback.
const gatewayEntryValues = `
mcp:
broker:
callbackURL: https://mecatl.example.com/v1/mcp/authorization/callback
signingKey:
secretName: mcp-gateway-signing
key: key.pem
fallbacks:
- {secretName: mcp-gateway-signing-old, key: key.pem}
servers:
- name: calendar
url: https://calendar.example.com/mcp
auth:
mode: oauth
oauth:
issuer: https://calendar.example.com
client:
mode: preregistered
preregistered:
id: mecatl
secretKeyRef: {name: calendar-oauth, key: client-secret}
scopes: [openid]
network: {additionalOrigins: [], privateOrigins: [], maxRedirects: 0}
identityBroker:
gateways:
- kind: embedded
as2Issuer: https://mecatl.example.com/v1/mcp/broker
tokenEndpoint: https://mecatl.example.com/v1/mcp/broker/oauth/token
resource: https://mecatl.example.com/v1/mcp/broker/mcp
servers: []
`

// renderGatewayEntry renders the production identity broker profile with
// gatewayEntryValues and then extra values.
func renderGatewayEntry(t *testing.T, extra ...string) (string, error) {
t.Helper()
path := filepath.Join(t.TempDir(), "gateway-values.yaml")
if err := os.WriteFile(path, []byte(gatewayEntryValues), 0o600); err != nil {
t.Fatal(err)
}
args := []string{"template", "ib", ".", "-f", "ci/production-identity-broker-values.yaml", "-f", path}
for _, value := range extra {
args = append(args, "--set", value)
}
return helm(t, args...)
}

// TestMecak8sHelmChart_GatewayEntryAndSigningKey pins the gateway entry in
// the identity broker's configuration file, which the broker's loader reads,
// and the embedded gateway's signing key, projected read-only to the paths
// settings.yaml names, which the runtime resolver carries to the MCP broker.
func TestMecak8sHelmChart_GatewayEntryAndSigningKey(t *testing.T) {
rendered, err := renderGatewayEntry(t)
if err != nil {
t.Fatalf("render: %v\n%s", err, rendered)
}
raw := identityBrokerConfigFromRender(t, rendered)
var doc struct {
Gateways []map[string]any `json:"gateways"`
}
if err := json.Unmarshal([]byte(raw), &doc); err != nil {
t.Fatalf("identity-broker.json: %v", err)
}
wantEntry := map[string]any{
"kind": "embedded", "as2_issuer": "https://mecatl.example.com/v1/mcp/broker",
"token_endpoint": "https://mecatl.example.com/v1/mcp/broker/oauth/token",
"resource": "https://mecatl.example.com/v1/mcp/broker/mcp", "servers": []any{},
}
if len(doc.Gateways) != 1 || !reflect.DeepEqual(doc.Gateways[0], wantEntry) {
t.Fatalf("gateways = %v, want [%v]", doc.Gateways, wantEntry)
}
// The broker's own loader accepts the rendered entry beside the hosted
// AS-1, whose issuer is on the same host under another path.
const dir = "/var/run/secrets/identity-broker/"
keys := t.TempDir()
write := func(rel string) string {
path := filepath.Join(keys, rel)
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
return path
}
client := brokerconfigtest.NewKey(t)
brokerconfigtest.WriteKey(t, write("agent-issuer/key.pem"), brokerconfigtest.NewKey(t))
brokerconfigtest.WriteKey(t, write("broker-client/key.pem"), client)
brokerconfigtest.WriteKeySet(t, write("broker-client-key-set/jwks.json"), client)
brokerconfigtest.WriteKey(t, write("as1-signing/signing-key.pem"), brokerconfigtest.NewKey(t))
configFile := filepath.Join(t.TempDir(), "identity-broker.json")
if err := os.WriteFile(configFile, []byte(strings.ReplaceAll(raw, dir, keys+"/")), 0o600); err != nil {
t.Fatal(err)
}
loaded, err := brokerconfig.Load(configFile, brokerconfig.Host{AS1: true, AS1Issuer: "https://mecatl.example.com/as1"})
if err != nil {
t.Fatalf("brokerconfig.Load of the rendered file: %v", err)
}
if len(loaded.Gateways) != 1 || loaded.Gateways[0].Kind != identitybroker.GatewayEmbedded || len(loaded.Gateways[0].Servers) != 0 {
t.Fatalf("loaded gateways = %+v, want the embedded entry routing no servers", loaded.Gateways)
}

profile := configMapFromRender(t, rendered, "ib-mecak8s-mcp").Data["settings.yaml"]
broker, ok := runtimeMCPAuthorityFromConfigMap(t, profile).Broker()
const keyDir = "/var/run/secrets/mcp-broker-signing-key/"
if !ok || broker.SigningKeyFile != keyDir+"signing-key/key.pem" || !slices.Equal(broker.FallbackKeyFiles, []string{keyDir + "fallback-0/key.pem"}) {
t.Fatalf("resolved signing key = %q %v, want the projected paths\n%s", broker.SigningKeyFile, broker.FallbackKeyFiles, profile)
}
spec := deploymentFromRender(t, rendered).Spec.Template.Spec
m := slices.IndexFunc(spec.Containers[0].VolumeMounts, func(m corev1.VolumeMount) bool { return m.Name == "mcp-broker-signing-key" })
if m < 0 || !spec.Containers[0].VolumeMounts[m].ReadOnly || spec.Containers[0].VolumeMounts[m].MountPath != strings.TrimSuffix(keyDir, "/") {
t.Fatalf("the signing key is not mounted read-only at %s: %+v", keyDir, spec.Containers[0].VolumeMounts)
}
i := slices.IndexFunc(spec.Volumes, func(v corev1.Volume) bool { return v.Name == "mcp-broker-signing-key" })
if i < 0 || spec.Volumes[i].Projected == nil || spec.Volumes[i].Projected.DefaultMode == nil || *spec.Volumes[i].Projected.DefaultMode != 0o440 {
t.Fatalf("mcp-broker-signing-key is not a 0440 projected volume: %+v", spec.Volumes)
}
projected := map[string]string{}
for _, source := range spec.Volumes[i].Projected.Sources {
for _, item := range source.Secret.Items {
projected[item.Path] = source.Secret.Name + "/" + item.Key
}
}
if want := map[string]string{"signing-key/key.pem": "mcp-gateway-signing/key.pem", "fallback-0/key.pem": "mcp-gateway-signing-old/key.pem"}; !reflect.DeepEqual(projected, want) {
t.Fatalf("projected signing keys = %v, want %v", projected, want)
}
}

// TestMecak8sHelmChart_GatewayEntryRefusals mirrors mecak8s's startup rules
// at render time.
func TestMecak8sHelmChart_GatewayEntryRefusals(t *testing.T) {
for name, tc := range map[string]struct {
set []string
want string
}{
"servers before AS-2 trusts AS-1": {[]string{"identityBroker.gateways[0].servers={calendar}"}, "must stay empty until the embedded gateway trusts AS-1"},
"a server that is not an OAuth route": {[]string{"identityBroker.gateways[0].servers={github}"}, `"github" is not an mcp.servers name`},
"an external entry": {[]string{"identityBroker.gateways[0].kind=external"}, "external gateways are not supported yet (#2812)"},
"two entries": {[]string{
"identityBroker.gateways[1].kind=embedded", "identityBroker.gateways[1].as2Issuer=https://other.example.com/v1/mcp/broker",
"identityBroker.gateways[1].tokenEndpoint=https://other.example.com/v1/mcp/broker/oauth/token", "identityBroker.gateways[1].resource=https://other.example.com/v1/mcp/broker/mcp",
}, "at most one entry"},
"another issuer": {[]string{"identityBroker.gateways[0].as2Issuer=https://other.example.com/v1/mcp/broker"}, "gateways[0].as2Issuer must be https://mecatl.example.com/v1/mcp/broker"},
"another token endpoint": {[]string{"identityBroker.gateways[0].tokenEndpoint=https://mecatl.example.com/token"}, "gateways[0].tokenEndpoint must be"},
"another resource": {[]string{"identityBroker.gateways[0].resource=https://mecatl.example.com/mcp"}, "gateways[0].resource must be"},
"AS-1's issuer": {[]string{
"identityBroker.as1.issuer=https://mecatl.example.com/v1/mcp/broker", "oidc.issuer=",
}, "must differ from identityBroker.as1.issuer"},
"AS-1's issuer with the default port and a trailing slash": {[]string{
"identityBroker.as1.issuer=https://MECATL.example.com:443/v1/mcp/broker/", "oidc.issuer=",
}, "must differ from identityBroker.as1.issuer"},
"no signing key": {[]string{"mcp.broker.signingKey.secretName=", "mcp.broker.signingKey.key=", "mcp.broker.signingKey.fallbacks=null"}, "identityBroker.gateways requires mcp.broker.signingKey"},
"a signing key without an entry": {[]string{"identityBroker.gateways=null"}, "mcp.broker.signingKey requires identityBroker.enabled with an identityBroker.gateways entry"},
"a signing key that is a broker key": {[]string{"mcp.broker.signingKey.secretName=identity-broker-agent-issuer"}, "name the same Secret key identity-broker-agent-issuer/key.pem"},
"no embedded gateway": {[]string{"mcp.broker.callbackURL="}, ""},
} {
t.Run(name, func(t *testing.T) {
output, err := renderGatewayEntry(t, tc.set...)
if err == nil || !strings.Contains(output, tc.want) {
t.Fatalf("render = %v, want failure %q:\n%s", err, tc.want, output)
}
})
}
// mecak8s compares paths case-sensitively, so an AS-1 issuer whose path
// differs from AS-2's only in case is a different issuer and renders.
if output, err := renderGatewayEntry(t, "identityBroker.as1.issuer=https://mecatl.example.com/V1/mcp/broker", "oidc.issuer="); err != nil {
t.Fatalf("render with AS-1's issuer differing in path case = %v, want success:\n%s", err, output)
}
// With the broker off, a signing key fails the render.
output, err := helm(t, "template", "off", ".", "--set", "mockProvider=true,permissionMode=yolo,redis.local.enabled=true",
"--set", "mcp.broker.signingKey.secretName=mcp-gateway-signing,mcp.broker.signingKey.key=key.pem")
if err == nil || !strings.Contains(output, "mcp.broker.signingKey requires identityBroker.enabled") {
t.Fatalf("render with the broker off = %v, want the signing key refused:\n%s", err, output)
}
}
Loading
Loading