firewall_linux_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package libnetwork
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/docker/docker/libnetwork/iptables"
  7. "github.com/docker/docker/libnetwork/netlabel"
  8. "github.com/docker/docker/libnetwork/options"
  9. "gotest.tools/v3/assert"
  10. )
  11. const (
  12. fwdChainName = "FORWARD"
  13. usrChainName = userChain
  14. )
  15. func TestUserChain(t *testing.T) {
  16. iptable4 := iptables.GetIptable(iptables.IPv4)
  17. iptable6 := iptables.GetIptable(iptables.IPv6)
  18. nc, err := New()
  19. assert.NilError(t, err)
  20. tests := []struct {
  21. iptables bool
  22. insert bool // insert other rules to FORWARD
  23. fwdChain []string
  24. userChain []string
  25. }{
  26. {
  27. iptables: false,
  28. insert: false,
  29. fwdChain: []string{"-P FORWARD ACCEPT"},
  30. },
  31. {
  32. iptables: true,
  33. insert: false,
  34. fwdChain: []string{"-P FORWARD ACCEPT", "-A FORWARD -j DOCKER-USER"},
  35. userChain: []string{"-N DOCKER-USER", "-A DOCKER-USER -j RETURN"},
  36. },
  37. {
  38. iptables: true,
  39. insert: true,
  40. fwdChain: []string{"-P FORWARD ACCEPT", "-A FORWARD -j DOCKER-USER", "-A FORWARD -j DROP"},
  41. userChain: []string{"-N DOCKER-USER", "-A DOCKER-USER -j RETURN"},
  42. },
  43. }
  44. resetIptables(t)
  45. for _, tc := range tests {
  46. tc := tc
  47. t.Run(fmt.Sprintf("iptables=%v,insert=%v", tc.iptables, tc.insert), func(t *testing.T) {
  48. c := nc.(*controller)
  49. c.cfg.DriverCfg["bridge"] = map[string]interface{}{
  50. netlabel.GenericData: options.Generic{
  51. "EnableIPTables": tc.iptables,
  52. "EnableIP6Tables": tc.iptables,
  53. },
  54. }
  55. // init. condition, FORWARD chain empty DOCKER-USER not exist
  56. assert.DeepEqual(t, getRules(t, iptables.IPv4, fwdChainName), []string{"-P FORWARD ACCEPT"})
  57. assert.DeepEqual(t, getRules(t, iptables.IPv6, fwdChainName), []string{"-P FORWARD ACCEPT"})
  58. if tc.insert {
  59. _, err = iptable4.Raw("-A", fwdChainName, "-j", "DROP")
  60. assert.NilError(t, err)
  61. _, err = iptable6.Raw("-A", fwdChainName, "-j", "DROP")
  62. assert.NilError(t, err)
  63. }
  64. arrangeUserFilterRule()
  65. assert.DeepEqual(t, getRules(t, iptables.IPv4, fwdChainName), tc.fwdChain)
  66. assert.DeepEqual(t, getRules(t, iptables.IPv6, fwdChainName), tc.fwdChain)
  67. if tc.userChain != nil {
  68. assert.DeepEqual(t, getRules(t, iptables.IPv4, usrChainName), tc.userChain)
  69. assert.DeepEqual(t, getRules(t, iptables.IPv6, usrChainName), tc.userChain)
  70. } else {
  71. _, err := iptable4.Raw("-S", usrChainName)
  72. assert.Assert(t, err != nil, "ipv4 chain %v: created unexpectedly", usrChainName)
  73. _, err = iptable6.Raw("-S", usrChainName)
  74. assert.Assert(t, err != nil, "ipv6 chain %v: created unexpectedly", usrChainName)
  75. }
  76. })
  77. resetIptables(t)
  78. }
  79. }
  80. func getRules(t *testing.T, ipVer iptables.IPVersion, chain string) []string {
  81. iptable := iptables.GetIptable(ipVer)
  82. t.Helper()
  83. output, err := iptable.Raw("-S", chain)
  84. assert.NilError(t, err, "chain %s: failed to get rules", chain)
  85. rules := strings.Split(string(output), "\n")
  86. if len(rules) > 0 {
  87. rules = rules[:len(rules)-1]
  88. }
  89. return rules
  90. }
  91. func resetIptables(t *testing.T) {
  92. t.Helper()
  93. for _, ipVer := range []iptables.IPVersion{iptables.IPv4, iptables.IPv6} {
  94. iptable := iptables.GetIptable(ipVer)
  95. _, err := iptable.Raw("-F", fwdChainName)
  96. assert.Check(t, err)
  97. _ = iptable.RemoveExistingChain(usrChainName, "")
  98. }
  99. }