Skip to content

Commit 34ac8d3

Browse files
committed
added checks for services with allow all policys with empty and label selectors
1 parent 71d0ced commit 34ac8d3

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

tools/azure-npm-to-cilium-validator.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ func checkEndportNetworkPolicies(policiesByNamespace map[string][]networkingv1.N
111111
foundEndPort = true
112112
if !networkPolicyWithEndport {
113113
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with endPort", "❌")
114-
fmt.Println("endPort unsupported till Cilium v1.16.")
115114
fmt.Println("Policies affected:")
116115
networkPolicyWithEndport = true
117116
}
@@ -150,7 +149,6 @@ func checkCIDRNetworkPolicies(policiesByNamespace map[string][]networkingv1.Netw
150149
// Print the network policy if it has an ingress cidr
151150
if !networkPolicyWithCIDR {
152151
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with cidr", "❌")
153-
fmt.Println("CIDR only works for external traffic in Cilium.")
154152
fmt.Println("Policies affected:")
155153
networkPolicyWithCIDR = true
156154
}
@@ -175,7 +173,6 @@ func checkCIDRNetworkPolicies(policiesByNamespace map[string][]networkingv1.Netw
175173
// Print the network policy if it has an egress cidr
176174
if !networkPolicyWithCIDR {
177175
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with cidr", "❌")
178-
fmt.Println("CIDR only works for external traffic in Cilium.")
179176
fmt.Println("Policies affected:")
180177
networkPolicyWithCIDR = true
181178
}
@@ -211,7 +208,6 @@ func checkForEgressPolicies(policiesByNamespace map[string][]networkingv1.Networ
211208
if !networkPolicyWithEgress {
212209
fmt.Printf("%-30s | %-30s \n", "NetworkPolicy with egress", "❌")
213210
fmt.Printf("%-30s | %-30s \n", "(Not allow all egress)", "")
214-
fmt.Println("NPM does not enforce policies on egress to local node IP. Cilium does.")
215211
fmt.Println("Policies affected:")
216212
networkPolicyWithEgress = true
217213
}
@@ -278,7 +274,6 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
278274
fmt.Printf("%-30s | %-30s \n", "Disruption for some", "❌")
279275
fmt.Printf("%-30s | %-30s \n", "Services with", "")
280276
fmt.Printf("%-30s | %-30s \n", "externalTrafficPolicy=Cluster", "")
281-
fmt.Println("NPM incorrectly allows traffic for services with externalTrafficPolicy=Cluster (applicable if Type=NodePort or Type=LoadBalancer)")
282277
fmt.Println("Services affected:")
283278
// If there are any no selector services or unsafe services then print them as they could be impacted by migration
284279
if len(noSelectorServices) > 0 {
@@ -353,24 +348,26 @@ func checkServiceRisk(service v1.Service, namespace string, servicePorts []strin
353348
return safeServices
354349
}
355350

356-
func checkPolicyMatchServiceLabels(serviceLabels, policyPodLabels map[string]string) bool {
357-
// Count the number of labels that match
358-
matchLabelCount := 0
351+
func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string) bool {
352+
// Return false if the policy has more labels than the service
353+
if len(policyLabels) > len(serviceLabels) {
354+
return false
355+
}
359356

360-
for policyKey, policyValue := range policyPodLabels {
357+
// Check for each policy label that that label is present in the service labels
358+
for policyKey, policyValue := range policyLabels {
359+
matchedPolicyLabelToServiceLabel := false
361360
for serviceKey, serviceValue := range serviceLabels {
362-
if serviceKey == policyKey && serviceValue == policyValue {
363-
matchLabelCount++
361+
if policyKey == serviceKey && policyValue == serviceValue {
362+
matchedPolicyLabelToServiceLabel = true
363+
break
364364
}
365365
}
366+
if !matchedPolicyLabelToServiceLabel {
367+
return false
368+
}
366369
}
367-
368-
// If the number of labels that match is equal to the number of labels in the policy pod selector then return true
369-
// as that means all the match labels in the policy pod selector are present in the service selector
370-
if matchLabelCount != 0 && matchLabelCount == len(policyPodLabels) {
371-
return true
372-
}
373-
return false
370+
return true
374371
}
375372

376373
func contains(slice []string, item string) bool {

0 commit comments

Comments
 (0)