From 824c911c428277976eba57d4110b17854df80833 Mon Sep 17 00:00:00 2001 From: Nanik T Date: Tue, 15 Oct 2019 18:34:05 +1100 Subject: [PATCH] Add validation checking for minikube profile Fixes : #4883 New function ProfileNameInReservedKeywords(..) has been added inside pkg/minikube/config/profile.go The new function perform validation to make sure the profile name is not in the list of keywords. Following are the keywords that are used: start, stop, status, delete, config, open, profile, addons, cache, logs The checking is case insensitive to cover combo of upper and lower case --- cmd/minikube/cmd/config/profile.go | 9 +++++++++ pkg/minikube/config/profile.go | 14 +++++++++++++- pkg/minikube/config/profile_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cmd/minikube/cmd/config/profile.go b/cmd/minikube/cmd/config/profile.go index cbf6f1fe6d..64f0f372f5 100644 --- a/cmd/minikube/cmd/config/profile.go +++ b/cmd/minikube/cmd/config/profile.go @@ -45,6 +45,15 @@ var ProfileCmd = &cobra.Command{ } profile := args[0] + /** + we need to add code over here to check whether the profile + name is in the list of reserved keywords + */ + if pkgConfig.ProfileNameInReservedKeywords(profile) { + out.ErrT(out.FailureType, `Profile name "{{.profilename}}" is minikube keyword. To delete profile use command minikube delete -p `, out.V{"profilename": profile}) + os.Exit(0) + } + if profile == "default" { profile = "minikube" } else { diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go index 2c09f4b446..b75ade5680 100644 --- a/pkg/minikube/config/profile.go +++ b/pkg/minikube/config/profile.go @@ -21,18 +21,20 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "github.com/golang/glog" "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/util/lock" ) +var keywords = []string{"start", "stop", "status", "delete", "config", "open", "profile", "addons", "cache", "logs"} + // IsValid checks if the profile has the essential info needed for a profile func (p *Profile) IsValid() bool { if p.Config == nil { return false } - if p.Config.MachineConfig.VMDriver == "" { return false } @@ -42,6 +44,16 @@ func (p *Profile) IsValid() bool { return true } +// check if the profile is an internal keywords +func ProfileNameInReservedKeywords(name string) bool { + for _, v := range keywords { + if strings.EqualFold(v, name) { + return true + } + } + return false +} + // ProfileExists returns true if there is a profile config (regardless of being valid) func ProfileExists(name string, miniHome ...string) bool { miniPath := localpath.MiniPath() diff --git a/pkg/minikube/config/profile_test.go b/pkg/minikube/config/profile_test.go index 2cd674a8ef..8852ea9b6f 100644 --- a/pkg/minikube/config/profile_test.go +++ b/pkg/minikube/config/profile_test.go @@ -72,6 +72,32 @@ func TestListProfiles(t *testing.T) { } } +func TestProfileNameInReservedKeywords(t *testing.T) { + var testCases = []struct { + name string + expected bool + }{ + {"start", true}, + {"stop", true}, + {"status", true}, + {"delete", true}, + {"config", true}, + {"open", true}, + {"profile", true}, + {"addons", true}, + {"cache", true}, + {"logs", true}, + {"myprofile", false}, + {"log", false}, + } + for _, tt := range testCases { + got := ProfileNameInReservedKeywords(tt.name) + if got != tt.expected { + t.Errorf("expected ProfileNameInReservedKeywords(%s)=%t but got %t ", tt.name, tt.expected, got) + } + } +} + func TestProfileExists(t *testing.T) { miniDir, err := filepath.Abs("./testdata/.minikube2") if err != nil {