Skip to content

Commit

Permalink
Add validation checking for minikube profile
Browse files Browse the repository at this point in the history
Fixes : kubernetes#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
  • Loading branch information
nanikjava committed Oct 15, 2019
1 parent 89bc592 commit cb6dc3a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ 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.MachineConfig.VMDriver == "" {
Expand All @@ -38,6 +41,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()
Expand Down
26 changes: 26 additions & 0 deletions pkg/minikube/config/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cb6dc3a

Please sign in to comment.