firewall_linux_test.go 2.6 KB

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