@@ -211,7 +211,7 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
211211 noSelectorServices = append (noSelectorServices , fmt .Sprintf ("%s/%s" , ns .Name , service .Name ))
212212 } else {
213213 // Check if are there services with selector that match the network policy
214- checkServiceRisk (& metav1. LabelSelector { MatchLabels : service . Spec . Selector } , ns .Name , servicePorts , policiesByNamespace [ns .Name ], safeServices )
214+ checkServiceRisk (service , ns .Name , servicePorts , policiesByNamespace [ns .Name ], safeServices )
215215 }
216216 }
217217 }
@@ -230,37 +230,32 @@ func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool {
230230 return false
231231}
232232
233- func checkServiceRisk (serviceSelector * metav1. LabelSelector , namespace string , servicePorts []string , policiesListAtNamespace []networkingv1.NetworkPolicy , safeServices []string ) {
233+ func checkServiceRisk (service v1. Service , namespace string , servicePorts []string , policiesListAtNamespace []networkingv1.NetworkPolicy , safeServices []string ) {
234234 for _ , policy := range policiesListAtNamespace {
235235 for _ , ingress := range policy .Spec .Ingress {
236- // If there are no ingress from or ports in the policy the service is safe
236+ // If there are no ingress from and ports in the policy check; check if the service is safe
237237 if len (ingress .From ) == 0 && len (ingress .Ports ) == 0 {
238- if matchSelector (serviceSelector , & policy .Spec .PodSelector ) {
239- safeServices = append (safeServices , fmt .Sprintf ("%s/%s" , namespace , serviceSelector ))
238+ if matchSelector (& metav1. LabelSelector { MatchLabels : service . Spec . Selector } , & policy .Spec .PodSelector ) {
239+ safeServices = append (safeServices , fmt .Sprintf ("%s/%s" , namespace , service . Name ))
240240 return
241241 }
242242 }
243+ // If there are no ingress from but there are ports in the policy; check if the service is safe
244+ if len (ingress .From ) == 0 && len (ingress .Ports ) > 0 {
245+ if matchSelector (& metav1.LabelSelector {MatchLabels : service .Spec .Selector }, & policy .Spec .PodSelector ) {
246+ matchingPorts := []string {}
247+ for _ , port := range ingress .Ports {
248+ matchingPorts = append (matchingPorts , fmt .Sprintf ("%d/%s" , port .Port .IntVal , string (* port .Protocol )))
249+ }
250+ for _ , sevicePort := range servicePorts {
251+ if contains (matchingPorts , sevicePort ) {
252+ safeServices = append (safeServices , fmt .Sprintf ("%s/%s" , namespace , service .Name ))
253+ return
254+ }
255+ }
256+ }
257+ }
243258 }
244- // for _, policy := range policiesListAtNamespace {
245- // for _, ingress := range policy.Spec.Ingress {
246- // // Check 4: are there policies with no From and Ports?
247- // if len(ingress.From) == 0 && len(ingress.Ports) > 0 {
248- // if matchSelector(serviceSelector, &policy.Spec.PodSelector) {
249- // matchingPorts := []string{}
250- // for _, port := range ingress.Ports {
251- // matchingPorts = append(matchingPorts, fmt.Sprintf("%d/%s", port.Port.IntVal, port.Protocol))
252- // }
253- // for _, svcPort := range servicePorts {
254- // if contains(matchingPorts, svcPort) {
255- // safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, serviceSelector))
256- // return
257- // }
258- // }
259- // }
260- // }
261- // }
262- // }
263- // }
264259 }
265260}
266261
@@ -273,15 +268,70 @@ func matchSelector(serviceSelector *metav1.LabelSelector, policyPodSelector *met
273268 policyPodLabels := policyPodSelector .MatchLabels
274269 serviceLabels := serviceSelector .MatchLabels
275270
276- // TODO CHECK
277- for key , value := range policyPodLabels {
278- if serviceLabels [key ] != value {
279- return false
280- }
271+ // If the labels in the policy pod selector are present in the service selector then return true
272+ if checkPolicyMatchServiceLabels (serviceLabels , policyPodLabels ) {
273+ return true
281274 }
282275
276+ // Get the expressions from the pod selector in the network policy and selector in the service
283277 policyPodExpressions := policyPodSelector .MatchExpressions
284278 serviceExpressions := serviceSelector .MatchExpressions
285279
280+ for _ , serviceExpression := range serviceExpressions {
281+ key := serviceExpression .Key
282+ operator := serviceExpression .Operator
283+ values := serviceExpression .Values
284+
285+ // Check if any of the policy expressions match the service expression
286+ matchingExpression := metav1.LabelSelectorRequirement {}
287+ foundMatchingExpression := false
288+ for _ , policyExpression := range policyPodExpressions {
289+ if policyExpression .Key == key {
290+ matchingExpression = policyExpression
291+ foundMatchingExpression = true
292+ break
293+ }
294+ }
295+ // If the expression is not found then return false
296+ if ! foundMatchingExpression {
297+ return false
298+ }
299+
300+ // Check if the values in the service expression are present in the matching policy expression
301+ if operator == metav1 .LabelSelectorOpIn {
302+ for _ , value := range values {
303+ if ! contains (matchingExpression .Values , value ) {
304+ return false
305+ }
306+ }
307+ } else if operator == metav1 .LabelSelectorOpNotIn {
308+ for _ , value := range values {
309+ if contains (matchingExpression .Values , value ) {
310+ return false
311+ }
312+ }
313+ }
314+ }
315+
286316 return true
287317}
318+
319+ func checkPolicyMatchServiceLabels (serviceLabels , policyPodLabels map [string ]string ) bool {
320+ // Count the number of labels that match
321+ matchLabelCount := 0
322+ for policyKey , policyValue := range policyPodLabels {
323+ for serviceKey , serviceValue := range serviceLabels {
324+ if serviceKey == policyKey && serviceValue == policyValue {
325+ matchLabelCount ++
326+ }
327+ }
328+ }
329+
330+ // If the number of labels that match is equal to the number of labels in the policy pod selector then return true
331+ // as that means all the match labels in the policy pod selector are present in the service selector
332+ if matchLabelCount == len (policyPodLabels ) {
333+ return true
334+ }
335+
336+ return false
337+ }
0 commit comments