-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serviceaccount): add service account input validation (#512)
- Loading branch information
Enda
authored
Mar 30, 2021
1 parent
2d19c7b
commit 199e5f3
Showing
6 changed files
with
261 additions
and
12 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package validation | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
|
||
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer" | ||
) | ||
|
||
const ( | ||
// name validation rules | ||
legalNameChars = "^[a-z]([-a-z0-9]*[a-z0-9])?$" | ||
maxNameLength = 50 | ||
minNameLength = 1 | ||
// description validation rules | ||
legalDescriptionChars = "^[a-z0-9\\s]*$" | ||
maxDescriptionLength = 255 | ||
) | ||
|
||
// ValidateName validates the name of the service account | ||
func ValidateName(val interface{}) error { | ||
name, ok := val.(string) | ||
if !ok { | ||
return errors.New(localizer.MustLocalize(&localizer.Config{ | ||
MessageID: "common.error.castError", | ||
TemplateData: map[string]interface{}{ | ||
"Value": val, | ||
"Type": "string", | ||
}, | ||
})) | ||
} | ||
|
||
if len(name) < minNameLength { | ||
return errors.New(localizer.MustLocalizeFromID("serviceAccount.common.validation.name.error.required")) | ||
} else if len(name) > maxNameLength { | ||
return errors.New(localizer.MustLocalize(&localizer.Config{ | ||
MessageID: "serviceAccount.common.validation.name.error.lengthError", | ||
TemplateData: map[string]interface{}{ | ||
"MaxNameLen": maxNameLength, | ||
}, | ||
})) | ||
} | ||
|
||
matched, _ := regexp.Match(legalNameChars, []byte(name)) | ||
|
||
if matched { | ||
return nil | ||
} | ||
|
||
return errors.New(localizer.MustLocalize(&localizer.Config{ | ||
MessageID: "serviceAccount.common.validation.name.error.invalidChars", | ||
TemplateData: map[string]interface{}{ | ||
"Name": name, | ||
}, | ||
})) | ||
} | ||
|
||
// ValidateDescription validates the service account description text | ||
func ValidateDescription(val interface{}) error { | ||
description, ok := val.(string) | ||
if !ok { | ||
return errors.New(localizer.MustLocalize(&localizer.Config{ | ||
MessageID: "common.error.castError", | ||
TemplateData: map[string]interface{}{ | ||
"Value": val, | ||
"Type": "string", | ||
}, | ||
})) | ||
} | ||
|
||
if description == "" { | ||
return nil | ||
} | ||
|
||
if len(description) > maxDescriptionLength { | ||
return errors.New(localizer.MustLocalize(&localizer.Config{ | ||
MessageID: "serviceAccount.common.validation.description.error.lengthError", | ||
TemplateData: map[string]interface{}{ | ||
"MaxNameLen": maxDescriptionLength, | ||
}, | ||
})) | ||
} | ||
|
||
matched, _ := regexp.Match(legalDescriptionChars, []byte(description)) | ||
|
||
if matched { | ||
return nil | ||
} | ||
|
||
return errors.New(localizer.MustLocalize(&localizer.Config{ | ||
MessageID: "serviceAccount.common.validation.description.error.invalidChars", | ||
TemplateData: map[string]interface{}{ | ||
"Description": description, | ||
}, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package validation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer" | ||
) | ||
|
||
func TestValidateName(t *testing.T) { | ||
_ = localizer.IncludeAssetsAndLoadMessageFiles() | ||
|
||
type args struct { | ||
val interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "fails when length is 0", | ||
args: args{""}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "passes when length is 1", | ||
args: args{"s"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "passes when length is 50 (max length)", | ||
args: args{"ssssssssssssssssssssssssssssssssssssssssssssssssss"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "fails when length exceeds max length", | ||
args: args{"sssssssssssssssssssssssssssssssssssssssssssssssssss"}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "passes on valid name", | ||
args: args{"svcacctone"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "passes on valid name with hyphens", | ||
args: args{"svc-acct-one"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "passes on valid name with digits", | ||
args: args{"svc-acct-1s"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "fails with capital letters", | ||
args: args{"Svc-acct-one"}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "fails number in first section", | ||
args: args{"1svc-acct-one"}, | ||
wantErr: true, | ||
}, | ||
} | ||
// nolint:scopelint | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := ValidateName(tt.args.val); (err != nil) != tt.wantErr { | ||
t.Errorf("ValidateName() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateDescription(t *testing.T) { | ||
_ = localizer.IncludeAssetsAndLoadMessageFiles() | ||
|
||
type args struct { | ||
val interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "passes when empty", | ||
args: args{""}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "passes on max length (255)", | ||
args: args{"trl1rmcyl6dp4xxqy0rwudhodbpjc4crja8ibf2yco6obalko6qor9n2a1wsqruolg0ewrndumw2xkezzuwg8pjo6ntsmi1cjw99hjcko4t2kjkxmaswzgk8ko75pcs4js0pzypuyjxxnld4dijxadzs8peioi6d5jjxxtfl9vicufmxuacvu7m8ycbwhsbiu9ipw5fxplf0ojs8bxd7hwt4rn4phbcdgivxdzprhyfjamkgjzytjz25cmqagtw"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "fails when exceeds max length", | ||
args: args{"trl1rmcyl6dp4xxqy0rwudhodbpjc4crja8ibf2yco6obalko6qor9n2a1wsqruolg0ewrndumw2xkezzuwg8pjo6ntsmi1cjw99hjcko4t2kjkxmaswzgk8ko75pcs4js0pzypuyjxxnld4dijxadzs8peioi6d5jjxxtfl9vicufmxuacvu7m8ycbwhsbiu9ipw5fxplf0ojs8bxd7hwt4rn4phbcdgivxdzprhyfjamkgjzytjz25cmqagtwa"}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "passes with spaces", | ||
args: args{"here is a description"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "fails with special character", | ||
args: args{"here is a description!"}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "fails with capital letters", | ||
args: args{"Hello"}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
// nolint:scopelint | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := ValidateDescription(tt.args.val); (err != nil) != tt.wantErr { | ||
t.Errorf("ValidateDescription() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |