Skip to content

Commit 2f7d338

Browse files
committed
fixed pointer logic and added unit tests for the checks except service
1 parent b9231e2 commit 2f7d338

File tree

2 files changed

+455
-250
lines changed

2 files changed

+455
-250
lines changed

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

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ func main() {
4141
log.Fatalf("Error getting namespaces: %v\n", err)
4242
}
4343

44-
// Store network policies and services in maps
45-
policiesByNamespace := make(map[string][]networkingv1.NetworkPolicy)
46-
servicesByNamespace := make(map[string][]corev1.Service)
47-
4844
// Copy namespaces.Items into a slice of pointers
4945
namespacePointers := make([]*corev1.Namespace, len(namespaces.Items))
5046
for i := range namespaces.Items {
5147
namespacePointers[i] = &namespaces.Items[i]
5248
}
5349

50+
// Store network policies and services in maps
51+
policiesByNamespace := make(map[string][]*networkingv1.NetworkPolicy)
52+
servicesByNamespace := make(map[string][]*corev1.Service)
53+
5454
// Iterate over namespaces and store policies/services
5555
for _, ns := range namespacePointers {
5656
// Get network policies
@@ -59,25 +59,31 @@ func main() {
5959
fmt.Printf("Error getting network policies in namespace %s: %v\n", ns.Name, err)
6060
continue
6161
}
62-
policiesByNamespace[ns.Name] = networkPolicies.Items
62+
policiesByNamespace[ns.Name] = make([]*networkingv1.NetworkPolicy, len(networkPolicies.Items))
63+
for i := range networkPolicies.Items {
64+
policiesByNamespace[ns.Name][i] = &networkPolicies.Items[i]
65+
}
6366

6467
// Get services
6568
services, err := clientset.CoreV1().Services(ns.Name).List(context.TODO(), metav1.ListOptions{})
6669
if err != nil {
6770
fmt.Printf("Error getting services in namespace %s: %v\n", ns.Name, err)
6871
continue
6972
}
70-
servicesByNamespace[ns.Name] = services.Items
73+
servicesByNamespace[ns.Name] = make([]*corev1.Service, len(services.Items))
74+
for i := range services.Items {
75+
servicesByNamespace[ns.Name][i] = &services.Items[i]
76+
}
7177
}
7278

7379
// Print the migration summary
74-
printMigrationSummary(namespaces, &policiesByNamespace, &servicesByNamespace)
80+
printMigrationSummary(namespaces, policiesByNamespace, servicesByNamespace)
7581
}
7682

77-
func checkEndportNetworkPolicies(policiesByNamespace *map[string][]networkingv1.NetworkPolicy) ([]string, []string) {
83+
func getEndportNetworkPolicies(policiesByNamespace map[string][]*networkingv1.NetworkPolicy) ([]string, []string) {
7884
var ingressPoliciesWithEndport []string
7985
var egressPoliciesWithEndport []string
80-
for namespace, policies := range *policiesByNamespace {
86+
for namespace, policies := range policiesByNamespace {
8187
for _, policy := range policies {
8288
// Check the ingress field for endport
8389
for _, ingress := range policy.Spec.Ingress {
@@ -108,10 +114,10 @@ func checkEndportInPolicyRules(ports *[]networkingv1.NetworkPolicyPort) bool {
108114
return false
109115
}
110116

111-
func checkCIDRNetworkPolicies(policiesByNamespace *map[string][]networkingv1.NetworkPolicy) ([]string, []string) {
117+
func getCIDRNetworkPolicies(policiesByNamespace map[string][]*networkingv1.NetworkPolicy) ([]string, []string) {
112118
var ingressPoliciesWithCIDR []string
113119
var egressPoliciesWithCIDR []string
114-
for namespace, policies := range *policiesByNamespace {
120+
for namespace, policies := range policiesByNamespace {
115121
for _, policy := range policies {
116122
// Check the ingress field for cidr
117123
for _, ingress := range policy.Spec.Ingress {
@@ -144,9 +150,9 @@ func checkCIDRInPolicyRules(rules *[]networkingv1.NetworkPolicyPeer) bool {
144150
return false
145151
}
146152

147-
func checkForEgressPolicies(policiesByNamespace *map[string][]networkingv1.NetworkPolicy) []string {
153+
func getEgressPolicies(policiesByNamespace map[string][]*networkingv1.NetworkPolicy) []string {
148154
var egressPolicies []string
149-
for namespace, policies := range *policiesByNamespace {
155+
for namespace, policies := range policiesByNamespace {
150156
for _, policy := range policies {
151157
for _, egress := range policy.Spec.Egress {
152158
// If the policy has a egress field thats not an egress allow all flag it
@@ -160,16 +166,16 @@ func checkForEgressPolicies(policiesByNamespace *map[string][]networkingv1.Netwo
160166
return egressPolicies
161167
}
162168

163-
func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servicesByNamespace *map[string][]corev1.Service, policiesByNamespace *map[string][]networkingv1.NetworkPolicy) ([]string, []string) {
169+
func getExternalTrafficPolicyClusterServices(namespaces *corev1.NamespaceList, servicesByNamespace map[string][]*corev1.Service, policiesByNamespace map[string][]*networkingv1.NetworkPolicy) ([]string, []string) {
164170
var servicesAtRisk, noSelectorServices, safeServices []string
165171

166172
for _, namespace := range namespaces.Items {
167173
// Check if are there ingress policies in the namespace if not skip
168-
policyListAtNamespace := (*policiesByNamespace)[namespace.Name]
169-
if !hasIngressPolicies(&policyListAtNamespace) {
174+
policyListAtNamespace := policiesByNamespace[namespace.Name]
175+
if !hasIngressPolicies(policyListAtNamespace) {
170176
continue
171177
}
172-
serviceListAtNamespace := (*servicesByNamespace)[namespace.Name]
178+
serviceListAtNamespace := servicesByNamespace[namespace.Name]
173179

174180
// Check if are there services with externalTrafficPolicy=Cluster (applicable if Type=NodePort or Type=LoadBalancer)
175181
for _, service := range serviceListAtNamespace {
@@ -184,7 +190,7 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
184190
noSelectorServices = append(noSelectorServices, fmt.Sprintf("%s/%s", namespace.Name, service.Name))
185191
} else {
186192
// Check if are there services with selector that match the network policy
187-
if checkServiceRisk(&service, &namespace.Name, &policyListAtNamespace) {
193+
if checkServiceRisk(service, &namespace.Name, policyListAtNamespace) {
188194
safeServices = append(safeServices, fmt.Sprintf("%s/%s", namespace.Name, service.Name))
189195
}
190196
}
@@ -199,9 +205,9 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
199205
return unsafeServices, noSelectorServices
200206
}
201207

202-
func hasIngressPolicies(policies *[]networkingv1.NetworkPolicy) bool {
208+
func hasIngressPolicies(policies []*networkingv1.NetworkPolicy) bool {
203209
// Check if any policy is ingress (including allow all and deny all)
204-
for _, policy := range *policies {
210+
for _, policy := range policies {
205211
for _, policyType := range policy.Spec.PolicyTypes {
206212
if policyType == networkingv1.PolicyTypeIngress {
207213
return true
@@ -211,8 +217,8 @@ func hasIngressPolicies(policies *[]networkingv1.NetworkPolicy) bool {
211217
return false
212218
}
213219

214-
func checkServiceRisk(service *corev1.Service, namespace *string, policiesListAtNamespace *[]networkingv1.NetworkPolicy) bool {
215-
for _, policy := range *policiesListAtNamespace {
220+
func checkServiceRisk(service *corev1.Service, namespace *string, policiesListAtNamespace []*networkingv1.NetworkPolicy) bool {
221+
for _, policy := range policiesListAtNamespace {
216222
// Skips deny all policies as they do not have any ingress rules
217223
for _, ingress := range policy.Spec.Ingress {
218224
// Check if there is an allow all ingress policy that matches labels the service is safe
@@ -314,38 +320,38 @@ func difference(slice1 *[]string, slice2 *[]string, slice3 *[]string) []string {
314320
return diff
315321
}
316322

317-
func printMigrationSummary(namespaces *corev1.NamespaceList, policiesByNamespace *map[string][]networkingv1.NetworkPolicy, servicesByNamespace *map[string][]corev1.Service) {
323+
func printMigrationSummary(namespaces *corev1.NamespaceList, policiesByNamespace map[string][]*networkingv1.NetworkPolicy, servicesByNamespace map[string][]*corev1.Service) {
318324
fmt.Println("Migration Summary:")
319325
fmt.Println("+------------------------------+-------------------------------+")
320326
fmt.Printf("%-30s | %-30s \n", "Breaking Change", "No Policy Changes Needed")
321327
fmt.Println("+------------------------------+-------------------------------+")
322328

323-
// Check the endports of the network policies
324-
ingressEndportNetworkPolicy, egressEndportNetworkPolicy := checkEndportNetworkPolicies(policiesByNamespace)
329+
// Get the endports of the network policies
330+
ingressEndportNetworkPolicy, egressEndportNetworkPolicy := getEndportNetworkPolicies(policiesByNamespace)
325331

326332
// Print the network policies with endport
327333
printPoliciesWithEndport(&ingressEndportNetworkPolicy, &egressEndportNetworkPolicy)
328334

329335
fmt.Println("+------------------------------+-------------------------------+")
330336

331-
// Check the cidr of the network policies
332-
ingressPoliciesWithCIDR, egressPoliciesWithCIDR := checkCIDRNetworkPolicies(policiesByNamespace)
337+
// Get the cidr of the network policies
338+
ingressPoliciesWithCIDR, egressPoliciesWithCIDR := getCIDRNetworkPolicies(policiesByNamespace)
333339

334340
// Print the network policies with CIDR
335341
printPoliciesWithCIDR(&ingressPoliciesWithCIDR, &egressPoliciesWithCIDR)
336342

337343
fmt.Println("+------------------------------+-------------------------------+")
338344

339-
// Check the egress of the network policies
340-
egressPolicies := checkForEgressPolicies(policiesByNamespace)
345+
// Get the egress of the network policies
346+
egressPolicies := getEgressPolicies(policiesByNamespace)
341347

342348
// Print the network policies with egress
343349
printEgressPolicies(&egressPolicies)
344350

345351
fmt.Println("+------------------------------+-------------------------------+")
346352

347-
// Check services that have externalTrafficPolicy!=Local
348-
unsafeServices, noSelectorServices := checkExternalTrafficPolicyServices(namespaces, servicesByNamespace, policiesByNamespace)
353+
// Get services that have externalTrafficPolicy!=Local
354+
unsafeServices, noSelectorServices := getExternalTrafficPolicyClusterServices(namespaces, servicesByNamespace, policiesByNamespace)
349355

350356
// Print the services that are at risk
351357
printUnsafeServices(&unsafeServices, &noSelectorServices)

0 commit comments

Comments
 (0)