Update profile name validation pattern. #8493

- prevents some incompatible characters from making their way into
profile names and causing DNS/hostnames issues
pull/8648/head
James Lucktaylor 2020-07-05 12:16:31 +01:00
parent 2660b92109
commit 2cf72f7ca8
No known key found for this signature in database
GPG Key ID: C705F79EC74F9953
1 changed files with 5 additions and 4 deletions

View File

@ -84,13 +84,13 @@ func PrimaryControlPlane(cc *ClusterConfig) (Node, error) {
return cp, nil
}
// ProfileNameValid checks if the profile name is container name friendly
// ProfileNameValid checks if the profile name is container name and DNS hostname/label friendly.
func ProfileNameValid(name string) bool {
// RestrictedNamePattern describes the characters allowed to represent a profile's name
const RestrictedNamePattern = `(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])`
// RestrictedNameChars collects the characters allowed to represent a name
const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
var validName = regexp.MustCompile(`^` + RestrictedNamePattern + `$`)
var validName = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)
return validName.MatchString(name)
}
@ -181,6 +181,7 @@ func SaveProfile(name string, cfg *ClusterConfig, miniHome ...string) error {
if err = os.Rename(tf.Name(), path); err != nil {
return err
}
return nil
}