firewall_linux_test.go 3.3 KB

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