From 08a63d790ad7e086c0035b2287f6f905a59c01a4 Mon Sep 17 00:00:00 2001 From: Joseph Sacchini Date: Wed, 31 Oct 2018 09:46:21 -0400 Subject: [PATCH 1/3] Implement a check to see if an ISO URL is valid --- cmd/minikube/cmd/config/config.go | 2 +- cmd/minikube/cmd/config/validations.go | 38 ++++++++++++++++++--- cmd/minikube/cmd/config/validations_test.go | 27 ++++++++++++++- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/cmd/minikube/cmd/config/config.go b/cmd/minikube/cmd/config/config.go index ab9e33b00d49..473e264c9267 100644 --- a/cmd/minikube/cmd/config/config.go +++ b/cmd/minikube/cmd/config/config.go @@ -90,7 +90,7 @@ var settings = []Setting{ { name: "iso-url", set: SetString, - validations: []setFn{IsValidURL}, + validations: []setFn{IsValidURL, IsURLExists}, }, { name: config.WantUpdateNotification, diff --git a/cmd/minikube/cmd/config/validations.go b/cmd/minikube/cmd/config/validations.go index c887ba3157dc..1eaee55f7406 100644 --- a/cmd/minikube/cmd/config/validations.go +++ b/cmd/minikube/cmd/config/validations.go @@ -18,15 +18,14 @@ package config import ( "fmt" + "github.com/docker/go-units" + "github.com/pkg/errors" + "k8s.io/minikube/pkg/minikube/assets" + "k8s.io/minikube/pkg/minikube/constants" "net" "net/url" "os" "strconv" - - units "github.com/docker/go-units" - "github.com/pkg/errors" - "k8s.io/minikube/pkg/minikube/assets" - "k8s.io/minikube/pkg/minikube/constants" ) func IsValidDriver(string, driver string) error { @@ -59,6 +58,35 @@ func IsValidURL(name string, location string) error { return nil } +func IsURLExists(name string, location string) error { + parsed, err := url.Parse(location) + if err != nil { + return fmt.Errorf("%s is not a valid URL", location) + } + + // we can only validate if local files exist, not other urls + if parsed.Scheme != "file" { + return nil + } + + // chop off "file://" from the location, giving us the real system path + sysPath := string([]rune(location[len("file://"):])) + stat, err := os.Stat(sysPath) + if err != nil { + if os.IsNotExist(err) { + return fmt.Errorf("%s does not exist", location) + } + + return err + } + + if stat.IsDir() { + return fmt.Errorf("%s is a directory", location) + } + + return nil +} + func IsPositive(name string, val string) error { i, err := strconv.Atoi(val) if err != nil { diff --git a/cmd/minikube/cmd/config/validations_test.go b/cmd/minikube/cmd/config/validations_test.go index da5f12444c35..e74d630d1900 100644 --- a/cmd/minikube/cmd/config/validations_test.go +++ b/cmd/minikube/cmd/config/validations_test.go @@ -16,7 +16,10 @@ limitations under the License. package config -import "testing" +import ( + "os" + "testing" +) type validationTest struct { value string @@ -94,3 +97,25 @@ func TestValidCIDR(t *testing.T) { runValidations(t, tests, "cidr", IsValidCIDR) } + +func TestIsURLExists(t *testing.T) { + + self, err := os.Executable() + if err != nil { + t.Error(err) + } + + tests := []validationTest{ + { + value: "file://" + self, + shouldErr: false, + }, + + { + value: "file://" + self + "/subpath-of-file", + shouldErr: true, + }, + } + + runValidations(t, tests, "url", IsURLExists) +} From e275b779d90b1a096c62f1601293c5b5dd104229 Mon Sep 17 00:00:00 2001 From: Joseph Sacchini Date: Wed, 31 Oct 2018 13:51:04 -0400 Subject: [PATCH 2/3] Use strings.TrimPrefix instead of []rune casting for 'file://' prefix removal --- cmd/minikube/cmd/config/validations.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/minikube/cmd/config/validations.go b/cmd/minikube/cmd/config/validations.go index 1eaee55f7406..d252722f8dd5 100644 --- a/cmd/minikube/cmd/config/validations.go +++ b/cmd/minikube/cmd/config/validations.go @@ -26,6 +26,7 @@ import ( "net/url" "os" "strconv" + "strings" ) func IsValidDriver(string, driver string) error { @@ -70,7 +71,7 @@ func IsURLExists(name string, location string) error { } // chop off "file://" from the location, giving us the real system path - sysPath := string([]rune(location[len("file://"):])) + sysPath := strings.TrimPrefix(location, "file://") stat, err := os.Stat(sysPath) if err != nil { if os.IsNotExist(err) { From a99c4c39515230d4861e979684b830579e6be407 Mon Sep 17 00:00:00 2001 From: Joseph Sacchini Date: Wed, 31 Oct 2018 13:51:47 -0400 Subject: [PATCH 3/3] Handle file permissions error with a user-friendly message --- cmd/minikube/cmd/config/validations.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/minikube/cmd/config/validations.go b/cmd/minikube/cmd/config/validations.go index d252722f8dd5..e9ee37d8ece0 100644 --- a/cmd/minikube/cmd/config/validations.go +++ b/cmd/minikube/cmd/config/validations.go @@ -78,6 +78,10 @@ func IsURLExists(name string, location string) error { return fmt.Errorf("%s does not exist", location) } + if os.IsPermission(err) { + return fmt.Errorf("%s could not be opened (permission error: %s)", location, err.Error()) + } + return err }