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

+13 -27
View File
@@ -1,7 +1,7 @@
// Package probercore implements a prober's poll loop: register once with
// its site identity, then each cycle fetch the set of IPs currently under
// test and run inbound reachability checks (TCP connect on each configured
// port, ICMP echo) directly against each one — this is the real,
// test and run inbound reachability checks (TCP, SSH, TLS and ICMP)
// directly against each one — this is the real,
// unmediated network test; only the control channel goes through the
// Control API.
package probercore
@@ -15,6 +15,7 @@ import (
"cloudipvalidator/internal/apiclient"
"cloudipvalidator/internal/checkrunner"
"cloudipvalidator/internal/config"
"cloudipvalidator/internal/inbound"
)
type Prober struct {
@@ -110,10 +111,11 @@ func (p *Prober) registerWithRetry(ctx context.Context) error {
}
type assignment struct {
IPID int64 `json:"ip_id"`
IPAddress string `json:"ip_address"`
Ports []int `json:"ports"`
ICMP bool `json:"icmp"`
IPID int64 `json:"ip_id"`
IPAddress string `json:"ip_address"`
Ports []int `json:"ports"`
ICMP bool `json:"icmp"`
TLSChecks []inbound.TLSCheck `json:"tls_checks"`
}
type resultDTO struct {
@@ -148,25 +150,6 @@ func (p *Prober) pollOnce(ctx context.Context) {
}
}
// extraChecksForPort returns checks to run in addition to the base
// TCPConnect for ports where a bare TCP handshake doesn't actually prove
// the expected service is behind it: a real TLS handshake on 443, a real
// SSH banner exchange on 22. nil for every other port.
func extraChecksForPort(host string, port int, timeout time.Duration) []func(ctx context.Context) checkrunner.Result {
switch port {
case 443:
return []func(ctx context.Context) checkrunner.Result{
checkrunner.TLSHandshake(host, port, timeout),
}
case 22:
return []func(ctx context.Context) checkrunner.Result{
checkrunner.SSHBanner(host, timeout),
}
default:
return nil
}
}
func (p *Prober) probeOne(ctx context.Context, a assignment) {
tcpTimeout := time.Duration(p.cfg.Checks.TCPTimeoutSeconds) * time.Second
icmpTimeout := time.Duration(p.cfg.Checks.ICMPTimeoutSeconds) * time.Second
@@ -182,10 +165,13 @@ func (p *Prober) probeOne(ctx context.Context, a assignment) {
for _, port := range a.Ports {
record(checkrunner.TCPConnect(a.IPAddress, port, tcpTimeout)(ctx))
for _, extra := range extraChecksForPort(a.IPAddress, port, tcpTimeout) {
record(extra(ctx))
if port == 22 {
record(checkrunner.SSHBanner(a.IPAddress, tcpTimeout)(ctx))
}
}
for _, check := range inbound.EffectiveTLSChecks(a.Ports, a.TLSChecks) {
record(checkrunner.TLSHandshake(a.IPAddress, check.Port, check.Version, tcpTimeout)(ctx))
}
if a.ICMP {
record(checkrunner.ICMPEcho(a.IPAddress, p.cfg.Checks.ICMPCount, icmpTimeout)(ctx))
}
+30 -27
View File
@@ -2,7 +2,10 @@ package probercore
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net"
"net/http"
"net/http/httptest"
"os"
@@ -13,6 +16,7 @@ import (
"cloudipvalidator/internal/apiclient"
"cloudipvalidator/internal/config"
"cloudipvalidator/internal/inbound"
)
func testLogger() *slog.Logger {
@@ -176,36 +180,35 @@ func TestRegisterWithRetryStopsOnCancel(t *testing.T) {
}
}
// TestExtraChecksForPort confirms only ports 443 (TLS) and 22 (SSH) get an
// extra protocol check beyond the baseline TCPConnect every port gets.
func TestExtraChecksForPort(t *testing.T) {
// Use a host nothing is listening on so the extra check's own network
// call fails fast — only its CheckType is asserted here, the
// port->check mapping, not real connectivity.
closedHost := "127.0.0.1"
cases := []struct {
port int
wantLen int
wantCheckType string
}{
{port: 443, wantLen: 1, wantCheckType: "tls-443"},
{port: 22, wantLen: 1, wantCheckType: "ssh"},
{port: 80, wantLen: 0},
{port: 8080, wantLen: 0},
}
for _, tc := range cases {
extra := extraChecksForPort(closedHost, tc.port, 100*time.Millisecond)
if len(extra) != tc.wantLen {
t.Fatalf("port %d: expected %d extra checks, got %d", tc.port, tc.wantLen, len(extra))
// TestProbeReportsCustomTLS checks the result sent for a TLS-only port.
func TestProbeReportsCustomTLS(t *testing.T) {
target := httptest.NewTLSServer(nil)
defer target.Close()
port := target.Listener.Addr().(*net.TCPAddr).Port
results := make(chan []resultDTO, 1)
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body struct {
Results []resultDTO `json:"results"`
}
if tc.wantLen == 0 {
continue
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
res := extra[0](context.Background())
if res.CheckType != tc.wantCheckType {
t.Fatalf("port %d: expected check type %q, got %q", tc.port, tc.wantCheckType, res.CheckType)
results <- body.Results
w.WriteHeader(http.StatusNoContent)
}))
defer api.Close()
cfg := &config.Prober{SiteID: "site-1", ControlAPIURL: api.URL, Checks: config.ProberChecks{TCPTimeoutSeconds: 1}}
New(cfg, testLogger()).probeOne(context.Background(), assignment{
IPID: 1, IPAddress: "127.0.0.1", TLSChecks: []inbound.TLSCheck{{Port: port, Version: "1.2"}},
})
select {
case got := <-results:
if len(got) != 1 || got[0].CheckType != fmt.Sprintf("tls-%d", port) || !got[0].Success || !got[0].Complete {
t.Fatalf("unexpected TLS results: %+v", got)
}
default:
t.Fatal("prober sent no results")
}
}