Skip to content

Commit 5803939

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 32a907c commit 5803939

9 files changed

+1234
-10
lines changed

api/v1beta1/types.go

Lines changed: 130 additions & 2 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
// DefaultRulesManagement determines the management policy for the default firewall rules
@@ -121,8 +233,24 @@ type FirewallSpec struct {
121233
// +optional
122234
// +kubebuilder:default:="Managed"
123235
DefaultRulesManagement RulesManagementPolicy `json:"defaultRulesManagement,omitempty"`
236+
237+
// FirewallRules is a list of additional firewall rules to create.
238+
// +optional
239+
FirewallRules []FirewallRule `json:"firewallRules,omitempty"`
124240
}
125241

242+
// FirewallRuleDirection is a string enum type for the direction of a firewall rule.
243+
// +kubebuilder:validation:Enum=INGRESS;EGRESS
244+
type FirewallRuleDirection string
245+
246+
const (
247+
// FirewallRuleDirectionIngress indicates that the firewall rule applies to incoming traffic.
248+
FirewallRuleDirectionIngress FirewallRuleDirection = "INGRESS"
249+
250+
// FirewallRuleDirectionEgress indicates that the firewall rule applies to outgoing traffic.
251+
FirewallRuleDirectionEgress FirewallRuleDirection = "EGRESS"
252+
)
253+
126254
// RulesManagementPolicy is a string enum type for managing firewall rules.
127255
// +kubebuilder:validation:Enum=Managed;Unmanaged
128256
type RulesManagementPolicy string
@@ -167,9 +295,9 @@ type NetworkSpec struct {
167295
// +optional
168296
HostProject *string `json:"hostProject,omitempty"`
169297

170-
// Firewall configuration.
298+
// FirewallSpec contains the firewall configuration associated with this network.
171299
// +optional
172-
Firewall FirewallSpec `json:"firewall,omitempty,omitzero"`
300+
FirewallSpec FirewallSpec `json:"firewall,omitempty,omitzero"`
173301

174302
// Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
175303
// 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: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"strconv"
23+
"strings"
2324
"time"
2425

2526
"github.com/pkg/errors"
@@ -111,7 +112,7 @@ func (s *ClusterScope) NetworkProject() string {
111112
// set to unmanaged or when the cluster will include a shared VPC, the default firewall
112113
// rule creation will be skipped.
113114
func (s *ClusterScope) SkipFirewallRuleCreation() bool {
114-
return (s.GCPCluster.Spec.Network.Firewall.DefaultRulesManagement == infrav1.RulesManagementUnmanaged) || s.IsSharedVpc()
115+
return (s.GCPCluster.Spec.Network.FirewallSpec.DefaultRulesManagement == infrav1.RulesManagementUnmanaged) || s.IsSharedVpc()
115116
}
116117

117118
// IsSharedVpc returns true If sharedVPC used else , returns false.
@@ -324,6 +325,40 @@ func (s *ClusterScope) FirewallRulesSpec() []*compute.Firewall {
324325
},
325326
}
326327

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

0 commit comments

Comments
 (0)