firewall_test.go 2.5 KB

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