From a9c4301c1e9a42df2688bdc31f4d61abdeaec2d9 Mon Sep 17 00:00:00 2001 From: Saeid Bostandoust Date: Fri, 16 Apr 2021 03:51:04 +0430 Subject: [PATCH 1/2] fix check datascale command for https endpoints --- etcdctl/ctlv3/command/check.go | 6 ++++-- etcdctl/ctlv3/command/util.go | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/etcdctl/ctlv3/command/check.go b/etcdctl/ctlv3/command/check.go index bc2f6036b24..530425e19e9 100644 --- a/etcdctl/ctlv3/command/check.go +++ b/etcdctl/ctlv3/command/check.go @@ -311,6 +311,8 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) { ExitWithError(ExitError, errEndpoints) } + sec := secureCfgFromCmd(cmd) + ctx, cancel := context.WithCancel(context.Background()) resp, err := clients[0].Get(ctx, checkDatascalePrefix, v3.WithPrefix(), v3.WithLimit(1)) cancel() @@ -329,7 +331,7 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) { wg.Add(len(clients)) // get the process_resident_memory_bytes and process_virtual_memory_bytes before the put operations - bytesBefore := endpointMemoryMetrics(eps[0]) + bytesBefore := endpointMemoryMetrics(eps[0], sec) if bytesBefore == 0 { fmt.Println("FAIL: Could not read process_resident_memory_bytes before the put operations.") os.Exit(ExitError) @@ -367,7 +369,7 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) { s := <-sc // get the process_resident_memory_bytes after the put operations - bytesAfter := endpointMemoryMetrics(eps[0]) + bytesAfter := endpointMemoryMetrics(eps[0], sec) if bytesAfter == 0 { fmt.Println("FAIL: Could not read process_resident_memory_bytes after the put operations.") os.Exit(ExitError) diff --git a/etcdctl/ctlv3/command/util.go b/etcdctl/ctlv3/command/util.go index a05e8d669ff..40af71a4a5f 100644 --- a/etcdctl/ctlv3/command/util.go +++ b/etcdctl/ctlv3/command/util.go @@ -15,6 +15,7 @@ package command import ( + "crypto/tls" "context" "encoding/hex" "fmt" @@ -90,14 +91,26 @@ func isCommandTimeoutFlagSet(cmd *cobra.Command) bool { return commandTimeoutFlag.Changed } -// get the process_resident_memory_bytes from /metrics -func endpointMemoryMetrics(host string) float64 { +// get the process_resident_memory_bytes from /metrics +func endpointMemoryMetrics(host string, scfg *secureCfg) float64 { residentMemoryKey := "process_resident_memory_bytes" var residentMemoryValue string - if !strings.HasPrefix(host, `http://`) { + if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") { host = "http://" + host } url := host + "/metrics" + if strings.HasPrefix(host, "https://") { + // load client certificate + cert, err := tls.LoadX509KeyPair(scfg.cert, scfg.key) + if err != nil { + fmt.Println(fmt.Sprintf("client certificate error: %v", err)) + return 0.0 + } + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{ + Certificates: []tls.Certificate{cert}, + InsecureSkipVerify: scfg.insecureSkipVerify, + } + } resp, err := http.Get(url) if err != nil { fmt.Println(fmt.Sprintf("fetch error: %v", err)) From 97a8affdd391eefde983286a8135538705423acc Mon Sep 17 00:00:00 2001 From: Saeid Bostandoust Date: Sat, 17 Apr 2021 14:24:56 +0430 Subject: [PATCH 2/2] fix util.go file --- etcdctl/ctlv3/command/util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etcdctl/ctlv3/command/util.go b/etcdctl/ctlv3/command/util.go index 40af71a4a5f..230b2b2d116 100644 --- a/etcdctl/ctlv3/command/util.go +++ b/etcdctl/ctlv3/command/util.go @@ -15,8 +15,8 @@ package command import ( - "crypto/tls" "context" + "crypto/tls" "encoding/hex" "fmt" "io/ioutil" @@ -107,7 +107,7 @@ func endpointMemoryMetrics(host string, scfg *secureCfg) float64 { return 0.0 } http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{ - Certificates: []tls.Certificate{cert}, + Certificates: []tls.Certificate{cert}, InsecureSkipVerify: scfg.insecureSkipVerify, } }