Skip to content

Commit 376df14

Browse files
Better url validation on plural cd login (#503)
We currently just take these values blindly which can cause rough ux if someone puts a correct-ish url we don't expect
1 parent d23cea8 commit 376df14

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

cmd/plural/cd.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ func (p *Plural) cdCommands() []cli.Command {
4444
},
4545
},
4646
{
47-
Name: "control-plane",
48-
Action: p.handleInstallControlPlane,
49-
Usage: "sets up the plural console in an existing k8s cluster",
47+
Name: "control-plane",
48+
Aliases: []string{"helm-values"},
49+
Action: p.handleInstallControlPlane,
50+
Usage: "sets up the plural console in an existing k8s cluster",
5051
},
5152
{
5253
Name: "control-plane-values",
@@ -69,7 +70,7 @@ func (p *Plural) cdCommands() []cli.Command {
6970
Action: p.handleCdLogin,
7071
Usage: "logs into your plural console",
7172
Flags: []cli.Flag{
72-
cli.StringFlag{Name: "url", Usage: "console url", Required: true},
73+
cli.StringFlag{Name: "url", Usage: "console url"},
7374
cli.StringFlag{Name: "token", Usage: "console access token"},
7475
},
7576
},
@@ -159,13 +160,22 @@ func confirmCluster(url, token string) (bool, error) {
159160

160161
func (p *Plural) handleCdLogin(c *cli.Context) (err error) {
161162
url := c.String("url")
163+
if url == "" {
164+
url, err = utils.ReadLine("Enter the url of your console: ")
165+
if err != nil {
166+
return
167+
}
168+
}
169+
162170
token := c.String("token")
163171
if token == "" {
164-
token, err = utils.ReadPwd("Enter your console access token")
172+
token, err = utils.ReadPwd("Enter your console access token: ")
165173
if err != nil {
166174
return
167175
}
168176
}
177+
178+
url = console.NormalizeUrl(url)
169179
conf := console.Config{Url: url, Token: token}
170180
return conf.Save()
171181
}

pkg/console/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package console
22

33
import (
4+
"fmt"
5+
"net/url"
46
"os"
57
"path/filepath"
68

@@ -12,6 +14,10 @@ const (
1214
ConfigName = "console.yml"
1315
)
1416

17+
var (
18+
errUrlFormat = fmt.Errorf("Url must be of format https://{your-console-domain}")
19+
)
20+
1521
type VersionedConfig struct {
1622
ApiVersion string `yaml:"apiVersion"`
1723
Kind string `yaml:"kind"`
@@ -41,7 +47,24 @@ func ReadConfig() (conf Config) {
4147
return
4248
}
4349

50+
func (conf *Config) Validate() error {
51+
url, err := url.Parse(conf.Url)
52+
if err != nil {
53+
return err
54+
}
55+
56+
if url.Scheme != "https" {
57+
return errUrlFormat
58+
}
59+
60+
return nil
61+
}
62+
4463
func (conf *Config) Save() error {
64+
if err := conf.Validate(); err != nil {
65+
return err
66+
}
67+
4568
versioned := &VersionedConfig{
4669
ApiVersion: "platform.plural.sh/v1alpha1",
4770
Kind: "Console",

pkg/console/console.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"strings"
78

89
consoleclient "github.com/pluralsh/console-client-go"
910
)
@@ -59,7 +60,6 @@ func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
5960
}
6061

6162
func NewConsoleClient(token, url string) (ConsoleClient, error) {
62-
6363
httpClient := http.Client{
6464
Transport: &authedTransport{
6565
token: token,
@@ -70,11 +70,22 @@ func NewConsoleClient(token, url string) (ConsoleClient, error) {
7070
return &consoleClient{
7171
url: url,
7272
token: token,
73-
client: consoleclient.NewClient(&httpClient, fmt.Sprintf("%s/gql", url), nil),
73+
client: consoleclient.NewClient(&httpClient, NormalizeUrl(url), nil),
7474
ctx: context.Background(),
7575
}, nil
7676
}
7777

78+
func NormalizeUrl(url string) string {
79+
if !strings.HasPrefix(url, "https://") {
80+
url = fmt.Sprintf("https://%s", url)
81+
}
82+
if !strings.HasSuffix(url, "/gql") {
83+
url = fmt.Sprintf("%s/gql", url)
84+
}
85+
86+
return url
87+
}
88+
7889
func (c *consoleClient) Url() string {
7990
return c.url
8091
}

0 commit comments

Comments
 (0)