Repository navigation
Expand file tree
/
Copy pathallowiprequest_test.go
More file actions
292 lines (247 loc) · 9.03 KB
/
Copy pathallowiprequest_test.go
File metadata and controls
292 lines (247 loc) · 9.03 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
package allowiprequest_test
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/CAJIO/allowiprequest"
)
func TestAllowIpR(t *testing.T) {
cfg := allowiprequest.CreateConfig()
cfg.KnockURL = "/knock-knock"
cfg.WhitelistDuration = "1s" // Short duration for testing
cfg.AllowedSubnets = []string{"192.168.0.0/24"}
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
rw.Write([]byte("OK"))
})
handler, err := allowiprequest.New(ctx, next, cfg, "allowiprequest-plugin")
if err != nil {
t.Fatal(err)
}
// 1. Pass through for any IP (filtering is delegated to Traefik via allowlist file)
req := httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
req.RemoteAddr = "10.0.0.1:1234"
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected 200 OK for pass-through, got %d", recorder.Code)
}
if body := recorder.Body.String(); body != "OK" {
t.Errorf("expected OK body, got %s", body)
}
// 2. Allow Request from Allowed Subnet
req = httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
req.RemoteAddr = "192.168.0.5:1234" // Allowed Subnet
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected 200 OK for allowed subnet, got %d", recorder.Code)
}
if body := recorder.Body.String(); body != "OK" {
t.Errorf("expected OK body, got %s", body)
}
// 3. Knock to Whitelist IP
knockIP := "10.0.0.2:4321"
req = httptest.NewRequest(http.MethodGet, "http://localhost/knock-knock", nil)
req.RemoteAddr = knockIP
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected 200 OK for knock, got %d", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "Access Granted") {
t.Errorf("expected success page, got %s", recorder.Body.String())
}
// 4. Access after Knock
req = httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
req.RemoteAddr = knockIP
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected 200 OK after knock, got %d", recorder.Code)
}
// 5. Pass through after expiration (filtering is handled by Traefik)
time.Sleep(1100 * time.Millisecond)
req = httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
req.RemoteAddr = knockIP
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected 200 OK after expiration (pass-through), got %d", recorder.Code)
}
// 6. Admin View Access (Allowed Subnet)
req = httptest.NewRequest(http.MethodGet, "http://localhost/view-allow-ips", nil)
req.RemoteAddr = "192.168.0.10:5678" // Allowed
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected 200 OK for admin view from allowed subnet, got %d", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "Current Whitelisted IPs") {
t.Errorf("expected admin page content, got %s", recorder.Body.String())
}
// 7. Admin View Access Blocked (External IP)
req = httptest.NewRequest(http.MethodGet, "http://localhost/view-allow-ips", nil)
req.RemoteAddr = "10.10.10.10:1234" // Not allowed
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusForbidden {
t.Errorf("expected 403 Forbidden for admin view from external IP, got %d", recorder.Code)
}
}
func TestAllowlistFileWrite(t *testing.T) {
tmpDir := t.TempDir()
allowlistPath := filepath.Join(tmpDir, "conf.d", "allowlist.yml")
cfg := allowiprequest.CreateConfig()
cfg.KnockURL = "/knock-knock"
cfg.WhitelistDuration = "1s"
cfg.AllowedSubnets = []string{"192.168.0.0/16"}
cfg.AllowlistFile = allowlistPath
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
})
handler, err := allowiprequest.New(ctx, next, cfg, "allowiprequest-plugin")
if err != nil {
t.Fatal(err)
}
// File should be created on init with static subnets
data, err := os.ReadFile(allowlistPath)
if err != nil {
t.Fatalf("allowlist file not created on init: %v", err)
}
content := string(data)
if !strings.Contains(content, "192.168.0.0/16") {
t.Errorf("allowlist file missing static subnet, got:\n%s", content)
}
if !strings.Contains(content, "IPAllowList") {
t.Errorf("allowlist file missing IPAllowList key, got:\n%s", content)
}
if !strings.Contains(content, "http:") {
t.Errorf("allowlist file missing http block, got:\n%s", content)
}
if !strings.Contains(content, "tcp:") {
t.Errorf("allowlist file missing tcp block, got:\n%s", content)
}
// Knock to add dynamic IP
req := httptest.NewRequest(http.MethodGet, "http://localhost/knock-knock", nil)
req.RemoteAddr = "10.0.0.2:4321"
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
data, err = os.ReadFile(allowlistPath)
if err != nil {
t.Fatalf("failed to read allowlist after knock: %v", err)
}
content = string(data)
if !strings.Contains(content, "10.0.0.2/32") {
t.Errorf("allowlist file missing knocked IP, got:\n%s", content)
}
if !strings.Contains(content, "192.168.0.0/16") {
t.Errorf("allowlist file lost static subnet after knock, got:\n%s", content)
}
// Wait for expiry, then trigger cleanup
time.Sleep(1100 * time.Millisecond)
req = httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
req.RemoteAddr = "10.0.0.2:4321"
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
data, err = os.ReadFile(allowlistPath)
if err != nil {
t.Fatalf("failed to read allowlist after expiry: %v", err)
}
content = string(data)
if strings.Contains(content, "10.0.0.2/32") {
t.Errorf("allowlist file still contains expired IP, got:\n%s", content)
}
}
func TestPersistFile(t *testing.T) {
tmpDir := t.TempDir()
persistPath := filepath.Join(tmpDir, "whitelist.persist")
cfg := allowiprequest.CreateConfig()
cfg.KnockURL = "/knock-knock"
cfg.WhitelistDuration = "10s"
cfg.AllowedSubnets = []string{"192.168.0.0/24"}
cfg.PersistFile = persistPath
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
rw.Write([]byte("OK"))
})
handler, err := allowiprequest.New(ctx, next, cfg, "allowiprequest-plugin")
if err != nil {
t.Fatal(err)
}
// Knock to whitelist an IP
req := httptest.NewRequest(http.MethodGet, "http://localhost/knock-knock", nil)
req.RemoteAddr = "10.0.0.1:1234"
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200 for knock, got %d", recorder.Code)
}
// Verify persist file was created and contains the IP
data, err := os.ReadFile(persistPath)
if err != nil {
t.Fatalf("persist file not created after knock: %v", err)
}
content := string(data)
if !strings.Contains(content, `"ip": "10.0.0.1"`) {
t.Errorf("persist file missing knocked IP, got:\n%s", content)
}
// Create a new plugin instance with allowlistFile to verify restored IPs appear in the file
allowlistPath := filepath.Join(tmpDir, "conf.d", "allowlist.yml")
cfg2 := allowiprequest.CreateConfig()
cfg2.KnockURL = "/knock-knock"
cfg2.WhitelistDuration = "10s"
cfg2.AllowedSubnets = []string{"192.168.0.0/24"}
cfg2.PersistFile = persistPath
cfg2.AllowlistFile = allowlistPath
_, err = allowiprequest.New(ctx, next, cfg2, "allowiprequest-plugin-2")
if err != nil {
t.Fatal(err)
}
// The allowlist file should contain the restored IP
data, err = os.ReadFile(allowlistPath)
if err != nil {
t.Fatalf("allowlist file not created after restore: %v", err)
}
if !strings.Contains(string(data), "10.0.0.1/32") {
t.Errorf("allowlist file missing restored IP, got:\n%s", string(data))
}
// Test that expired entries are not restored
cfgExpired := allowiprequest.CreateConfig()
cfgExpired.KnockURL = "/knock-knock"
cfgExpired.WhitelistDuration = "1s"
cfgExpired.AllowedSubnets = []string{"192.168.0.0/24"}
cfgExpired.PersistFile = persistPath
cfgExpired.AllowlistFile = allowlistPath
handlerExp, err := allowiprequest.New(ctx, next, cfgExpired, "allowiprequest-plugin-exp")
if err != nil {
t.Fatal(err)
}
// Knock with short duration
req = httptest.NewRequest(http.MethodGet, "http://localhost/knock-knock", nil)
req.RemoteAddr = "10.0.0.50:5678"
recorder = httptest.NewRecorder()
handlerExp.ServeHTTP(recorder, req)
// Wait for expiry
time.Sleep(1100 * time.Millisecond)
// Create a new instance — the expired IP should not appear in the allowlist
_, err = allowiprequest.New(ctx, next, cfgExpired, "allowiprequest-plugin-3")
if err != nil {
t.Fatal(err)
}
data, err = os.ReadFile(allowlistPath)
if err != nil {
t.Fatalf("failed to read allowlist after expired restore: %v", err)
}
if strings.Contains(string(data), "10.0.0.50/32") {
t.Errorf("allowlist file should not contain expired IP, got:\n%s", string(data))
}
}