Skip to content

Commit 71d0ced

Browse files
committed
added checks for allow all ingress policies
1 parent cdb91b2 commit 71d0ced

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,17 @@ func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool {
317317
func checkServiceRisk(service v1.Service, namespace string, servicePorts []string, policiesListAtNamespace []networkingv1.NetworkPolicy, safeServices []string) []string {
318318
for _, policy := range policiesListAtNamespace {
319319
for _, ingress := range policy.Spec.Ingress {
320-
// Check if there is an allow all policy that matches labels the service is safe
320+
// Check if there is an allow all ingress policy that matches labels the service is safe
321321
if len(ingress.From) == 0 && len(ingress.Ports) == 0 {
322-
if matchAllServiceSelector(&metav1.LabelSelector{MatchLabels: service.Spec.Selector}, &policy.Spec.PodSelector) {
322+
// Check if there is an allow all ingress policy with empty selectors return true as the policy allows all services in the namespace
323+
if len(policy.Spec.PodSelector.MatchLabels) == 0 {
324+
fmt.Printf("found an allow all ingress policy: %s with empty selectors so service %s in the namespace %s is safe\n", policy.Name, service.Name, namespace)
325+
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, service.Name))
326+
return safeServices
327+
}
328+
// Check if there is an allow all ingress policy that matches the service labels
329+
if checkPolicyMatchServiceLabels(service.Spec.Selector, policy.Spec.PodSelector.MatchLabels) {
330+
fmt.Printf("found an allow all ingress policy: %s with matching selectors so service %s in the namespace %s is safe\n", policy.Name, service.Name, namespace)
323331
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, service.Name))
324332
return safeServices
325333
}
@@ -345,26 +353,10 @@ func checkServiceRisk(service v1.Service, namespace string, servicePorts []strin
345353
return safeServices
346354
}
347355

348-
func matchAllServiceSelector(serviceSelector *metav1.LabelSelector, policyPodSelector *metav1.LabelSelector) bool {
349-
if serviceSelector == nil || policyPodSelector == nil {
350-
return false
351-
}
352-
353-
// Get the labels from the pod selector in the network policy and selector in the service
354-
policyPodLabels := policyPodSelector.MatchLabels
355-
serviceLabels := serviceSelector.MatchLabels
356-
357-
// If the labels in the policy pod selector are present in the service selector then return true
358-
if checkPolicyMatchServiceLabels(serviceLabels, policyPodLabels) {
359-
return true
360-
}
361-
362-
return false
363-
}
364-
365356
func checkPolicyMatchServiceLabels(serviceLabels, policyPodLabels map[string]string) bool {
366357
// Count the number of labels that match
367358
matchLabelCount := 0
359+
368360
for policyKey, policyValue := range policyPodLabels {
369361
for serviceKey, serviceValue := range serviceLabels {
370362
if serviceKey == policyKey && serviceValue == policyValue {
@@ -375,7 +367,7 @@ func checkPolicyMatchServiceLabels(serviceLabels, policyPodLabels map[string]str
375367

376368
// If the number of labels that match is equal to the number of labels in the policy pod selector then return true
377369
// as that means all the match labels in the policy pod selector are present in the service selector
378-
if matchLabelCount == len(policyPodLabels) {
370+
if matchLabelCount != 0 && matchLabelCount == len(policyPodLabels) {
379371
return true
380372
}
381373
return false

0 commit comments

Comments
 (0)