Skip to content

Commit

Permalink
better job of fixing url parsing and host name extraction for logcli
Browse files Browse the repository at this point in the history
  • Loading branch information
slim-bean committed Jul 18, 2019
1 parent c3e74b3 commit da6a133
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
39 changes: 22 additions & 17 deletions cmd/logcli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,29 @@ func listLabelValues(name string) (*logproto.LabelResponse, error) {
}

func doRequest(path string, out interface{}) error {
fullURL, err := url.Parse(addrURL.String() + path)
if err != nil {
return err
}
url := fullURL.String()
us := *addr + path
if !*quiet {
log.Print(url)
log.Print(us)
}

req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequest("GET", us, nil)
if err != nil {
return err
}

req.SetBasicAuth(*username, *password)

// Parse the URL to extract the host
u, err := url.Parse(us)
if err != nil {
return err
}
clientConfig := config.HTTPClientConfig{
TLSConfig: config.TLSConfig{
CAFile: *tlsCACertPath,
CertFile: *tlsClientCertPath,
KeyFile: *tlsClientCertKeyPath,
ServerName: fullURL.Host,
ServerName: u.Host,
InsecureSkipVerify: *tlsSkipVerify,
},
}
Expand Down Expand Up @@ -109,27 +110,31 @@ func liveTailQueryConn() (*websocket.Conn, error) {
}

func wsConnect(path string) (*websocket.Conn, error) {
us := *addr + path

// Parse the URL to extract the host
u, err := url.Parse(us)
if err != nil {
return nil, err
}
tlsConfig, err := config.NewTLSConfig(&config.TLSConfig{
CAFile: *tlsCACertPath,
CertFile: *tlsClientCertPath,
KeyFile: *tlsClientCertKeyPath,
ServerName: addrURL.Host,
ServerName: u.Host,
InsecureSkipVerify: *tlsSkipVerify,
})
if err != nil {
return nil, err
}

addrURL.Path = path
url := addrURL.String()
if strings.HasPrefix(url, "https") {
url = strings.Replace(url, "https", "wss", 1)
} else if strings.HasPrefix(url, "http") {
url = strings.Replace(url, "http", "ws", 1)
if strings.HasPrefix(us, "https") {
us = strings.Replace(us, "https", "wss", 1)
} else if strings.HasPrefix(us, "http") {
us = strings.Replace(us, "http", "ws", 1)
}
if !*quiet {
log.Println(url)
log.Println(us)
}

h := http.Header{"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte(*username+":"+*password))}}
Expand All @@ -138,7 +143,7 @@ func wsConnect(path string) (*websocket.Conn, error) {
TLSClientConfig: tlsConfig,
}

c, resp, err := ws.Dial(url, h)
c, resp, err := ws.Dial(us, h)

if err != nil {
if resp == nil {
Expand Down
10 changes: 1 addition & 9 deletions cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"log"
"net/url"
"os"

"gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -13,8 +12,7 @@ var (
quiet = app.Flag("quiet", "suppress everything but log lines").Default("false").Short('q').Bool()
outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]").Default("default").Short('o').Enum("default", "raw", "jsonl")

addr = app.Flag("addr", "Server address.").Default("https://logs-us-west1.grafana.net").Envar("GRAFANA_ADDR").String()
addrURL url.URL
addr = app.Flag("addr", "Server address.").Default("https://logs-us-west1.grafana.net").Envar("GRAFANA_ADDR").String()

username = app.Flag("username", "Username for HTTP basic auth.").Default("").Envar("GRAFANA_USERNAME").String()
password = app.Flag("password", "Password for HTTP basic auth.").Default("").Envar("GRAFANA_PASSWORD").String()
Expand Down Expand Up @@ -51,12 +49,6 @@ func main() {
log.Fatalln("Server address cannot be empty")
}

u, err := url.Parse(*addr)
if err != nil {
log.Fatalf("Failed to parse addr into URL: %v", err)
}
addrURL = *u

switch cmd {
case queryCmd.FullCommand():
doQuery()
Expand Down

0 comments on commit da6a133

Please sign in to comment.