-
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
BREAKING CHANGE: This change restricts to possible parameters for service account name and description, whereas before you could enter any value.
- Loading branch information
Enda Phelan
committed
Mar 29, 2021
1 parent
bb1d9b4
commit f4a3e21
Showing
7 changed files
with
132 additions
and
14 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
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,92 @@ | ||
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 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{}{ | ||
"Name": description, | ||
}, | ||
})) | ||
} |