Skip to content

Commit afa4e9d

Browse files
committed
added a check for ingress deny all and updated port check function to be a nested loop
1 parent dd25cc8 commit afa4e9d

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,10 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
294294
}
295295

296296
func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool {
297-
// Check if any policy is ingress
297+
// Check if any policy is ingress (including allow all and deny all)
298298
for _, policy := range policies {
299-
for _, ingress := range policy.Spec.Ingress {
300-
if len(ingress.From) > 0 || len(ingress.Ports) > 0 {
299+
for _, policyType := range policy.Spec.PolicyTypes {
300+
if policyType == networkingv1.PolicyTypeIngress {
301301
return true
302302
}
303303
}
@@ -307,6 +307,7 @@ func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool {
307307

308308
func checkServiceRisk(service corev1.Service, namespace string, policiesListAtNamespace []networkingv1.NetworkPolicy) bool {
309309
for _, policy := range policiesListAtNamespace {
310+
// Skips deny all policies as they do not have any ingress rules
310311
for _, ingress := range policy.Spec.Ingress {
311312
// Check if there is an allow all ingress policy that matches labels the service is safe
312313
if len(ingress.From) == 0 && len(ingress.Ports) == 0 {
@@ -365,34 +366,27 @@ func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string
365366
}
366367

367368
func checkServiceTargetPortMatchPolicyPorts(servicePorts []corev1.ServicePort, policyPorts []networkingv1.NetworkPolicyPort) bool {
368-
ingressPorts := []string{}
369-
for _, port := range policyPorts {
370-
ingressPorts = append(ingressPorts, fmt.Sprintf("%d/%s", port.Port.IntVal, string(*port.Protocol)))
371-
}
372-
373-
// Check if all the services target ports are in the policies ingress ports
374-
for _, port := range servicePorts {
369+
for _, servicePort := range servicePorts {
375370
// If the target port is a string then it is a named port and service is at risk
376-
if port.TargetPort.Type == intstr.String {
371+
if servicePort.TargetPort.Type == intstr.String {
377372
return false
378373
}
379-
servicePort := fmt.Sprintf("%d/%s", port.TargetPort.IntValue(), port.Protocol)
380-
if !contains(ingressPorts, servicePort) {
374+
375+
// Check if all the services target ports are in the policies ingress ports
376+
serviceTargetPortPolicyPort := false
377+
for _, policyPort := range policyPorts {
378+
if servicePort.TargetPort.IntValue() == int(policyPort.Port.IntVal) && string(servicePort.Protocol) == string(*policyPort.Protocol) {
379+
serviceTargetPortPolicyPort = true
380+
break
381+
}
382+
}
383+
if !serviceTargetPortPolicyPort {
381384
return false
382385
}
383386
}
384387
return true
385388
}
386389

387-
func contains(slice []string, item string) bool {
388-
for _, s := range slice {
389-
if s == item {
390-
return true
391-
}
392-
}
393-
return false
394-
}
395-
396390
func difference(slice1, slice2, slice3 []string) []string {
397391
m := make(map[string]bool)
398392
for _, s := range slice2 {

0 commit comments

Comments
 (0)