From e8ca6da6e4dc4ae082f54f95e8ae847bf4dd8593 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Tue, 9 Apr 2024 18:49:34 -0400 Subject: [PATCH] Better url validation on `plural cd login` We currently just take these values blindly which can cause rough ux if someone puts a correct-ish url we don't expect --- cmd/plural/cd.go | 13 +++++++++++-- pkg/console/config.go | 23 +++++++++++++++++++++++ pkg/console/console.go | 15 +++++++++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/cmd/plural/cd.go b/cmd/plural/cd.go index 44fe4a68b..a40f52401 100644 --- a/cmd/plural/cd.go +++ b/cmd/plural/cd.go @@ -69,7 +69,7 @@ func (p *Plural) cdCommands() []cli.Command { Action: p.handleCdLogin, Usage: "logs into your plural console", Flags: []cli.Flag{ - cli.StringFlag{Name: "url", Usage: "console url", Required: true}, + cli.StringFlag{Name: "url", Usage: "console url"}, cli.StringFlag{Name: "token", Usage: "console access token"}, }, }, @@ -159,13 +159,22 @@ func confirmCluster(url, token string) (bool, error) { func (p *Plural) handleCdLogin(c *cli.Context) (err error) { url := c.String("url") + if url == "" { + url, err = utils.ReadLine("Enter the url of your console: ") + if err != nil { + return + } + } + token := c.String("token") if token == "" { - token, err = utils.ReadPwd("Enter your console access token") + token, err = utils.ReadPwd("Enter your console access token: ") if err != nil { return } } + + url = console.NormalizeUrl(url) conf := console.Config{Url: url, Token: token} return conf.Save() } diff --git a/pkg/console/config.go b/pkg/console/config.go index 64ec0750a..4aef35604 100644 --- a/pkg/console/config.go +++ b/pkg/console/config.go @@ -1,6 +1,8 @@ package console import ( + "fmt" + "net/url" "os" "path/filepath" @@ -12,6 +14,10 @@ const ( ConfigName = "console.yml" ) +var ( + errUrlFormat = fmt.Errorf("Url must be of format https://{your-console-domain}") +) + type VersionedConfig struct { ApiVersion string `yaml:"apiVersion"` Kind string `yaml:"kind"` @@ -41,7 +47,24 @@ func ReadConfig() (conf Config) { return } +func (conf *Config) Validate() error { + url, err := url.Parse(conf.Url) + if err != nil { + return err + } + + if url.Scheme != "https" { + return errUrlFormat + } + + return nil +} + func (conf *Config) Save() error { + if err := conf.Validate(); err != nil { + return err + } + versioned := &VersionedConfig{ ApiVersion: "platform.plural.sh/v1alpha1", Kind: "Console", diff --git a/pkg/console/console.go b/pkg/console/console.go index d55d681c0..933c01ea5 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strings" consoleclient "github.com/pluralsh/console-client-go" ) @@ -59,7 +60,6 @@ func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) { } func NewConsoleClient(token, url string) (ConsoleClient, error) { - httpClient := http.Client{ Transport: &authedTransport{ token: token, @@ -70,11 +70,22 @@ func NewConsoleClient(token, url string) (ConsoleClient, error) { return &consoleClient{ url: url, token: token, - client: consoleclient.NewClient(&httpClient, fmt.Sprintf("%s/gql", url), nil), + client: consoleclient.NewClient(&httpClient, NormalizeUrl(url), nil), ctx: context.Background(), }, nil } +func NormalizeUrl(url string) string { + if !strings.HasPrefix(url, "https://") { + url = fmt.Sprintf("https://%s", url) + } + if !strings.HasSuffix(url, "/gql") { + url = fmt.Sprintf("%s/gql", url) + } + + return url +} + func (c *consoleClient) Url() string { return c.url }