diff --git a/metadata/environment.go b/metadata/environment.go index 8d66ea8e1069a50716c4d78d5910775659eeb103..8e23beb11140a9dd21db56abbdf0dbf7613d8171 100644 --- a/metadata/environment.go +++ b/metadata/environment.go @@ -20,7 +20,6 @@ import ( "fmt" "os" "path/filepath" - "regexp" "strings" log "github.com/sirupsen/logrus" @@ -77,7 +76,7 @@ func (m *manager) createEnvironment(name, uri string, extensionsLibData, k8sLibD // ensure environment name does not contain punctuation if !isValidName(name) { - return fmt.Errorf("Environment '%s' is not valid; must not contain punctuation or trailing slashes", name) + return fmt.Errorf("Environment name '%s' is not valid; must not contain punctuation, spaces, or begin or end with a slash", name) } log.Infof("Creating environment '%s' with uri '%s'", name, uri) @@ -249,7 +248,7 @@ func (m *manager) SetEnvironment(name string, desired *Environment) error { // ensure new environment name does not contain punctuation if !isValidName(desired.Name) { - return fmt.Errorf("Environment '%s' is not valid; must not contain punctuation or trailing slashes", desired.Name) + return fmt.Errorf("Environment name '%s' is not valid; must not contain punctuation, spaces, or begin or end with a slash", name) } // If the name has changed, the directory location needs to be moved to @@ -349,10 +348,3 @@ func (m *manager) environmentExists(name string) (bool, error) { return envExists, nil } - -// regex matcher to ensure environment name does not contain punctuation -func isValidName(envName string) bool { - hasPunctuation := regexp.MustCompile(`[,;.':!()?"{}\[\]*&%@$]+`).MatchString - hasTrailingSlashes := regexp.MustCompile(`/+$`).MatchString - return !hasPunctuation(envName) && !hasTrailingSlashes(envName) -} diff --git a/metadata/interface.go b/metadata/interface.go index c59c9b7777da02ff2931637293d952b7aad51d9c..4d13ed72206658064729d417add5fab3e7d89bf3 100644 --- a/metadata/interface.go +++ b/metadata/interface.go @@ -17,6 +17,8 @@ package metadata import ( "os" + "regexp" + "strings" "github.com/spf13/afero" ) @@ -85,6 +87,23 @@ func ParseClusterSpec(specFlag string) (ClusterSpec, error) { return parseClusterSpec(specFlag, appFS) } +// isValidName returns true if a name (e.g., for an environment) is valid. +// Broadly, this means it does not contain punctuation, whitespace, leading or +// trailing slashes. +func isValidName(name string) bool { + // No unicode whitespace is allowed. `Fields` doesn't handle trailing or + // leading whitespace. + fields := strings.Fields(name) + if len(fields) > 1 || len(strings.TrimSpace(name)) != len(name) { + return false + } + + hasPunctuation := regexp.MustCompile(`[\\,;':!()?"{}\[\]*&%@$]+`).MatchString + hasTrailingSlashes := regexp.MustCompile(`/+$`).MatchString + hasLeadingSlashes := regexp.MustCompile(`^/+`).MatchString + return len(name) != 0 && !hasPunctuation(name) && !hasTrailingSlashes(name) && !hasLeadingSlashes(name) +} + func init() { appFS = afero.NewOsFs() }