fake.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testing
  14. import (
  15. "bytes"
  16. "fmt"
  17. "strings"
  18. "time"
  19. "k8s.io/kubernetes/pkg/util/iptables"
  20. )
  21. const (
  22. // Destination represents the destination address flag
  23. Destination = "-d "
  24. // Source represents the source address flag
  25. Source = "-s "
  26. // DPort represents the destination port flag
  27. DPort = "--dport "
  28. // Protocol represents the protocol flag
  29. Protocol = "-p "
  30. // Jump represents jump flag specifies the jump target
  31. Jump = "-j "
  32. // Reject specifies the reject target
  33. Reject = "REJECT"
  34. // ToDest represents the flag used to specify the destination address in DNAT
  35. ToDest = "--to-destination "
  36. // Recent represents the sub-command recent that allows to dynamically create list of IP address to match against
  37. Recent = "recent "
  38. // MatchSet represents the flag which match packets against the specified set
  39. MatchSet = "--match-set "
  40. // SrcType represents the --src-type flag which matches if the source address is of given type
  41. SrcType = "--src-type "
  42. // Masquerade represents the target that is used in nat table.
  43. Masquerade = "MASQUERADE "
  44. )
  45. // Rule holds a map of rules.
  46. type Rule map[string]string
  47. // FakeIPTables is no-op implementation of iptables Interface.
  48. type FakeIPTables struct {
  49. hasRandomFully bool
  50. Lines []byte
  51. ipv6 bool
  52. }
  53. // NewFake returns a no-op iptables.Interface
  54. func NewFake() *FakeIPTables {
  55. return &FakeIPTables{}
  56. }
  57. // NewIpv6Fake returns a no-op iptables.Interface with IsIPv6() == true
  58. func NewIpv6Fake() *FakeIPTables {
  59. return &FakeIPTables{ipv6: true}
  60. }
  61. // SetHasRandomFully is part of iptables.Interface
  62. func (f *FakeIPTables) SetHasRandomFully(can bool) *FakeIPTables {
  63. f.hasRandomFully = can
  64. return f
  65. }
  66. // EnsureChain is part of iptables.Interface
  67. func (*FakeIPTables) EnsureChain(table iptables.Table, chain iptables.Chain) (bool, error) {
  68. return true, nil
  69. }
  70. // FlushChain is part of iptables.Interface
  71. func (*FakeIPTables) FlushChain(table iptables.Table, chain iptables.Chain) error {
  72. return nil
  73. }
  74. // DeleteChain is part of iptables.Interface
  75. func (*FakeIPTables) DeleteChain(table iptables.Table, chain iptables.Chain) error {
  76. return nil
  77. }
  78. // EnsureRule is part of iptables.Interface
  79. func (*FakeIPTables) EnsureRule(position iptables.RulePosition, table iptables.Table, chain iptables.Chain, args ...string) (bool, error) {
  80. return true, nil
  81. }
  82. // DeleteRule is part of iptables.Interface
  83. func (*FakeIPTables) DeleteRule(table iptables.Table, chain iptables.Chain, args ...string) error {
  84. return nil
  85. }
  86. // IsIpv6 is part of iptables.Interface
  87. func (f *FakeIPTables) IsIpv6() bool {
  88. return f.ipv6
  89. }
  90. // Save is part of iptables.Interface
  91. func (f *FakeIPTables) Save(table iptables.Table) ([]byte, error) {
  92. lines := make([]byte, len(f.Lines))
  93. copy(lines, f.Lines)
  94. return lines, nil
  95. }
  96. // SaveInto is part of iptables.Interface
  97. func (f *FakeIPTables) SaveInto(table iptables.Table, buffer *bytes.Buffer) error {
  98. buffer.Write(f.Lines)
  99. return nil
  100. }
  101. // Restore is part of iptables.Interface
  102. func (*FakeIPTables) Restore(table iptables.Table, data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
  103. return nil
  104. }
  105. // RestoreAll is part of iptables.Interface
  106. func (f *FakeIPTables) RestoreAll(data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
  107. f.Lines = data
  108. return nil
  109. }
  110. // Monitor is part of iptables.Interface
  111. func (f *FakeIPTables) Monitor(canary iptables.Chain, tables []iptables.Table, reloadFunc func(), interval time.Duration, stopCh <-chan struct{}) {
  112. }
  113. func getToken(line, separator string) string {
  114. tokens := strings.Split(line, separator)
  115. if len(tokens) == 2 {
  116. return strings.Split(tokens[1], " ")[0]
  117. }
  118. return ""
  119. }
  120. // GetRules is part of iptables.Interface
  121. func (f *FakeIPTables) GetRules(chainName string) (rules []Rule) {
  122. for _, l := range strings.Split(string(f.Lines), "\n") {
  123. if strings.Contains(l, fmt.Sprintf("-A %v", chainName)) {
  124. newRule := Rule(map[string]string{})
  125. for _, arg := range []string{Destination, Source, DPort, Protocol, Jump, ToDest, Recent, MatchSet, SrcType, Masquerade} {
  126. tok := getToken(l, arg)
  127. if tok != "" {
  128. newRule[arg] = tok
  129. }
  130. }
  131. rules = append(rules, newRule)
  132. }
  133. }
  134. return
  135. }
  136. // HasRandomFully is part of iptables.Interface
  137. func (f *FakeIPTables) HasRandomFully() bool {
  138. return f.hasRandomFully
  139. }
  140. var _ = iptables.Interface(&FakeIPTables{})