Merge pull request #260 from openziti/verify-apiEndpoint

Added validation on setting apiEndpoint (#258)
This commit is contained in:
Michael Quigley 2023-03-10 16:29:42 -05:00 committed by GitHub
commit 1c39fd5f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,10 +2,12 @@ package main
import (
"fmt"
"net/url"
"os"
"github.com/openziti/zrok/tui"
"github.com/openziti/zrok/zrokdir"
"github.com/spf13/cobra"
"os"
)
func init() {
@ -42,6 +44,15 @@ func (cmd *configSetCommand) run(_ *cobra.Command, args []string) {
if zrd.Cfg == nil {
zrd.Cfg = &zrokdir.Config{}
}
ok, err := isValidUrl(value)
if err != nil {
fmt.Println("unable to validate api endpoint")
os.Exit(1)
}
if !ok {
fmt.Println("invalid apiEndpoint, please make sure scheme and host is provided")
os.Exit(1)
}
zrd.Cfg.ApiEndpoint = value
modified = true
@ -62,3 +73,14 @@ func (cmd *configSetCommand) run(_ *cobra.Command, args []string) {
fmt.Println("zrok configuration not changed")
}
}
func isValidUrl(rawUrl string) (bool, error) {
u, err := url.Parse(rawUrl)
if err != nil {
return false, err
}
if u.Scheme == "" || u.Host == "" {
return false, nil
}
return true, nil
}