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

+7 -4
View File
@@ -1,5 +1,7 @@
package httpapi
import "cloudipvalidator/internal/inbound"
// Request/response bodies for the /api/v1 surface. Kept in one file since
// they're small and mostly 1:1 with a single handler each.
@@ -86,10 +88,11 @@ type registerProberRequest struct {
}
type proberAssignment 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 proberResultDTO struct {
+4 -5
View File
@@ -4,6 +4,7 @@ import (
"time"
"cloudipvalidator/internal/db"
"cloudipvalidator/internal/inbound"
)
// DTOs for the admin queue-management and dynamic-config endpoints
@@ -254,12 +255,10 @@ type putOrchestratorSettingsRequest struct {
SelfCheckMaxAttempts *int `json:"self_check_max_attempts"`
}
// inboundChecksDTO doubles as both the GET response and the PUT request
// body for /api/v1/admin/config/inbound-checks. Same field shape as
// proberAssignment.Ports/ICMP (dto.go).
type inboundChecksDTO struct {
Ports []int `json:"ports"`
ICMP bool `json:"icmp"`
Ports []int `json:"ports"`
ICMP bool `json:"icmp"`
TLSChecks []inbound.TLSCheck `json:"tls_checks"`
}
// autoCycleDTO is the status+parameters object returned by every
+19 -7
View File
@@ -1,11 +1,13 @@
package httpapi
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"cloudipvalidator/internal/db"
"cloudipvalidator/internal/inbound"
)
// --- validators ---
@@ -261,21 +263,31 @@ func (s *Server) handleConfigGetInboundChecks(w http.ResponseWriter, r *http.Req
writeDBError(w, err)
return
}
writeJSON(w, http.StatusOK, inboundChecksDTO{Ports: settings.Ports, ICMP: settings.ICMP})
writeJSON(w, http.StatusOK, inboundChecksDTO{Ports: settings.Ports, ICMP: settings.ICMP, TLSChecks: settings.TLSChecks})
}
// handleConfigPutInboundChecks writes directly to the DB (unlike the
// orchestrator-settings PUT above) because port-range/duplicate validation
// is purely local — no cross-field dependency on other orchestrator config.
func (s *Server) handleConfigPutInboundChecks(w http.ResponseWriter, r *http.Request) {
var req inboundChecksDTO
var req struct {
Ports []int `json:"ports"`
ICMP bool `json:"icmp"`
TLSChecks json.RawMessage `json:"tls_checks"`
}
if err := readJSON(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
return
}
if err := s.DB.SetInboundChecks(r.Context(), req.Ports, req.ICMP); err != nil {
var tlsChecks *[]inbound.TLSCheck
if req.TLSChecks != nil {
var checks []inbound.TLSCheck
if err := json.Unmarshal(req.TLSChecks, &checks); err != nil {
writeError(w, http.StatusBadRequest, "invalid tls_checks: "+err.Error())
return
}
tlsChecks = &checks
}
if err := s.DB.SetInboundChecks(r.Context(), req.Ports, req.ICMP, tlsChecks); err != nil {
writeDBError(w, err)
return
}
writeJSON(w, http.StatusOK, inboundChecksDTO{Ports: req.Ports, ICMP: req.ICMP})
s.handleConfigGetInboundChecks(w, r)
}
+39 -1
View File
@@ -13,6 +13,7 @@ import (
"cloudipvalidator/internal/config"
"cloudipvalidator/internal/db"
"cloudipvalidator/internal/inbound"
"cloudipvalidator/internal/openstack"
"cloudipvalidator/internal/orchestrator"
)
@@ -623,7 +624,9 @@ func TestInboundChecksReflectedInProberAssignmentsWithoutRestart(t *testing.T) {
t.Fatalf("expected seeded {[22 80] true} before PUT, got %+v", assignments)
}
resp, body = fc.do(http.MethodPut, "/api/v1/admin/config/inbound-checks", inboundChecksDTO{Ports: []int{8080}, ICMP: false})
resp, body = fc.do(http.MethodPut, "/api/v1/admin/config/inbound-checks", inboundChecksDTO{
Ports: []int{8080}, TLSChecks: []inbound.TLSCheck{{Port: 8443, Version: "1.2"}, {Port: 9443, Version: "1.3"}},
})
if resp.StatusCode != http.StatusOK {
t.Fatalf("put inbound checks: status=%d body=%s", resp.StatusCode, body)
}
@@ -638,6 +641,41 @@ func TestInboundChecksReflectedInProberAssignmentsWithoutRestart(t *testing.T) {
if len(assignments) != 1 || len(assignments[0].Ports) != 1 || assignments[0].Ports[0] != 8080 || assignments[0].ICMP {
t.Fatalf("expected updated {[8080] false} without restart, got %+v", assignments)
}
if checks := assignments[0].TLSChecks; len(checks) != 2 ||
checks[0] != (inbound.TLSCheck{Port: 8443, Version: "1.2"}) ||
checks[1] != (inbound.TLSCheck{Port: 9443, Version: "1.3"}) {
t.Fatalf("unexpected TLS checks in assignment: %+v", checks)
}
}
func TestInboundTLSModesAndValidation(t *testing.T) {
fc, _, _, _ := newConfigTestHarness(t)
const path = "/api/v1/admin/config/inbound-checks"
for _, tc := range []struct {
input, wantTLS string
status int
}{
{`{"ports":[443],"tls_checks":[{"port":8443,"version":"1.2"}]}`, `[{"port":8443,"version":"1.2"}]`, http.StatusOK},
{`{"ports":[22]}`, `[{"port":8443,"version":"1.2"}]`, http.StatusOK},
{`{"ports":[443],"tls_checks":[{"port":0,"version":"1.2"}]}`, "", http.StatusBadRequest},
{`{"ports":[443],"tls_checks":[{"port":8443,"version":"1.1"}]}`, "", http.StatusBadRequest},
{`{"ports":[443],"tls_checks":[{"port":8443,"version":"1.2"},{"port":8443,"version":"1.3"}]}`, "", http.StatusBadRequest},
{`{"ports":[443],"tls_checks":[]}`, "[]", http.StatusOK},
{`{"ports":[443],"tls_checks":null}`, "null", http.StatusOK},
} {
resp, body := fc.do(http.MethodPut, path, json.RawMessage(tc.input))
if resp.StatusCode != tc.status {
t.Fatalf("%s: status=%d, body=%s", tc.input, resp.StatusCode, body)
}
if tc.status == http.StatusOK {
var got struct {
TLSChecks json.RawMessage `json:"tls_checks"`
}
if err := json.Unmarshal(body, &got); err != nil || string(got.TLSChecks) != tc.wantTLS {
t.Fatalf("%s: want tls_checks=%s, got %s (%v)", tc.input, tc.wantTLS, got.TLSChecks, err)
}
}
}
}
// TestProberRegisterSetsHostnameAndIdleState proves POST
+1 -1
View File
@@ -74,7 +74,7 @@ func (s *Server) handleProberAssignments(w http.ResponseWriter, r *http.Request)
for _, item := range items {
out = append(out, proberAssignment{
IPID: item.ID, IPAddress: item.IPAddress,
Ports: inbound.Ports, ICMP: inbound.ICMP,
Ports: inbound.Ports, ICMP: inbound.ICMP, TLSChecks: inbound.TLSChecks,
})
}
writeJSON(w, http.StatusOK, out)