Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.24] Fix how getSecurityRuleName() handles IPv6 addr prefix #3353

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/provider/azure_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ func (az *Cloud) getloadbalancerHAmodeRuleName(service *v1.Service) string {
}

func (az *Cloud) getSecurityRuleName(service *v1.Service, port v1.ServicePort, sourceAddrPrefix string) string {
safePrefix := strings.Replace(sourceAddrPrefix, "/", "_", -1)
safePrefix = strings.Replace(safePrefix, ":", ".", -1) // Consider IPv6 address
if useSharedSecurityRule(service) {
safePrefix := strings.Replace(sourceAddrPrefix, "/", "_", -1)
return fmt.Sprintf("shared-%s-%d-%s", port.Protocol, port.Port, safePrefix)
}
safePrefix := strings.Replace(sourceAddrPrefix, "/", "_", -1)
rulePrefix := az.getRulePrefix(service)
return fmt.Sprintf("%s-%s-%d-%s", rulePrefix, port.Protocol, port.Port, safePrefix)
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/provider/azure_standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2180,3 +2180,67 @@ func TestGetNodeVMSetName(t *testing.T) {
assert.Equal(t, tc.expectedVMSetName, vmSetName, tc.description)
}
}

func TestGetSecurityRuleName(t *testing.T) {
testcases := []struct {
desc string
svc *v1.Service
port v1.ServicePort
sourceAddrPrefix string
expectedRuleName string
}{
{
"IPv4",
&v1.Service{
ObjectMeta: meta.ObjectMeta{
UID: "257b9655-5137-4ad2-b091-ef3f07043ad3",
},
},
v1.ServicePort{
Protocol: v1.ProtocolTCP,
Port: 80,
},
"10.0.0.1/24",
"a257b965551374ad2b091ef3f07043ad-TCP-80-10.0.0.1_24",
},
{
"IPv4-shared",
&v1.Service{
ObjectMeta: meta.ObjectMeta{
UID: "257b9655-5137-4ad2-b091-ef3f07043ad3",
Annotations: map[string]string{consts.ServiceAnnotationSharedSecurityRule: "true"},
},
},
v1.ServicePort{
Protocol: v1.ProtocolTCP,
Port: 80,
},
"10.0.0.1/24",
"shared-TCP-80-10.0.0.1_24",
},
{
"IPv6",
&v1.Service{
ObjectMeta: meta.ObjectMeta{
UID: "257b9655-5137-4ad2-b091-ef3f07043ad3",
},
},
v1.ServicePort{
Protocol: v1.ProtocolTCP,
Port: 80,
},
"2001:0:0::1/64",
"a257b965551374ad2b091ef3f07043ad-TCP-80-2001.0.0..1_64",
},
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()
az := GetTestCloud(ctrl)
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
ruleName := az.getSecurityRuleName(tc.svc, tc.port, tc.sourceAddrPrefix)
assert.Equal(t, tc.expectedRuleName, ruleName)
})
}
}
3 changes: 2 additions & 1 deletion tests/e2e/network/network_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ var _ = Describe("Network security group", Label(utils.TestSuiteLabelNSG), func(
By("Checking if there is a LoadBalancerSourceRanges rule")
nsgs, err = tc.GetClusterSecurityGroups()
Expect(err).NotTo(HaveOccurred())
found = validateLoadBalancerSourceRangesRuleExists(nsgs, internalIP, allowCIDR, fmt.Sprintf("%s_%d", hostExecPodIP, mask))
ipRangesSuffix := strings.Replace(fmt.Sprintf("%s_%d", hostExecPodIP, mask), ":", ".", -1) // Handled in pkg/provider/getSecurityRuleName()
found = validateLoadBalancerSourceRangesRuleExists(nsgs, internalIP, allowCIDR, ipRangesSuffix)
Expect(found).To(BeTrue())

By("Checking if there is a deny_all rule")
Expand Down