owned_test.go 5.5 KB

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