Skip to content

Commit

Permalink
Implement a check to see if an ISO URL is valid (#3287)
Browse files Browse the repository at this point in the history
* Implement a check to see if an ISO URL is valid

* Use strings.TrimPrefix instead of []rune casting for 'file://' prefix removal

* Handle file permissions error with a user-friendly message
  • Loading branch information
Twister915 authored and balopat committed Nov 27, 2018
1 parent 49f038a commit 41bd1de
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var settings = []Setting{
{
name: "iso-url",
set: SetString,
validations: []setFn{IsValidURL},
validations: []setFn{IsValidURL, IsURLExists},
},
{
name: config.WantUpdateNotification,
Expand Down
43 changes: 38 additions & 5 deletions cmd/minikube/cmd/config/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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"
"strings"
)

func IsValidDriver(string, driver string) error {
Expand Down Expand Up @@ -59,6 +59,39 @@ 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 := strings.TrimPrefix(location, "file://")
stat, err := os.Stat(sysPath)
if err != nil {
if os.IsNotExist(err) {
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
}

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 {
Expand Down
27 changes: 26 additions & 1 deletion cmd/minikube/cmd/config/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package config

import "testing"
import (
"os"
"testing"
)

type validationTest struct {
value string
Expand Down Expand Up @@ -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)
}

0 comments on commit 41bd1de

Please sign in to comment.