Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better url validation on plural cd login #503

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions cmd/plural/cd.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading