Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const secondInMillis = 1000
type ruleConverter interface {
Convert(Rule, string, bool) []rules.IPTablesRule
BulkConvert([]Rule, string, bool) []rules.IPTablesRule
DeduplicateRules([]rules.IPTablesRule) []rules.IPTablesRule
}

type OutConn struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (c *NetOutChain) IPTablesRules(containerHandle string, containerWorkload st
}

iptablesRules := c.Converter.BulkConvert(ruleSpec, logChain, c.ASGLogging)
iptablesRules = c.Converter.DeduplicateRules(iptablesRules)

iptablesRules = append(iptablesRules, c.denyNetworksRules(containerWorkload)...)

if c.Conn.Limit || c.Conn.DryRun {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var _ = Describe("NetOutChain", func() {
}

converter.BulkConvertReturns(genericRules)

converter.DeduplicateRulesReturns(genericRules)
})

It("prepends allow rules to the container's netout chain", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ func (c *RuleConverter) BulkConvert(ruleSpec []Rule, logChainName string, global
return iptablesRules
}

func (c *RuleConverter) DeduplicateRules(iptablesRules []rules.IPTablesRule) []rules.IPTablesRule {
keys := make(map[string]bool)
dedupedRules := []rules.IPTablesRule{}

for _, rule := range iptablesRules {
key := strings.Join(rule, " ")
if !keys[key] {

keys[key] = true
dedupedRules = append(dedupedRules, rule)
}
}

return dedupedRules
}

func (c *RuleConverter) Convert(rule Rule, logChainName string, globalLogging bool) []rules.IPTablesRule {
ruleSpec := []rules.IPTablesRule{}
for _, network := range rule.Networks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,62 @@ var _ = Describe("RuleConverter", func() {
})

})
Describe("DeduplicateRules", func() {
var unfilteredRules []rules.IPTablesRule
Context("when there are duplicate iptables rules", func() {
BeforeEach(func() {
unfilteredRules = []rules.IPTablesRule{
{
"-m", "iprange", "-p", "tcp",
"--dst-range", "1.1.1.1-2.2.2.2",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT",
},
{
"-m", "iprange", "-p", "tcp",
"--dst-range", "1.1.1.1-2.2.2.2",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT",
},
{
"-m", "iprange", "-p", "tcp",
"--dst-range", "1.1.1.1-2.2.2.2",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT",
},
{
"-m", "iprange", "-p", "tcp",
"--dst-range", "3.3.3.3-4.4.4.4",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT",
},
{
"-m", "iprange", "-p", "tcp",
"--dst-range", "4.4.4.4-5.5.5.5",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT",
},
}
})

It("removes the duplicate iptables rules", func() {
dedupedRules := converter.DeduplicateRules(unfilteredRules)

Expect(dedupedRules).To(ConsistOf(
rules.IPTablesRule{"-m", "iprange", "-p", "tcp",
"--dst-range", "1.1.1.1-2.2.2.2",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT"},
rules.IPTablesRule{"-m", "iprange", "-p", "tcp",
"--dst-range", "4.4.4.4-5.5.5.5",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT"},
rules.IPTablesRule{"-m", "iprange", "-p", "tcp",
"--dst-range", "3.3.3.3-4.4.4.4",
"-m", "tcp", "--destination-port", "9000:9999",
"--jump", "ACCEPT"},
))
})
})
})
})