Skip to content

Commit

Permalink
Security group and Security group rules validation
Browse files Browse the repository at this point in the history
Add/Update Validations and validations tests to SecurityGroup and SecurityGroupRule
  • Loading branch information
itzikb-redhat committed Jan 12, 2025
1 parent 7625aa3 commit 136fc93
Show file tree
Hide file tree
Showing 13 changed files with 785 additions and 126 deletions.
83 changes: 68 additions & 15 deletions api/v1alpha1/securitygroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,92 @@ type RuleDirection string
// +kubebuilder:validation:MaxLength:=16
type Protocol string

const (
ProtocolANY Protocol = "any"
ProtocolAH Protocol = "ah"
ProtocolDCCP Protocol = "dccp"
ProtocolEGP Protocol = "egp"
ProtocolESP Protocol = "esp"
ProtocolGRE Protocol = "gre"
ProtocolICMP Protocol = "icmp"
ProtocolICMPV6 Protocol = "icmpv6"
ProtocolIGMP Protocol = "igmp"
ProtocolIPIP Protocol = "ipip"
ProtocolIPV6ENCAP Protocol = "ipv6-encap"
ProtocolIPV6FRAG Protocol = "ipv6-frag"
ProtocolIPV6ICMP Protocol = "ipv6-icmp"
ProtocolIPV6NONXT Protocol = "ipv6-nonxt"
ProtocolIPV6OPTS Protocol = "ipv6-opts"
ProtocolIPV6ROUTE Protocol = "ipv6-route"
ProtocolOSPF Protocol = "ospf"
ProtocolPGM Protocol = "pgm"
ProtocolRSVP Protocol = "rsvp"
ProtocolSCTP Protocol = "sctp"
ProtocolTCP Protocol = "tcp"
ProtocolUDP Protocol = "udp"
ProtocolUDPLITE Protocol = "udplite"
ProtocolVRRP Protocol = "vrrp"
)

// +kubebuilder:validation:Enum:=IPv4;IPv6
// +kubebuilder:validation:MinLength:=1
// +kubebuilder:validation:MaxLength:=16
// +kubebuilder:validation:MaxLength:=4
type Ethertype string

const (
EthertypeIPv4 Ethertype = "IPv4"
EthertypeIPv6 Ethertype = "IPv6"
)

// +kubebuilder:validation:Minimum:=1
// +kubebuilder:validation:Maximum:=65535
type PortNumber int32

type PortRangeSpec struct {
Min PortNumber `json:"min"`
Max PortNumber `json:"max"`
}
type PortRangeStatus struct {
Min int32 `json:"min"`
Max int32 `json:"max"`
}

// SecurityGroupRule defines a Security Group rule
// +kubebuilder:validation:MinProperties:=1
// +kubebuilder:validation:XValidation:rule="(!has(self.portRange)|| !(self.protocol == 'tcp'|| self.protocol == 'udp' || self.protocol == 'dccp' || self.protocol == 'sctp' || self.protocol == 'udplite') || (self.portRange.min <= self.portRange.max))",message="portRangeMax should be equal or greater than portRange.min"
// +kubebuilder:validation:XValidation:rule="!(self.protocol == 'icmp' || self.protocol == 'icmpv6') || !has(self.portRange)|| (self.portRange.min >= 0 && self.portRange.min <= 255)",message="When protocol is ICMP or ICMPv6 portRange.min should be between 0 and 255"
// +kubebuilder:validation:XValidation:rule="!(self.protocol == 'icmp' || self.protocol == 'icmpv6') || !has(self.portRange)|| (self.portRange.max >= 0 && self.portRange.max <= 255)",message="When protocol is ICMP or ICMPv6 portRange.max should be between 0 and 255"
// +kubebuilder:validation:XValidation:rule="!has(self.remoteIPPrefix) || (isCIDR(self.remoteIPPrefix) && cidr(self.remoteIPPrefix).ip().family() == 4 && self.ethertype == 'IPv4') || (isCIDR(self.remoteIPPrefix) && cidr(self.remoteIPPrefix).ip().family() == 6 && self.ethertype == 'IPv6')",message="remoteIPPrefix should be a valid CIDR and match the ethertype"
type SecurityGroupRule struct {
// Description of the existing resource
// +optional
Description *OpenStackDescription `json:"description,omitempty"`

// Direction represents the direction in which the security group rule
// is applied. Can be ingress or egress.
// +optional
Direction *RuleDirection `json:"direction,omitempty"`

// RemoteAddressGroupId (Not in gophercloud)

// RemoteIPPrefix
// RemoteIPPrefix is an IP address block. Should match the Ethertype (IPv4 or IPv6)
// +optional
RemoteIPPrefix *CIDR `json:"remoteIPPrefix,omitempty"`

// Protocol is the IP protocol can be represented by a string, an
// integer, or null
Protocol *Protocol `json:"protocol,omitempty"`
// Protocol is the IP protocol can be represented by a string or an
// integer represented as a string.
// +required
Protocol Protocol `json:"protocol"`

// EtherType must be IPv4 or IPv6, and addresses represented in CIDR
// Ethertype must be IPv4 or IPv6, and addresses represented in CIDR
// must match the ingress or egress rules.
Ethertype *Ethertype `json:"ethertype,omitempty"`

PortRangeMin *int32 `json:"portRangeMin,omitempty"`
PortRangeMax *int32 `json:"portRangeMax,omitempty"`
// +kubebuilder:validation:Required
Ethertype Ethertype `json:"ethertype"`
// If the protocol is [tcp, udp, dccp sctp,udplite] PortRange.Min must be less than
// or equal to the PortRange.Max attribute value.
// If the protocol is ICMP, this PortRamge.Min must be an ICMP code and PortRange.Max
// should be an ICMP type
// +kubebuilder:validation:XValidation:rule="self.min >=0 && self.max>=0",message=""
// +optional
PortRange *PortRangeSpec `json:"portRange,omitempty"`
}

type SecurityGroupRuleStatus struct {
Expand Down Expand Up @@ -87,9 +142,7 @@ type SecurityGroupRuleStatus struct {
// must match the ingress or egress rules.
Ethertype string `json:"ethertype,omitempty"`

PortRangeMin int `json:"portRangeMin,omitempty"`
PortRangeMax int `json:"portRangeMax,omitempty"`

PortRange *PortRangeStatus `json:"portRange,omitempty"`
// FIXME(mandre) This field is not yet returned by gophercloud
// BelongsToDefaultSG bool `json:"belongsToDefaultSG,omitempty"`

Expand Down
60 changes: 41 additions & 19 deletions api/v1alpha1/zz_generated.deepcopy.go

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

92 changes: 71 additions & 21 deletions cmd/models-schema/zz_generated.openapi.go

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

Loading

0 comments on commit 136fc93

Please sign in to comment.