Skip to content

Commit c1b4d4a

Browse files
committed
updated to parse cidr to check for load balancer ip
1 parent 4312ba8 commit c1b4d4a

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

tools/azure-npm-to-cilium-validator/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ cd azure-container-networking/tools/azure-npm-to-cilium-validator
2626
Initialize the Go module and download dependencies:
2727

2828
```bash
29-
go mod tidy
30-
go mod vendor
29+
go mod tidy && go mod vendor
3130
```
3231

3332
## Running the Tool

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"log"
8+
"net"
89
"os"
910
"strings"
1011

@@ -139,9 +140,9 @@ func getCIDRNetworkPolicies(policiesByNamespace map[string][]*networkingv1.Netwo
139140
}
140141

141142
// Check for CIDR in ingress or egress rules
142-
func checkCIDRInPolicyRules(rules []networkingv1.NetworkPolicyPeer) bool {
143-
for _, rule := range rules {
144-
if rule.IPBlock != nil && rule.IPBlock.CIDR != "" {
143+
func checkCIDRInPolicyRules(to []networkingv1.NetworkPolicyPeer) bool {
144+
for _, toRule := range to {
145+
if toRule.IPBlock != nil && toRule.IPBlock.CIDR != "" {
145146
return true
146147
}
147148
}
@@ -239,8 +240,8 @@ func checkNoServiceRisk(service *corev1.Service, policiesListAtNamespace []*netw
239240
}
240241
}
241242
// Check if service is a loadbalancer and policy allows 168.63.129.16 and has no ports
242-
if service.Spec.Type == corev1.ServiceTypeLoadBalancer && len(ingress.From) > 0 && len(ingress.Ports) == 0 {
243-
if checkPolicyMatchServiceLabels(service.Spec.Selector, policy.Spec.PodSelector) && checkAllowsLoadBalancerIngressCIDR(ingress.From) {
243+
if service.Spec.Type == corev1.ServiceTypeLoadBalancer && len(ingress.Ports) == 0 {
244+
if checkPolicyMatchServiceLabels(service.Spec.Selector, policy.Spec.PodSelector) && checkAllowsLoadBalancerIP(ingress.From) {
244245
return true
245246
}
246247
}
@@ -349,10 +350,17 @@ func checkServiceTargetPortMatchPolicyPorts(servicePorts []corev1.ServicePort, p
349350
return true
350351
}
351352

352-
func checkAllowsLoadBalancerIngressCIDR(from []networkingv1.NetworkPolicyPeer) bool {
353-
for _, peer := range from {
354-
if peer.IPBlock != nil && peer.IPBlock.CIDR == "168.63.129.16/32" {
355-
return true
353+
func checkAllowsLoadBalancerIP(from []networkingv1.NetworkPolicyPeer) bool {
354+
loadBalancerIP := net.ParseIP("168.63.129.16")
355+
for _, fromRule := range from {
356+
if fromRule.IPBlock != nil && fromRule.IPBlock.CIDR != "" {
357+
_, cidr, err := net.ParseCIDR(fromRule.IPBlock.CIDR)
358+
if err != nil {
359+
continue
360+
}
361+
if cidr.Contains(loadBalancerIP) {
362+
return true
363+
}
356364
}
357365
}
358366
return false

0 commit comments

Comments
 (0)