-
Notifications
You must be signed in to change notification settings - Fork 66
fix(serviceaccount): add service account input validation #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should change
cobra.ExactArgs(0)tocobra.NoArgsthroughout the app in a follow up PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. thanks!