From fba5f9a2d3756d976dec642ca2248a50d1ea4ccb Mon Sep 17 00:00:00 2001 From: Pablo Caderno Date: Mon, 13 Apr 2020 10:19:26 +1000 Subject: [PATCH] Added validation for profile name It must be container friendly --- cmd/minikube/cmd/config/profile.go | 5 +++++ cmd/minikube/cmd/start.go | 4 ++++ pkg/minikube/config/profile.go | 11 +++++++++++ pkg/minikube/config/profile_test.go | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/cmd/minikube/cmd/config/profile.go b/cmd/minikube/cmd/config/profile.go index 46afa5237..182b48060 100644 --- a/cmd/minikube/cmd/config/profile.go +++ b/cmd/minikube/cmd/config/profile.go @@ -44,6 +44,11 @@ var ProfileCmd = &cobra.Command{ } profile := args[0] + // Check whether the profile name is container friendly + if !config.ProfileNameValid(profile) { + out.WarningT("Profile name '{{.profilename}}' is not valid", out.V{"profilename": profile}) + exit.UsageT("Only alphanumeric, dots, underscores and dashes '-' are permitted. Minimum 2 characters, starting by alphanumeric.") + } /** we need to add code over here to check whether the profile name is in the list of reserved keywords diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 7855e9108..edd592b1e 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -144,6 +144,10 @@ func runStart(cmd *cobra.Command, args []string) { registryMirror = viper.GetStringSlice("registry_mirror") } + if !config.ProfileNameValid(ClusterFlagValue()) { + out.WarningT("Profile name '{{.name}}' is not valid", out.V{"name": ClusterFlagValue()}) + exit.UsageT("Only alphanumeric, dots, underscores and dashes '-' are permitted. Minimum 2 characters, starting by alphanumeric.") + } existing, err := config.Load(ClusterFlagValue()) if err != nil && !config.IsNotExist(err) { exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err}) diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go index bfb6298c7..8345341c5 100644 --- a/pkg/minikube/config/profile.go +++ b/pkg/minikube/config/profile.go @@ -21,6 +21,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "strings" "github.com/golang/glog" @@ -83,6 +84,16 @@ func PrimaryControlPlane(cc *ClusterConfig) (Node, error) { return cp, nil } +// ProfileNameValid checks if the profile name is container name friendly +func ProfileNameValid(name string) bool { + + // RestrictedNameChars collects the characters allowed to represent a name + const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]` + + var validName = regexp.MustCompile(`^` + RestrictedNameChars + `+$`) + return validName.MatchString(name) +} + // ProfileNameInReservedKeywords checks if the profile is an internal keywords func ProfileNameInReservedKeywords(name string) bool { for _, v := range keywords { diff --git a/pkg/minikube/config/profile_test.go b/pkg/minikube/config/profile_test.go index 805123ed1..92e580f9f 100644 --- a/pkg/minikube/config/profile_test.go +++ b/pkg/minikube/config/profile_test.go @@ -72,6 +72,27 @@ func TestListProfiles(t *testing.T) { } } +func TestProfileNameValid(t *testing.T) { + var testCases = []struct { + name string + expected bool + }{ + {"meaningful_name", true}, + {"meaningful_name@", false}, + {"n_a_m_e_2", true}, + {"n", false}, + {"_name", false}, + {"N__a.M--E12567", true}, + } + for _, tt := range testCases { + got := ProfileNameValid(tt.name) + if got != tt.expected { + t.Errorf("expected ProfileNameValid(%s)=%t but got %t ", tt.name, tt.expected, got) + } + } + +} + func TestProfileNameInReservedKeywords(t *testing.T) { var testCases = []struct { name string -- GitLab