Skip to content

Commit

Permalink
Better url validation on plural cd login (#503)
Browse files Browse the repository at this point in the history
We currently just take these values blindly which can cause rough ux if someone puts a correct-ish url we don't expect
  • Loading branch information
michaeljguarino committed Apr 10, 2024
1 parent d23cea8 commit 376df14
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
20 changes: 15 additions & 5 deletions cmd/plural/cd.go
Expand Up @@ -44,9 +44,10 @@ func (p *Plural) cdCommands() []cli.Command {
},
},
{
Name: "control-plane",
Action: p.handleInstallControlPlane,
Usage: "sets up the plural console in an existing k8s cluster",
Name: "control-plane",
Aliases: []string{"helm-values"},
Action: p.handleInstallControlPlane,
Usage: "sets up the plural console in an existing k8s cluster",
},
{
Name: "control-plane-values",
Expand All @@ -69,7 +70,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"},
},
},
Expand Down Expand Up @@ -159,13 +160,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()
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/console/config.go
@@ -1,6 +1,8 @@
package console

import (
"fmt"
"net/url"
"os"
"path/filepath"

Expand All @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
15 changes: 13 additions & 2 deletions pkg/console/console.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strings"

consoleclient "github.com/pluralsh/console-client-go"
)
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit 376df14

Please sign in to comment.