Skip to content

Commit

Permalink
added a check for services with target ports
Browse files Browse the repository at this point in the history
  • Loading branch information
rayaisaiah committed Feb 3, 2025
1 parent 087b1c1 commit eb5f727
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions tools/azure-npm-to-cilium-validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,6 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
// Check if are there services with externalTrafficPolicy=Cluster (applicable if Type=NodePort or Type=LoadBalancer)
for _, service := range serviceListAtNamespace {

Check failure on line 240 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

rangeValCopy: each iteration copies 592 bytes (consider pointers or indexing) (gocritic)

Check failure on line 240 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

rangeValCopy: each iteration copies 592 bytes (consider pointers or indexing) (gocritic)

Check failure on line 240 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

rangeValCopy: each iteration copies 592 bytes (consider pointers or indexing) (gocritic)

Check failure on line 240 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

rangeValCopy: each iteration copies 592 bytes (consider pointers or indexing) (gocritic)
if service.Spec.Type == v1.ServiceTypeLoadBalancer || service.Spec.Type == v1.ServiceTypeNodePort {
servicePorts := []string{}
// get the Port and Protocol of the service
for _, port := range service.Spec.Ports {
servicePorts = append(servicePorts, fmt.Sprintf("%d/%s", port.Port, port.Protocol))
}
externalTrafficPolicy := service.Spec.ExternalTrafficPolicy
// If the service has externalTrafficPolicy is set to "Cluster" add it to the servicesAtRisk list (ExternalTrafficPolicy: "" defaults to Cluster)
if externalTrafficPolicy != v1.ServiceExternalTrafficPolicyTypeLocal {
Expand All @@ -254,7 +249,7 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
noSelectorServices = append(noSelectorServices, fmt.Sprintf("%s/%s", namespace.Name, service.Name))
} else {
// Check if are there services with selector that match the network policy
safeServices = checkServiceRisk(service, namespace.Name, servicePorts, policiesByNamespace[namespace.Name], safeServices)
safeServices = checkServiceRisk(service, namespace.Name, policiesByNamespace[namespace.Name], safeServices)
}
}
}
Expand Down Expand Up @@ -309,7 +304,7 @@ func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool {
return false
}

func checkServiceRisk(service v1.Service, namespace string, servicePorts []string, policiesListAtNamespace []networkingv1.NetworkPolicy, safeServices []string) []string {
func checkServiceRisk(service v1.Service, namespace string, policiesListAtNamespace []networkingv1.NetworkPolicy, safeServices []string) []string {
for _, policy := range policiesListAtNamespace {

Check failure on line 308 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

rangeValCopy: each iteration copies 368 bytes (consider pointers or indexing) (gocritic)

Check failure on line 308 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

rangeValCopy: each iteration copies 368 bytes (consider pointers or indexing) (gocritic)

Check failure on line 308 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

rangeValCopy: each iteration copies 368 bytes (consider pointers or indexing) (gocritic)

Check failure on line 308 in tools/azure-npm-to-cilium-validator.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

rangeValCopy: each iteration copies 368 bytes (consider pointers or indexing) (gocritic)
for _, ingress := range policy.Spec.Ingress {
// Check if there is an allow all ingress policy that matches labels the service is safe
Expand All @@ -327,22 +322,16 @@ func checkServiceRisk(service v1.Service, namespace string, servicePorts []strin
return safeServices
}
}
// Check if all the labels in
// // If there are no ingress from but there are ports in the policy; check if the service is safe
// if len(ingress.From) == 0 && len(ingress.Ports) > 0 {
// if matchAllServiceSelector(&metav1.LabelSelector{MatchLabels: service.Spec.Selector}, &policy.Spec.PodSelector) {
// matchingPorts := []string{}
// for _, port := range ingress.Ports {
// matchingPorts = append(matchingPorts, fmt.Sprintf("%d/%s", port.Port.IntVal, string(*port.Protocol)))
// }
// for _, sevicePort := range servicePorts {
// if contains(matchingPorts, sevicePort) {
// safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, service.Name))
// return
// }
// }
// }
// }
// If there are no ingress from but there are ports in the policy; check if the service is safe
if len(ingress.From) == 0 && len(ingress.Ports) > 0 {
// 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
if len(policy.Spec.PodSelector.MatchLabels) == 0 || checkPolicyMatchServiceLabels(service.Spec.Selector, policy.Spec.PodSelector.MatchLabels) {
if checkServiceTargetPortMatchPolicyPorts(service.Spec.Ports, ingress.Ports) {
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace, service.Name))
return safeServices
}
}
}
}
}
return safeServices
Expand Down Expand Up @@ -370,6 +359,22 @@ func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string
return true
}

func checkServiceTargetPortMatchPolicyPorts(servicePorts []v1.ServicePort, policyPorts []networkingv1.NetworkPolicyPort) bool {
ingressPorts := []string{}
for _, port := range policyPorts {
ingressPorts = append(ingressPorts, fmt.Sprintf("%d/%s", port.Port.IntVal, string(*port.Protocol)))
}

// Check if all the services target ports are in the policies ingress ports
for _, port := range servicePorts {
servicePort := fmt.Sprintf("%d/%s", port.TargetPort.IntValue(), port.Protocol)
if !contains(ingressPorts, servicePort) {
return false
}
}
return true
}

func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
Expand Down

0 comments on commit eb5f727

Please sign in to comment.