fake.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2019 the Kilo authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package iptables
  15. import (
  16. "fmt"
  17. "strings"
  18. "sync/atomic"
  19. "github.com/coreos/go-iptables/iptables"
  20. )
  21. type statusExiter interface {
  22. ExitStatus() int
  23. }
  24. var _ statusExiter = (*iptables.Error)(nil)
  25. var _ statusExiter = statusError(0)
  26. type statusError int
  27. func (s statusError) Error() string {
  28. return fmt.Sprintf("%d", s)
  29. }
  30. func (s statusError) ExitStatus() int {
  31. return int(s)
  32. }
  33. type fakeClient struct {
  34. calls uint64
  35. storage []Rule
  36. }
  37. var _ Client = &fakeClient{}
  38. func (f *fakeClient) InsertUnique(table, chain string, pos int, spec ...string) error {
  39. atomic.AddUint64(&f.calls, 1)
  40. exists, err := f.Exists(table, chain, spec...)
  41. if err != nil {
  42. return err
  43. }
  44. if exists {
  45. return nil
  46. }
  47. index := pos - 1 // iptables are 1-based
  48. rule := &rule{table: table, chain: chain, spec: spec}
  49. prefix := append([]Rule{}, f.storage[:index]...)
  50. suffix := append([]Rule{}, f.storage[index:]...)
  51. prefix = append(prefix, rule)
  52. f.storage = append(prefix, suffix...)
  53. return nil
  54. }
  55. func (f *fakeClient) AppendUnique(table, chain string, spec ...string) error {
  56. atomic.AddUint64(&f.calls, 1)
  57. exists, err := f.Exists(table, chain, spec...)
  58. if err != nil {
  59. return err
  60. }
  61. if exists {
  62. return nil
  63. }
  64. f.storage = append(f.storage, &rule{table: table, chain: chain, spec: spec})
  65. return nil
  66. }
  67. func (f *fakeClient) Delete(table, chain string, spec ...string) error {
  68. atomic.AddUint64(&f.calls, 1)
  69. r := &rule{table: table, chain: chain, spec: spec}
  70. for i := range f.storage {
  71. if f.storage[i].String() == r.String() {
  72. copy(f.storage[i:], f.storage[i+1:])
  73. f.storage[len(f.storage)-1] = nil
  74. f.storage = f.storage[:len(f.storage)-1]
  75. break
  76. }
  77. }
  78. return nil
  79. }
  80. func (f *fakeClient) Exists(table, chain string, spec ...string) (bool, error) {
  81. atomic.AddUint64(&f.calls, 1)
  82. r := &rule{table: table, chain: chain, spec: spec}
  83. for i := range f.storage {
  84. if f.storage[i].String() == r.String() {
  85. return true, nil
  86. }
  87. }
  88. return false, nil
  89. }
  90. func (f *fakeClient) List(table, chain string) ([]string, error) {
  91. atomic.AddUint64(&f.calls, 1)
  92. var rs []string
  93. for i := range f.storage {
  94. switch r := f.storage[i].(type) {
  95. case *rule:
  96. if r.table == table && r.chain == chain {
  97. rs = append(rs, strings.TrimSpace(strings.TrimPrefix(r.String(), table)))
  98. }
  99. }
  100. }
  101. return rs, nil
  102. }
  103. func (f *fakeClient) ClearChain(table, name string) error {
  104. atomic.AddUint64(&f.calls, 1)
  105. for i := range f.storage {
  106. r, ok := f.storage[i].(*rule)
  107. if !ok {
  108. continue
  109. }
  110. if table == r.table && name == r.chain {
  111. if err := f.Delete(table, name, r.spec...); err != nil {
  112. return nil
  113. }
  114. }
  115. }
  116. if err := f.DeleteChain(table, name); err != nil {
  117. return err
  118. }
  119. return f.NewChain(table, name)
  120. }
  121. func (f *fakeClient) DeleteChain(table, name string) error {
  122. atomic.AddUint64(&f.calls, 1)
  123. for i := range f.storage {
  124. r, ok := f.storage[i].(*rule)
  125. if !ok {
  126. continue
  127. }
  128. if table == r.table && name == r.chain {
  129. return fmt.Errorf("cannot delete chain %s; rules exist", name)
  130. }
  131. }
  132. c := &chain{table: table, chain: name}
  133. for i := range f.storage {
  134. if f.storage[i].String() == c.String() {
  135. copy(f.storage[i:], f.storage[i+1:])
  136. f.storage[len(f.storage)-1] = nil
  137. f.storage = f.storage[:len(f.storage)-1]
  138. break
  139. }
  140. }
  141. return nil
  142. }
  143. func (f *fakeClient) NewChain(table, name string) error {
  144. atomic.AddUint64(&f.calls, 1)
  145. c := &chain{table: table, chain: name}
  146. for i := range f.storage {
  147. if f.storage[i].String() == c.String() {
  148. return statusError(1)
  149. }
  150. }
  151. f.storage = append(f.storage, c)
  152. return nil
  153. }
  154. func (f *fakeClient) ListChains(table string) ([]string, error) {
  155. atomic.AddUint64(&f.calls, 1)
  156. var cs []string
  157. for i := range f.storage {
  158. switch c := f.storage[i].(type) {
  159. case *chain:
  160. if c.table == table {
  161. cs = append(cs, c.chain)
  162. }
  163. }
  164. }
  165. return cs, nil
  166. }