Skip to content

Commit

Permalink
ovirt: Add basic engine URL validation
Browse files Browse the repository at this point in the history
Add a basic URL validator to avoid adding urls with no
scheme or scheme different from https

Signed-off-by: Roberto Ciatti <rciatti@redhat.com>
  • Loading branch information
Roberto Ciatti committed Jul 28, 2020
1 parent 01f5643 commit 34c5161
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/ovirt/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func askCredentials() (Config, error) {
Message: "oVirt API endpoint URL",
Help: "The URL of the oVirt engine API. For example, https://ovirt-engine-fqdn/ovirt-engine/api.",
},
Validate: survey.ComposeValidators(survey.Required),
Validate: survey.ComposeValidators(survey.Required, validURL),
},
}, &c.URL)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/asset/installconfig/ovirt/validaton.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ovirt

import (
"fmt"
"net/url"
"reflect"

ovirtsdk "github.com/ovirt/go-ovirt"
"github.com/pkg/errors"
Expand Down Expand Up @@ -125,3 +127,19 @@ func validateVNICProfile(platform ovirt.Platform, con *ovirtsdk.Connection) erro
}
return nil
}

func validURL(val interface{}) error {
if urlStr, ok := val.(string); ok {
url, err := url.ParseRequestURI(urlStr)
if err != nil {
return errors.Errorf("The specified URL is invalid, got %s", urlStr)
}

if url.Scheme != "https" {
return errors.Errorf("The only URL scheme accepted is https, but got %s", url.Scheme)
}
} else {
return fmt.Errorf("cannot check url validity on type %v", reflect.TypeOf(val).Name())
}
return nil
}
33 changes: 33 additions & 0 deletions pkg/asset/installconfig/ovirt/validaton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,36 @@ func Test_validateAuth(t *testing.T) {
func CreateMockOvirtServer(handler http.HandlerFunc) *httptest.Server {
return httptest.NewServer(handler)
}

func Test_validateURL(t *testing.T) {
tests := []struct {
url string
expectSuccess bool
}{
{
url: "engine.example.com",
expectSuccess: false,
},
{
url: "ftp://engine.example.com",
expectSuccess: false,
},
{
url: "http://engine.example.com",
expectSuccess: false,
},
{
url: "https://engine.example.com",
expectSuccess: true,
},
}

for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
got := validURL(tt.url)

assert.Equal(t, tt.expectSuccess, got == nil, "got this %s", got)
t.Log(got)
})
}
}

0 comments on commit 34c5161

Please sign in to comment.