Add self-check via control-api (self_check.methods)

control-api is hosted outside the cloud and validators reach it directly,
so it sees the floating IP as the connection's source address. New open
route GET /api/v1/agents/{id}/observed-ip returns that address (taken only
from the TCP peer; forwarding headers are ignored so a validator cannot
forge it).

The agent gets self_check.methods, a priority-ordered list of ip_echo
(unchanged) and control_api; the default stays [ip_echo]. The self-check
passes when any method confirms the address; the next method is tried on
no answer and on a mismatch. Each method has its own timeout so a hung
first method cannot starve the fallback, and control_api uses a new TCP
connection per call (a connection opened before the floating IP was
attached would keep reporting the old address).

Also: docker agent template/env, example config, docs, plan in
docs/changes, e2e script switch E2E_SELF_CHECK_METHODS, rebuilt
bin/control-api and bin/validator-agent.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-02 03:24:20 +03:00
1 parent 146259cabb
commit abbee9a08a
23 files changed
+765 -35

No files matched your search

+45
View File
@@ -1,7 +1,12 @@
package httpapi
import (
"database/sql"
"errors"
"fmt"
"net"
"net/http"
"net/netip"
"time"
"cloudipvalidator/internal/db"
@@ -64,6 +69,46 @@ func (s *Server) handleAgentAssignment(w http.ResponseWriter, r *http.Request) {
})
}
// handleAgentObservedIP tells a validator which source address control-api
// sees for its connection, so the agent's self-check can confirm the floating
// IP without a third-party IP-echo service. The route is open, so it is
// limited to known validators to keep it from becoming a public "what is my
// IP" service.
func (s *Server) handleAgentObservedIP(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if _, err := s.DB.GetValidator(r.Context(), id); err != nil {
if errors.Is(err, sql.ErrNoRows) || errors.Is(err, db.ErrNotFound) {
writeError(w, http.StatusNotFound, "unknown validator: "+id)
return
}
writeError(w, http.StatusInternalServerError, err.Error())
return
}
ip, err := clientIP(r)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, http.StatusOK, observedIPResponse{IP: ip, Source: "remote_addr"})
}
// clientIP returns the peer address of the TCP connection in canonical form
// (IPv4-mapped IPv6 unmapped to plain IPv4). It deliberately ignores
// X-Forwarded-For / X-Real-IP: validators connect directly, and trusting a
// client-supplied header would let a validator forge the address and pass
// the self-check.
func clientIP(r *http.Request) (string, error) {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", fmt.Errorf("parse remote address %q: %w", r.RemoteAddr, err)
}
addr, err := netip.ParseAddr(host)
if err != nil {
return "", fmt.Errorf("parse remote address %q: %w", r.RemoteAddr, err)
}
return addr.Unmap().String(), nil
}
func (s *Server) handleAgentSelfCheck(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
var req selfCheckRequest