Support configurable TLS probes in the API and dashboard
This commit is contained in:
1 parent
0bde5627f0
commit
c7fdd2c430
38 files changed
+495
-203
No files matched your search
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user