Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hostname parameter for TCP probe #981

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions prober/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo
}
}

if module.Prober == "tcp" && hostname != "" {
electron0zero marked this conversation as resolved.
Show resolved Hide resolved
if module.TCP.TLSConfig.ServerName == "" {
module.TCP.TLSConfig.ServerName = hostname
}
}

sl := newScrapeLogger(logger, moduleName, target)
level.Info(sl).Log("msg", "Beginning probe", "probe", module.Prober, "timeout_seconds", timeoutSeconds)

Expand Down
54 changes: 54 additions & 0 deletions prober/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package prober
import (
"bytes"
"fmt"
"net"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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())
}

}