-
Notifications
You must be signed in to change notification settings - Fork 3
/
fail2ban_test.go
158 lines (123 loc) · 3.83 KB
/
fail2ban_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
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
package caddy_fail2ban
import (
"context"
"fmt"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func setupTest(t *testing.T) (string, string) {
t.Helper()
tempDir, err := os.MkdirTemp("", "caddy-fail2ban-test")
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
fail2banFile := path.Join(tempDir, "banned-ips")
return tempDir, fail2banFile
}
func cleanupTest(t *testing.T, tempDir string) {
t.Helper()
err := os.RemoveAll(tempDir)
if err != nil {
t.Fatalf("error removing temp directory: %v", err)
}
}
func TestModule(t *testing.T) {
tempDir, fail2banFile := setupTest(t)
defer cleanupTest(t, tempDir)
d := caddyfile.NewTestDispenser(fmt.Sprintf(`fail2ban %s`, fail2banFile))
m := Fail2Ban{}
err := m.UnmarshalCaddyfile(d)
if err != nil {
t.Errorf("unmarshal error: %v", err)
}
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err = m.Provision(ctx)
if err != nil {
t.Errorf("error provisioning: %v", err)
}
req := httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))
if got, exp := m.Match(req), false; got != exp {
t.Errorf("unexpected match. got: %t, exp: %t", got, exp)
}
bannedIps, err := m.banlist.getBannedIps()
if err != nil {
t.Errorf("error loading banned ips: %v", err)
}
if got, exp := len(bannedIps), 0; got != exp {
t.Errorf("unexpected number of banned IPs. got: %d, exp: %d", got, exp)
}
}
func TestHeaderBan(t *testing.T) {
tempDir, fail2banFile := setupTest(t)
defer cleanupTest(t, tempDir)
d := caddyfile.NewTestDispenser(fmt.Sprintf(`fail2ban %s`, fail2banFile))
m := Fail2Ban{}
err := m.UnmarshalCaddyfile(d)
if err != nil {
t.Errorf("unmarshal error: %v", err)
}
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err = m.Provision(ctx)
if err != nil {
t.Errorf("error provisioning: %v", err)
}
req := httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))
req.Header.Add("X-Caddy-Ban", "1")
if got, exp := m.Match(req), true; got != exp {
t.Errorf("unexpected match. got: %t, exp: %t", got, exp)
}
// Trigger explicit reload just to give the goroutine enough time to spin up,
// otherwise the defer above will delete the temporary directory before it
// had time to initialise
m.banlist.Reload()
}
func TestBanIp(t *testing.T) {
tempDir, fail2banFile := setupTest(t)
defer cleanupTest(t, tempDir)
d := caddyfile.NewTestDispenser(fmt.Sprintf(`fail2ban %s`, fail2banFile))
m := Fail2Ban{}
err := m.UnmarshalCaddyfile(d)
if err != nil {
t.Errorf("unmarshal error: %v", err)
}
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err = m.Provision(ctx)
if err != nil {
t.Errorf("error provisioning: %v", err)
}
req := httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))
req.RemoteAddr = "127.0.0.1:1337"
if m.Match(req) {
t.Errorf("IP banned unexpectedly")
}
// ban IP
reloadEvent := make(chan bool)
m.banlist.subscribeToReload(reloadEvent)
os.WriteFile(fail2banFile, []byte("127.0.0.1"), 0644)
<-reloadEvent
req = httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))
req.RemoteAddr = "127.0.0.1:1337"
if !m.Match(req) {
t.Errorf("IP should have been banned")
}
// unban by writing to a new file and moving it over
reloadEvent = make(chan bool)
m.banlist.subscribeToReload(reloadEvent)
newBanlistFile := path.Join(tempDir, "new-bans")
os.WriteFile(newBanlistFile, []byte(""), 0644)
os.Rename(newBanlistFile, fail2banFile)
<-reloadEvent
req = httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))
req.RemoteAddr = "127.0.0.1:1337"
if m.Match(req) {
t.Errorf("IP should NOT have been banned any more")
}
}