@@ -150,6 +150,13 @@ func getEgressPolicies(policiesByNamespace map[string][]*networkingv1.NetworkPol
150150 var egressPolicies []string
151151 for namespace , policies := range policiesByNamespace {
152152 for _ , policy := range policies {
153+ for _ , policyType := range policy .Spec .PolicyTypes {
154+ // If the policy is an egress type and has no egress field it is an deny all flag it
155+ if policyType == networkingv1 .PolicyTypeEgress && len (policy .Spec .Egress ) == 0 {
156+ egressPolicies = append (egressPolicies , fmt .Sprintf ("%s/%s" , namespace , policy .Name ))
157+ break
158+ }
159+ }
153160 for _ , egress := range policy .Spec .Egress {
154161 // If the policy has a egress field thats not an egress allow all flag it
155162 if len (egress .To ) > 0 || len (egress .Ports ) > 0 {
@@ -271,6 +278,11 @@ func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string
271278}
272279
273280func checkServiceTargetPortMatchPolicyPorts (servicePorts * []corev1.ServicePort , policyPorts * []networkingv1.NetworkPolicyPort ) bool {
281+ // If the service has no ports then it is at risk
282+ if len (* servicePorts ) == 0 {
283+ return false
284+ }
285+
274286 for _ , servicePort := range * servicePorts {
275287 // If the target port is a string then it is a named port and service is at risk
276288 if servicePort .TargetPort .Type == intstr .String {
@@ -280,10 +292,17 @@ func checkServiceTargetPortMatchPolicyPorts(servicePorts *[]corev1.ServicePort,
280292 // Check if all the services target ports are in the policies ingress ports
281293 matchedserviceTargetPortToPolicyPort := false
282294 for _ , policyPort := range * policyPorts {
283- // Check if the policys port exists
284- if policyPort .Port == nil {
295+ // Check if the policys port and protocol exists
296+ if policyPort .Port == nil && policyPort . Protocol == nil {
285297 return false
286298 }
299+ // If the policy only has a protocol check the protocol against the service
300+ if policyPort .Port == nil && policyPort .Protocol != nil {
301+ if string (servicePort .Protocol ) == string (* policyPort .Protocol ) {
302+ matchedserviceTargetPortToPolicyPort = true
303+ break
304+ }
305+ }
287306 // If the port is a string then it is a named port and service is at risk
288307 if policyPort .Port .Type == intstr .String {
289308 return false
0 commit comments