Skip to content

Commit e0602b2

Browse files
author
Rahul Sharma
committed
limit fw-rule description to 100 chars
1 parent 9a56e2d commit e0602b2

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

cloud/linode/firewall/firewalls.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
const (
2222
maxFirewallRuleLabelLen = 32
23+
maxFirewallRuleDescLen = 100
2324
maxIPsPerFirewall = 255
2425
maxRulesPerFirewall = 25
2526
)
@@ -205,10 +206,16 @@ func processACL(fwcreateOpts *linodego.FirewallCreateOptions, aclType, label, sv
205206
ipv4chunks := chunkIPs(ipv4s)
206207
for i, chunk := range ipv4chunks {
207208
v4chunk := chunk
209+
desc := fmt.Sprintf("Rule %d, Created by linode-ccm: %s, for %s", i, label, svcName)
210+
if len(desc) > maxFirewallRuleDescLen {
211+
newDesc := desc[0:maxFirewallRuleDescLen-3] + "..."
212+
klog.Infof("Firewall rule description '%s' is too long. Stripping it to '%s'", desc, newDesc)
213+
desc = newDesc
214+
}
208215
fwcreateOpts.Rules.Inbound = append(fwcreateOpts.Rules.Inbound, linodego.FirewallRule{
209216
Action: aclType,
210217
Label: ruleLabel,
211-
Description: fmt.Sprintf("Rule %d, Created by linode-ccm: %s, for %s", i, label, svcName),
218+
Description: desc,
212219
Protocol: linodego.TCP, // Nodebalancers support only TCP.
213220
Ports: ports,
214221
Addresses: linodego.NetworkAddresses{IPv4: &v4chunk},
@@ -218,20 +225,32 @@ func processACL(fwcreateOpts *linodego.FirewallCreateOptions, aclType, label, sv
218225
ipv6chunks := chunkIPs(ipv6s)
219226
for i, chunk := range ipv6chunks {
220227
v6chunk := chunk
228+
desc := fmt.Sprintf("Rule %d, Created by linode-ccm: %s, for %s", i, label, svcName)
229+
if len(desc) > maxFirewallRuleDescLen {
230+
newDesc := desc[0:maxFirewallRuleDescLen-3] + "..."
231+
klog.Infof("Firewall rule description '%s' is too long. Stripping it to '%s'", desc, newDesc)
232+
desc = newDesc
233+
}
221234
fwcreateOpts.Rules.Inbound = append(fwcreateOpts.Rules.Inbound, linodego.FirewallRule{
222235
Action: aclType,
223236
Label: ruleLabel,
224-
Description: fmt.Sprintf("Rule %d, Created by linode-ccm: %s, for %s", i, label, svcName),
237+
Description: desc,
225238
Protocol: linodego.TCP, // Nodebalancers support only TCP.
226239
Ports: ports,
227240
Addresses: linodego.NetworkAddresses{IPv6: &v6chunk},
228241
})
229242
}
230243
} else {
244+
desc := fmt.Sprintf("Created by linode-ccm: %s, for %s", label, svcName)
245+
if len(desc) > maxFirewallRuleDescLen {
246+
newDesc := desc[0:maxFirewallRuleDescLen-3] + "..."
247+
klog.Infof("Firewall rule description '%s' is too long. Stripping it to '%s'", desc, newDesc)
248+
desc = newDesc
249+
}
231250
fwcreateOpts.Rules.Inbound = append(fwcreateOpts.Rules.Inbound, linodego.FirewallRule{
232251
Action: aclType,
233252
Label: ruleLabel,
234-
Description: fmt.Sprintf("Created by linode-ccm: %s, for %s", label, svcName),
253+
Description: desc,
235254
Protocol: linodego.TCP, // Nodebalancers support only TCP.
236255
Ports: ports,
237256
Addresses: ips,
@@ -453,7 +472,7 @@ func (l *LinodeClient) updateNodeBalancerFirewallWithACL(
453472
return nil
454473
}
455474

456-
fwCreateOpts, err := CreateFirewallOptsForSvc(service.Name, []string{""}, service)
475+
fwCreateOpts, err := CreateFirewallOptsForSvc(firewalls[0].Label, []string{""}, service)
457476
if err != nil {
458477
return err
459478
}

cloud/linode/loadbalancers_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ func TestCCMLoadBalancers(t *testing.T) {
258258
name: "Update Load Balancer - No Nodes",
259259
f: testUpdateLoadBalancerNoNodes,
260260
},
261+
{
262+
name: "Create Load Balancer - Very long Service name",
263+
f: testVeryLongServiceName,
264+
},
261265
}
262266

263267
for _, tc := range testCases {
@@ -816,6 +820,59 @@ func testUpdateLoadBalancerAddPortAnnotation(t *testing.T, client *linodego.Clie
816820
}
817821
}
818822

823+
func testVeryLongServiceName(t *testing.T, client *linodego.Client, _ *fakeAPI) {
824+
svc := &v1.Service{
825+
ObjectMeta: metav1.ObjectMeta{
826+
Name: strings.Repeat(randString(), 6),
827+
UID: "foobar123",
828+
Annotations: map[string]string{
829+
annotations.AnnLinodeCloudFirewallACL: `{
830+
"denyList": {
831+
"ipv4": ["2.2.2.2/32"],
832+
"ipv6": ["2001:db8::/128"]
833+
}
834+
}`,
835+
},
836+
},
837+
Spec: v1.ServiceSpec{
838+
Ports: []v1.ServicePort{
839+
{
840+
Name: randString(),
841+
Protocol: "TCP",
842+
Port: int32(80),
843+
NodePort: int32(30000),
844+
},
845+
},
846+
},
847+
}
848+
849+
nodes := []*v1.Node{
850+
{
851+
Status: v1.NodeStatus{
852+
Addresses: []v1.NodeAddress{
853+
{
854+
Type: v1.NodeInternalIP,
855+
Address: "127.0.0.1",
856+
},
857+
},
858+
},
859+
},
860+
}
861+
862+
lb := newLoadbalancers(client, "us-west").(*loadbalancers)
863+
fakeClientset := fake.NewSimpleClientset()
864+
lb.kubeClient = fakeClientset
865+
866+
defer func() {
867+
_ = lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc)
868+
}()
869+
870+
_, err := lb.EnsureLoadBalancer(context.TODO(), "linodelb", svc, nodes)
871+
if err != nil {
872+
t.Errorf("EnsureLoadBalancer returned an error: %s", err)
873+
}
874+
}
875+
819876
func testUpdateLoadBalancerAddTags(t *testing.T, client *linodego.Client, _ *fakeAPI) {
820877
svc := &v1.Service{
821878
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)