Support configurable TLS probes in the API and dashboard

This commit is contained in:
ayurishchev committed 2026-11-20 21:15:22 +03:00
1 parent 0bde5627f0
commit c7fdd2c430
38 files changed
+495 -203

No files matched your search

+21 -3
View File
@@ -3,6 +3,7 @@ package checkrunner
import (
"context"
"crypto/tls"
"fmt"
"net"
"strconv"
"time"
@@ -16,15 +17,25 @@ import (
// to expect a signed cert yet. This checks "is there a real TLS listener
// here", not "is its certificate valid" — same scope-limiting principle
// SSHBanner already applies (banner only, no auth handshake).
func TLSHandshake(host string, port int, timeout time.Duration) func(ctx context.Context) Result {
func TLSHandshake(host string, port int, version string, timeout time.Duration) func(ctx context.Context) Result {
target := net.JoinHostPort(host, strconv.Itoa(port))
checkType := "tls-" + strconv.Itoa(port)
return run(checkType, target, func(ctx context.Context) error {
probe := run(checkType, target, func(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
config := &tls.Config{InsecureSkipVerify: true}
switch version {
case "":
case "1.2":
config.MinVersion, config.MaxVersion = tls.VersionTLS12, tls.VersionTLS12
case "1.3":
config.MinVersion, config.MaxVersion = tls.VersionTLS13, tls.VersionTLS13
default:
return fmt.Errorf("unsupported TLS version %q", version)
}
d := tls.Dialer{
NetDialer: &net.Dialer{Timeout: timeout},
Config: &tls.Config{InsecureSkipVerify: true},
Config: config,
}
conn, err := d.DialContext(ctx, "tcp", target)
if err != nil {
@@ -32,4 +43,11 @@ func TLSHandshake(host string, port int, timeout time.Duration) func(ctx context
}
return conn.Close()
})
return func(ctx context.Context) Result {
result := probe(ctx)
if version != "" {
result.Detail = "TLS " + version + ": " + result.Detail
}
return result
}
}
+29 -3
View File
@@ -2,9 +2,11 @@ package checkrunner
import (
"context"
"crypto/tls"
"net"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
@@ -29,7 +31,7 @@ func TestTLSHandshakeSucceedsAgainstTLSServer(t *testing.T) {
defer ts.Close()
host, port := splitHostPortInt(t, ts.Listener.Addr().String())
res := TLSHandshake(host, port, time.Second)(context.Background())
res := TLSHandshake(host, port, "", time.Second)(context.Background())
if !res.Success {
t.Fatalf("expected success, got failure: %s", res.Detail)
}
@@ -47,7 +49,7 @@ func TestTLSHandshakeFailsAgainstPlainTCP(t *testing.T) {
defer ts.Close()
host, port := splitHostPortInt(t, ts.Listener.Addr().String())
res := TLSHandshake(host, port, time.Second)(context.Background())
res := TLSHandshake(host, port, "", time.Second)(context.Background())
if res.Success {
t.Fatalf("expected failure against a plain TCP listener, got success")
}
@@ -63,8 +65,32 @@ func TestTLSHandshakeFailsOnConnectionRefused(t *testing.T) {
_, port := splitHostPortInt(t, l.Addr().String())
l.Close() // close immediately so the port refuses connections
res := TLSHandshake("127.0.0.1", port, time.Second)(context.Background())
res := TLSHandshake("127.0.0.1", port, "", time.Second)(context.Background())
if res.Success {
t.Fatalf("expected failure against a closed port, got success")
}
}
func TestTLSHandshakeRequiresExactVersion(t *testing.T) {
for _, server := range []struct {
name string
version uint16
}{{"1.2", tls.VersionTLS12}, {"1.3", tls.VersionTLS13}} {
t.Run(server.name, func(t *testing.T) {
ts := httptest.NewUnstartedServer(nil)
ts.TLS = &tls.Config{MinVersion: server.version, MaxVersion: server.version}
ts.StartTLS()
defer ts.Close()
host, port := splitHostPortInt(t, ts.Listener.Addr().String())
for _, requested := range []string{"", "1.2", "1.3"} {
result := TLSHandshake(host, port, requested, time.Second)(context.Background())
if result.Success != (requested == "" || requested == server.name) {
t.Errorf("TLS %q against %s: %+v", requested, server.name, result)
}
if requested != "" && !strings.HasPrefix(result.Detail, "TLS "+requested+": ") {
t.Errorf("missing requested version in result detail: %q", result.Detail)
}
}
})
}
}