-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathproxy_auth_test.go
More file actions
450 lines (396 loc) · 12.7 KB
/
Copy pathproxy_auth_test.go
File metadata and controls
450 lines (396 loc) · 12.7 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
package pivnet_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
pivnet "github.com/pivotal-cf/go-pivnet/v9"
)
// mockAuthenticator is a mock implementation of ProxyAuthenticator for testing
type mockAuthenticator struct {
authenticateCalled int
authenticateError error
headerToSet string
headerValue string
}
func (m *mockAuthenticator) Authenticate(req *http.Request) error {
m.authenticateCalled++
if m.authenticateError != nil {
return m.authenticateError
}
if m.headerToSet != "" {
req.Header.Set(m.headerToSet, m.headerValue)
}
return nil
}
// mockRoundTripper is a mock implementation of http.RoundTripper for testing
type mockRoundTripper struct {
roundTripCalled int
response *http.Response
err error
}
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
m.roundTripCalled++
if m.err != nil {
return nil, m.err
}
if m.response != nil {
return m.response, nil
}
return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: http.NoBody,
Request: req,
}, nil
}
func TestNewProxyAuthTransport(t *testing.T) {
tests := []struct {
name string
transport http.RoundTripper
authenticator pivnet.ProxyAuthenticator
expectError bool
errorContains string
}{
{
name: "valid transport and authenticator",
transport: &mockRoundTripper{},
authenticator: &mockAuthenticator{},
expectError: false,
},
{
name: "nil transport",
transport: nil,
authenticator: &mockAuthenticator{},
expectError: true,
errorContains: "transport cannot be nil",
},
{
name: "nil authenticator",
transport: &mockRoundTripper{},
authenticator: nil,
expectError: true,
errorContains: "authenticator cannot be nil",
},
{
name: "both nil",
transport: nil,
authenticator: nil,
expectError: true,
errorContains: "transport cannot be nil",
},
{
name: "with http.Transport",
transport: &http.Transport{},
authenticator: &mockAuthenticator{},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
transport, err := pivnet.NewProxyAuthTransport(tt.transport, tt.authenticator)
if tt.expectError {
if err == nil {
t.Errorf("expected error containing '%s', but got no error", tt.errorContains)
return
}
if tt.errorContains != "" && !contains(err.Error(), tt.errorContains) {
t.Errorf("expected error containing '%s', but got: %v", tt.errorContains, err)
}
if transport != nil {
t.Errorf("expected nil transport on error, but got: %v", transport)
}
} else {
if err != nil {
t.Errorf("expected no error, but got: %v", err)
return
}
if transport == nil {
t.Errorf("expected transport to be non-nil")
}
}
})
}
}
func TestProxyAuthTransport_RoundTrip(t *testing.T) {
tests := []struct {
name string
authenticateError error
roundTripError error
expectError bool
errorContains string
expectedAuthCalls int
expectedRoundTripCalls int
}{
{
name: "successful round trip",
authenticateError: nil,
roundTripError: nil,
expectError: false,
expectedAuthCalls: 1,
expectedRoundTripCalls: 1,
},
{
name: "authentication fails",
authenticateError: fmt.Errorf("auth failed"),
roundTripError: nil,
expectError: true,
errorContains: "failed to authenticate proxy request",
expectedAuthCalls: 1,
expectedRoundTripCalls: 0,
},
{
name: "round trip fails",
authenticateError: nil,
roundTripError: fmt.Errorf("connection failed"),
expectError: true,
errorContains: "connection failed",
expectedAuthCalls: 1,
expectedRoundTripCalls: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAuth := &mockAuthenticator{
authenticateError: tt.authenticateError,
headerToSet: "Proxy-Authorization",
headerValue: "Bearer test-token",
}
mockRT := &mockRoundTripper{
err: tt.roundTripError,
}
transport, err := pivnet.NewProxyAuthTransport(mockRT, mockAuth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
req := httptest.NewRequest("GET", "http://example.com", nil)
resp, err := transport.RoundTrip(req)
if tt.expectError {
if err == nil {
t.Errorf("expected error containing '%s', but got no error", tt.errorContains)
return
}
if tt.errorContains != "" && !contains(err.Error(), tt.errorContains) {
t.Errorf("expected error containing '%s', but got: %v", tt.errorContains, err)
}
} else {
if err != nil {
t.Errorf("expected no error, but got: %v", err)
return
}
if resp == nil {
t.Errorf("expected response to be non-nil")
}
}
if mockAuth.authenticateCalled != tt.expectedAuthCalls {
t.Errorf("expected %d authenticate calls, got %d", tt.expectedAuthCalls, mockAuth.authenticateCalled)
}
if mockRT.roundTripCalled != tt.expectedRoundTripCalls {
t.Errorf("expected %d round trip calls, got %d", tt.expectedRoundTripCalls, mockRT.roundTripCalled)
}
})
}
}
func TestProxyAuthTransport_GetProxyConnectHeader(t *testing.T) {
t.Run("http.Transport sets GetProxyConnectHeader", func(t *testing.T) {
mockAuth := &mockAuthenticator{
headerToSet: "Proxy-Authorization",
headerValue: "Bearer connect-token",
}
httpTransport := &http.Transport{}
transport, err := pivnet.NewProxyAuthTransport(httpTransport, mockAuth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
// Verify GetProxyConnectHeader was set
if httpTransport.GetProxyConnectHeader == nil {
t.Fatal("expected GetProxyConnectHeader to be set")
}
// Test the GetProxyConnectHeader function
ctx := context.Background()
proxyURL, _ := url.Parse("http://proxy.example.com:8080")
headers, err := httpTransport.GetProxyConnectHeader(ctx, proxyURL, "example.com:443")
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}
if headers.Get("Proxy-Authorization") != "Bearer connect-token" {
t.Errorf("expected Proxy-Authorization header to be set, got: %s", headers.Get("Proxy-Authorization"))
}
if mockAuth.authenticateCalled != 1 {
t.Errorf("expected 1 authenticate call, got %d", mockAuth.authenticateCalled)
}
// Ensure transport is properly wrapped
if transport == nil {
t.Error("expected transport to be non-nil")
}
})
t.Run("GetProxyConnectHeader handles authentication error", func(t *testing.T) {
mockAuth := &mockAuthenticator{
authenticateError: fmt.Errorf("auth error"),
}
httpTransport := &http.Transport{}
_, err := pivnet.NewProxyAuthTransport(httpTransport, mockAuth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
ctx := context.Background()
proxyURL, _ := url.Parse("http://proxy.example.com:8080")
_, err = httpTransport.GetProxyConnectHeader(ctx, proxyURL, "example.com:443")
if err == nil {
t.Error("expected error, but got none")
}
if !contains(err.Error(), "failed to authenticate CONNECT request") {
t.Errorf("expected error to contain 'failed to authenticate CONNECT request', got: %v", err)
}
})
t.Run("non-http.Transport doesn't set GetProxyConnectHeader", func(t *testing.T) {
mockAuth := &mockAuthenticator{}
mockRT := &mockRoundTripper{}
transport, err := pivnet.NewProxyAuthTransport(mockRT, mockAuth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
if transport == nil {
t.Error("expected transport to be non-nil")
}
})
}
func TestProxyAuthTransport_WithMockProxy(t *testing.T) {
t.Run("mock proxy requiring authentication", func(t *testing.T) {
// Create a mock proxy server that requires authentication
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for Proxy-Authorization header
authHeader := r.Header.Get("Proxy-Authorization")
if authHeader == "" {
w.Header().Set("Proxy-Authenticate", "Basic realm=\"Test Proxy\"")
w.WriteHeader(http.StatusProxyAuthRequired)
w.Write([]byte("Proxy authentication required"))
return
}
// Validate the auth header
expectedAuth := "Basic dGVzdHVzZXI6dGVzdHBhc3M=" // testuser:testpass
if authHeader != expectedAuth {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Invalid credentials"))
return
}
// Authentication successful - proxy the request
w.WriteHeader(http.StatusOK)
w.Write([]byte("Proxy authenticated successfully"))
}))
defer proxyServer.Close()
// Create authenticator
auth := pivnet.NewBasicProxyAuth("testuser", "testpass")
// Create transport with proxy
transport, err := pivnet.NewProxyAuthTransport(http.DefaultTransport, auth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
// Make request through the "proxy" (simulated)
client := &http.Client{Transport: transport}
resp, err := client.Get(proxyServer.URL)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
})
t.Run("mock proxy rejecting invalid credentials", func(t *testing.T) {
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Proxy-Authorization")
// Always reject with wrong credentials
if authHeader != "Basic Y29ycmVjdDpjcmVkcw==" { // correct:creds
w.Header().Set("Proxy-Authenticate", "Basic realm=\"Test Proxy\"")
w.WriteHeader(http.StatusProxyAuthRequired)
w.Write([]byte("Authentication required"))
return
}
w.WriteHeader(http.StatusOK)
}))
defer proxyServer.Close()
// Use wrong credentials
auth := pivnet.NewBasicProxyAuth("wrong", "credentials")
transport, err := pivnet.NewProxyAuthTransport(http.DefaultTransport, auth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
client := &http.Client{Transport: transport}
resp, err := client.Get(proxyServer.URL)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
// Should get 407 Proxy Authentication Required
if resp.StatusCode != http.StatusProxyAuthRequired {
t.Errorf("expected status 407, got %d", resp.StatusCode)
}
})
t.Run("mock proxy forwarding to target server", func(t *testing.T) {
// Create target server
targetServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Target server response"))
}))
defer targetServer.Close()
// Create proxy server that actually forwards to target
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Proxy-Authorization")
if authHeader == "" {
w.WriteHeader(http.StatusProxyAuthRequired)
return
}
// Actually forward the request to target server
targetResp, err := http.Get(targetServer.URL)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte(fmt.Sprintf("Proxy failed to reach target: %v", err)))
return
}
defer targetResp.Body.Close()
// Copy response from target
body := make([]byte, 1024)
n, _ := targetResp.Body.Read(body)
w.WriteHeader(targetResp.StatusCode)
w.Write(body[:n])
}))
defer proxyServer.Close()
auth := pivnet.NewBasicProxyAuth("proxyuser", "proxypass")
transport, err := pivnet.NewProxyAuthTransport(http.DefaultTransport, auth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
client := &http.Client{Transport: transport}
resp, err := client.Get(proxyServer.URL)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
// Verify we got the response from the target server
body := make([]byte, 1024)
n, _ := resp.Body.Read(body)
responseBody := string(body[:n])
if responseBody != "Target server response" {
t.Errorf("expected 'Target server response', got '%s'", responseBody)
}
})
}
// Helper function to check if a string contains a substring
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(s) > 0 && len(substr) > 0 && containsHelper(s, substr)))
}
func containsHelper(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}