Skip to content

Commit 3ee1deb

Browse files
committed
updated port detection when policy just has a protocol and to flag all egress policies except allow all
1 parent bbe17e6 commit 3ee1deb

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ func getEgressPolicies(policiesByNamespace map[string][]*networkingv1.NetworkPol
150150
var egressPolicies []string
151151
for namespace, policies := range policiesByNamespace {
152152
for _, policy := range policies {
153+
for _, policyType := range policy.Spec.PolicyTypes {
154+
// If the policy is an egress type and has no egress field it is an deny all flag it
155+
if policyType == networkingv1.PolicyTypeEgress && len(policy.Spec.Egress) == 0 {
156+
egressPolicies = append(egressPolicies, fmt.Sprintf("%s/%s", namespace, policy.Name))
157+
break
158+
}
159+
}
153160
for _, egress := range policy.Spec.Egress {
154161
// If the policy has a egress field thats not an egress allow all flag it
155162
if len(egress.To) > 0 || len(egress.Ports) > 0 {
@@ -271,6 +278,11 @@ func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string
271278
}
272279

273280
func checkServiceTargetPortMatchPolicyPorts(servicePorts *[]corev1.ServicePort, policyPorts *[]networkingv1.NetworkPolicyPort) bool {
281+
// If the service has no ports then it is at risk
282+
if len(*servicePorts) == 0 {
283+
return false
284+
}
285+
274286
for _, servicePort := range *servicePorts {
275287
// If the target port is a string then it is a named port and service is at risk
276288
if servicePort.TargetPort.Type == intstr.String {
@@ -280,10 +292,17 @@ func checkServiceTargetPortMatchPolicyPorts(servicePorts *[]corev1.ServicePort,
280292
// Check if all the services target ports are in the policies ingress ports
281293
matchedserviceTargetPortToPolicyPort := false
282294
for _, policyPort := range *policyPorts {
283-
// Check if the policys port exists
284-
if policyPort.Port == nil {
295+
// Check if the policys port and protocol exists
296+
if policyPort.Port == nil && policyPort.Protocol == nil {
285297
return false
286298
}
299+
// If the policy only has a protocol check the protocol against the service
300+
if policyPort.Port == nil && policyPort.Protocol != nil {
301+
if string(servicePort.Protocol) == string(*policyPort.Protocol) {
302+
matchedserviceTargetPortToPolicyPort = true
303+
break
304+
}
305+
}
287306
// If the port is a string then it is a named port and service is at risk
288307
if policyPort.Port.Type == intstr.String {
289308
return false

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ func TestGetEgressPolicies(t *testing.T) {
572572
},
573573
},
574574
},
575-
expectedEgressPolicies: []string{},
575+
expectedEgressPolicies: []string{"namespace1/deny-all-egress-policy"},
576576
},
577577
{
578578
name: "Egress policy with To field",
@@ -1565,6 +1565,9 @@ func TestGetExternalTrafficPolicyClusterServices(t *testing.T) {
15651565
expectedUnsafeRiskServices: []string{},
15661566
expectedUnsafeNoSelectorServices: []string{},
15671567
},
1568+
// add ut where target port is nil
1569+
// add a ut where target port matches to portocol
1570+
15681571
// Scenarios where there are LoadBalancer or NodePort services with externalTrafficPolicy=Cluster and there are multiple policies
15691572

15701573
// Scenarios where there are LoadBalancer or NodePort services with externalTrafficPolicy=Cluster and there are multiple services

0 commit comments

Comments
 (0)