|
| 1 | +//go:build server |
| 2 | + |
| 3 | +package broker |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 11 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/transport" |
| 12 | +) |
| 13 | + |
| 14 | +// fakeResolver returns a per-user token so the injector can be exercised without |
| 15 | +// a real store/exchanger. It records the per-user resolution so tests can assert |
| 16 | +// one user's credential is never produced for another (FR-018). |
| 17 | +type fakeResolver struct { |
| 18 | + tokens map[string]string // userID -> access token |
| 19 | + err error |
| 20 | +} |
| 21 | + |
| 22 | +func (f *fakeResolver) Resolve(_ context.Context, userID string, _ *config.ServerConfig) (*UpstreamCredential, error) { |
| 23 | + if f.err != nil { |
| 24 | + return nil, f.err |
| 25 | + } |
| 26 | + tok, ok := f.tokens[userID] |
| 27 | + if !ok { |
| 28 | + return nil, ErrNoCredential |
| 29 | + } |
| 30 | + return &UpstreamCredential{AccessToken: tok, TokenType: "Bearer"}, nil |
| 31 | +} |
| 32 | + |
| 33 | +func httpBrokerServer() *config.ServerConfig { |
| 34 | + s := &config.ServerConfig{ |
| 35 | + Name: "ghe", |
| 36 | + URL: "https://ghe.example/mcp", |
| 37 | + Protocol: "streamable-http", |
| 38 | + AuthBroker: &config.AuthBrokerConfig{ |
| 39 | + Mode: config.AuthBrokerModeTokenExchange, |
| 40 | + TokenEndpoint: "https://idp.example/token", |
| 41 | + }, |
| 42 | + } |
| 43 | + s.AuthBroker.ApplyDefaults() |
| 44 | + return s |
| 45 | +} |
| 46 | + |
| 47 | +// FR-016: the resolved per-user credential is rendered into the configured |
| 48 | +// header/format (default Authorization: Bearer {token}). |
| 49 | +func TestInjector_InjectFor_ProducesBrokeredAuth(t *testing.T) { |
| 50 | + inj := NewHeaderInjector(&fakeResolver{tokens: map[string]string{"alice": "alice-tok"}}) |
| 51 | + |
| 52 | + ba, err := inj.InjectFor(context.Background(), "alice", httpBrokerServer()) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("InjectFor: %v", err) |
| 55 | + } |
| 56 | + if ba.Header != "Authorization" { |
| 57 | + t.Fatalf("header = %q, want Authorization", ba.Header) |
| 58 | + } |
| 59 | + if ba.HeaderValue() != "Bearer alice-tok" { |
| 60 | + t.Fatalf("header value = %q, want %q", ba.HeaderValue(), "Bearer alice-tok") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// FR-018 + SC-002/003: two users brokered against the SAME shared upstream get |
| 65 | +// two distinct outbound tokens; one user's credential is never reused for the |
| 66 | +// other. |
| 67 | +func TestInjector_TwoUsers_TwoTokens(t *testing.T) { |
| 68 | + inj := NewHeaderInjector(&fakeResolver{tokens: map[string]string{ |
| 69 | + "alice": "alice-tok", |
| 70 | + "bob": "bob-tok", |
| 71 | + }}) |
| 72 | + server := httpBrokerServer() |
| 73 | + |
| 74 | + aliceBA, err := inj.InjectFor(context.Background(), "alice", server) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("alice: %v", err) |
| 77 | + } |
| 78 | + bobBA, err := inj.InjectFor(context.Background(), "bob", server) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("bob: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if aliceBA.HeaderValue() == bobBA.HeaderValue() { |
| 84 | + t.Fatalf("two users produced the same outbound token %q (FR-018 violation)", aliceBA.HeaderValue()) |
| 85 | + } |
| 86 | + |
| 87 | + // And the effective outbound headers must carry each user's own token. |
| 88 | + aliceHdr := transport.EffectiveHeaders(nil, aliceBA) |
| 89 | + bobHdr := transport.EffectiveHeaders(nil, bobBA) |
| 90 | + if aliceHdr["Authorization"] != "Bearer alice-tok" || bobHdr["Authorization"] != "Bearer bob-tok" { |
| 91 | + t.Fatalf("cross-user token leak: alice=%q bob=%q", aliceHdr["Authorization"], bobHdr["Authorization"]) |
| 92 | + } |
| 93 | + |
| 94 | + // Per-(user,server) connection keys must differ so connections are never |
| 95 | + // pooled across users (FR-018). |
| 96 | + if ConnectionKey("alice", server) == ConnectionKey("bob", server) { |
| 97 | + t.Fatalf("connection key collided across users (FR-018 violation)") |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// FR-018: ConnectionKey is stable for the same (user, server) and distinct per |
| 102 | +// user and per server so a brokered connection is never reused across either. |
| 103 | +func TestConnectionKey_StableAndDistinct(t *testing.T) { |
| 104 | + s1 := httpBrokerServer() |
| 105 | + s2 := httpBrokerServer() |
| 106 | + s2.Name = "other" |
| 107 | + s2.URL = "https://other.example/mcp" |
| 108 | + |
| 109 | + k1 := ConnectionKey("alice", s1) |
| 110 | + k1again := ConnectionKey("alice", s1) |
| 111 | + if k1 != k1again { |
| 112 | + t.Fatal("ConnectionKey must be stable for the same (user, server)") |
| 113 | + } |
| 114 | + if ConnectionKey("alice", s1) == ConnectionKey("alice", s2) { |
| 115 | + t.Fatal("ConnectionKey must differ per server") |
| 116 | + } |
| 117 | + if ConnectionKey("alice", s1) == ConnectionKey("bob", s1) { |
| 118 | + t.Fatal("ConnectionKey must differ per user") |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// FR-002: brokering on a stdio upstream is rejected with a clear, actionable |
| 123 | +// message — never silently injected. |
| 124 | +func TestInjector_RejectsStdio(t *testing.T) { |
| 125 | + inj := NewHeaderInjector(&fakeResolver{tokens: map[string]string{"alice": "x"}}) |
| 126 | + stdio := &config.ServerConfig{ |
| 127 | + Name: "local", |
| 128 | + Protocol: "stdio", |
| 129 | + Command: "my-mcp", |
| 130 | + AuthBroker: &config.AuthBrokerConfig{ |
| 131 | + Mode: config.AuthBrokerModeTokenExchange, |
| 132 | + TokenEndpoint: "https://idp.example/token", |
| 133 | + }, |
| 134 | + } |
| 135 | + stdio.AuthBroker.ApplyDefaults() |
| 136 | + |
| 137 | + _, err := inj.InjectFor(context.Background(), "alice", stdio) |
| 138 | + if !errors.Is(err, ErrBrokerStdioUnsupported) { |
| 139 | + t.Fatalf("stdio brokering must be rejected with ErrBrokerStdioUnsupported, got %v", err) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// A server with no auth_broker block is not brokered: the injector returns |
| 144 | +// ErrBrokerNotConfigured and the caller proceeds with today's behaviour. |
| 145 | +func TestInjector_NotConfigured(t *testing.T) { |
| 146 | + inj := NewHeaderInjector(&fakeResolver{}) |
| 147 | + plain := &config.ServerConfig{Name: "plain", URL: "https://x/mcp", Protocol: "streamable-http"} |
| 148 | + |
| 149 | + _, err := inj.InjectFor(context.Background(), "alice", plain) |
| 150 | + if !errors.Is(err, ErrBrokerNotConfigured) { |
| 151 | + t.Fatalf("non-brokered server must return ErrBrokerNotConfigured, got %v", err) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// An empty userID is rejected before any resolution — brokering is strictly |
| 156 | +// per-user (FR-014). |
| 157 | +func TestInjector_RejectsAnonymous(t *testing.T) { |
| 158 | + inj := NewHeaderInjector(&fakeResolver{tokens: map[string]string{"alice": "x"}}) |
| 159 | + _, err := inj.InjectFor(context.Background(), "", httpBrokerServer()) |
| 160 | + if !errors.Is(err, ErrUnauthenticated) { |
| 161 | + t.Fatalf("anonymous caller must be rejected with ErrUnauthenticated, got %v", err) |
| 162 | + } |
| 163 | +} |
0 commit comments