Skip to content

Commit e3f7cd6

Browse files
committed
Fix gcpfirewalls cr creation to properly set Allow - tcp:all instead of Allow - all
While testing gcpfirewalls cr creation inside ingress-gce we encountered an issue when Allow - TPC:all rule is requested. The former implementation would create a cr with Allow:all instead, which is a violation of the specification: https://cloud.google.com/kubernetes-engine/docs/concepts/firewall-rules#ingress-fws
1 parent 11e3f7d commit e3f7cd6

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/firewalls/firewalls_l7_cr.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ func NewFirewallCR(name string, ports, srcRanges, dstRanges []string, enforced b
169169
}
170170
protocolPorts = append(protocolPorts, protocolPort)
171171
}
172+
173+
// TCP:all is the default if the ports' list is empty.
174+
if len(protocolPorts) == 0 {
175+
protocolPort := gcpfirewallv1.ProtocolPort{
176+
Protocol: gcpfirewallv1.ProtocolTCP,
177+
}
178+
protocolPorts = append(protocolPorts, protocolPort)
179+
}
180+
172181
firewallCR.Spec.Ports = protocolPorts
173182

174183
var src_cidrs, dst_cidrs []gcpfirewallv1.CIDR

pkg/firewalls/firewalls_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,30 @@ func TestFirewallPoolSyncSrcRanges(t *testing.T) {
131131
func TestFirewallPoolSyncPorts(t *testing.T) {
132132
fwp := NewFakeFirewallsProvider(false, false)
133133
fwClient := firewallclient.NewSimpleClientset()
134-
fp := NewFirewallPool(fwp, defaultNamer, srcRanges, portRanges(), klog.TODO())
135-
fcrp := NewFirewallCRPool(fwClient, fwp, defaultNamer, srcRanges, portRanges(), true, klog.TODO())
136134
nodes := []string{"node-a", "node-b", "node-c"}
135+
emptyPortRanges := make([]string, 0)
136+
137+
// Verify empty ports' list
138+
fp := NewFirewallPool(fwp, defaultNamer, srcRanges, emptyPortRanges, klog.TODO())
139+
fcrp := NewFirewallCRPool(fwClient, fwp, defaultNamer, srcRanges, emptyPortRanges, true, klog.TODO())
137140

138141
if err := fp.Sync(nodes, nil, nil, true); err != nil {
139142
t.Fatal(err)
140143
}
144+
verifyFirewallRule(fwp, ruleName, nodes, srcRanges, emptyPortRanges, t)
145+
146+
if err := fcrp.Sync(nodes, nil, nil, true); err != nil {
147+
t.Fatal(err)
148+
}
149+
verifyFirewallCR(fwClient, ruleName, srcRanges, emptyPortRanges, true, t)
150+
151+
// Verify a preset ports' list
152+
fp = NewFirewallPool(fwp, defaultNamer, srcRanges, portRanges(), klog.TODO())
153+
fcrp = NewFirewallCRPool(fwClient, fwp, defaultNamer, srcRanges, portRanges(), true, klog.TODO())
154+
155+
if err := fp.Sync(nodes, nil, nil, true); err != nil {
156+
t.Errorf("unexpected err when syncing firewall, err: %v", err)
157+
}
141158
verifyFirewallRule(fwp, ruleName, nodes, srcRanges, portRanges(), t)
142159

143160
if err := fcrp.Sync(nodes, nil, nil, true); err != nil {
@@ -399,14 +416,20 @@ func verifyFirewallCR(firewallclient *firewallclient.Clientset, ruleName string,
399416
ports := sets.NewString(expectedPorts...)
400417
srcranges := sets.NewString(sourceRanges...)
401418

419+
// Empty ports' list would mean that all protocols are permitted
420+
// (not only TCP)
421+
if len(actualFW.Spec.Ports) == 0 {
422+
t.Errorf("Empty list of allowed protocols is not permited")
423+
}
424+
402425
actualPorts := sets.NewString()
403426
for _, protocolports := range actualFW.Spec.Ports {
404427
if protocolports.Protocol != "TCP" {
405428
t.Errorf("Protocol isn't TCP")
406429
}
407430
if protocolports.EndPort != nil {
408431
actualPorts.Insert(fmt.Sprintf("%d-%d", *protocolports.StartPort, *protocolports.EndPort))
409-
} else {
432+
} else if protocolports.StartPort != nil {
410433
actualPorts.Insert(fmt.Sprintf("%d", *protocolports.StartPort))
411434
}
412435

0 commit comments

Comments
 (0)