84b185be39
Phase A - Stability: - Engine.Start(): recovers orphaned jobs (running/queued/waking_up) after crash - Engine.Stop(): graceful shutdown - cancels all in-flight jobs and waits - Queue keyed by jobID (not syncPairID): cancel now targets exact job - Local rsync uses jobCtx (context.Background() replaced) - Migrations wrapped in transactions; checksums stored Phase B - Security: - admin/admin default removed: SYNCSERVER_ADMIN_PASSWORD required on first run - Path validation: rejects .., leading -, null bytes in sync pair paths - Rsync flags allowlist: dangerous flags blocked (--rsync-path, -e, --files-from) - Shell concat in RunRemote replaced with proper sh -c escaping - knownhosts: replaced custom parser with golang.org/x/crypto/ssh/knownhosts - RequireAdmin wired: machine CRUD, SSH key ops, settings require admin role - deploy-keys: uses authorized_keys only (no private key upload) - Hardcoded /var/lib/syncserver/ssh paths replaced with cfg.SSHDir() Phase C - Operational: - /readyz health check: DB query + SSH dir accessibility - /metrics endpoint: Prometheus text format (jobs, queue, machines) - Event struct JSON tags: job_id, machine_id, type (snake_case) - EventBus broadcast: fanned out to all subscribers - SQLite VACUUM INTO backup: scheduled before cleanup if BackupDir set - Filesystem job log cleanup: removes .log files for purged jobs - Backup retention: old backups auto-purged Phase D - Frontend: - Schedules page: REST API + full CRUD UI for cron schedules - Dashboard: cancel button for running/queued jobs - JobDetail: server-side log download via API - Settings: displays data_dir from server - 404 page: proper NotFound component Phase E - Tests: - auth_test.go: JWT, bcrypt, middleware, seed (18 tests) - models_test.go: Job, Machine, SyncPair, Schedule repos (18 tests) - go test -race: no data races found
234 lines
6.7 KiB
Go
234 lines
6.7 KiB
Go
package api
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/syncserver/internal/models"
|
|
)
|
|
|
|
type SyncPairHandler struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewSyncPairHandler(db *sql.DB) *SyncPairHandler {
|
|
return &SyncPairHandler{db: db}
|
|
}
|
|
|
|
var directionRegex = regexp.MustCompile(`^(push|pull|mirror)$`)
|
|
|
|
func ValidatePath(path string) error {
|
|
path = strings.TrimSpace(path)
|
|
if path == "" {
|
|
return fmt.Errorf("path cannot be empty")
|
|
}
|
|
if strings.Contains(path, "..") {
|
|
return fmt.Errorf("path cannot contain '..'")
|
|
}
|
|
if strings.HasPrefix(path, "-") {
|
|
return fmt.Errorf("path cannot start with '-'")
|
|
}
|
|
if strings.Contains(path, "\x00") {
|
|
return fmt.Errorf("path cannot contain null bytes")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *SyncPairHandler) List(w http.ResponseWriter, r *http.Request) {
|
|
repo := models.NewSyncPairRepository(h.db)
|
|
pairs, err := repo.GetAll()
|
|
if err != nil {
|
|
slog.Error("failed to fetch sync pairs", "error", err)
|
|
writeError(w, http.StatusInternalServerError, "failed to fetch sync pairs")
|
|
return
|
|
}
|
|
out := make([]SyncPairResponse, len(pairs))
|
|
for i, p := range pairs {
|
|
out[i] = syncPairToResp(p)
|
|
}
|
|
writeJSON(w, out)
|
|
}
|
|
|
|
func (h *SyncPairHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid id")
|
|
return
|
|
}
|
|
repo := models.NewSyncPairRepository(h.db)
|
|
p, err := repo.GetByID(id)
|
|
if err == sql.ErrNoRows {
|
|
writeError(w, http.StatusNotFound, "sync pair not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
slog.Error("failed to fetch sync pair", "id", id, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "failed to fetch sync pair")
|
|
return
|
|
}
|
|
writeJSON(w, syncPairToResp(*p))
|
|
}
|
|
|
|
func (h *SyncPairHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|
var req SyncPairRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if req.Name == "" || req.SourcePath == "" || req.DestPath == "" {
|
|
writeError(w, http.StatusBadRequest, "name, source_path and dest_path are required")
|
|
return
|
|
}
|
|
if err := ValidatePath(req.SourcePath); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid source_path: "+err.Error())
|
|
return
|
|
}
|
|
if err := ValidatePath(req.DestPath); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid dest_path: "+err.Error())
|
|
return
|
|
}
|
|
if req.Direction == "" {
|
|
req.Direction = "push"
|
|
}
|
|
if !directionRegex.MatchString(req.Direction) {
|
|
writeError(w, http.StatusBadRequest, "direction must be push, pull, or mirror")
|
|
return
|
|
}
|
|
if req.SourceMachineID != nil && req.DestMachineID != nil && *req.SourceMachineID == *req.DestMachineID {
|
|
writeError(w, http.StatusBadRequest, "source and destination cannot be the same machine")
|
|
return
|
|
}
|
|
if req.RsyncFlags == "" {
|
|
req.RsyncFlags = "-aP"
|
|
}
|
|
if !req.Enabled {
|
|
req.Enabled = true
|
|
}
|
|
|
|
sp := &models.SyncPair{
|
|
Name: req.Name,
|
|
SourceMachineID: req.SourceMachineID,
|
|
SourcePath: req.SourcePath,
|
|
DestMachineID: req.DestMachineID,
|
|
DestPath: req.DestPath,
|
|
Direction: req.Direction,
|
|
RsyncFlags: req.RsyncFlags,
|
|
ExcludePatterns: req.ExcludePatterns,
|
|
Enabled: req.Enabled,
|
|
}
|
|
|
|
repo := models.NewSyncPairRepository(h.db)
|
|
id, err := repo.Create(sp)
|
|
if err != nil {
|
|
slog.Error("failed to create sync pair", "name", req.Name, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "failed to create sync pair")
|
|
return
|
|
}
|
|
sp.ID = id
|
|
w.Header().Set("Location", "/api/sync-pairs/"+strconv.FormatInt(id, 10))
|
|
writeJSON(w, syncPairToResp(*sp), http.StatusCreated)
|
|
}
|
|
|
|
func (h *SyncPairHandler) Update(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
var req SyncPairRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if req.Name == "" || req.SourcePath == "" || req.DestPath == "" {
|
|
writeError(w, http.StatusBadRequest, "name, source_path and dest_path are required")
|
|
return
|
|
}
|
|
if err := ValidatePath(req.SourcePath); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid source_path: "+err.Error())
|
|
return
|
|
}
|
|
if err := ValidatePath(req.DestPath); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid dest_path: "+err.Error())
|
|
return
|
|
}
|
|
if !directionRegex.MatchString(req.Direction) {
|
|
writeError(w, http.StatusBadRequest, "direction must be push, pull, or mirror")
|
|
return
|
|
}
|
|
if req.SourceMachineID != nil && req.DestMachineID != nil && *req.SourceMachineID == *req.DestMachineID {
|
|
writeError(w, http.StatusBadRequest, "source and destination cannot be the same machine")
|
|
return
|
|
}
|
|
|
|
repo := models.NewSyncPairRepository(h.db)
|
|
existing, err := repo.GetByID(id)
|
|
if err == sql.ErrNoRows {
|
|
writeError(w, http.StatusNotFound, "sync pair not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
slog.Error("failed to fetch sync pair", "id", id, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "failed to fetch sync pair")
|
|
return
|
|
}
|
|
|
|
existing.Name = req.Name
|
|
existing.SourceMachineID = req.SourceMachineID
|
|
existing.SourcePath = req.SourcePath
|
|
existing.DestMachineID = req.DestMachineID
|
|
existing.DestPath = req.DestPath
|
|
existing.Direction = req.Direction
|
|
existing.RsyncFlags = req.RsyncFlags
|
|
existing.ExcludePatterns = req.ExcludePatterns
|
|
existing.Enabled = req.Enabled
|
|
|
|
if err := repo.Update(existing); err != nil {
|
|
slog.Error("failed to update sync pair", "id", id, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "failed to update sync pair")
|
|
return
|
|
}
|
|
writeJSON(w, syncPairToResp(*existing))
|
|
}
|
|
|
|
func (h *SyncPairHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
repo := models.NewSyncPairRepository(h.db)
|
|
if err := repo.Delete(id); err != nil {
|
|
slog.Error("failed to delete sync pair", "id", id, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "failed to delete sync pair")
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func syncPairToResp(p models.SyncPair) SyncPairResponse {
|
|
return SyncPairResponse{
|
|
ID: p.ID,
|
|
Name: p.Name,
|
|
SourceMachineID: p.SourceMachineID,
|
|
SourcePath: p.SourcePath,
|
|
DestMachineID: p.DestMachineID,
|
|
DestPath: p.DestPath,
|
|
Direction: p.Direction,
|
|
RsyncFlags: p.RsyncFlags,
|
|
ExcludePatterns: p.ExcludePatterns,
|
|
Enabled: p.Enabled,
|
|
}
|
|
}
|