9cc0caff1e
Lands the protocol layer end-to-end: an agent can be enrolled through the operator UI, store credentials, dial back to the server over WS, complete the protocol_version handshake, and stay connected with periodic heartbeats. Server side: - P1-09 ws.Hub: one Conn per host_id, last-write-wins eviction, json envelope writer with a write mutex, reader, error envelopes. - P1-09 ws.AgentHandler: bearer-auth, accept upgrade, hello-stage (10s deadline, protocol_version checked against api.MinAgentProtocolVersion → ErrProtocolTooOld with help URL on reject), main read loop, defer hub register/unregister. - P1-10 POST /api/agents/enroll consumes a one-time token, mints a persistent agent bearer (sha-256 stored), creates a host row. - P1-10 POST /api/enrollment-tokens (operator, session-auth) issues a 1h one-time token. - P1-11 hello upserts agent_version + restic_version + protocol_version on the host row, flips status to online. - P1-12 heartbeat touches last_seen_at; background sweeper marks hosts offline after 90s without one. - store: hosts table accessors, host_schedule_version, enrollment_tokens FK on consumed_host dropped (audit-only field; the token gets burned before the host row exists). Agent side: - P1-13 internal/agent/config: yaml at /etc/restic-manager/agent.yaml, atomic Save (tmp+fsync+rename), Enrolled() helper. - P1-15 internal/agent/wsclient: dial with bearer + optional TLS cert pinning (sha-256 of leaf), exponential backoff with jitter (1s → 60s cap), heartbeat goroutine, fatal handling for ErrProtocolTooOld. - P1-15 wsclient.Enroll: HTTP POST /api/agents/enroll with sysinfo. - P1-17 internal/agent/sysinfo: hostname/OS/arch/restic-version collection. restic detected by `restic version` parse; absent restic doesn't block startup. - cmd/agent: -enroll-server / -enroll-token flags drive first-run enrollment then exit (so the install script can hand off to systemd to run the persistent service). End-to-end smoke verified: bootstrap → login → issue token → enroll → run agent → server logs `ws agent connected` with the right host_id and protocol_version 1. All tests still pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
146 lines
4.1 KiB
Go
146 lines
4.1 KiB
Go
// Package ws hosts the WebSocket transport for agent ↔ server. The
|
|
// Hub tracks one active connection per host id; subsequent connections
|
|
// from the same host evict the prior one (last-write-wins).
|
|
package ws
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
|
|
"gitea.dcglab.co.uk/steve/restic-manager/internal/api"
|
|
)
|
|
|
|
// Hub owns the live agent connections and routes messages.
|
|
type Hub struct {
|
|
mu sync.RWMutex
|
|
conns map[string]*Conn // hostID → conn
|
|
}
|
|
|
|
// NewHub returns an empty hub.
|
|
func NewHub() *Hub {
|
|
return &Hub{conns: make(map[string]*Conn)}
|
|
}
|
|
|
|
// Conn is one agent WS connection. Send is safe for concurrent use;
|
|
// Read is single-reader (the connection's run loop).
|
|
type Conn struct {
|
|
HostID string
|
|
c *websocket.Conn
|
|
|
|
writeMu sync.Mutex
|
|
}
|
|
|
|
// Register installs c as the canonical connection for hostID. Any
|
|
// previous connection for that host is closed.
|
|
func (h *Hub) Register(hostID string, c *Conn) {
|
|
h.mu.Lock()
|
|
if prev, ok := h.conns[hostID]; ok {
|
|
// Best-effort close — a stuck old socket shouldn't block new one.
|
|
go func(old *Conn) {
|
|
_ = old.c.Close(websocket.StatusPolicyViolation, "superseded")
|
|
}(prev)
|
|
}
|
|
h.conns[hostID] = c
|
|
h.mu.Unlock()
|
|
}
|
|
|
|
// Unregister removes c iff it is still the canonical conn (a race
|
|
// where a newer conn already replaced it must not unregister it).
|
|
func (h *Hub) Unregister(hostID string, c *Conn) {
|
|
h.mu.Lock()
|
|
if cur, ok := h.conns[hostID]; ok && cur == c {
|
|
delete(h.conns, hostID)
|
|
}
|
|
h.mu.Unlock()
|
|
}
|
|
|
|
// Send delivers an envelope to the host if connected. Returns an error
|
|
// if the host is offline; caller may queue the message for later.
|
|
func (h *Hub) Send(ctx context.Context, hostID string, env api.Envelope) error {
|
|
h.mu.RLock()
|
|
c, ok := h.conns[hostID]
|
|
h.mu.RUnlock()
|
|
if !ok {
|
|
return fmt.Errorf("ws: host %q is offline", hostID)
|
|
}
|
|
return c.Send(ctx, env)
|
|
}
|
|
|
|
// Connected reports whether hostID has an active connection.
|
|
func (h *Hub) Connected(hostID string) bool {
|
|
h.mu.RLock()
|
|
_, ok := h.conns[hostID]
|
|
h.mu.RUnlock()
|
|
return ok
|
|
}
|
|
|
|
// ----- Conn methods --------------------------------------------------
|
|
|
|
// NewConn wraps a freshly-accepted websocket for a given hostID.
|
|
func NewConn(hostID string, c *websocket.Conn) *Conn {
|
|
return &Conn{HostID: hostID, c: c}
|
|
}
|
|
|
|
// Send writes an envelope as a JSON text message. Concurrent calls
|
|
// are serialised; the underlying socket is not safe for parallel
|
|
// writers.
|
|
func (c *Conn) Send(ctx context.Context, env api.Envelope) error {
|
|
c.writeMu.Lock()
|
|
defer c.writeMu.Unlock()
|
|
raw, err := json.Marshal(env)
|
|
if err != nil {
|
|
return fmt.Errorf("ws: marshal envelope: %w", err)
|
|
}
|
|
return c.c.Write(ctx, websocket.MessageText, raw)
|
|
}
|
|
|
|
// SendError writes an error envelope and closes the socket. Used by
|
|
// the hello handshake when an agent is rejected.
|
|
func (c *Conn) SendError(ctx context.Context, code api.ErrorCode, msg, helpURL string) {
|
|
env, err := api.Marshal(api.MsgError, "", api.ErrorPayload{
|
|
Code: code, Message: msg, HelpURL: helpURL,
|
|
})
|
|
if err == nil {
|
|
writeCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
_ = c.Send(writeCtx, env)
|
|
}
|
|
_ = c.c.Close(websocket.StatusPolicyViolation, string(code))
|
|
}
|
|
|
|
// Close shuts the socket down with a normal-closure status code.
|
|
func (c *Conn) Close() error {
|
|
return c.c.Close(websocket.StatusNormalClosure, "")
|
|
}
|
|
|
|
// Read pulls the next JSON envelope off the wire. The caller's
|
|
// context controls cancellation and timeouts (e.g. read deadlines).
|
|
func (c *Conn) Read(ctx context.Context) (api.Envelope, error) {
|
|
mt, raw, err := c.c.Read(ctx)
|
|
if err != nil {
|
|
return api.Envelope{}, err
|
|
}
|
|
if mt != websocket.MessageText {
|
|
return api.Envelope{}, errors.New("ws: expected text frame")
|
|
}
|
|
var env api.Envelope
|
|
if err := json.Unmarshal(raw, &env); err != nil {
|
|
return api.Envelope{}, fmt.Errorf("ws: unmarshal envelope: %w", err)
|
|
}
|
|
return env, nil
|
|
}
|
|
|
|
// ----- helpers -------------------------------------------------------
|
|
|
|
// LogValue emits a slog-friendly representation of a Conn.
|
|
func (c *Conn) LogValue() slog.Value {
|
|
return slog.GroupValue(slog.String("host_id", c.HostID))
|
|
}
|