forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassthrough_internal_test.go
More file actions
594 lines (556 loc) · 18.1 KB
/
Copy pathpassthrough_internal_test.go
File metadata and controls
594 lines (556 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package aibridge
import (
"crypto/tls"
"io"
"maps"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"strings"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/aibridge/config"
"github.com/coder/coder/v2/aibridge/internal/testutil"
"github.com/coder/coder/v2/aibridge/keypool"
"github.com/coder/coder/v2/aibridge/provider"
"github.com/coder/quartz"
)
var testTracer = otel.Tracer("bridge_test")
func TestPassthroughRoutes(t *testing.T) {
t.Parallel()
upstreamRespBody := "upstream response"
tests := []struct {
name string
baseURLPath string
reqPath string
reqHost string
reqRemoteAddr string
reqHeaders http.Header
expectRequestPath string
expectQuery string
expectHeaders http.Header
expectRespStatus int
expectRespBody string
}{
{
name: "passthrough_route_no_path",
reqPath: "/v1/conversations",
expectRequestPath: "/v1/conversations",
expectRespStatus: http.StatusOK,
expectRespBody: upstreamRespBody,
},
{
name: "base_URL_path_is_preserved_in_passthrough_routes",
baseURLPath: "/api/v2",
reqPath: "/v1/models",
expectRequestPath: "/api/v2/v1/models",
expectRespStatus: http.StatusOK,
expectRespBody: upstreamRespBody,
},
{
name: "passthrough_route_break_parse_base_url",
baseURLPath: "/%zz",
reqPath: "/v1/models/",
expectRespStatus: http.StatusBadGateway,
expectRespBody: "invalid provider base URL",
},
{
name: "passthrough_route_rejects_invalid_base_url_path",
baseURLPath: "/%25",
reqPath: "/v1/models",
expectRespStatus: http.StatusBadGateway,
expectRespBody: "invalid provider base URL",
},
{
name: "proxy_headers_are_set_and_forwarded_chain_is_appended",
reqPath: "/v1/models",
reqHost: "client.example.com",
reqRemoteAddr: "1.1.1.1:1111",
reqHeaders: http.Header{
"X-Forwarded-For": {"2.2.2.2, 3.3.3.3"},
},
expectRequestPath: "/v1/models",
expectRespStatus: http.StatusOK,
expectRespBody: upstreamRespBody,
expectHeaders: http.Header{
"Accept-Encoding": {"gzip"},
"User-Agent": {"aibridge"},
"X-Forwarded-For": {"2.2.2.2, 3.3.3.3, 1.1.1.1"},
"X-Forwarded-Host": {"client.example.com"},
"X-Forwarded-Proto": {"http"},
},
},
{
name: "query_string_is_preserved",
reqPath: "/v1/models?search=gpt&limit=10",
expectRequestPath: "/v1/models",
expectQuery: "search=gpt&limit=10",
expectRespStatus: http.StatusOK,
expectRespBody: upstreamRespBody,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
logger := slogtest.Make(t, nil)
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tc.expectRequestPath, r.URL.Path)
assert.Equal(t, tc.expectQuery, r.URL.RawQuery)
if tc.expectHeaders != nil {
assert.Equal(t, tc.expectHeaders, r.Header)
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(upstreamRespBody))
}))
t.Cleanup(upstream.Close)
prov := &testutil.MockProvider{
URL: upstream.URL + tc.baseURLPath,
}
handler := newPassthroughRouter(prov, logger, nil, testTracer)
req := httptest.NewRequest("", tc.reqPath, nil)
maps.Copy(req.Header, tc.reqHeaders)
req.Host = tc.reqHost
req.RemoteAddr = tc.reqRemoteAddr
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, tc.expectRespStatus, resp.Code)
assert.Contains(t, resp.Body.String(), tc.expectRespBody)
})
}
}
func TestRewritePassthroughRequest(t *testing.T) {
t.Parallel()
tests := []struct {
name string
reqPath string
reqRemoteAddr string
reqHeaders http.Header
reqTLS bool
provider *testutil.MockProvider
expectURL string
expectHeaders http.Header
}{
{
name: "sets_upstream_url_and_forwarded_headers_from_client_peer",
reqPath: "http://client-host/chat?stream=true",
reqRemoteAddr: "1.1.1.1:1111",
provider: &testutil.MockProvider{URL: "https://upstream-host/base"},
expectURL: "https://upstream-host/base/chat?stream=true",
expectHeaders: http.Header{
"X-Forwarded-Host": {"client-host"},
"X-Forwarded-Proto": {"http"},
"X-Forwarded-For": {"1.1.1.1"},
"User-Agent": {"aibridge"},
},
},
{
name: "preserves_client_user_agent",
reqPath: "http://client-host/chat",
reqRemoteAddr: "1.1.1.1:1111",
reqHeaders: http.Header{"User-Agent": {"custom-agent/1.0"}},
provider: &testutil.MockProvider{URL: "https://upstream-host/base"},
expectURL: "https://upstream-host/base/chat",
expectHeaders: http.Header{
"X-Forwarded-Host": {"client-host"},
"X-Forwarded-Proto": {"http"},
"X-Forwarded-For": {"1.1.1.1"},
"User-Agent": {"custom-agent/1.0"},
},
},
{
name: "appends_remote_addr_to_existing_forwarded_for_chain",
reqPath: "http://client-host/chat",
reqRemoteAddr: "1.1.1.1:1111",
reqHeaders: http.Header{
"X-Forwarded-For": {"2.2.2.2, 3.3.3.3"},
},
provider: &testutil.MockProvider{URL: "https://upstream-host/base"},
expectURL: "https://upstream-host/base/chat",
expectHeaders: http.Header{
"X-Forwarded-Host": {"client-host"},
"X-Forwarded-Proto": {"http"},
"X-Forwarded-For": {"2.2.2.2, 3.3.3.3, 1.1.1.1"},
"User-Agent": {"aibridge"},
},
},
{
name: "tls_request_sets_forwarded_proto_to_https",
reqPath: "http://client-host/chat",
reqRemoteAddr: "1.1.1.1:1111",
reqTLS: true,
provider: &testutil.MockProvider{URL: "https://upstream-host/base"},
expectURL: "https://upstream-host/base/chat",
expectHeaders: http.Header{
"X-Forwarded-Host": {"client-host"},
"X-Forwarded-Proto": {"https"},
"X-Forwarded-For": {"1.1.1.1"},
"User-Agent": {"aibridge"},
},
},
{
// This is an edge case where whole `X-Forwarded-For` header
// is dropped if last hop (remote addr) is not parseable.
// This is how library handles this case and is not directly
// related to our code. Added it to verify that we
// don't accidentally break this behavior.
name: "omits_forwarded_for_when_remote_addr_is_not_parseable",
reqPath: "http://client-host/chat",
reqRemoteAddr: "not-a-socket-address",
reqHeaders: http.Header{
"X-Forwarded-For": {"1.1.1.1"},
},
provider: &testutil.MockProvider{URL: "https://upstream-host/base"},
expectURL: "https://upstream-host/base/chat",
expectHeaders: http.Header{
"X-Forwarded-Host": {"client-host"},
"X-Forwarded-Proto": {"http"},
"User-Agent": {"aibridge"},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
r := httptest.NewRequest(http.MethodGet, tc.reqPath, nil)
maps.Copy(r.Header, tc.reqHeaders)
r.RemoteAddr = tc.reqRemoteAddr
if tc.reqTLS {
r.TLS = &tls.ConnectionState{}
}
provBaseURL, err := url.Parse(tc.provider.URL)
assert.NoError(t, err)
pr := &httputil.ProxyRequest{
In: r,
Out: r.Clone(r.Context()),
}
rewritePassthroughRequest(pr, provBaseURL)
assert.Equal(t, tc.expectURL, pr.Out.URL.String())
assert.Equal(t, "", pr.Out.Host)
assert.Equal(t, tc.expectHeaders, pr.Out.Header)
})
}
}
func TestPassthroughRouterReusesProxyInstance(t *testing.T) {
t.Parallel()
var newConnections atomic.Int32
upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
}))
upstream.Config.ConnState = func(_ net.Conn, state http.ConnState) {
if state == http.StateNew {
newConnections.Add(1)
}
}
upstream.Start()
t.Cleanup(upstream.Close)
logger := slogtest.Make(t, nil)
prov := &testutil.MockProvider{URL: upstream.URL}
handler := newPassthroughRouter(prov, logger, nil, testTracer)
for i := range 2 {
req := httptest.NewRequest(http.MethodGet, "http://proxy.example.test/v1/models", nil)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equalf(t, http.StatusOK, resp.Code, "request %d", i+1)
assert.Equal(t, "ok", resp.Body.String())
}
assert.EqualValues(t, 1, newConnections.Load())
}
// TestPassthrough_KeyFailover exercises the KeyFailoverTransport
// end-to-end through the passthrough proxy, parameterised over
// providers (anthropic, openai). Each scenario asserts the upstream
// request count, the response status and Retry-After, and the final
// pool state.
func TestPassthrough_KeyFailover(t *testing.T) {
t.Parallel()
type upstreamResponse struct {
statusCode int
body string
headers map[string]string
}
const (
rateLimitBody = `{"error":"rate"}`
authErrorBody = `{"error":"unauthorized"}`
serverErrorBody = `{"error":"server"}`
successBody = `{"data":[]}`
)
// providers parameterises the table over the providers exposed
// to the failover transport. Each entry encapsulates the
// provider-specific bits the test needs: how the mock upstream
// extracts the key from the request, how a BYOK request sets
// it, and how the provider is constructed for a given pool.
providers := []struct {
name string
byokOnly bool
extractKey func(*http.Request) string
setBYOK func(*http.Request, string)
newProvider func(baseURL string, pool *keypool.Pool) provider.Provider
}{
{
name: "anthropic",
extractKey: func(r *http.Request) string {
return r.Header.Get("X-Api-Key")
},
setBYOK: func(r *http.Request, key string) {
r.Header.Set("X-Api-Key", key)
},
newProvider: func(baseURL string, pool *keypool.Pool) provider.Provider {
return provider.NewAnthropic(config.Anthropic{
BaseURL: baseURL,
KeyPool: pool,
}, nil)
},
},
{
name: "openai",
extractKey: func(r *http.Request) string {
return strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
},
setBYOK: func(r *http.Request, key string) {
r.Header.Set("Authorization", "Bearer "+key)
},
newProvider: func(baseURL string, pool *keypool.Pool) provider.Provider {
cfg := config.OpenAI{BaseURL: baseURL}
if pool != nil {
cfg.KeyPool = pool
}
return provider.NewOpenAI(cfg)
},
},
// Copilot is BYOK-only: its KeyFailoverConfig is zero-value
// so the failover transport short-circuits.
{
name: "copilot",
byokOnly: true,
extractKey: func(r *http.Request) string {
return strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
},
setBYOK: func(r *http.Request, key string) {
r.Header.Set("Authorization", "Bearer "+key)
},
newProvider: func(baseURL string, _ *keypool.Pool) provider.Provider {
return provider.NewCopilot(config.Copilot{BaseURL: baseURL})
},
},
}
tests := []struct {
name string
// Centralized pool keys. Empty when byokKey is set.
keys []string
// BYOK key. Empty when keys is set.
byokKey string
// Scripted upstream responses keyed by API key value.
responses map[string]upstreamResponse
expectedRequestCount int32
expectedStatusCode int
expectedRetryAfter string
// Expected key states after the request, by index in keys.
expectedKeyStates []keypool.KeyState
}{
{
// Given: 1 valid key returning 200.
// Then: 1 request, 200 response, key remains valid.
name: "single_valid_key",
keys: []string{"k0"},
responses: map[string]upstreamResponse{
"k0": {statusCode: http.StatusOK, body: successBody},
},
expectedRequestCount: 1,
expectedStatusCode: http.StatusOK,
expectedKeyStates: []keypool.KeyState{keypool.KeyStateValid},
},
{
// Given: 2 keys; key-0 returns 429, key-1 returns 200.
// Then: 2 requests, 200 response, key-0 temporary, key-1 valid.
name: "failover_after_429",
keys: []string{"k0", "k1"},
responses: map[string]upstreamResponse{
"k0": {
statusCode: http.StatusTooManyRequests,
headers: map[string]string{"Retry-After": "5"},
body: rateLimitBody,
},
"k1": {statusCode: http.StatusOK, body: successBody},
},
expectedRequestCount: 2,
expectedStatusCode: http.StatusOK,
expectedKeyStates: []keypool.KeyState{
keypool.KeyStateTemporary,
keypool.KeyStateValid,
},
},
{
// Given: 2 keys; key-0 returns 401, key-1 returns 200.
// Then: 2 requests, 200 response, key-0 permanent, key-1 valid.
name: "failover_after_401",
keys: []string{"k0", "k1"},
responses: map[string]upstreamResponse{
"k0": {statusCode: http.StatusUnauthorized, body: authErrorBody},
"k1": {statusCode: http.StatusOK, body: successBody},
},
expectedRequestCount: 2,
expectedStatusCode: http.StatusOK,
expectedKeyStates: []keypool.KeyState{
keypool.KeyStatePermanent,
keypool.KeyStateValid,
},
},
{
// Given: 2 keys; key-0 returns 403, key-1 returns 200.
// Then: 2 requests, 200 response, key-0 permanent, key-1 valid.
name: "failover_after_403",
keys: []string{"k0", "k1"},
responses: map[string]upstreamResponse{
"k0": {statusCode: http.StatusForbidden, body: authErrorBody},
"k1": {statusCode: http.StatusOK, body: successBody},
},
expectedRequestCount: 2,
expectedStatusCode: http.StatusOK,
expectedKeyStates: []keypool.KeyState{
keypool.KeyStatePermanent,
keypool.KeyStateValid,
},
},
{
// Given: 3 keys; all return 429 with cooldowns 5s, 3s, 10s.
// Then: 3 requests, 429 response with smallest Retry-After,
// all keys temporary.
name: "all_keys_rate_limited",
keys: []string{"k0", "k1", "k2"},
responses: map[string]upstreamResponse{
"k0": {
statusCode: http.StatusTooManyRequests,
headers: map[string]string{"Retry-After": "5"},
body: rateLimitBody,
},
"k1": {
statusCode: http.StatusTooManyRequests,
headers: map[string]string{"Retry-After": "3"},
body: rateLimitBody,
},
"k2": {
statusCode: http.StatusTooManyRequests,
headers: map[string]string{"Retry-After": "10"},
body: rateLimitBody,
},
},
expectedRequestCount: 3,
expectedStatusCode: http.StatusTooManyRequests,
expectedRetryAfter: "3",
expectedKeyStates: []keypool.KeyState{
keypool.KeyStateTemporary,
keypool.KeyStateTemporary,
keypool.KeyStateTemporary,
},
},
{
// Given: 2 keys; both return 401.
// Then: 2 requests, 502 response, both keys permanent.
name: "all_keys_unauthorized",
keys: []string{"k0", "k1"},
responses: map[string]upstreamResponse{
"k0": {statusCode: http.StatusUnauthorized, body: authErrorBody},
"k1": {statusCode: http.StatusUnauthorized, body: authErrorBody},
},
expectedRequestCount: 2,
expectedStatusCode: http.StatusBadGateway,
expectedKeyStates: []keypool.KeyState{
keypool.KeyStatePermanent,
keypool.KeyStatePermanent,
},
},
{
// Given: 2 keys; key-0 returns 500.
// Then: 1 request, 500 response, both keys remain valid.
name: "server_error_no_failover",
keys: []string{"k0", "k1"},
responses: map[string]upstreamResponse{
"k0": {statusCode: http.StatusInternalServerError, body: serverErrorBody},
},
expectedRequestCount: 1,
expectedStatusCode: http.StatusInternalServerError,
expectedKeyStates: []keypool.KeyState{
keypool.KeyStateValid,
keypool.KeyStateValid,
},
},
{
// Given: BYOK with a single user-supplied key returning 429.
// Then: 1 request, 429 forwarded as-is, no failover.
name: "byok_no_failover",
byokKey: "user-byok",
responses: map[string]upstreamResponse{
"user-byok": {
statusCode: http.StatusTooManyRequests,
headers: map[string]string{"Retry-After": "5"},
body: rateLimitBody,
},
},
expectedRequestCount: 1,
expectedStatusCode: http.StatusTooManyRequests,
expectedRetryAfter: "5",
},
}
for _, prov := range providers {
for _, tc := range tests {
// BYOK-only providers don't use the pool, so pool-based
// cases don't apply.
if prov.byokOnly && tc.byokKey == "" {
continue
}
t.Run(prov.name+"/"+tc.name, func(t *testing.T) {
t.Parallel()
// Mock upstream: counts requests and returns
// scripted responses keyed by API key. An unmapped
// key falls through to 500 so misconfigured cases
// surface via the status assertion.
var requestCount atomic.Int32
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestCount.Add(1)
_, _ = io.Copy(io.Discard, r.Body)
resp, ok := tc.responses[prov.extractKey(r)]
if !ok {
resp = upstreamResponse{statusCode: http.StatusInternalServerError}
}
w.Header().Set("Content-Type", "application/json")
for hk, hv := range resp.headers {
w.Header().Set(hk, hv)
}
w.WriteHeader(resp.statusCode)
_, _ = w.Write([]byte(resp.body))
}))
t.Cleanup(upstream.Close)
var pool *keypool.Pool
if len(tc.keys) > 0 {
var err error
pool, err = keypool.New(tc.keys, quartz.NewMock(t))
require.NoError(t, err)
}
p := prov.newProvider(upstream.URL, pool)
// IgnoreErrors: MarkKey logs at ERROR level when a
// key is marked permanent (401/403); slogtest would
// otherwise fail those scenarios.
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
handler := newPassthroughRouter(p, logger, nil, testTracer)
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
if tc.byokKey != "" {
prov.setBYOK(req, tc.byokKey)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
assert.Equal(t, tc.expectedRequestCount, requestCount.Load(), "upstream request count")
assert.Equal(t, tc.expectedStatusCode, w.Code, "response status code")
assert.Equal(t, tc.expectedRetryAfter, w.Header().Get("Retry-After"), "Retry-After header")
if pool != nil {
assert.Equal(t, tc.expectedKeyStates, pool.PoolState(), "key states")
}
})
}
}
}