Skip to content

Commit dd25cc8

Browse files
committed
responded to service comments
1 parent d6ec15e commit dd25cc8

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
248248
noSelectorServices = append(noSelectorServices, fmt.Sprintf("%s/%s", namespace.Name, service.Name))
249249
} else {
250250
// Check if are there services with selector that match the network policy
251-
safeServices = checkServiceRisk(service, namespace.Name, policiesByNamespace[namespace.Name], safeServices)
251+
if checkServiceRisk(service, namespace.Name, policiesByNamespace[namespace.Name]) {
252+
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace.Name, service.Name))
253+
}
252254
}
253255
}
254256
}
@@ -303,37 +305,40 @@ func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool {
303305
return false
304306
}
305307

306-
func checkServiceRisk(service corev1.Service, namespace string, policiesListAtNamespace []networkingv1.NetworkPolicy, safeServices []string) []string {
308+
func checkServiceRisk(service corev1.Service, namespace string, policiesListAtNamespace []networkingv1.NetworkPolicy) bool {
307309
for _, policy := range policiesListAtNamespace {
308310
for _, ingress := range policy.Spec.Ingress {
309311
// Check if there is an allow all ingress policy that matches labels the service is safe
310312
if len(ingress.From) == 0 && len(ingress.Ports) == 0 {
311313
// Check if there is an allow all ingress policy with empty selectors return true as the policy allows all services in the namespace
312-
if len(policy.Spec.PodSelector.MatchLabels) == 0 {
314+
if checkPolicySelectorsAreEmpty(policy.Spec.PodSelector) {
313315
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)
314-
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, service.Name))
315-
return safeServices
316+
return true
316317
}
317318
// Check if there is an allow all ingress policy that matches the service labels
318319
if checkPolicyMatchServiceLabels(service.Spec.Selector, policy.Spec.PodSelector.MatchLabels) {
320+
// TODO add this to above logic and check in one if statement after i am done printing the logs
319321
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)
320-
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, service.Name))
321-
return safeServices
322+
return true
322323
}
323324
}
324325
// If there are no ingress from but there are ports in the policy; check if the service is safe
325326
if len(ingress.From) == 0 && len(ingress.Ports) > 0 {
326327
// If the policy targets all pods (allow all) or only pods that are in the service selector, check if traffic is allowed to all the service's target ports
327-
if len(policy.Spec.PodSelector.MatchLabels) == 0 || checkPolicyMatchServiceLabels(service.Spec.Selector, policy.Spec.PodSelector.MatchLabels) {
328+
if checkPolicySelectorsAreEmpty(policy.Spec.PodSelector) || checkPolicyMatchServiceLabels(service.Spec.Selector, policy.Spec.PodSelector.MatchLabels) {
328329
if checkServiceTargetPortMatchPolicyPorts(service.Spec.Ports, ingress.Ports) {
329-
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, service.Name))
330-
return safeServices
330+
fmt.Printf("found an ingress port policy: %s with matching selectors and target ports so service %s in the namespace %s is safe\n", policy.Name, service.Name, namespace)
331+
return true
331332
}
332333
}
333334
}
334335
}
335336
}
336-
return safeServices
337+
return false
338+
}
339+
340+
func checkPolicySelectorsAreEmpty(podSelector metav1.LabelSelector) bool {
341+
return len(podSelector.MatchLabels) == 0 && len(podSelector.MatchExpressions) == 0
337342
}
338343

339344
func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string) bool {
@@ -343,6 +348,7 @@ func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string
343348
}
344349

345350
// Check for each policy label that that label is present in the service labels
351+
// Note does not check matchExpressions
346352
for policyKey, policyValue := range policyLabels {
347353
matchedPolicyLabelToServiceLabel := false
348354
for serviceKey, serviceValue := range serviceLabels {
@@ -371,10 +377,7 @@ func checkServiceTargetPortMatchPolicyPorts(servicePorts []corev1.ServicePort, p
371377
return false
372378
}
373379
servicePort := fmt.Sprintf("%d/%s", port.TargetPort.IntValue(), port.Protocol)
374-
fmt.Printf("servicePort %s\n", servicePort)
375-
fmt.Printf("ingressPorts %v\n", ingressPorts)
376380
if !contains(ingressPorts, servicePort) {
377-
fmt.Printf("Service port %s is not allowed in the policy\n", servicePort)
378381
return false
379382
}
380383
}

0 commit comments

Comments
 (0)