Skip to content

Commit

Permalink
add sources to the security rule struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nawazkh committed Dec 21, 2023
1 parent 1845be1 commit dee2344
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 7 deletions.
12 changes: 8 additions & 4 deletions api/v1beta1/azurecluster_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func validateSubnets(subnets Subnets, vnet VnetSpec, fldPath *field.Path) field.
rule,
fldPath.Index(i).Child("securityGroup").Child("securityRules").Index(i),
); err != nil {
allErrs = append(allErrs, err)
allErrs = append(allErrs, err...)

Check warning on line 242 in api/v1beta1/azurecluster_validation.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azurecluster_validation.go#L242

Added line #L242 was not covered by tests
}
}
allErrs = append(allErrs, validateSubnetCIDR(subnet.CIDRBlocks, vnet.CIDRBlocks, fldPath.Index(i).Child("cidrBlocks"))...)
Expand Down Expand Up @@ -356,12 +356,16 @@ func validateInternalLBIPAddress(address string, cidrs []string, fldPath *field.
}

// validateSecurityRule validates a SecurityRule.
func validateSecurityRule(rule SecurityRule, fldPath *field.Path) *field.Error {
func validateSecurityRule(rule SecurityRule, fldPath *field.Path) (allErrs field.ErrorList) {
if rule.Priority < minRulePriority || rule.Priority > maxRulePriority {
return field.Invalid(fldPath, rule.Priority, fmt.Sprintf("security rule priorities should be between %d and %d", minRulePriority, maxRulePriority))
allErrs = append(allErrs, field.Invalid(fldPath, rule.Priority, fmt.Sprintf("security rule priorities should be between %d and %d", minRulePriority, maxRulePriority)))
}

return nil
if rule.Source != nil && rule.Sources != nil {
allErrs = append(allErrs, field.Invalid(fldPath, rule.Source, "security rule cannot have both source and sources"))
}

return allErrs
}

func validateAPIServerLB(lb LoadBalancerSpec, old LoadBalancerSpec, cidrs []string, fldPath *field.Path) field.ErrorList {
Expand Down
33 changes: 31 additions & 2 deletions api/v1beta1/azurecluster_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,33 @@ func TestValidateSecurityRule(t *testing.T) {
},
wantErr: true,
},
{
name: "security rule - invalid sources priority",
validRule: SecurityRule{
Name: "allow_apiserver",
Description: "Allow K8s API Server",
Priority: 4000,
Source: ptr.To("*"),
Sources: []*string{
ptr.To("*"),
ptr.To("unknown"),
},
},
wantErr: true,
},
{
name: "security rule - valid sources",
validRule: SecurityRule{
Name: "allow_apiserver",
Description: "Allow K8s API Server",
Priority: 4000,
Sources: []*string{
ptr.To("*"),
ptr.To("unknown"),
},
},
wantErr: false,
},
}
for _, testCase := range tests {
testCase := testCase
Expand All @@ -674,9 +701,11 @@ func TestValidateSecurityRule(t *testing.T) {
field.NewPath("spec").Child("networkSpec").Child("subnets").Index(0).Child("securityGroup").Child("securityRules").Index(0),
)
if testCase.wantErr {
g.Expect(err).To(HaveOccurred())
g.Expect(err).NotTo(BeNil())
g.Expect(len(err)).To(Equal(1))
} else {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(err).To(BeNil())
g.Expect(len(err)).To(Equal(0))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/azureclustertemplate_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func validateSubnetTemplates(subnets SubnetTemplatesSpec, vnet VnetTemplateSpec,
rule,
fld.Index(i).Child("securityGroup").Child("securityGroup").Child("securityRules").Index(j),
); err != nil {
allErrs = append(allErrs, err)
allErrs = append(allErrs, err...)
}
}
allErrs = append(allErrs, validateSubnetCIDR(subnet.CIDRBlocks, vnet.CIDRBlocks, fld.Index(i).Child("cidrBlocks"))...)
Expand Down
2 changes: 2 additions & 0 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ type SecurityRule struct {
// Source specifies the CIDR or source IP range. Asterix '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used. If this is an ingress rule, specifies where network traffic originates from.
// +optional
Source *string `json:"source,omitempty"`
// Sources specifies The CIDR or source IP ranges.
Sources []*string `json:"sources,omitempty"`
// Destination is the destination address prefix. CIDR or destination IP range. Asterix '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used.
// +optional
Destination *string `json:"destination,omitempty"`
Expand Down
11 changes: 11 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions azure/converters/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func SecurityRuleToSDK(rule infrav1.SecurityRule) *armnetwork.SecurityRule {
Properties: &armnetwork.SecurityRulePropertiesFormat{
Description: ptr.To(rule.Description),
SourceAddressPrefix: rule.Source,
SourceAddressPrefixes: rule.Sources,

Check warning on line 32 in azure/converters/rules.go

View check run for this annotation

Codecov / codecov/patch

azure/converters/rules.go#L32

Added line #L32 was not covered by tests
SourcePortRange: rule.SourcePorts,
DestinationAddressPrefix: rule.Destination,
DestinationPortRange: rule.DestinationPorts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ spec:
Asterix '*' can also be used to match all
ports.
type: string
sources:
description: Sources specifies The CIDR or source
IP ranges.
items:
type: string
type: array
required:
- description
- direction
Expand Down Expand Up @@ -1127,6 +1133,12 @@ spec:
or range. Integer or range between 0 and 65535.
Asterix '*' can also be used to match all ports.
type: string
sources:
description: Sources specifies The CIDR or source
IP ranges.
items:
type: string
type: array
required:
- description
- direction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ spec:
0 and 65535. Asterix '*' can also
be used to match all ports.
type: string
sources:
description: Sources specifies The CIDR
or source IP ranges.
items:
type: string
type: array
required:
- description
- direction
Expand Down Expand Up @@ -774,6 +780,12 @@ spec:
0 and 65535. Asterix '*' can also be
used to match all ports.
type: string
sources:
description: Sources specifies The CIDR
or source IP ranges.
items:
type: string
type: array
required:
- description
- direction
Expand Down

0 comments on commit dee2344

Please sign in to comment.