From f859c8e2d6518f9abbdd790ebc905b9ae4374653 Mon Sep 17 00:00:00 2001 From: Lyas Spiehler Date: Tue, 25 Oct 2022 15:18:38 +0000 Subject: [PATCH 1/5] add hostname parameter for tcp probe Signed-off-by: Lyas Spiehler --- prober/handler.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/prober/handler.go b/prober/handler.go index f449cace..830c8572 100644 --- a/prober/handler.go +++ b/prober/handler.go @@ -107,6 +107,14 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo } } + if module.Prober == "tcp" && hostname != "" { + err = setTLSServerName(hostname, &module) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + } + sl := newScrapeLogger(logger, moduleName, target) level.Info(sl).Log("msg", "Beginning probe", "probe", module.Prober, "timeout_seconds", timeoutSeconds) @@ -137,6 +145,15 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo h.ServeHTTP(w, r) } +func setTLSServerName(hostname string, module *config.Module) error { + // By creating a new hashmap and copying values there we + // ensure that the initial configuration remain intact. + if module.TCP.TLSConfig.ServerName == "" { + module.TCP.TLSConfig.ServerName = hostname + } + return nil +} + func setHTTPHost(hostname string, module *config.Module) error { // By creating a new hashmap and copying values there we // ensure that the initial configuration remain intact. From d6f683933f59f93d83efb6aa00e7251540e8d37e Mon Sep 17 00:00:00 2001 From: Lyas Spiehler Date: Wed, 26 Oct 2022 20:10:48 +0000 Subject: [PATCH 2/5] only add servername if TLS is true Signed-off-by: Lyas Spiehler --- prober/handler.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/prober/handler.go b/prober/handler.go index 830c8572..3705a36f 100644 --- a/prober/handler.go +++ b/prober/handler.go @@ -148,8 +148,10 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo func setTLSServerName(hostname string, module *config.Module) error { // By creating a new hashmap and copying values there we // ensure that the initial configuration remain intact. - if module.TCP.TLSConfig.ServerName == "" { - module.TCP.TLSConfig.ServerName = hostname + if module.TCP.TLS { + if module.TCP.TLSConfig.ServerName == "" { + module.TCP.TLSConfig.ServerName = hostname + } } return nil } From 0c9850966faf9023c57875ab1ef4efcdc17851c6 Mon Sep 17 00:00:00 2001 From: Lyas Spiehler Date: Fri, 28 Oct 2022 14:03:48 +0000 Subject: [PATCH 3/5] add even if TLS isn't true in case of STARTTLS Signed-off-by: Lyas Spiehler --- prober/handler.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/prober/handler.go b/prober/handler.go index 3705a36f..830c8572 100644 --- a/prober/handler.go +++ b/prober/handler.go @@ -148,10 +148,8 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo func setTLSServerName(hostname string, module *config.Module) error { // By creating a new hashmap and copying values there we // ensure that the initial configuration remain intact. - if module.TCP.TLS { - if module.TCP.TLSConfig.ServerName == "" { - module.TCP.TLSConfig.ServerName = hostname - } + if module.TCP.TLSConfig.ServerName == "" { + module.TCP.TLSConfig.ServerName = hostname } return nil } From a832718c356f0d14ec15c289bd255920cabe5012 Mon Sep 17 00:00:00 2001 From: Lyas Spiehler Date: Wed, 23 Nov 2022 16:43:46 +0000 Subject: [PATCH 4/5] added test hostname parameter with TCP probe Signed-off-by: Lyas Spiehler --- prober/handler_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/prober/handler_test.go b/prober/handler_test.go index a144fb95..40039d48 100644 --- a/prober/handler_test.go +++ b/prober/handler_test.go @@ -16,8 +16,10 @@ package prober import ( "bytes" "fmt" + "net" "net/http" "net/http/httptest" + "strconv" "strings" "testing" "time" @@ -203,3 +205,55 @@ func TestHostnameParam(t *testing.T) { t.Errorf("probe request handler returned wrong status code: %v, want %v", status, http.StatusBadRequest) } } + +func TestTCPHostnameParam(t *testing.T) { + c := &config.Config{ + Modules: map[string]config.Module{ + "tls_connect": { + Prober: "tcp", + Timeout: 10 * time.Second, + TCP: config.TCPProbe{ + TLS: true, + IPProtocol: "ip4", + TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: true}, + }, + }, + }, + } + + // check that 'hostname' parameter make its way to server_name in the tls_config + hostname := "foo.example.com" + + ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Host != hostname { + t.Errorf("Unexpected Host: expected %q, got %q.", hostname, r.Host) + } + w.WriteHeader(http.StatusOK) + })) + defer ts.Close() + + requrl := fmt.Sprintf("?module=tls_connect&debug=true&hostname=%s&target=%s", hostname, ts.Listener.Addr().(*net.TCPAddr).IP.String()+":"+strconv.Itoa(ts.Listener.Addr().(*net.TCPAddr).Port)) + + req, err := http.NewRequest("GET", requrl, nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + Handler(w, r, c, log.NewNopLogger(), &ResultHistory{}, 0.5, nil) + }) + + handler.ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("probe request handler returned wrong status code: %v, want %v", status, http.StatusOK) + } + + // check debug output to confirm the server_name is set in tls_config and matches supplied hostname + if !strings.Contains(rr.Body.String(), "server_name: "+hostname) { + t.Errorf("probe failed, response body: %v", rr.Body.String()) + } + +} From d207f6430ae9ed36e7f6c2c5c563a108ae6db051 Mon Sep 17 00:00:00 2001 From: Lyas Spiehler Date: Wed, 1 Feb 2023 12:33:28 +0000 Subject: [PATCH 5/5] remove unnecessary function and inline assignment Signed-off-by: Lyas Spiehler --- prober/handler.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/prober/handler.go b/prober/handler.go index 830c8572..2a8a1d9c 100644 --- a/prober/handler.go +++ b/prober/handler.go @@ -108,10 +108,8 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo } if module.Prober == "tcp" && hostname != "" { - err = setTLSServerName(hostname, &module) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return + if module.TCP.TLSConfig.ServerName == "" { + module.TCP.TLSConfig.ServerName = hostname } } @@ -145,15 +143,6 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo h.ServeHTTP(w, r) } -func setTLSServerName(hostname string, module *config.Module) error { - // By creating a new hashmap and copying values there we - // ensure that the initial configuration remain intact. - if module.TCP.TLSConfig.ServerName == "" { - module.TCP.TLSConfig.ServerName = hostname - } - return nil -} - func setHTTPHost(hostname string, module *config.Module) error { // By creating a new hashmap and copying values there we // ensure that the initial configuration remain intact.