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
pull/5624/head
Nanik T 2019-10-15 18:34:05 +11:00
parent 47f63a488d
commit 824c911c42
3 changed files with 48 additions and 1 deletions

View File

@ -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 <profile name> `, out.V{"profilename": profile})
os.Exit(0)
}
if profile == "default" {
profile = "minikube"
} else {

View File

@ -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()

View File

@ -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 {