-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCKER-USER chain not created when IPTableEnable=false.
This fix addresses https://docker.atlassian.net/browse/ENGCORE-1115 Expected behaviors upon docker engine restarts: 1. IPTableEnable=true, DOCKER-USER chain present -- no change to DOCKER-USER chain 2. IPTableEnable=true, DOCKER-USER chain not present -- DOCKER-USER chain created and inserted top of FORWARD chain. 3. IPTableEnable=false, DOCKER-USER chain present -- no change to DOCKER-USER chain the rational is that DOCKER-USER is populated and may be used by end-user for purpose other than filtering docker container traffic. Thus even if IPTableEnable=false, docker engine does not touch pre-existing DOCKER-USER chain. 4. IPTableEnable=false, DOCKER-USER chain not present -- DOCKER-USER chain is not created. Signed-off-by: Su Wang <su.wang@docker.com>
- Loading branch information
Su Wang
committed
Nov 7, 2019
1 parent
5717832
commit 3806be4
Showing
4 changed files
with
147 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package libnetwork | ||
|
||
import ( | ||
"fmt" | ||
"gotest.tools/assert" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/docker/libnetwork/iptables" | ||
"github.com/docker/libnetwork/netlabel" | ||
"github.com/docker/libnetwork/options" | ||
) | ||
|
||
const ( | ||
fwdChainName = "FORWARD" | ||
usrChainName = userChain | ||
) | ||
|
||
func TestUserChain(t *testing.T) { | ||
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 { | ||
_, err = iptables.Raw("-A", fwdChainName, "-j", "DROP") | ||
assert.NilError(t, err) | ||
} | ||
setupArrangeUserFilterRule(c) | ||
arrangeUserFilterRule() | ||
|
||
assert.DeepEqual(t, getRules(t, fwdChainName), tc.fwdChain) | ||
if tc.userChain != nil { | ||
assert.DeepEqual(t, getRules(t, usrChainName), tc.userChain) | ||
} else { | ||
_, err := iptables.Raw("-S", usrChainName) | ||
assert.Assert(t, err != nil, "chain %v: created unexpectedly", usrChainName) | ||
} | ||
}) | ||
resetIptables(t) | ||
} | ||
} | ||
|
||
func getRules(t *testing.T, chain string) []string { | ||
t.Helper() | ||
output, err := iptables.Raw("-S", chain) | ||
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) { | ||
t.Helper() | ||
_, err := iptables.Raw("-F", fwdChainName) | ||
assert.NilError(t, err) | ||
_ = iptables.RemoveExistingChain(usrChainName, "") | ||
} |