Skip to content

Commit 14e8a98

Browse files
authored
Add logic to hanle different order between namespace selectoer, pod selector, ip block rules. (#604)
1 parent d96246e commit 14e8a98

File tree

3 files changed

+69
-25
lines changed

3 files changed

+69
-25
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: NetworkPolicy
3+
metadata:
4+
name: k8s-example-policy
5+
namespace: default
6+
spec:
7+
podSelector:
8+
matchLabels:
9+
role: db
10+
policyTypes:
11+
- Ingress
12+
- Egress
13+
ingress:
14+
- from:
15+
- namespaceSelector:
16+
matchLabels:
17+
project: myproject
18+
- podSelector:
19+
matchLabels:
20+
role: frontend
21+
- ipBlock:
22+
cidr: 172.17.0.0/16
23+
except:
24+
- 172.17.1.0/24
25+
ports:
26+
- protocol: TCP
27+
port: 6379
28+
egress:
29+
- to:
30+
- ipBlock:
31+
cidr: 10.0.0.0/24
32+
except:
33+
- 10.0.0.1/32
34+
ports:
35+
- protocol: TCP
36+
port: 5978
37+
38+

npm/translatePolicy.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
159159
ipCidrs [][]string
160160
entries []*iptm.IptEntry
161161
fromRuleEntries []*iptm.IptEntry
162+
addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
162163
addedIngressFromEntry, addedPortEntry bool // add drop entries at the end of the chain when there are non ALLOW-ALL* rules
163164
)
164165

@@ -291,7 +292,7 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
291292
}
292293
addedIngressFromEntry = true
293294
}
294-
if j != 0 {
295+
if j != 0 && addedCidrEntry {
295296
continue
296297
}
297298
if portRuleExists {
@@ -324,7 +325,7 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
324325
util.IptablesCommentModuleFlag,
325326
util.IptablesCommentFlag,
326327
"ALLOW-"+cidrIpsetName+
327-
"-:-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
328+
"-AND-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
328329
"-TO-"+targetSelectorComment,
329330
)
330331
fromRuleEntries = append(fromRuleEntries, entry)
@@ -353,19 +354,19 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
353354
util.IptablesCommentModuleFlag,
354355
util.IptablesCommentFlag,
355356
"ALLOW-"+cidrIpsetName+
356-
"-:-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
357+
"-AND-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
357358
"-TO-"+targetSelectorComment,
358359
)
359360
fromRuleEntries = append(fromRuleEntries, entry)
360361
}
361362
}
362363
} else {
363-
cidrEntry := &iptm.IptEntry{
364+
entry := &iptm.IptEntry{
364365
Chain: util.IptablesAzureIngressFromChain,
365366
Specs: append([]string(nil), targetSelectorIptEntrySpec...),
366367
}
367-
cidrEntry.Specs = append(
368-
cidrEntry.Specs,
368+
entry.Specs = append(
369+
entry.Specs,
369370
util.IptablesModuleFlag,
370371
util.IptablesSetModuleFlag,
371372
util.IptablesMatchSetFlag,
@@ -379,9 +380,10 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
379380
"ALLOW-"+cidrIpsetName+
380381
"-TO-"+targetSelectorComment,
381382
)
382-
fromRuleEntries = append(fromRuleEntries, cidrEntry)
383+
fromRuleEntries = append(fromRuleEntries, entry)
383384
addedIngressFromEntry = true
384385
}
386+
addedCidrEntry = true
385387
}
386388
continue
387389
}
@@ -799,6 +801,7 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
799801
ipCidrs [][]string
800802
entries []*iptm.IptEntry
801803
toRuleEntries []*iptm.IptEntry
804+
addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
802805
addedEgressToEntry, addedPortEntry bool // add drop entry when there are non ALLOW-ALL* rules
803806
)
804807

@@ -927,7 +930,7 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
927930
}
928931
addedEgressToEntry = true
929932
}
930-
if j != 0 {
933+
if j != 0 && addedCidrEntry {
931934
continue
932935
}
933936
if portRuleExists {
@@ -960,7 +963,7 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
960963
util.IptablesCommentModuleFlag,
961964
util.IptablesCommentFlag,
962965
"ALLOW-"+cidrIpsetName+
963-
"-:-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
966+
"-AND-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
964967
"-FROM-"+targetSelectorComment,
965968
)
966969
toRuleEntries = append(toRuleEntries, entry)
@@ -989,30 +992,30 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
989992
util.IptablesCommentModuleFlag,
990993
util.IptablesCommentFlag,
991994
"ALLOW-"+cidrIpsetName+
992-
"-:-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
995+
"-AND-"+craftPartialIptablesCommentFromPort(portRule, util.IptablesDstPortFlag)+
993996
"-FROM-"+targetSelectorComment,
994997
)
995998
toRuleEntries = append(toRuleEntries, entry)
996999
}
9971000
}
9981001
} else {
999-
cidrEntry := &iptm.IptEntry{
1002+
entry := &iptm.IptEntry{
10001003
Chain: util.IptablesAzureEgressToChain,
10011004
}
1002-
cidrEntry.Specs = append(
1003-
cidrEntry.Specs,
1005+
entry.Specs = append(
1006+
entry.Specs,
10041007
targetSelectorIptEntrySpec...,
10051008
)
1006-
cidrEntry.Specs = append(
1007-
cidrEntry.Specs,
1009+
entry.Specs = append(
1010+
entry.Specs,
10081011
util.IptablesModuleFlag,
10091012
util.IptablesSetModuleFlag,
10101013
util.IptablesMatchSetFlag,
10111014
util.GetHashedName(cidrIpsetName),
10121015
util.IptablesDstFlag,
10131016
)
1014-
cidrEntry.Specs = append(
1015-
cidrEntry.Specs,
1017+
entry.Specs = append(
1018+
entry.Specs,
10161019
util.IptablesJumpFlag,
10171020
util.IptablesAccept,
10181021
util.IptablesModuleFlag,
@@ -1021,9 +1024,10 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
10211024
"ALLOW-"+cidrIpsetName+
10221025
"-FROM-"+targetSelectorComment,
10231026
)
1024-
toRuleEntries = append(toRuleEntries, cidrEntry)
1027+
toRuleEntries = append(toRuleEntries, entry)
10251028
addedEgressToEntry = true
10261029
}
1030+
addedCidrEntry = true
10271031
}
10281032
continue
10291033
}

npm/translatePolicy_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,18 +2914,20 @@ func TestAllowAppFrontendToTCPPort53UDPPort53Policy(t *testing.T) {
29142914

29152915
func TestComplexPolicy(t *testing.T) {
29162916
k8sExamplePolicy, err := readPolicyYaml("testpolicies/complex-policy.yaml")
2917+
k8sExamplePolicyDiffOrder, err := readPolicyYaml("testpolicies/complex-policy-diff-order.yaml")
29172918
if err != nil {
29182919
t.Fatal(err)
29192920
}
29202921

29212922
sets, _, lists, ingressIPCidrs, egressIPCidrs, iptEntries := translatePolicy(k8sExamplePolicy)
2923+
setsDiffOrder, _, listsDiffOrder, ingressIPCidrsDiffOrder, egressIPCidrsDiffOrder, iptEntriesDiffOrder := translatePolicy(k8sExamplePolicyDiffOrder)
29222924

29232925
expectedSets := []string{
29242926
"role:db",
29252927
"ns-default",
29262928
"role:frontend",
29272929
}
2928-
if !reflect.DeepEqual(sets, expectedSets) {
2930+
if !reflect.DeepEqual(sets, expectedSets) || !reflect.DeepEqual(setsDiffOrder, expectedSets) {
29292931
t.Errorf("translatedPolicy failed @ k8s-example-policy sets comparison")
29302932
t.Errorf("sets: %v", sets)
29312933
t.Errorf("expectedSets: %v", expectedSets)
@@ -2934,7 +2936,7 @@ func TestComplexPolicy(t *testing.T) {
29342936
expectedLists := []string{
29352937
"ns-project:myproject",
29362938
}
2937-
if !reflect.DeepEqual(lists, expectedLists) {
2939+
if !reflect.DeepEqual(lists, expectedLists) || !reflect.DeepEqual(listsDiffOrder, expectedLists) {
29382940
t.Errorf("translatedPolicy failed @ k8s-example-policy lists comparison")
29392941
t.Errorf("lists: %v", lists)
29402942
t.Errorf("expectedLists: %v", expectedLists)
@@ -2948,13 +2950,13 @@ func TestComplexPolicy(t *testing.T) {
29482950
{"", "10.0.0.0/24", "10.0.0.1/32nomatch"},
29492951
}
29502952

2951-
if !reflect.DeepEqual(ingressIPCidrs, expectedIngressIPCidrs) {
2953+
if !reflect.DeepEqual(ingressIPCidrs, expectedIngressIPCidrs) || !reflect.DeepEqual(ingressIPCidrsDiffOrder, expectedIngressIPCidrs){
29522954
t.Errorf("translatedPolicy failed @ k8s-example-policy ingress IP Cidrs comparison")
29532955
t.Errorf("ingress IP Cidrs: %v", ingressIPCidrs)
29542956
t.Errorf("expected ingress IP Cidrs: %v", expectedIngressIPCidrs)
29552957
}
29562958

2957-
if !reflect.DeepEqual(egressIPCidrs, expectedEgressIPCidrs) {
2959+
if !reflect.DeepEqual(egressIPCidrs, expectedEgressIPCidrs) || !reflect.DeepEqual(egressIPCidrsDiffOrder, expectedEgressIPCidrs) {
29582960
t.Errorf("translatedPolicy failed @ k8s-example-policy egress IP Cidrs comparison")
29592961
t.Errorf("egress IP Cidrs: %v", egressIPCidrs)
29602962
t.Errorf("expected egress IP Cidrs: %v", expectedEgressIPCidrs)
@@ -2991,7 +2993,7 @@ func TestComplexPolicy(t *testing.T) {
29912993
util.IptablesModuleFlag,
29922994
util.IptablesCommentModuleFlag,
29932995
util.IptablesCommentFlag,
2994-
"ALLOW-" + cidrIngressIpsetName + "-:-TCP-PORT-6379-TO-role:db-IN-ns-default",
2996+
"ALLOW-" + cidrIngressIpsetName + "-AND-TCP-PORT-6379-TO-role:db-IN-ns-default",
29952997
},
29962998
},
29972999
&iptm.IptEntry{
@@ -3130,7 +3132,7 @@ func TestComplexPolicy(t *testing.T) {
31303132
util.IptablesModuleFlag,
31313133
util.IptablesCommentModuleFlag,
31323134
util.IptablesCommentFlag,
3133-
"ALLOW-" + cidrEgressIpsetName + "-:-TCP-PORT-5978-FROM-role:db-IN-ns-default",
3135+
"ALLOW-" + cidrEgressIpsetName + "-AND-TCP-PORT-5978-FROM-role:db-IN-ns-default",
31343136
},
31353137
},
31363138
&iptm.IptEntry{
@@ -3222,7 +3224,7 @@ func TestComplexPolicy(t *testing.T) {
32223224
}
32233225
expectedIptEntries = append(expectedIptEntries, nonKubeSystemEntries...)
32243226
expectedIptEntries = append(expectedIptEntries, getDefaultDropEntries("testnamespace", k8sExamplePolicy.Spec.PodSelector, false, false)...)
3225-
if !reflect.DeepEqual(iptEntries, expectedIptEntries) {
3227+
if !reflect.DeepEqual(iptEntries, expectedIptEntries) || !reflect.DeepEqual(iptEntriesDiffOrder, expectedIptEntries) {
32263228
t.Errorf("translatedPolicy failed @ k8s-example-policy policy comparison")
32273229
marshalledIptEntries, _ := json.Marshal(iptEntries)
32283230
marshalledExpectedIptEntries, _ := json.Marshal(expectedIptEntries)

0 commit comments

Comments
 (0)