firewall_linux_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. c, err := New()
  48. assert.NilError(t, err)
  49. defer c.Stop()
  50. c.cfg.DriverCfg["bridge"] = map[string]interface{}{
  51. netlabel.GenericData: options.Generic{
  52. "EnableIPTables": tc.iptables,
  53. },
  54. }
  55. // init. condition, FORWARD chain empty DOCKER-USER not exist
  56. assert.DeepEqual(t, getRules(t, fwdChainName), []string{"-P FORWARD ACCEPT"})
  57. if tc.insert {
  58. _, err = iptable.Raw("-A", fwdChainName, "-j", "DROP")
  59. assert.NilError(t, err)
  60. }
  61. arrangeUserFilterRule()
  62. assert.DeepEqual(t, getRules(t, fwdChainName), tc.fwdChain)
  63. if tc.userChain != nil {
  64. assert.DeepEqual(t, getRules(t, usrChainName), tc.userChain)
  65. } else {
  66. _, err := iptable.Raw("-S", usrChainName)
  67. assert.Assert(t, err != nil, "chain %v: created unexpectedly", usrChainName)
  68. }
  69. })
  70. }
  71. }
  72. func getRules(t *testing.T, chain string) []string {
  73. iptable := iptables.GetIptable(iptables.IPv4)
  74. t.Helper()
  75. output, err := iptable.Raw("-S", chain)
  76. assert.NilError(t, err, "chain %s: failed to get rules", chain)
  77. rules := strings.Split(string(output), "\n")
  78. if len(rules) > 0 {
  79. rules = rules[:len(rules)-1]
  80. }
  81. return rules
  82. }
  83. func resetIptables(t *testing.T) {
  84. iptable := iptables.GetIptable(iptables.IPv4)
  85. t.Helper()
  86. _, err := iptable.Raw("-F", fwdChainName)
  87. assert.NilError(t, err)
  88. _ = iptable.RemoveExistingChain(usrChainName, "")
  89. }