Skip to content

Commit 226f310

Browse files
committed
Adding the ability for users to specify what firewall rules should be created.
** Currently the basic/default/required firewall rules are created by CAPG. Users should be given the ability to create the firewall rules associated with VPC that CAPG will create.
1 parent cebbdc3 commit 226f310

9 files changed

+1232
-11
lines changed

api/v1beta1/types.go

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,118 @@ type Network struct {
107107
APIInternalForwardingRule *string `json:"apiInternalForwardingRule,omitempty"`
108108
}
109109

110+
// FirewallDescriptor describes a GCP firewall rule.
111+
type FirewallDescriptor struct {
112+
// IPProtocol: The IP protocol to which this rule applies. The protocol type is
113+
// required when creating a firewall rule. This value can either be one of the
114+
// following well known protocol strings (tcp, udp, icmp, esp, ah, ipip, sctp)
115+
// or the IP protocol number.
116+
IPProtocol string `json:"IPProtocol,omitempty"`
117+
// Ports: An optional list of ports to which this rule applies. This field is
118+
// only applicable for the UDP or TCP protocol. Each entry must be either an
119+
// integer or a range. If not specified, this rule applies to connections
120+
// through any port. Example inputs include: ["22"], ["80","443"], and
121+
// ["12345-12349"].
122+
Ports []string `json:"ports,omitempty"`
123+
}
124+
125+
// FirewallRule describes a GCP firewall rule.
126+
type FirewallRule struct {
127+
// Allowed: The list of ALLOW rules specified by this firewall. Each rule
128+
// specifies a protocol and port-range tuple that describes a permitted
129+
// connection.
130+
Allowed []*FirewallDescriptor `json:"allowed,omitempty"`
131+
// Denied: The list of DENY rules specified by this firewall. Each rule
132+
// specifies a protocol and port-range tuple that describes a denied
133+
// connection.
134+
Denied []*FirewallDescriptor `json:"denied,omitempty"`
135+
// Description: An optional description of this resource. Provide this field
136+
// when you create the resource.
137+
Description *string `json:"description,omitempty"`
138+
// DestinationRanges: If destination ranges are specified, the firewall rule
139+
// applies only to traffic that has destination IP address in these ranges.
140+
// These ranges must be expressed in CIDR format. Both IPv4 and IPv6 are
141+
// supported.
142+
DestinationRanges []string `json:"destinationRanges,omitempty"`
143+
// Direction: Direction of traffic to which this firewall applies, either
144+
// `INGRESS` or `EGRESS`. The default is `INGRESS`. For `EGRESS` traffic, you
145+
// cannot specify the sourceTags fields.
146+
//
147+
// Possible values:
148+
// "EGRESS" - Indicates that firewall should apply to outgoing traffic.
149+
// "INGRESS" - Indicates that firewall should apply to incoming traffic.
150+
// +kubebuilder:validation:Enum=INGRESS;EGRESS
151+
// +kubebuilder:default=INGRESS
152+
// +optional
153+
Direction *FirewallRuleDirection `json:"direction,omitempty"`
154+
// Disabled: Denotes whether the firewall rule is disabled. When set to true,
155+
// the firewall rule is not enforced and the network behaves as if it did not
156+
// exist. If this is unspecified, the firewall rule will be enabled.
157+
Disabled *bool `json:"disabled,omitempty"`
158+
// Name: Name of the resource; provided by the client when the resource is
159+
// created. The name must be 1-63 characters long, and comply with RFC1035.
160+
// Specifically, the name must be 1-63 characters long and match the regular
161+
// expression `[a-z]([-a-z0-9]*[a-z0-9])?`. The first character must be a
162+
// lowercase letter, and all following characters (except for the last
163+
// character) must be a dash, lowercase letter, or digit. The last character
164+
// must be a lowercase letter or digit.
165+
Name *string `json:"name,omitempty"`
166+
// Priority: Priority for this rule. This is an integer between `0` and
167+
// `65535`, both inclusive. The default value is `1000`. Relative priorities
168+
// determine which rule takes effect if multiple rules apply. Lower values
169+
// indicate higher priority. For example, a rule with priority `0` has higher
170+
// precedence than a rule with priority `1`. DENY rules take precedence over
171+
// ALLOW rules if they have equal priority. Note that VPC networks have implied
172+
// rules with a priority of `65535`. To avoid conflicts with the implied rules,
173+
// use a priority number less than `65535`.
174+
Priority *int64 `json:"priority,omitempty"`
175+
// SourceRanges: If source ranges are specified, the firewall rule applies only
176+
// to traffic that has a source IP address in these ranges. These ranges must
177+
// be expressed in CIDR format. One or both of sourceRanges and sourceTags may
178+
// be set. If both fields are set, the rule applies to traffic that has a
179+
// source IP address within sourceRanges OR a source IP from a resource with a
180+
// matching tag listed in the sourceTags field. The connection does not need to
181+
// match both fields for the rule to apply. Both IPv4 and IPv6 are supported.
182+
SourceRanges []string `json:"sourceRanges,omitempty"`
183+
// SourceServiceAccounts: If source service accounts are specified, the
184+
// firewall rules apply only to traffic originating from an instance with a
185+
// service account in this list. Source service accounts cannot be used to
186+
// control traffic to an instance's external IP address because service
187+
// accounts are associated with an instance, not an IP address. sourceRanges
188+
// can be set at the same time as sourceServiceAccounts. If both are set, the
189+
// firewall applies to traffic that has a source IP address within the
190+
// sourceRanges OR a source IP that belongs to an instance with service account
191+
// listed in sourceServiceAccount. The connection does not need to match both
192+
// fields for the firewall to apply. sourceServiceAccounts cannot be used at
193+
// the same time as sourceTags or targetTags.
194+
SourceServiceAccounts []string `json:"sourceServiceAccounts,omitempty"`
195+
// SourceTags: If source tags are specified, the firewall rule applies only to
196+
// traffic with source IPs that match the primary network interfaces of VM
197+
// instances that have the tag and are in the same VPC network. Source tags
198+
// cannot be used to control traffic to an instance's external IP address, it
199+
// only applies to traffic between instances in the same virtual network.
200+
// Because tags are associated with instances, not IP addresses. One or both of
201+
// sourceRanges and sourceTags may be set. If both fields are set, the firewall
202+
// applies to traffic that has a source IP address within sourceRanges OR a
203+
// source IP from a resource with a matching tag listed in the sourceTags
204+
// field. The connection does not need to match both fields for the firewall to
205+
// apply.
206+
SourceTags []string `json:"sourceTags,omitempty"`
207+
// TargetServiceAccounts: A list of service accounts indicating sets of
208+
// instances located in the network that may make network connections as
209+
// specified in allowed[]. targetServiceAccounts cannot be used at the same
210+
// time as targetTags or sourceTags. If neither targetServiceAccounts nor
211+
// targetTags are specified, the firewall rule applies to all instances on the
212+
// specified network.
213+
TargetServiceAccounts []string `json:"targetServiceAccounts,omitempty"`
214+
// TargetTags: A list of tags that controls which instances the firewall rule
215+
// applies to. If targetTags are specified, then the firewall rule applies only
216+
// to instances in the VPC network that have one of those tags. If no
217+
// targetTags are specified, the firewall rule applies to all instances on the
218+
// specified network.
219+
TargetTags []string `json:"targetTags,omitempty"`
220+
}
221+
110222
// FirewallSpec contains configuration for the firewall.
111223
type FirewallSpec struct {
112224
// RulesManagement determines the management policy for firewall rules.
@@ -118,8 +230,24 @@ type FirewallSpec struct {
118230
// +optional
119231
// +kubebuilder:default:="Managed"
120232
RulesManagement RulesManagementPolicy `json:"rulesManagement,omitempty"`
233+
234+
// FirewallRules is a list of additional firewall rules to create.
235+
// +optional
236+
FirewallRules []FirewallRule `json:"firewallRules,omitempty"`
121237
}
122238

239+
// FirewallRuleDirection is a string enum type for the direction of a firewall rule.
240+
// +kubebuilder:validation:Enum=INGRESS;EGRESS
241+
type FirewallRuleDirection string
242+
243+
const (
244+
// FirewallRuleDirectionIngress indicates that the firewall rule applies to incoming traffic.
245+
FirewallRuleDirectionIngress FirewallRuleDirection = "INGRESS"
246+
247+
// FirewallRuleDirectionEgress indicates that the firewall rule applies to outgoing traffic.
248+
FirewallRuleDirectionEgress FirewallRuleDirection = "EGRESS"
249+
)
250+
123251
// RulesManagementPolicy is a string enum type for managing firewall rules.
124252
// +kubebuilder:validation:Enum=Managed;Unmanaged
125253
type RulesManagementPolicy string
@@ -134,7 +262,6 @@ const (
134262
RulesManagementUnmanaged RulesManagementPolicy = "Unmanaged"
135263
)
136264

137-
138265
// NetworkSpec encapsulates all things related to a GCP network.
139266
type NetworkSpec struct {
140267
// Name is the name of the network to be used.
@@ -165,9 +292,9 @@ type NetworkSpec struct {
165292
// +optional
166293
HostProject *string `json:"hostProject,omitempty"`
167294

168-
// Firewall configuration.
295+
// FirewallSpec contains the firewall configuration associated with this network.
169296
// +optional
170-
Firewall FirewallSpec `json:"firewall,omitempty"`
297+
FirewallSpec FirewallSpec `json:"firewall,omitempty"`
171298

172299
// Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
173300
// 1300 and the maximum value is 8896. The suggested value is 1500, which is

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 120 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/scope/cluster.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (s *ClusterScope) NetworkProject() string {
109109
// SkipFirewallRuleCreation returns whether the spec indicates that firewall rules
110110
// should be created or not.
111111
func (s *ClusterScope) SkipFirewallRuleCreation() bool {
112-
return (s.GCPCluster.Spec.Network.Firewall.RulesManagement == infrav1.RulesManagementUnmanaged) || s.IsSharedVpc()
112+
return (s.GCPCluster.Spec.Network.FirewallSpec.RulesManagement == infrav1.RulesManagementUnmanaged) || s.IsSharedVpc()
113113
}
114114

115115
// IsSharedVpc returns true If sharedVPC used else , returns false.
@@ -321,6 +321,40 @@ func (s *ClusterScope) FirewallRulesSpec() []*compute.Firewall {
321321
},
322322
}
323323

324+
// Add user defined firewall rules.
325+
for _, rule := range s.GCPCluster.Spec.Network.FirewallSpec.FirewallRules {
326+
allowed := []*compute.FirewallAllowed{}
327+
for _, a := range rule.Allowed {
328+
allowed = append(allowed, &compute.FirewallAllowed{
329+
IPProtocol: a.IPProtocol,
330+
Ports: a.Ports,
331+
})
332+
}
333+
334+
denied := []*compute.FirewallDenied{}
335+
for _, d := range rule.Denied {
336+
denied = append(denied, &compute.FirewallDenied{
337+
IPProtocol: d.IPProtocol,
338+
Ports: d.Ports,
339+
})
340+
}
341+
342+
direction := string(ptr.Deref(rule.Direction, infrav1.FirewallRuleDirectionIngress))
343+
firewallRules = append(firewallRules, &compute.Firewall{
344+
Name: ptr.Deref(rule.Name, fmt.Sprintf("%s-%s", s.Name(), direction)),
345+
Description: ptr.Deref(rule.Description, fmt.Sprintf("Firewall rule %s is created by Cluster API GCP Provider.", s.Name())),
346+
Network: s.NetworkLink(),
347+
Allowed: allowed,
348+
Denied: denied,
349+
Direction: direction,
350+
Priority: ptr.Deref(rule.Priority, int64(1000)),
351+
Disabled: ptr.Deref(rule.Disabled, false),
352+
SourceRanges: rule.SourceRanges,
353+
TargetTags: rule.TargetTags,
354+
SourceTags: rule.SourceTags,
355+
})
356+
}
357+
324358
return firewallRules
325359
}
326360

0 commit comments

Comments
 (0)