2019-01-18 01:50:10 +00:00
|
|
|
// Copyright 2019 the Kilo authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-16 13:00:07 +00:00
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
2019-01-18 01:50:10 +00:00
|
|
|
|
|
|
|
"github.com/coreos/go-iptables/iptables"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statusExiter interface {
|
|
|
|
ExitStatus() int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ statusExiter = (*iptables.Error)(nil)
|
|
|
|
var _ statusExiter = statusError(0)
|
|
|
|
|
|
|
|
type statusError int
|
|
|
|
|
|
|
|
func (s statusError) Error() string {
|
|
|
|
return fmt.Sprintf("%d", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s statusError) ExitStatus() int {
|
|
|
|
return int(s)
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:23:18 +00:00
|
|
|
type fakeClient struct {
|
2021-02-16 13:00:07 +00:00
|
|
|
calls uint64
|
2019-09-25 11:23:18 +00:00
|
|
|
storage []Rule
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
|
2020-02-20 11:24:52 +00:00
|
|
|
var _ Client = &fakeClient{}
|
2019-01-18 01:50:10 +00:00
|
|
|
|
2019-09-25 11:23:18 +00:00
|
|
|
func (f *fakeClient) AppendUnique(table, chain string, spec ...string) error {
|
2021-02-16 13:00:07 +00:00
|
|
|
atomic.AddUint64(&f.calls, 1)
|
2019-09-25 11:23:18 +00:00
|
|
|
exists, err := f.Exists(table, chain, spec...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-12 14:48:01 +00:00
|
|
|
f.storage = append(f.storage, &rule{table: table, chain: chain, spec: spec})
|
2019-01-18 01:50:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:23:18 +00:00
|
|
|
func (f *fakeClient) Delete(table, chain string, spec ...string) error {
|
2021-02-16 13:00:07 +00:00
|
|
|
atomic.AddUint64(&f.calls, 1)
|
2020-03-12 14:48:01 +00:00
|
|
|
r := &rule{table: table, chain: chain, spec: spec}
|
2019-09-25 11:23:18 +00:00
|
|
|
for i := range f.storage {
|
|
|
|
if f.storage[i].String() == r.String() {
|
|
|
|
copy(f.storage[i:], f.storage[i+1:])
|
|
|
|
f.storage[len(f.storage)-1] = nil
|
|
|
|
f.storage = f.storage[:len(f.storage)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:23:18 +00:00
|
|
|
func (f *fakeClient) Exists(table, chain string, spec ...string) (bool, error) {
|
2021-02-16 13:00:07 +00:00
|
|
|
atomic.AddUint64(&f.calls, 1)
|
2020-03-12 14:48:01 +00:00
|
|
|
r := &rule{table: table, chain: chain, spec: spec}
|
2019-09-25 11:23:18 +00:00
|
|
|
for i := range f.storage {
|
|
|
|
if f.storage[i].String() == r.String() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 13:00:07 +00:00
|
|
|
func (f *fakeClient) List(table, chain string) ([]string, error) {
|
|
|
|
atomic.AddUint64(&f.calls, 1)
|
|
|
|
var rs []string
|
|
|
|
for i := range f.storage {
|
|
|
|
switch r := f.storage[i].(type) {
|
|
|
|
case *rule:
|
|
|
|
if r.table == table && r.chain == chain {
|
|
|
|
rs = append(rs, strings.TrimSpace(strings.TrimPrefix(r.String(), table)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:23:18 +00:00
|
|
|
func (f *fakeClient) ClearChain(table, name string) error {
|
2021-02-16 13:00:07 +00:00
|
|
|
atomic.AddUint64(&f.calls, 1)
|
2019-09-25 11:23:18 +00:00
|
|
|
for i := range f.storage {
|
|
|
|
r, ok := f.storage[i].(*rule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if table == r.table && name == r.chain {
|
|
|
|
if err := f.Delete(table, name, r.spec...); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-16 13:00:07 +00:00
|
|
|
if err := f.DeleteChain(table, name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return f.NewChain(table, name)
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:23:18 +00:00
|
|
|
func (f *fakeClient) DeleteChain(table, name string) error {
|
2021-02-16 13:00:07 +00:00
|
|
|
atomic.AddUint64(&f.calls, 1)
|
2019-09-25 11:23:18 +00:00
|
|
|
for i := range f.storage {
|
|
|
|
r, ok := f.storage[i].(*rule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if table == r.table && name == r.chain {
|
2019-01-18 01:50:10 +00:00
|
|
|
return fmt.Errorf("cannot delete chain %s; rules exist", name)
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 14:48:01 +00:00
|
|
|
c := &chain{table: table, chain: name}
|
2019-09-25 11:23:18 +00:00
|
|
|
for i := range f.storage {
|
|
|
|
if f.storage[i].String() == c.String() {
|
|
|
|
copy(f.storage[i:], f.storage[i+1:])
|
|
|
|
f.storage[len(f.storage)-1] = nil
|
|
|
|
f.storage = f.storage[:len(f.storage)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:23:18 +00:00
|
|
|
func (f *fakeClient) NewChain(table, name string) error {
|
2021-02-16 13:00:07 +00:00
|
|
|
atomic.AddUint64(&f.calls, 1)
|
2020-03-12 14:48:01 +00:00
|
|
|
c := &chain{table: table, chain: name}
|
2019-09-25 11:23:18 +00:00
|
|
|
for i := range f.storage {
|
|
|
|
if f.storage[i].String() == c.String() {
|
|
|
|
return statusError(1)
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
2019-09-25 11:23:18 +00:00
|
|
|
f.storage = append(f.storage, c)
|
2019-01-18 01:50:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-16 13:00:07 +00:00
|
|
|
|
|
|
|
func (f *fakeClient) ListChains(table string) ([]string, error) {
|
|
|
|
atomic.AddUint64(&f.calls, 1)
|
|
|
|
var cs []string
|
|
|
|
for i := range f.storage {
|
|
|
|
switch c := f.storage[i].(type) {
|
|
|
|
case *chain:
|
|
|
|
if c.table == table {
|
|
|
|
cs = append(cs, c.chain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cs, nil
|
|
|
|
}
|