From 5d3d496095a5d5424b69a516ed532d626eaeee5b Mon Sep 17 00:00:00 2001 From: CyrilPeponnet Date: Wed, 1 May 2019 21:47:19 -0700 Subject: [PATCH 01/10] impr/clients: Handle TLS config and MTLS for logcli and promtail --- cmd/logcli/client.go | 40 +++++++++++++++- cmd/logcli/main.go | 6 +++ docs/logcli.md | 20 +++++--- docs/promtail-setup.md | 28 +++++++++-- pkg/helpers/tls.go | 87 +++++++++++++++++++++++++++++++++++ pkg/promtail/client/client.go | 45 ++++++++++++++++-- 6 files changed, 207 insertions(+), 19 deletions(-) create mode 100644 pkg/helpers/tls.go diff --git a/cmd/logcli/client.go b/cmd/logcli/client.go index 3acaea6b65eea..980fd71df6264 100644 --- a/cmd/logcli/client.go +++ b/cmd/logcli/client.go @@ -13,6 +13,7 @@ import ( "github.com/gorilla/websocket" + "github.com/grafana/loki/pkg/helpers" "github.com/grafana/loki/pkg/logproto" ) @@ -60,9 +61,27 @@ func doRequest(path string, out interface{}) error { if err != nil { return err } + req.SetBasicAuth(*username, *password) - resp, err := http.DefaultClient.Do(req) + tlsConfig, err := helpers.NewTLSConfigFromOptions( + url, + *tlsCACertPath, + *tlsClientCertPath, + *tlsClientCertKeyPath, + *tlsClientCertKeyPass, + *tlsSkipVerify) + if err != nil { + return err + } + + client := &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: tlsConfig, + }, + } + + resp, err := client.Do(req) if err != nil { return err } @@ -86,6 +105,18 @@ func liveTailQueryConn() (*websocket.Conn, error) { } func wsConnect(path string) (*websocket.Conn, error) { + + tlsConfig, err := helpers.NewTLSConfigFromOptions( + *addr, + *tlsCACertPath, + *tlsClientCertPath, + *tlsClientCertKeyPath, + *tlsClientCertKeyPass, + *tlsSkipVerify) + if err != nil { + return nil, err + } + url := *addr + path if strings.HasPrefix(url, "https") { url = strings.Replace(url, "https", "wss", 1) @@ -95,7 +126,12 @@ func wsConnect(path string) (*websocket.Conn, error) { fmt.Println(url) h := http.Header{"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte(*username+":"+*password))}} - c, resp, err := websocket.DefaultDialer.Dial(url, h) + + ws := websocket.Dialer{ + TLSClientConfig: tlsConfig, + } + + c, resp, err := ws.Dial(url, h) if err != nil { if resp == nil { diff --git a/cmd/logcli/main.go b/cmd/logcli/main.go index 596bdda6f0f0e..c910840181a96 100644 --- a/cmd/logcli/main.go +++ b/cmd/logcli/main.go @@ -14,6 +14,12 @@ var ( 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() + tlsCACertPath = app.Flag("ca-cert", "Path to the server Certificate Authority.").Default("").Envar("LOKI_CA_CERT_PATH").String() + tlsSkipVerify = app.Flag("tls-skip-verify", "Server certificate TLS skip verify.").Default("false").Bool() + tlsClientCertPath = app.Flag("cert", "Path to the client certificate.").Default("").Envar("LOKI_CLIENT_CERT_PATH").String() + tlsClientCertKeyPath = app.Flag("key", "Path to the client certificate key.").Default("").Envar("LOKI_CLIENT_KEY_PATH").String() + tlsClientCertKeyPass = app.Flag("key-pass", "Client certificate key password.").Default("").Envar("LOKI_CLIENT_KEY_PASS").String() + queryCmd = app.Command("query", "Run a LogQL query.") queryStr = queryCmd.Arg("query", "eg '{foo=\"bar\",baz=\"blip\"}'").Required().String() regexpStr = queryCmd.Arg("regex", "").String() diff --git a/docs/logcli.md b/docs/logcli.md index d7330a2826bbb..2b8b85cec7b0b 100644 --- a/docs/logcli.md +++ b/docs/logcli.md @@ -44,8 +44,8 @@ Common labels: {job="cortex-ops/consul", namespace="cortex-ops"} ### Configuration - Configuration values are considered in the following order (lowest to highest): + - environment value - command line @@ -53,17 +53,23 @@ The URLs of the requests are printed to help with integration work. ### Details -``` +```console $ logcli help usage: logcli [] [ ...] A command-line for loki. Flags: - --help Show context-sensitive help (also try --help-long and --help-man). - --addr="" Server address, need to specify. - --username="" Username for HTTP basic auth. - --password="" Password for HTTP basic auth. + --help Show context-sensitive help (also try --help-long and --help-man). + --addr="https://logs-us-west1.grafana.net" + Server address. + --username="" Username for HTTP basic auth. + --password="" Password for HTTP basic auth. + --ca-cert="" Path to the server Certificate Authority. + --tls-skip-verify Server certificate TLS skip verify. + --cert="" Path to the client certificate. + --key="" Path to the client certificate key. + --key-pass="" Client certificate key password. Commands: help [...] @@ -72,7 +78,7 @@ Commands: query [] [] Run a LogQL query. - labels