owned_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package firewall
  2. import (
  3. "os"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func TestOwnedPayloadPreservesForeignRulesAndPolicies(t *testing.T) {
  10. live := "*filter\n:INPUT DROP [0:0]\n-A INPUT -m comment --comment foreign -j ACCEPT\n-A INPUT -m comment --comment \"id:filter-old\" -j DROP\nCOMMIT\n"
  11. payload := BuildIPTablesRestore([]FilterRule{{ID: "filter-new", Chain: "input", Action: "accept"}}, nil, nil)
  12. result := ownedPayload(payload, live)
  13. if strings.Contains(result, ":INPUT ACCEPT") || strings.Contains(result, "-F INPUT") || strings.Contains(result, "-D INPUT -m comment --comment foreign") {
  14. t.Fatal("changed a foreign rule or host policy")
  15. }
  16. if !strings.Contains(result, "-D INPUT -m comment --comment \"id:filter-old\"") || !strings.Contains(result, "-F DT_filter_INPUT") {
  17. t.Fatal(result)
  18. }
  19. second := ownedPayload(payload, live+"\n*filter\n-A INPUT -m comment --comment debtik:hook -j DT_filter_INPUT\n")
  20. if strings.Contains(strings.Split(second, "*filter\n")[1], "-I INPUT 1") {
  21. t.Fatal("duplicated hook")
  22. }
  23. }
  24. func TestOwnedFirewallInNamespace(t *testing.T) {
  25. if os.Getenv("DEBTIK_FIREWALL_INTEGRATION") != "1" {
  26. t.Skip("requires an isolated network namespace")
  27. }
  28. run := func(args ...string) string {
  29. t.Helper()
  30. out, err := exec.Command("iptables", args...).CombinedOutput()
  31. if err != nil {
  32. t.Fatalf("%v: %s: %v", args, out, err)
  33. }
  34. return string(out)
  35. }
  36. run("-P", "INPUT", "DROP")
  37. run("-A", "INPUT", "-m", "comment", "--comment", "foreign", "-j", "ACCEPT")
  38. rules := []FilterRule{{ID: "filter-test", Chain: "input", Action: "drop", SrcAddress: "192.0.2.0/24"}}
  39. if err := ApplyDualStack(rules, nil, nil); err != nil {
  40. t.Fatal(err)
  41. }
  42. if err := ApplyDualStack(rules, nil, nil); err != nil {
  43. t.Fatal(err)
  44. }
  45. state := run("-S")
  46. if !strings.Contains(state, "-P INPUT DROP") || !strings.Contains(state, "--comment foreign") || strings.Count(state, "-j DT_filter_INPUT") != 1 {
  47. t.Fatal(state)
  48. }
  49. rules[0].Action = "accept"
  50. if err := ApplyDualStack(rules, nil, nil); err != nil {
  51. t.Fatal(err)
  52. }
  53. if strings.Contains(run("-S", "DT_filter_INPUT"), "-j DROP") {
  54. t.Fatal("old rule retained")
  55. }
  56. rules = append(rules, FilterRule{ID: "filter-v6", IPVersion: 6, Chain: "input", Action: "drop", SrcAddress: "2001:db8::/32"})
  57. if err := ApplyDualStack(rules, nil, nil); err != nil {
  58. t.Fatal(err)
  59. }
  60. out, err := exec.Command("ip6tables", "-S", "DT_filter_INPUT").CombinedOutput()
  61. if err != nil || !strings.Contains(string(out), "2001:db8::/32") {
  62. t.Fatal(string(out), err)
  63. }
  64. }
  65. func TestMoveNATRulesWithinChainInNamespace(t *testing.T) {
  66. if os.Getenv("DEBTIK_FIREWALL_PERSIST_TEST") != "1" {
  67. t.Skip("isolated mount and network namespaces required")
  68. }
  69. dir := t.TempDir()
  70. if out, err := exec.Command("mount", "--bind", dir, "/etc/debtik/firewall").CombinedOutput(); err != nil {
  71. t.Fatalf("bind: %s: %v", out, err)
  72. }
  73. m := &FirewallManager{natRules: []NATRule{
  74. {ID: "nat-first", Chain: "srcnat", Action: "accept"},
  75. {ID: "nat-other", Chain: "output", Action: "accept"},
  76. {ID: "nat-last", Chain: "postrouting", Action: "accept"},
  77. }}
  78. m.remember()
  79. if err := m.Apply(); err != nil {
  80. t.Fatal(err)
  81. }
  82. if err := m.MoveNATRule("nat-first", "down"); err != nil {
  83. t.Fatal(err)
  84. }
  85. out, err := exec.Command("iptables", "-t", "nat", "-S", "DT_nat_POSTROUTING").CombinedOutput()
  86. if err != nil {
  87. t.Fatalf("iptables: %s: %v", out, err)
  88. }
  89. last, first := strings.Index(string(out), "id:nat-last"), strings.Index(string(out), "id:nat-first")
  90. if last < 0 || first < 0 || last >= first {
  91. t.Fatalf("wrong chain order: %s", out)
  92. }
  93. }
  94. // Run only under both an isolated mount namespace and network namespace.
  95. func TestFirewallFailedSaveRollsBack(t *testing.T) {
  96. if os.Getenv("DEBTIK_FIREWALL_PERSIST_TEST") != "1" {
  97. t.Skip("requires isolated mount and network namespaces")
  98. }
  99. dir := t.TempDir()
  100. if err := exec.Command("mount", "--bind", dir, "/etc/debtik/firewall").Run(); err != nil {
  101. t.Fatal(err)
  102. }
  103. m := NewFirewallManager()
  104. good := FilterRule{ID: "filter-transaction", Chain: "input", Action: "accept", Protocol: "tcp", DstPort: "22"}
  105. if err := m.SaveFilterRule(good); err != nil {
  106. t.Fatal(err)
  107. }
  108. before := string(m.snapshot())
  109. disk, _ := os.ReadFile(FilterStorePath)
  110. bad := good
  111. bad.Protocol = "not-a-real-protocol"
  112. if err := m.SaveFilterRule(bad); err == nil {
  113. t.Fatal("invalid firewall save accepted")
  114. }
  115. afterDisk, _ := os.ReadFile(FilterStorePath)
  116. if string(m.snapshot()) != before || string(disk) != string(afterDisk) {
  117. t.Fatal("failed apply changed saved state")
  118. }
  119. if err := SyncAddressLists([]AddressListEntry{{Name: "test", Address: "192.0.2.0/24"}, {Name: "test", Address: "2001:db8::/32"}}, "test"); err != nil {
  120. t.Fatal(err)
  121. }
  122. out, err := exec.Command("ipset", "list", staticIPSetName("test")+"_v6").CombinedOutput()
  123. if err != nil || !strings.Contains(string(out), "2001:db8::/32") {
  124. t.Fatal(string(out), err)
  125. }
  126. if err := SyncAddressLists([]AddressListEntry{{Name: "test", Address: "invalid-address"}}, "test"); err == nil {
  127. t.Fatal("invalid list address accepted")
  128. }
  129. out, err = exec.Command("ipset", "list", staticIPSetName("test")).CombinedOutput()
  130. if err != nil || !strings.Contains(string(out), "192.0.2.0/24") {
  131. t.Fatal("failed list update destroyed old set", string(out), err)
  132. }
  133. }
  134. func TestProductionFirewallMigration(t *testing.T) {
  135. fixture := os.Getenv("DEBTIK_FIREWALL_PRODUCTION_FIXTURE")
  136. if fixture == "" {
  137. t.Skip("production snapshot in private namespaces only")
  138. }
  139. dir := t.TempDir()
  140. if _, err := os.Stat(fixture + "/metadata.json"); err == nil {
  141. if err = exec.Command("mount", "--bind", fixture+"/metadata.json", "/etc/debtik/metadata.json").Run(); err != nil {
  142. t.Fatal(err)
  143. }
  144. }
  145. if out, err := exec.Command("cp", "-a", fixture+"/config/.", dir).CombinedOutput(); err != nil {
  146. t.Fatal(string(out), err)
  147. }
  148. if err := exec.Command("mount", "--bind", dir, "/etc/debtik/firewall").Run(); err != nil {
  149. t.Fatal(err)
  150. }
  151. restore := func(binary, path string) {
  152. t.Helper()
  153. data, err := os.ReadFile(path)
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. if len(data) == 0 {
  158. return
  159. }
  160. cmd := exec.Command(binary)
  161. if binary == "ipset" {
  162. cmd = exec.Command("ipset", "restore")
  163. }
  164. cmd.Stdin = strings.NewReader(string(data))
  165. if out, err := cmd.CombinedOutput(); err != nil {
  166. t.Fatal(binary, string(out), err)
  167. }
  168. }
  169. restore("ipset", fixture+"/ipset.commands")
  170. restore("iptables-restore", fixture+"/iptables.save")
  171. restore("ip6tables-restore", fixture+"/ip6tables.save")
  172. m := NewFirewallManager()
  173. select {
  174. case <-m.GeoIP().restoreDone:
  175. case <-time.After(30 * time.Second):
  176. t.Fatal("GeoIP restore did not finish")
  177. }
  178. if last := m.GeoIP().GetStatus()["last_error"]; last != "" {
  179. t.Fatal("production GeoIP restore failed", last)
  180. }
  181. if err := m.Reapply(); err != nil {
  182. t.Fatal("production migration failed", err)
  183. }
  184. if err := m.Reapply(); err != nil {
  185. t.Fatal("second apply failed", err)
  186. }
  187. }