2019-10-13 00:54:49 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/iptables"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
2020-08-31 11:55:22 -04:00
|
|
|
"gotest.tools/v3/assert"
|
2019-10-13 00:54:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
fwdChainName = "FORWARD"
|
|
|
|
usrChainName = userChain
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserChain(t *testing.T) {
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
|
|
|
|
2019-10-13 00:54:49 -04:00
|
|
|
nc, err := New()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
iptables bool
|
|
|
|
insert bool // insert other rules to FORWARD
|
|
|
|
fwdChain []string
|
|
|
|
userChain []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
iptables: false,
|
|
|
|
insert: false,
|
|
|
|
fwdChain: []string{"-P FORWARD ACCEPT"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
iptables: true,
|
|
|
|
insert: false,
|
|
|
|
fwdChain: []string{"-P FORWARD ACCEPT", "-A FORWARD -j DOCKER-USER"},
|
|
|
|
userChain: []string{"-N DOCKER-USER", "-A DOCKER-USER -j RETURN"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
iptables: true,
|
|
|
|
insert: true,
|
|
|
|
fwdChain: []string{"-P FORWARD ACCEPT", "-A FORWARD -j DOCKER-USER", "-A FORWARD -j DROP"},
|
|
|
|
userChain: []string{"-N DOCKER-USER", "-A DOCKER-USER -j RETURN"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resetIptables(t)
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(fmt.Sprintf("iptables=%v,insert=%v", tc.iptables, tc.insert), func(t *testing.T) {
|
|
|
|
c := nc.(*controller)
|
|
|
|
c.cfg.Daemon.DriverCfg["bridge"] = map[string]interface{}{
|
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"EnableIPTables": tc.iptables,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// init. condition, FORWARD chain empty DOCKER-USER not exist
|
|
|
|
assert.DeepEqual(t, getRules(t, fwdChainName), []string{"-P FORWARD ACCEPT"})
|
|
|
|
|
|
|
|
if tc.insert {
|
2017-11-28 16:15:55 -05:00
|
|
|
_, err = iptable.Raw("-A", fwdChainName, "-j", "DROP")
|
2019-10-13 00:54:49 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
arrangeUserFilterRule()
|
|
|
|
|
|
|
|
assert.DeepEqual(t, getRules(t, fwdChainName), tc.fwdChain)
|
|
|
|
if tc.userChain != nil {
|
|
|
|
assert.DeepEqual(t, getRules(t, usrChainName), tc.userChain)
|
|
|
|
} else {
|
2017-11-28 16:15:55 -05:00
|
|
|
_, err := iptable.Raw("-S", usrChainName)
|
2019-10-13 00:54:49 -04:00
|
|
|
assert.Assert(t, err != nil, "chain %v: created unexpectedly", usrChainName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
resetIptables(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRules(t *testing.T, chain string) []string {
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
|
|
|
|
2019-10-13 00:54:49 -04:00
|
|
|
t.Helper()
|
2017-11-28 16:15:55 -05:00
|
|
|
output, err := iptable.Raw("-S", chain)
|
2019-10-13 00:54:49 -04:00
|
|
|
assert.NilError(t, err, "chain %s: failed to get rules", chain)
|
|
|
|
|
|
|
|
rules := strings.Split(string(output), "\n")
|
|
|
|
if len(rules) > 0 {
|
|
|
|
rules = rules[:len(rules)-1]
|
|
|
|
}
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetIptables(t *testing.T) {
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
|
|
|
|
2019-10-13 00:54:49 -04:00
|
|
|
t.Helper()
|
2017-11-28 16:15:55 -05:00
|
|
|
_, err := iptable.Raw("-F", fwdChainName)
|
2019-10-13 00:54:49 -04:00
|
|
|
assert.NilError(t, err)
|
2017-11-28 16:15:55 -05:00
|
|
|
_ = iptable.RemoveExistingChain(usrChainName, "")
|
2019-10-13 00:54:49 -04:00
|
|
|
}
|