Skip to content

Commit f46f886

Browse files
committed
Add validation for namespace in nonkube commands
1 parent 002106a commit f46f886

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+528
-60
lines changed

internal/cmd/skupper/connector/nonkube/connector_create.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) error {
5454
numberValidator := validator.NewNumberValidator()
5555
connectorTypeValidator := validator.NewOptionValidator(common.ConnectorTypes)
5656
hostStringValidator := validator.NewHostStringValidator()
57+
namespaceStringValidator := validator.NamespaceStringValidator()
58+
59+
if cmd.namespace != "" {
60+
ok, err := namespaceStringValidator.Evaluate(cmd.namespace)
61+
if !ok {
62+
validationErrors = append(validationErrors, fmt.Errorf("namespace is not valid: %s", err))
63+
}
64+
}
5765

5866
// Validate arguments name and port
5967
if len(args) < 2 {

internal/cmd/skupper/connector/nonkube/connector_create_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
func TestNonKubeCmdConnectorCreate_ValidateInput(t *testing.T) {
1616
type test struct {
1717
name string
18+
namespace string
1819
args []string
1920
k8sObjects []runtime.Object
2021
skupperObjects []runtime.Object
@@ -26,54 +27,63 @@ func TestNonKubeCmdConnectorCreate_ValidateInput(t *testing.T) {
2627
testTable := []test{
2728
{
2829
name: "Connector name and port are not specified",
30+
namespace: "test",
2931
args: []string{},
3032
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
3133
expectedError: "connector name and port must be configured",
3234
},
3335
{
3436
name: "Connector name is not valid",
37+
namespace: "test",
3538
args: []string{"my new Connector", "8080"},
3639
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
3740
expectedError: "connector name is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])*(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])*)*$",
3841
},
3942
{
4043
name: "Connector name is empty",
44+
namespace: "test",
4145
args: []string{"", "1234"},
4246
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
4347
expectedError: "connector name must not be empty",
4448
},
4549
{
4650
name: "connector port empty",
51+
namespace: "test",
4752
args: []string{"my-name-port-empty", ""},
4853
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
4954
expectedError: "connector port must not be empty",
5055
},
5156
{
5257
name: "port is not valid",
58+
namespace: "test",
5359
args: []string{"my-connector-port", "abcd"},
5460
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
5561
expectedError: "connector port is not valid: strconv.Atoi: parsing \"abcd\": invalid syntax",
5662
},
5763
{
5864
name: "port not positive",
65+
namespace: "test",
5966
args: []string{"my-port-positive", "-45"},
6067
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
6168
expectedError: "connector port is not valid: value is not positive",
6269
},
6370
{
6471
name: "more than two arguments was specified",
72+
namespace: "test",
6573
args: []string{"my", "Connector", "test"},
6674
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
6775
expectedError: "only two arguments are allowed for this command",
6876
},
6977
{
7078
name: "type is not valid",
79+
namespace: "test",
7180
args: []string{"my-connector", "8080"},
7281
flags: &common.CommandConnectorCreateFlags{ConnectorType: "not-valid", Host: "1.2.3.4"},
7382
expectedError: "connector type is not valid: value not-valid not allowed. It should be one of this options: [tcp]",
7483
},
7584
{
7685
name: "routing key is not valid",
86+
namespace: "test",
7787
args: []string{"my-connector-rk", "8080"},
7888
flags: &common.CommandConnectorCreateFlags{RoutingKey: "not-valid$", Host: "1.2.3.4"},
7989
expectedError: "routing key is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])*(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])*)*$",
@@ -96,7 +106,6 @@ func TestNonKubeCmdConnectorCreate_ValidateInput(t *testing.T) {
96106
flags: &common.CommandConnectorCreateFlags{},
97107
},
98108
{
99-
100109
name: "kubernetes flags are not valid on this platform",
101110
args: []string{"my-connector", "8080"},
102111
flags: &common.CommandConnectorCreateFlags{Host: "1.2.3.4"},
@@ -106,8 +115,16 @@ func TestNonKubeCmdConnectorCreate_ValidateInput(t *testing.T) {
106115
},
107116
},
108117
{
109-
name: "flags all valid",
110-
args: []string{"my-connector-flags", "8080"},
118+
name: "invalid namespace",
119+
namespace: "TestInvalid",
120+
args: []string{"my-connector-invalid", "8080"},
121+
flags: &common.CommandConnectorCreateFlags{},
122+
expectedError: "namespace is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
123+
},
124+
{
125+
name: "flags all valid",
126+
namespace: "test",
127+
args: []string{"my-connector-flags", "8080"},
111128
flags: &common.CommandConnectorCreateFlags{
112129
RoutingKey: "routingkeyname",
113130
TlsCredentials: "secretname",
@@ -126,6 +143,7 @@ func TestNonKubeCmdConnectorCreate_ValidateInput(t *testing.T) {
126143
if test.flags != nil {
127144
command.Flags = test.flags
128145
}
146+
command.namespace = test.namespace
129147

130148
if test.cobraGenericFlags != nil && len(test.cobraGenericFlags) > 0 {
131149
for name, value := range test.cobraGenericFlags {

internal/cmd/skupper/connector/nonkube/connector_delete.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (cmd *CmdConnectorDelete) ValidateInput(args []string) error {
3434
var validationErrors []error
3535
opts := fs.GetOptions{RuntimeFirst: false, LogWarning: false}
3636
resourceStringValidator := validator.NewResourceStringValidator()
37+
namespaceStringValidator := validator.NamespaceStringValidator()
3738

3839
if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameContext) != nil && cmd.CobraCmd.Flag(common.FlagNameContext).Value.String() != "" {
3940
fmt.Println("Warning: --context flag is not supported on this platform")
@@ -43,6 +44,13 @@ func (cmd *CmdConnectorDelete) ValidateInput(args []string) error {
4344
fmt.Println("Warning: --kubeconfig flag is not supported on this platform")
4445
}
4546

47+
if cmd.namespace != "" {
48+
ok, err := namespaceStringValidator.Evaluate(cmd.namespace)
49+
if !ok {
50+
validationErrors = append(validationErrors, fmt.Errorf("namespace is not valid: %s", err))
51+
}
52+
}
53+
4654
// Validate arguments name
4755
if len(args) < 1 {
4856
validationErrors = append(validationErrors, fmt.Errorf("connector name must be configured"))

internal/cmd/skupper/connector/nonkube/connector_delete_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
func TestCmdConnectorDelete_ValidateInput(t *testing.T) {
2020
type test struct {
2121
name string
22+
namespace string
2223
args []string
2324
flags *common.CommandConnectorDeleteFlags
2425
cobraGenericFlags map[string]string
@@ -36,12 +37,14 @@ func TestCmdConnectorDelete_ValidateInput(t *testing.T) {
3637
testTable := []test{
3738
{
3839
name: "connector name is not specified",
40+
namespace: "test",
3941
args: []string{},
4042
flags: &common.CommandConnectorDeleteFlags{},
4143
expectedError: "connector name must be configured",
4244
},
4345
{
4446
name: "connector name is nil",
47+
namespace: "test",
4548
args: []string{""},
4649
flags: &common.CommandConnectorDeleteFlags{},
4750
expectedError: "connector name must not be empty",
@@ -74,6 +77,13 @@ func TestCmdConnectorDelete_ValidateInput(t *testing.T) {
7477
common.FlagNameKubeconfig: "test",
7578
},
7679
},
80+
{
81+
name: "invalid namespace",
82+
namespace: "Test5",
83+
args: []string{"my-connector"},
84+
flags: &common.CommandConnectorDeleteFlags{},
85+
expectedError: "namespace is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
86+
},
7787
}
7888

7989
//Add a temp file so connector exists for update tests will pass
@@ -105,6 +115,7 @@ func TestCmdConnectorDelete_ValidateInput(t *testing.T) {
105115
if test.flags != nil {
106116
command.Flags = test.flags
107117
}
118+
command.namespace = test.namespace
108119

109120
if test.cobraGenericFlags != nil && len(test.cobraGenericFlags) > 0 {
110121
for name, value := range test.cobraGenericFlags {

internal/cmd/skupper/connector/nonkube/connector_generate.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func (cmd *CmdConnectorGenerate) ValidateInput(args []string) error {
5757
connectorTypeValidator := validator.NewOptionValidator(common.ConnectorTypes)
5858
outputTypeValidator := validator.NewOptionValidator(common.OutputTypes)
5959
hostStringValidator := validator.NewHostStringValidator()
60+
namespaceStringValidator := validator.NamespaceStringValidator()
61+
62+
if cmd.namespace != "" {
63+
ok, err := namespaceStringValidator.Evaluate(cmd.namespace)
64+
if !ok {
65+
validationErrors = append(validationErrors, fmt.Errorf("namespace is not valid: %s", err))
66+
}
67+
}
6068

6169
// Validate arguments name and port
6270
if len(args) < 2 {

internal/cmd/skupper/connector/nonkube/connector_generate_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
func TestNonKubeCmdConnectorGenerate_ValidateInput(t *testing.T) {
1616
type test struct {
1717
name string
18+
namespace string
1819
args []string
1920
k8sObjects []runtime.Object
2021
skupperObjects []runtime.Object
@@ -26,42 +27,49 @@ func TestNonKubeCmdConnectorGenerate_ValidateInput(t *testing.T) {
2627
testTable := []test{
2728
{
2829
name: "Connector name and port are not specified",
30+
namespace: "test",
2931
args: []string{},
3032
flags: &common.CommandConnectorGenerateFlags{Host: "1.2.3.4"},
3133
expectedError: "connector name and port must be configured",
3234
},
3335
{
3436
name: "Connector name is not valid",
37+
namespace: "test",
3538
args: []string{"my new Connector", "8080"},
3639
flags: &common.CommandConnectorGenerateFlags{Host: "1.2.3.4"},
3740
expectedError: "connector name is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])*(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])*)*$",
3841
},
3942
{
4043
name: "Connector name is empty",
44+
namespace: "test",
4145
args: []string{"", "1234"},
4246
flags: &common.CommandConnectorGenerateFlags{Host: "1.2.3.4"},
4347
expectedError: "connector name must not be empty",
4448
},
4549
{
4650
name: "connector port empty",
51+
namespace: "test",
4752
args: []string{"my-name-port-empty", ""},
4853
flags: &common.CommandConnectorGenerateFlags{Host: "1.2.3.4"},
4954
expectedError: "connector port must not be empty",
5055
},
5156
{
5257
name: "port is not valid",
58+
namespace: "test",
5359
args: []string{"my-connector-port", "abcd"},
5460
flags: &common.CommandConnectorGenerateFlags{Host: "1.2.3.4"},
5561
expectedError: "connector port is not valid: strconv.Atoi: parsing \"abcd\": invalid syntax",
5662
},
5763
{
5864
name: "port not positive",
65+
namespace: "test",
5966
args: []string{"my-port-positive", "-45"},
6067
flags: &common.CommandConnectorGenerateFlags{Host: "1.2.3.4"},
6168
expectedError: "connector port is not valid: value is not positive",
6269
},
6370
{
6471
name: "more than two arguments was specified",
72+
namespace: "test",
6573
args: []string{"my", "Connector", "test"},
6674
flags: &common.CommandConnectorGenerateFlags{Host: "1.2.3.4"},
6775
expectedError: "only two arguments are allowed for this command",
@@ -91,12 +99,14 @@ func TestNonKubeCmdConnectorGenerate_ValidateInput(t *testing.T) {
9199
},
92100
{
93101
name: "tlsCredentials is not valid",
102+
namespace: "test",
94103
args: []string{"my-connector-tls", "8080"},
95104
flags: &common.CommandConnectorGenerateFlags{TlsCredentials: "not-valid$", Host: "1.2.3.4"},
96105
expectedError: "tlsCredentials is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])*(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])*)*$",
97106
},
98107
{
99108
name: "output format is not valid",
109+
namespace: "test",
100110
args: []string{"my-connector", "8080"},
101111
flags: &common.CommandConnectorGenerateFlags{Output: "not-valid", Host: "1.2.3.4"},
102112
expectedError: "output type is not valid: value not-valid not allowed. It should be one of this options: [json yaml]",
@@ -110,6 +120,13 @@ func TestNonKubeCmdConnectorGenerate_ValidateInput(t *testing.T) {
110120
common.FlagNameKubeconfig: "test",
111121
},
112122
},
123+
{
124+
name: "invalid namespace",
125+
namespace: "TestInvalid",
126+
args: []string{"my-connector", "8080"},
127+
flags: &common.CommandConnectorGenerateFlags{},
128+
expectedError: "namespace is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
129+
},
113130
{
114131
name: "flags all valid",
115132
args: []string{"my-connector-flags", "8080"},
@@ -131,6 +148,7 @@ func TestNonKubeCmdConnectorGenerate_ValidateInput(t *testing.T) {
131148
if test.flags != nil {
132149
command.Flags = test.flags
133150
}
151+
command.namespace = test.namespace
134152

135153
if test.cobraGenericFlags != nil && len(test.cobraGenericFlags) > 0 {
136154
for name, value := range test.cobraGenericFlags {

internal/cmd/skupper/connector/nonkube/connector_update.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (cmd *CmdConnectorUpdate) ValidateInput(args []string) error {
4848
numberValidator := validator.NewNumberValidator()
4949
connectorTypeValidator := validator.NewOptionValidator(common.ConnectorTypes)
5050
hostStringValidator := validator.NewHostStringValidator()
51+
namespaceStringValidator := validator.NamespaceStringValidator()
5152

5253
if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameContext) != nil && cmd.CobraCmd.Flag(common.FlagNameContext).Value.String() != "" {
5354
fmt.Println("Warning: --context flag is not supported on this platform")
@@ -57,6 +58,13 @@ func (cmd *CmdConnectorUpdate) ValidateInput(args []string) error {
5758
fmt.Println("Warning: --kubeconfig flag is not supported on this platform")
5859
}
5960

61+
if cmd.namespace != "" {
62+
ok, err := namespaceStringValidator.Evaluate(cmd.namespace)
63+
if !ok {
64+
validationErrors = append(validationErrors, fmt.Errorf("namespace is not valid: %s", err))
65+
}
66+
}
67+
6068
// Validate arguments name
6169
if len(args) < 1 {
6270
validationErrors = append(validationErrors, fmt.Errorf("connector name must be configured"))

internal/cmd/skupper/connector/nonkube/connector_update_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
func TestCmdConnectorUpdate_ValidateInput(t *testing.T) {
1919
type test struct {
2020
name string
21+
namespace string
2122
args []string
2223
flags *common.CommandConnectorUpdateFlags
2324
cobraGenericFlags map[string]string
@@ -35,12 +36,14 @@ func TestCmdConnectorUpdate_ValidateInput(t *testing.T) {
3536
testTable := []test{
3637
{
3738
name: "connector is not updated because get connector returned error",
39+
namespace: "test",
3840
args: []string{"no-connector"},
3941
flags: &common.CommandConnectorUpdateFlags{Host: "1.2.3.4"},
4042
expectedError: "connector no-connector must exist in namespace test to be updated",
4143
},
4244
{
4345
name: "connector name is not specified",
46+
namespace: "test",
4447
args: []string{},
4548
flags: &common.CommandConnectorUpdateFlags{Host: "localhost"},
4649
expectedError: "connector name must be configured",
@@ -104,8 +107,16 @@ func TestCmdConnectorUpdate_ValidateInput(t *testing.T) {
104107
expectedError: "",
105108
},
106109
{
107-
name: "flags all valid",
108-
args: []string{"my-connector"},
110+
name: "invalid namespace",
111+
namespace: "TestInvalid",
112+
args: []string{"my-connector"},
113+
flags: &common.CommandConnectorUpdateFlags{Host: "localhost"},
114+
expectedError: "namespace is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
115+
},
116+
{
117+
name: "flags all valid",
118+
namespace: "test",
119+
args: []string{"my-connector"},
109120
flags: &common.CommandConnectorUpdateFlags{
110121
RoutingKey: "routingkeyname",
111122
TlsCredentials: "secretname",
@@ -151,6 +162,7 @@ func TestCmdConnectorUpdate_ValidateInput(t *testing.T) {
151162
if test.flags != nil {
152163
command.Flags = test.flags
153164
}
165+
command.namespace = test.namespace
154166

155167
if test.cobraGenericFlags != nil && len(test.cobraGenericFlags) > 0 {
156168
for name, value := range test.cobraGenericFlags {

internal/cmd/skupper/link/nonkube/link_delete.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ func (cmd *CmdLinkDelete) NewClient(cobraCommand *cobra.Command, args []string)
3333
func (cmd *CmdLinkDelete) ValidateInput(args []string) error {
3434
var validationErrors []error
3535
resourceStringValidator := validator.NewResourceStringValidator()
36+
namespaceStringValidator := validator.NamespaceStringValidator()
37+
38+
if cmd.namespace != "" {
39+
ok, err := namespaceStringValidator.Evaluate(cmd.namespace)
40+
if !ok {
41+
validationErrors = append(validationErrors, fmt.Errorf("namespace is not valid: %s", err))
42+
}
43+
}
3644

3745
// Validate arguments name if specified
3846
if len(args) > 1 {

0 commit comments

Comments
 (0)