-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudfront_test.go
126 lines (111 loc) · 3.41 KB
/
cloudfront_test.go
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
package traefik_cloudfront_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/invit/traefik-cloudfront"
)
func TestCloudfront(t *testing.T) {
tests := []struct {
name string
trustedIPs []string
viewerAddress string
clientIP string
remoteAddr string
headers []string
trusted bool
}{
{
name: "IPv4 remote / IPv4 trusted",
trustedIPs: []string{"10.0.0.0/8"},
viewerAddress: "1.2.3.4:1585",
clientIP: "1.2.3.4",
remoteAddr: "10.0.0.1",
headers: []string{"X-Forwarded-For", "X-Real-IP"},
trusted: true,
}, {
name: "IPv4 remote / IPv4 all trusted",
trustedIPs: []string{"0.0.0.0/0"},
viewerAddress: "1.2.3.4:1585",
clientIP: "1.2.3.4",
remoteAddr: "10.0.0.1",
headers: []string{"X-Forwarded-For", "X-Real-IP"},
trusted: true,
}, {
name: "IPv4 remote / IPv4 untrusted",
trustedIPs: []string{"10.0.0.0/8"},
viewerAddress: "1.2.3.4:1585",
clientIP: "1.2.3.4",
remoteAddr: "100.0.0.1",
headers: []string{"X-Forwarded-For", "X-Real-IP"},
trusted: false,
}, {
name: "IPv6 remote (full length) / IPv4 trusted",
trustedIPs: []string{"10.0.0.0/8"},
viewerAddress: "1:2:3:4:5:6:7:8:9:4242",
clientIP: "1:2:3:4:5:6:7:8:9",
remoteAddr: "10.0.0.1",
headers: []string{"X-Forwarded-For", "X-Real-IP"},
trusted: true,
}, {
name: "IPv6 remote (shortened) / IPv4 trusted",
trustedIPs: []string{"10.0.0.0/8"},
viewerAddress: "1::9:4242",
clientIP: "1::9",
remoteAddr: "10.0.0.1",
headers: []string{"X-Forwarded-For", "X-Real-IP"},
trusted: true,
}, {
name: "IPv6 remote (full length) / IPv6 trusted",
trustedIPs: []string{"10::0/64"},
viewerAddress: "1:2:3:4:5:6:7:8:9:4242",
clientIP: "1:2:3:4:5:6:7:8:9",
remoteAddr: "10::ffff:ffff:ffff:fffe",
headers: []string{"X-Forwarded-For", "X-Real-IP"},
trusted: true,
}, {
name: "IPv6 remote (full length) / IPv6 untrusted",
trustedIPs: []string{"10::0/64"},
viewerAddress: "1:2:3:4:5:6:7:8:9:4242",
clientIP: "1:2:3:4:5:6:7:8:9",
remoteAddr: "11::ffff:ffff:ffff:fffe",
headers: []string{"X-Forwarded-For", "X-Real-IP"},
trusted: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
cfg := traefik_cloudfront.CreateConfig()
cfg.TrustedIPs = tt.trustedIPs
cfg.Headers = tt.headers
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})
handler, err := traefik_cloudfront.New(ctx, next, cfg, "cloudfront")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = tt.remoteAddr
req.Header.Set(traefik_cloudfront.RemoteAddressHeader, tt.viewerAddress)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
expected := ""
if tt.trusted {
expected = tt.clientIP
}
for _, h := range tt.headers {
assertHeader(t, req, h, expected)
}
})
}
}
func assertHeader(t *testing.T, req *http.Request, key, expected string) {
t.Helper()
if req.Header.Get(key) != expected {
t.Errorf("invalid header value: %s", req.Header.Get(key))
}
}