AUTH-2173: Prepends access login url with scheme if one doesnt exist

This commit is contained in:
Michael Borkenstein 2019-10-22 10:41:44 -05:00
parent 7133eceb9b
commit ad9559c66a
4 changed files with 40 additions and 4 deletions

View File

@ -23,7 +23,7 @@ func ssh(c *cli.Context) error {
if err != nil || rawHostName == "" { if err != nil || rawHostName == "" {
return cli.ShowCommandHelp(c, "ssh") return cli.ShowCommandHelp(c, "ssh")
} }
originURL := "https://" + hostname originURL := ensureURLScheme(hostname)
// get the headers from the cmdline and add them // get the headers from the cmdline and add them
headers := buildRequestHeaders(c.StringSlice(sshHeaderFlag)) headers := buildRequestHeaders(c.StringSlice(sshHeaderFlag))

View File

@ -191,7 +191,8 @@ func login(c *cli.Context) error {
raven.SetDSN(sentryDSN) raven.SetDSN(sentryDSN)
logger := log.CreateLogger() logger := log.CreateLogger()
args := c.Args() args := c.Args()
appURL, err := url.Parse(args.First()) rawURL := ensureURLScheme(args.First())
appURL, err := url.Parse(rawURL)
if args.Len() < 1 || err != nil { if args.Len() < 1 || err != nil {
logger.Errorf("Please provide the url of the Access application\n") logger.Errorf("Please provide the url of the Access application\n")
return err return err
@ -211,6 +212,16 @@ func login(c *cli.Context) error {
return nil return nil
} }
// ensureURLScheme prepends a URL with https:// if it doesnt have a scheme. http:// URLs will not be converted.
func ensureURLScheme(url string) string {
url = strings.Replace(strings.ToLower(url), "http://", "https://", 1)
if !strings.HasPrefix(url, "https://") {
url = fmt.Sprintf("https://%s", url)
}
return url
}
// curl provides a wrapper around curl, passing Access JWT along in request // curl provides a wrapper around curl, passing Access JWT along in request
func curl(c *cli.Context) error { func curl(c *cli.Context) error {
raven.SetDSN(sentryDSN) raven.SetDSN(sentryDSN)
@ -294,7 +305,7 @@ func sshGen(c *cli.Context) error {
return cli.ShowCommandHelp(c, "ssh-gen") return cli.ShowCommandHelp(c, "ssh-gen")
} }
originURL, err := url.Parse("https://" + hostname) originURL, err := url.Parse(ensureURLScheme(hostname))
if err != nil { if err != nil {
return err return err
} }

View File

@ -0,0 +1,25 @@
package access
import "testing"
func Test_ensureURLScheme(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
want string
}{
{"no scheme", args{"localhost:123"}, "https://localhost:123"},
{"http scheme", args{"http://test"}, "https://test"},
{"https scheme", args{"https://test"}, "https://test"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ensureURLScheme(tt.args.url); got != tt.want {
t.Errorf("ensureURLScheme() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -45,7 +45,7 @@ func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncr
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Please open the following URL and log in with your Cloudflare account:\n\n%s\n\nLeave cloudflared running to download the %s automatically.\n", requestURL, resourceName) fmt.Fprintf(os.Stderr, "Please open the following URL and log in with your Cloudflare account:\n\n%s\n\nLeave cloudflared running to download the %s automatically.\n", requestURL, resourceName)
} else { } else {
fmt.Fprintf(os.Stderr, "A browser window should have opened at the following URL:\n\n%s\n\nIf the browser failed to open, open it yourself and visit the URL above.\n", requestURL) fmt.Fprintf(os.Stderr, "A browser window should have opened at the following URL:\n\n%s\n\nIf the browser failed to open, please visit the URL above directly in your browser.\n", requestURL)
} }
var resourceData []byte var resourceData []byte