Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve validation for extra-config. #10886

Merged
merged 9 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/minikube/config/extra_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func (es *ExtraOptionSlice) Exists(value string) bool {

// Set parses the string value into a slice
func (es *ExtraOptionSlice) Set(value string) error {

// Check we don't end with suffix quotation.
prefixExists := strings.HasPrefix(value, "”") || strings.HasPrefix(value, "“")
suffixExists := strings.HasSuffix(value, "”") || strings.HasSuffix(value, "“")
if !prefixExists && suffixExists {
return fmt.Errorf("invalid value: extra-config cannot contain end quotation: %q", value)
}

// The component is the value before the first dot.
componentSplit := strings.SplitN(value, ".", 2)
if len(componentSplit) < 2 {
Expand Down
52 changes: 52 additions & 0 deletions pkg/minikube/config/extra_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"flag"
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -79,6 +80,57 @@ func TestValidFlags(t *testing.T) {
}
}

func createQuoteError(value string) error {
return fmt.Errorf("invalid value: extra-config cannot contain end quotation: %q", value)
}

func createPeriodError(value string) error {
return fmt.Errorf("invalid value: must contain at least one period: %q", value)
}

func createEqualError(value string) error {
return fmt.Errorf("invalid value: must contain one equal sign: %q", value)
}

func TestSet(t *testing.T) {
extraOptions := ExtraOptionSlice{}

// Examples which will error from checks from the Set function.
for _, tc := range []struct {
valuesToSet string
expErr error
extraOption ExtraOptionSlice
}{
{"etcd.client-cert-auth=false”", createQuoteError("etcd.client-cert-auth=false”"),
extraOptions},
{"etcdclient-cert-auth=false", createPeriodError("etcdclient-cert-auth=false"),
extraOptions},
{"etcd.client-cert-authfalse", createEqualError("etcd.client-cert-authfalse"),
extraOptions},
} {
if res := tc.extraOption.Set(tc.valuesToSet); res.Error() != tc.expErr.Error() {
t.Errorf("Result error of: %s does not match expected error: %s",
tc.expErr.Error(), res.Error())
}
}

// Examples which will not error from the Set function.
for _, tc := range []struct {
valuesToSet string
extraOption ExtraOptionSlice
}{
{"etcd.client-cert-auth=false", extraOptions},
// Will fail from the check in start.go, but not from Set function.
{"“etcd.client-cert-auth=false”", extraOptions},
// Will fail from the check in start.go, but not from Set function.
{"“etcd.client-cert-auth=false", extraOptions},
} {
if res := tc.extraOption.Set(tc.valuesToSet); res != nil {
t.Errorf("Unexpected error: %s", res.Error())
}
}
}

func TestExists(t *testing.T) {
extraOptions := ExtraOptionSlice{
ExtraOption{Component: "c1", Key: "bar", Value: "c1-bar"},
Expand Down