Skip to content

Commit c88ab85

Browse files
Prefix validation
1 parent d24b5f3 commit c88ab85

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

cloud/linode/cloud.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"strconv"
1010
"time"
11+
"regexp"
1112

1213
"github.com/spf13/pflag"
1314
"golang.org/x/exp/slices"
@@ -201,8 +202,9 @@ func newCloud() (cloudprovider.Interface, error) {
201202
return nil, fmt.Errorf("%s", msg)
202203
}
203204

204-
if len(Options.NodeBalancerPrefix) == 0 {
205-
msg := "nodebalancer-prefix cannot be empty string"
205+
validPrefix := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
206+
if !validPrefix.MatchString(Options.NodeBalancerPrefix) {
207+
msg := fmt.Sprintf("nodebalancer-prefix must be no empty and use only letters, numbers, underscores, and dashes: %s\n", Options.NodeBalancerPrefix)
206208
klog.Error(msg)
207209
return nil, fmt.Errorf("%s", msg)
208210
}

cloud/linode/cloud_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestNewCloudRouteControllerDisabled(t *testing.T) {
2020
t.Setenv("LINODE_API_TOKEN", "dummyapitoken")
2121
t.Setenv("LINODE_REGION", "us-east")
2222
t.Setenv("LINODE_REQUEST_TIMEOUT_SECONDS", "10")
23+
Options.NodeBalancerPrefix = "ccm"
2324

2425
t.Run("should not fail if vpc is empty and routecontroller is disabled", func(t *testing.T) {
2526
Options.VPCName = ""
@@ -45,6 +46,7 @@ func TestNewCloud(t *testing.T) {
4546
t.Setenv("LINODE_REQUEST_TIMEOUT_SECONDS", "10")
4647
t.Setenv("LINODE_ROUTES_CACHE_TTL_SECONDS", "60")
4748
Options.LinodeGoDebug = true
49+
Options.NodeBalancerPrefix = "ccm"
4850

4951
t.Run("should fail if api token is empty", func(t *testing.T) {
5052
t.Setenv("LINODE_API_TOKEN", "")
@@ -132,20 +134,11 @@ func TestNewCloud(t *testing.T) {
132134
rtEnabled := Options.EnableRouteController
133135
Options.EnableRouteController = false
134136
Options.LoadBalancerType = "nodebalancer"
135-
Options.VPCNames = "vpc-test1,vpc-test2"
136-
Options.NodeBalancerBackendIPv4SubnetName = "t1"
137-
vpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
138-
subnetIDs = map[string]int{"t1": 1, "t2": 2, "t3": 3}
139137
Options.NodeBalancerPrefix = strings.Repeat("a", 21)
140138
defer func() {
141139
Options.NodeBalancerPrefix = prefix
142140
Options.LoadBalancerType = ""
143141
Options.EnableRouteController = rtEnabled
144-
Options.VPCNames = ""
145-
Options.NodeBalancerBackendIPv4SubnetID = 0
146-
Options.NodeBalancerBackendIPv4SubnetName = ""
147-
vpcIDs = map[string]int{}
148-
subnetIDs = map[string]int{}
149142
}()
150143
_, err := newCloud()
151144
t.Log(err)
@@ -158,25 +151,33 @@ func TestNewCloud(t *testing.T) {
158151
rtEnabled := Options.EnableRouteController
159152
Options.EnableRouteController = false
160153
Options.LoadBalancerType = "nodebalancer"
161-
Options.VPCNames = "vpc-test1,vpc-test2"
162-
Options.NodeBalancerBackendIPv4SubnetName = "t1"
163-
vpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
164-
subnetIDs = map[string]int{"t1": 1, "t2": 2, "t3": 3}
165-
Options.NodeBalancerPrefix = strings.Repeat("a", 21)
154+
Options.NodeBalancerPrefix = ""
166155
defer func() {
167156
Options.NodeBalancerPrefix = prefix
168157
Options.LoadBalancerType = ""
169158
Options.EnableRouteController = rtEnabled
170-
Options.VPCNames = ""
171-
Options.NodeBalancerBackendIPv4SubnetID = 0
172-
Options.NodeBalancerBackendIPv4SubnetName = ""
173-
vpcIDs = map[string]int{}
174-
subnetIDs = map[string]int{}
175159
}()
176160
_, err := newCloud()
177161
t.Log(err)
178162
require.Error(t, err, "expected error if nodebalancer-prefix is empty")
179-
require.ErrorContains(t, err, "nodebalancer-prefix cannot be empty")
163+
require.ErrorContains(t, err, "nodebalancer-prefix must be no empty")
164+
})
165+
166+
t.Run("should fail if nodebalancer-prefix name validation", func(t *testing.T) {
167+
prefix := Options.NodeBalancerPrefix
168+
rtEnabled := Options.EnableRouteController
169+
Options.EnableRouteController = false
170+
Options.LoadBalancerType = "nodebalancer"
171+
Options.NodeBalancerPrefix = "\\+x"
172+
defer func() {
173+
Options.NodeBalancerPrefix = prefix
174+
Options.LoadBalancerType = ""
175+
Options.EnableRouteController = rtEnabled
176+
}()
177+
_, err := newCloud()
178+
t.Log(err)
179+
require.Error(t, err, "expected error if not validated nodebalancer-prefix")
180+
require.ErrorContains(t, err, "nodebalancer-prefix must be no empty and use only letters, numbers, underscores, and dashes")
180181
})
181182
}
182183

0 commit comments

Comments
 (0)