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
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"cloudipvalidator/internal/config"
|
||||
"cloudipvalidator/internal/inbound"
|
||||
)
|
||||
|
||||
// BootstrapFromConfig ensures the database's baseline state matches the
|
||||
@@ -37,7 +38,7 @@ func (d *DB) BootstrapFromConfig(ctx context.Context, cfg *config.ControlAPI) er
|
||||
if err := d.bootstrapSettings(ctx, cfg.Orchestrator.FIPSettleSeconds); err != nil {
|
||||
return fmt.Errorf("bootstrap settings: %w", err)
|
||||
}
|
||||
if err := d.bootstrapInboundChecks(ctx, cfg.Inbound.Ports, cfg.Inbound.ICMP); err != nil {
|
||||
if err := d.bootstrapInboundChecks(ctx, cfg.Inbound); err != nil {
|
||||
return fmt.Errorf("bootstrap inbound checks: %w", err)
|
||||
}
|
||||
if err := d.SeedQueue(ctx, cfg.IPAddresses); err != nil {
|
||||
@@ -125,7 +126,7 @@ func (d *DB) bootstrapSettings(ctx context.Context, fipSettleSeconds int) error
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DB) bootstrapInboundChecks(ctx context.Context, ports []int, icmp bool) error {
|
||||
func (d *DB) bootstrapInboundChecks(ctx context.Context, settings config.InboundConfig) error {
|
||||
var count int
|
||||
if err := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM inbound_checks_settings`).Scan(&count); err != nil {
|
||||
return err
|
||||
@@ -133,16 +134,23 @@ func (d *DB) bootstrapInboundChecks(ctx context.Context, ports []int, icmp bool)
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
if ports == nil {
|
||||
ports = []int{}
|
||||
if err := inbound.ValidateTLSChecks(settings.TLSChecks); err != nil {
|
||||
return err
|
||||
}
|
||||
payload, err := json.Marshal(ports)
|
||||
if settings.Ports == nil {
|
||||
settings.Ports = []int{}
|
||||
}
|
||||
payload, err := json.Marshal(settings.Ports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tlsChecks, err := json.Marshal(settings.TLSChecks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := timeToDB(Now())
|
||||
_, err = d.ExecContext(ctx, `
|
||||
INSERT INTO inbound_checks_settings (id, ports, icmp, created_at, updated_at) VALUES (1, ?, ?, ?, ?)
|
||||
`, string(payload), icmp, now, now)
|
||||
INSERT INTO inbound_checks_settings (id, ports, icmp, tls_checks, created_at, updated_at) VALUES (1, ?, ?, ?, ?, ?)
|
||||
`, string(payload), settings.ICMP, string(tlsChecks), now, now)
|
||||
return err
|
||||
}
|
||||
@@ -56,6 +56,9 @@ var dropChecksIndexSchema string
|
||||
//go:embed migrations/0014_diagnostic_holds.sql
|
||||
var diagnosticHoldsSchema string
|
||||
|
||||
//go:embed migrations/0015_inbound_tls_checks.sql
|
||||
var inboundTLSChecksSchema string
|
||||
|
||||
// migrations is the ordered list of schema versions. Each entry's SQL is
|
||||
// applied, in order, for any version greater than the database's current
|
||||
// PRAGMA user_version — so a fresh database walks the whole list and an
|
||||
@@ -78,6 +81,7 @@ var migrations = []struct {
|
||||
{12, selfCheckFailuresSchema},
|
||||
{13, dropChecksIndexSchema},
|
||||
{14, diagnosticHoldsSchema},
|
||||
{15, inboundTLSChecksSchema},
|
||||
}
|
||||
|
||||
// connPragmas are applied to every new connection. A larger WAL checkpoint
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE inbound_checks_settings ADD COLUMN tls_checks TEXT NOT NULL DEFAULT 'null';
|
||||
@@ -3,6 +3,8 @@ package db
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cloudipvalidator/internal/inbound"
|
||||
)
|
||||
|
||||
// Validator and IP lifecycle states. Kept as typed string constants rather
|
||||
@@ -294,11 +296,12 @@ const (
|
||||
)
|
||||
|
||||
// InboundChecksSettings is the singleton row describing what the prober
|
||||
// checks on every site for every in-flight IP (TCP ports + optional ICMP).
|
||||
// checks on every site for every in-flight IP.
|
||||
// Admin-configurable at runtime (see queries_inbound.go).
|
||||
type InboundChecksSettings struct {
|
||||
Ports []int
|
||||
ICMP bool
|
||||
TLSChecks []inbound.TLSCheck
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"cloudipvalidator/internal/inbound"
|
||||
)
|
||||
|
||||
// GetInboundChecks returns the singleton inbound-checks settings row.
|
||||
@@ -12,10 +14,10 @@ import (
|
||||
// normal condition.
|
||||
func (d *DB) GetInboundChecks(ctx context.Context) (InboundChecksSettings, error) {
|
||||
var s InboundChecksSettings
|
||||
var portsJSON, createdAt, updatedAt string
|
||||
var portsJSON, tlsJSON, createdAt, updatedAt string
|
||||
err := d.QueryRowContext(ctx, `
|
||||
SELECT ports, icmp, created_at, updated_at FROM inbound_checks_settings WHERE id=1
|
||||
`).Scan(&portsJSON, &s.ICMP, &createdAt, &updatedAt)
|
||||
SELECT ports, icmp, tls_checks, created_at, updated_at FROM inbound_checks_settings WHERE id=1
|
||||
`).Scan(&portsJSON, &s.ICMP, &tlsJSON, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
return InboundChecksSettings{}, err
|
||||
}
|
||||
@@ -25,6 +27,9 @@ func (d *DB) GetInboundChecks(ctx context.Context) (InboundChecksSettings, error
|
||||
if s.Ports == nil {
|
||||
s.Ports = []int{}
|
||||
}
|
||||
if err := json.Unmarshal([]byte(tlsJSON), &s.TLSChecks); err != nil {
|
||||
return InboundChecksSettings{}, fmt.Errorf("decode inbound TLS checks: %w", err)
|
||||
}
|
||||
if s.CreatedAt, err = dbToTime(createdAt); err != nil {
|
||||
return InboundChecksSettings{}, err
|
||||
}
|
||||
@@ -34,12 +39,9 @@ func (d *DB) GetInboundChecks(ctx context.Context) (InboundChecksSettings, error
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// SetInboundChecks persists a new prober check configuration. Validation is
|
||||
// purely local (port range, no duplicates) — unlike fip_settle_seconds,
|
||||
// there's no cross-field dependency on other orchestrator config, so this
|
||||
// is called directly by the HTTP handler without going through
|
||||
// orchestrator.Orchestrator.
|
||||
func (d *DB) SetInboundChecks(ctx context.Context, ports []int, icmp bool) error {
|
||||
// SetInboundChecks preserves the TLS configuration when tlsChecks is nil.
|
||||
// A pointer to a nil slice selects automatic mode.
|
||||
func (d *DB) SetInboundChecks(ctx context.Context, ports []int, icmp bool, tlsChecks *[]inbound.TLSCheck) error {
|
||||
seen := make(map[int]bool, len(ports))
|
||||
for _, p := range ports {
|
||||
if p < 1 || p > 65535 {
|
||||
@@ -57,9 +59,19 @@ func (d *DB) SetInboundChecks(ctx context.Context, ports []int, icmp bool) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := timeToDB(Now())
|
||||
var tlsJSON any
|
||||
if tlsChecks != nil {
|
||||
if err := inbound.ValidateTLSChecks(*tlsChecks); err != nil {
|
||||
return fmt.Errorf("%v: %w", err, ErrValidation)
|
||||
}
|
||||
encoded, err := json.Marshal(*tlsChecks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tlsJSON = string(encoded)
|
||||
}
|
||||
_, err = d.ExecContext(ctx, `
|
||||
UPDATE inbound_checks_settings SET ports=?, icmp=?, updated_at=? WHERE id=1
|
||||
`, string(payload), icmp, now)
|
||||
UPDATE inbound_checks_settings SET ports=?, icmp=?, tls_checks=COALESCE(?, tls_checks), updated_at=? WHERE id=1
|
||||
`, string(payload), icmp, tlsJSON, timeToDB(Now()))
|
||||
return err
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func TestSetInboundChecksRejectsPortOutOfRange(t *testing.T) {
|
||||
t.Fatalf("bootstrap: %v", err)
|
||||
}
|
||||
for _, p := range []int{0, -1, 65536, 70000} {
|
||||
if err := d.SetInboundChecks(ctx, []int{p}, false); !errors.Is(err, ErrValidation) {
|
||||
if err := d.SetInboundChecks(ctx, []int{p}, false, nil); !errors.Is(err, ErrValidation) {
|
||||
t.Fatalf("port %d: expected ErrValidation, got %v", p, err)
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ func TestSetInboundChecksRejectsDuplicatePorts(t *testing.T) {
|
||||
if err := d.BootstrapFromConfig(ctx, &config.ControlAPI{}); err != nil {
|
||||
t.Fatalf("bootstrap: %v", err)
|
||||
}
|
||||
if err := d.SetInboundChecks(ctx, []int{22, 80, 22}, false); !errors.Is(err, ErrValidation) {
|
||||
if err := d.SetInboundChecks(ctx, []int{22, 80, 22}, false, nil); !errors.Is(err, ErrValidation) {
|
||||
t.Fatalf("expected ErrValidation for duplicate port, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ func TestGetSetInboundChecksRoundTrip(t *testing.T) {
|
||||
t.Fatalf("expected default {[] false}, got %+v", before)
|
||||
}
|
||||
|
||||
if err := d.SetInboundChecks(ctx, []int{22, 443, 8080}, true); err != nil {
|
||||
if err := d.SetInboundChecks(ctx, []int{22, 443, 8080}, true, nil); err != nil {
|
||||
t.Fatalf("set inbound checks: %v", err)
|
||||
}
|
||||
after, err := d.GetInboundChecks(ctx)
|
||||
@@ -90,7 +90,7 @@ func TestGetSetInboundChecksRoundTrip(t *testing.T) {
|
||||
|
||||
// Setting an empty port list + icmp:false is legal — it's the "disable
|
||||
// all inbound checks without touching sites" configuration.
|
||||
if err := d.SetInboundChecks(ctx, nil, false); err != nil {
|
||||
if err := d.SetInboundChecks(ctx, nil, false, nil); err != nil {
|
||||
t.Fatalf("set empty inbound checks: %v", err)
|
||||
}
|
||||
cleared, err := d.GetInboundChecks(ctx)
|
||||
|
||||
@@ -391,7 +391,7 @@ func TestMigration0011BuildsRunsFromExistingData(t *testing.T) {
|
||||
}
|
||||
var ver int
|
||||
d.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&ver)
|
||||
if ver != 14 {
|
||||
if ver != 15 {
|
||||
t.Errorf("user_version = %d", ver)
|
||||
}
|
||||
}
|
||||
@@ -240,7 +240,7 @@ func TestMigration0010MarksRowsAfterVerdict(t *testing.T) {
|
||||
t.Errorf("ssh: after_verdict=%d recorded=%s created=%s", a, rec, cr)
|
||||
}
|
||||
var ver int
|
||||
if err := d.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&ver); err != nil || ver != 14 {
|
||||
if err := d.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&ver); err != nil || ver != 15 {
|
||||
t.Errorf("user_version=%d err=%v", ver, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user