fake.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  52. // NewFake returns a no-op iptables.Interface
  53. func NewFake() *FakeIPTables {
  54. return &FakeIPTables{}
  55. }
  56. // SetHasRandomFully is part of iptables.Interface
  57. func (f *FakeIPTables) SetHasRandomFully(can bool) *FakeIPTables {
  58. f.hasRandomFully = can
  59. return f
  60. }
  61. // EnsureChain is part of iptables.Interface
  62. func (*FakeIPTables) EnsureChain(table iptables.Table, chain iptables.Chain) (bool, error) {
  63. return true, nil
  64. }
  65. // FlushChain is part of iptables.Interface
  66. func (*FakeIPTables) FlushChain(table iptables.Table, chain iptables.Chain) error {
  67. return nil
  68. }
  69. // DeleteChain is part of iptables.Interface
  70. func (*FakeIPTables) DeleteChain(table iptables.Table, chain iptables.Chain) error {
  71. return nil
  72. }
  73. // EnsureRule is part of iptables.Interface
  74. func (*FakeIPTables) EnsureRule(position iptables.RulePosition, table iptables.Table, chain iptables.Chain, args ...string) (bool, error) {
  75. return true, nil
  76. }
  77. // DeleteRule is part of iptables.Interface
  78. func (*FakeIPTables) DeleteRule(table iptables.Table, chain iptables.Chain, args ...string) error {
  79. return nil
  80. }
  81. // IsIpv6 is part of iptables.Interface
  82. func (*FakeIPTables) IsIpv6() bool {
  83. return false
  84. }
  85. // Save is part of iptables.Interface
  86. func (f *FakeIPTables) Save(table iptables.Table) ([]byte, error) {
  87. lines := make([]byte, len(f.Lines))
  88. copy(lines, f.Lines)
  89. return lines, nil
  90. }
  91. // SaveInto is part of iptables.Interface
  92. func (f *FakeIPTables) SaveInto(table iptables.Table, buffer *bytes.Buffer) error {
  93. buffer.Write(f.Lines)
  94. return nil
  95. }
  96. // Restore is part of iptables.Interface
  97. func (*FakeIPTables) Restore(table iptables.Table, data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
  98. return nil
  99. }
  100. // RestoreAll is part of iptables.Interface
  101. func (f *FakeIPTables) RestoreAll(data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
  102. f.Lines = data
  103. return nil
  104. }
  105. // Monitor is part of iptables.Interface
  106. func (f *FakeIPTables) Monitor(canary iptables.Chain, tables []iptables.Table, reloadFunc func(), interval time.Duration, stopCh <-chan struct{}) {
  107. }
  108. func getToken(line, separator string) string {
  109. tokens := strings.Split(line, separator)
  110. if len(tokens) == 2 {
  111. return strings.Split(tokens[1], " ")[0]
  112. }
  113. return ""
  114. }
  115. // GetRules is part of iptables.Interface
  116. func (f *FakeIPTables) GetRules(chainName string) (rules []Rule) {
  117. for _, l := range strings.Split(string(f.Lines), "\n") {
  118. if strings.Contains(l, fmt.Sprintf("-A %v", chainName)) {
  119. newRule := Rule(map[string]string{})
  120. for _, arg := range []string{Destination, Source, DPort, Protocol, Jump, ToDest, Recent, MatchSet, SrcType, Masquerade} {
  121. tok := getToken(l, arg)
  122. if tok != "" {
  123. newRule[arg] = tok
  124. }
  125. }
  126. rules = append(rules, newRule)
  127. }
  128. }
  129. return
  130. }
  131. // HasRandomFully is part of iptables.Interface
  132. func (f *FakeIPTables) HasRandomFully() bool {
  133. return f.hasRandomFully
  134. }
  135. var _ = iptables.Interface(&FakeIPTables{})