check_client_ip_job_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package job
  2. import (
  3. "os"
  4. "path/filepath"
  5. "reflect"
  6. "runtime"
  7. "testing"
  8. "time"
  9. )
  10. func TestMergeClientIps_EvictsStaleOldEntries(t *testing.T) {
  11. // #4077: after a ban expires, a single IP that reconnects used to get
  12. // banned again immediately because a long-disconnected IP stayed in the
  13. // DB with an ancient timestamp and kept "protecting" itself against
  14. // eviction. Guard against that regression here.
  15. old := []IPWithTimestamp{
  16. {IP: "1.1.1.1", Timestamp: 100}, // stale — client disconnected long ago
  17. {IP: "2.2.2.2", Timestamp: 1900}, // fresh — still connecting
  18. }
  19. new := []IPWithTimestamp{
  20. {IP: "2.2.2.2", Timestamp: 2000}, // same IP, newer log line
  21. }
  22. got := mergeClientIps(old, new, 1000)
  23. want := map[string]int64{"2.2.2.2": 2000}
  24. if !reflect.DeepEqual(got, want) {
  25. t.Fatalf("stale 1.1.1.1 should have been dropped\ngot: %v\nwant: %v", got, want)
  26. }
  27. }
  28. func TestMergeClientIps_KeepsFreshOldEntriesUnchanged(t *testing.T) {
  29. // Backwards-compat: entries that aren't stale are still carried forward,
  30. // so enforcement survives access-log rotation.
  31. old := []IPWithTimestamp{
  32. {IP: "1.1.1.1", Timestamp: 1500},
  33. }
  34. got := mergeClientIps(old, nil, 1000)
  35. want := map[string]int64{"1.1.1.1": 1500}
  36. if !reflect.DeepEqual(got, want) {
  37. t.Fatalf("fresh old IP should have been retained\ngot: %v\nwant: %v", got, want)
  38. }
  39. }
  40. func TestMergeClientIps_PrefersLaterTimestampForSameIp(t *testing.T) {
  41. old := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 1500}}
  42. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 1700}}
  43. got := mergeClientIps(old, new, 1000)
  44. if got["1.1.1.1"] != 1700 {
  45. t.Fatalf("expected latest timestamp 1700, got %d", got["1.1.1.1"])
  46. }
  47. }
  48. func TestMergeClientIps_DropsStaleNewEntries(t *testing.T) {
  49. // A log line with a clock-skewed old timestamp must not resurrect a
  50. // stale IP past the cutoff.
  51. new := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 500}}
  52. got := mergeClientIps(nil, new, 1000)
  53. if len(got) != 0 {
  54. t.Fatalf("stale new IP should have been dropped, got %v", got)
  55. }
  56. }
  57. func TestMergeClientIps_NoStaleCutoffStillWorks(t *testing.T) {
  58. // Defensive: a zero cutoff (e.g. during very first run on a fresh
  59. // install) must not over-evict.
  60. old := []IPWithTimestamp{{IP: "1.1.1.1", Timestamp: 100}}
  61. new := []IPWithTimestamp{{IP: "2.2.2.2", Timestamp: 200}}
  62. got := mergeClientIps(old, new, 0)
  63. want := map[string]int64{"1.1.1.1": 100, "2.2.2.2": 200}
  64. if !reflect.DeepEqual(got, want) {
  65. t.Fatalf("zero cutoff should keep everything\ngot: %v\nwant: %v", got, want)
  66. }
  67. }
  68. func collectIps(entries []IPWithTimestamp) []string {
  69. out := make([]string, 0, len(entries))
  70. for _, e := range entries {
  71. out = append(out, e.IP)
  72. }
  73. return out
  74. }
  75. func TestPartitionLiveIps_SingleLiveNotStarvedByStillFreshHistoricals(t *testing.T) {
  76. // #4091: db holds A, B, C from minutes ago (still in the 30min
  77. // window) but they're not connecting anymore. only D is. old code
  78. // merged all four, sorted ascending, kept [A,B,C] and banned D
  79. // every tick. pin the new rule: only live ips count toward the limit.
  80. ipMap := map[string]int64{
  81. "A": 1000,
  82. "B": 1100,
  83. "C": 1200,
  84. "D": 2000,
  85. }
  86. observed := map[string]bool{"D": true}
  87. live, historical := partitionLiveIps(ipMap, observed)
  88. if got := collectIps(live); !reflect.DeepEqual(got, []string{"D"}) {
  89. t.Fatalf("live set should only contain the ip observed this scan\ngot: %v\nwant: [D]", got)
  90. }
  91. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"A", "B", "C"}) {
  92. t.Fatalf("historical set should contain db-only ips in ascending order\ngot: %v\nwant: [A B C]", got)
  93. }
  94. }
  95. func TestPartitionLiveIps_ConcurrentLiveIpsSortedAscending(t *testing.T) {
  96. // when several ips are really live, partition returns them all in the
  97. // live set sorted ascending by timestamp. updateInboundClientIps then
  98. // keeps the newest and bans the oldest (last-IP-wins, #4699).
  99. ipMap := map[string]int64{
  100. "A": 5000,
  101. "B": 5500,
  102. }
  103. observed := map[string]bool{"A": true, "B": true}
  104. live, historical := partitionLiveIps(ipMap, observed)
  105. if got := collectIps(live); !reflect.DeepEqual(got, []string{"A", "B"}) {
  106. t.Fatalf("both live ips should be in the live set, ascending\ngot: %v\nwant: [A B]", got)
  107. }
  108. if len(historical) != 0 {
  109. t.Fatalf("no historical ips expected, got %v", historical)
  110. }
  111. }
  112. func TestPartitionLiveIps_EmptyScanLeavesDbIntact(t *testing.T) {
  113. // quiet tick: nothing observed => nothing live. everything merged
  114. // is historical. keeps the panel from wiping recent-but-idle ips.
  115. ipMap := map[string]int64{
  116. "A": 1000,
  117. "B": 1100,
  118. }
  119. observed := map[string]bool{}
  120. live, historical := partitionLiveIps(ipMap, observed)
  121. if len(live) != 0 {
  122. t.Fatalf("no live ips expected, got %v", live)
  123. }
  124. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"A", "B"}) {
  125. t.Fatalf("all merged entries should flow to historical\ngot: %v\nwant: [A B]", got)
  126. }
  127. }
  128. func TestPartitionLiveIps_RecentSyncedIpIsLive(t *testing.T) {
  129. // Synced IPs from other nodes within 2 minutes should be counted as live
  130. // even if they weren't observed in the local scan.
  131. now := time.Now().Unix()
  132. ipMap := map[string]int64{
  133. "A": now - 30, // synced 30s ago -> live
  134. "B": now - 150, // synced 2m30s ago -> historical
  135. }
  136. observed := map[string]bool{}
  137. live, historical := partitionLiveIps(ipMap, observed)
  138. if got := collectIps(live); !reflect.DeepEqual(got, []string{"A"}) {
  139. t.Fatalf("recent IP should be live\ngot: %v\nwant: [A]", got)
  140. }
  141. if got := collectIps(historical); !reflect.DeepEqual(got, []string{"B"}) {
  142. t.Fatalf("older IP should be historical\ngot: %v\nwant: [B]", got)
  143. }
  144. }
  145. func TestCheckFail2BanInstalled_DisabledEnvSkipsClientProbe(t *testing.T) {
  146. t.Setenv("XUI_ENABLE_FAIL2BAN", "false")
  147. marker := fakeFail2BanClient(t)
  148. if (&CheckClientIpJob{}).checkFail2BanInstalled() {
  149. t.Fatal("fail2ban should be unavailable when XUI_ENABLE_FAIL2BAN=false")
  150. }
  151. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  152. t.Fatalf("fail2ban-client should not have been executed, stat error: %v", err)
  153. }
  154. }
  155. func TestCheckFail2BanInstalled_EmptyEnvSkipsClientProbe(t *testing.T) {
  156. t.Setenv("XUI_ENABLE_FAIL2BAN", "")
  157. marker := fakeFail2BanClient(t)
  158. if (&CheckClientIpJob{}).checkFail2BanInstalled() {
  159. t.Fatal("fail2ban should be unavailable when XUI_ENABLE_FAIL2BAN is empty")
  160. }
  161. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  162. t.Fatalf("fail2ban-client should not have been executed, stat error: %v", err)
  163. }
  164. }
  165. func TestIsFail2BanEnabled_DefaultsToEnabledWhenUnset(t *testing.T) {
  166. value, ok := os.LookupEnv("XUI_ENABLE_FAIL2BAN")
  167. os.Unsetenv("XUI_ENABLE_FAIL2BAN")
  168. t.Cleanup(func() {
  169. if ok {
  170. os.Setenv("XUI_ENABLE_FAIL2BAN", value)
  171. } else {
  172. os.Unsetenv("XUI_ENABLE_FAIL2BAN")
  173. }
  174. })
  175. if !isFail2BanEnabled() {
  176. t.Fatal("fail2ban should default to enabled when XUI_ENABLE_FAIL2BAN is unset")
  177. }
  178. }
  179. func TestCheckFail2BanInstalled_EnabledEnvProbesClient(t *testing.T) {
  180. t.Setenv("XUI_ENABLE_FAIL2BAN", "true")
  181. marker := fakeFail2BanClient(t)
  182. if !(&CheckClientIpJob{}).checkFail2BanInstalled() {
  183. t.Fatal("fail2ban should be available when the client probe succeeds")
  184. }
  185. if _, err := os.Stat(marker); err != nil {
  186. t.Fatalf("fail2ban-client should have been executed: %v", err)
  187. }
  188. }
  189. func fakeFail2BanClient(t *testing.T) string {
  190. t.Helper()
  191. dir := t.TempDir()
  192. marker := filepath.Join(dir, "probe-called")
  193. fakeClient := filepath.Join(dir, "fail2ban-client")
  194. script := "#!/bin/sh\n: > \"$FAIL2BAN_PROBE_MARKER\"\nexit 0\n"
  195. if runtime.GOOS == "windows" {
  196. fakeClient += ".bat"
  197. script = "@echo off\ntype nul > \"%FAIL2BAN_PROBE_MARKER%\"\nexit /b 0\n"
  198. }
  199. if err := os.WriteFile(fakeClient, []byte(script), 0o755); err != nil {
  200. t.Fatalf("write fake fail2ban-client: %v", err)
  201. }
  202. t.Setenv("FAIL2BAN_PROBE_MARKER", marker)
  203. t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
  204. return marker
  205. }