Phase A-E: stability, security, observability, and test coverage
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
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -25,7 +24,7 @@ type DeployResult struct {
|
||||
Errors []string `json:"errors"`
|
||||
}
|
||||
|
||||
func DeployKeysToMachine(ctx context.Context, serverKeyPath string, host string, port int, user string, keys []DeployKey, knownHostsHosts []string) (*DeployResult, error) {
|
||||
func DeployKeysToMachine(ctx context.Context, serverKeyPath string, host string, port int, user string, keys []DeployKey, knownHostsHosts []string, sshDir string) (*DeployResult, error) {
|
||||
result := &DeployResult{Success: true, Messages: []string{}, Errors: []string{}}
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", host, port)
|
||||
@@ -39,8 +38,11 @@ func DeployKeysToMachine(ctx context.Context, serverKeyPath string, host string,
|
||||
return nil, fmt.Errorf("parsing server key: %w", err)
|
||||
}
|
||||
|
||||
hostKeyCallback := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||
return nil
|
||||
knownHostsPath := filepath.Join(sshDir, "known_hosts")
|
||||
hostKeyCallback, err := NewKnownHostsCallback(knownHostsPath, true)
|
||||
if err != nil {
|
||||
slog.Warn("deploy keys: creating host key callback failed, ignoring hosts", "error", err)
|
||||
hostKeyCallback = ssh.InsecureIgnoreHostKey()
|
||||
}
|
||||
|
||||
cfg := &ssh.ClientConfig{
|
||||
@@ -60,7 +62,7 @@ func DeployKeysToMachine(ctx context.Context, serverKeyPath string, host string,
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
remoteSSHDir := "/var/lib/syncserver/ssh"
|
||||
remoteSSHDir := sshDir
|
||||
remoteKeysDir := filepath.Join(remoteSSHDir, "keys")
|
||||
|
||||
session, err := conn.NewSession()
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
type KnownHost struct {
|
||||
@@ -17,6 +18,23 @@ type KnownHost struct {
|
||||
Fingerprint string
|
||||
}
|
||||
|
||||
func NewKnownHostsCallback(knownHostsPath string, strictHostKeyChecking bool) (ssh.HostKeyCallback, error) {
|
||||
if !strictHostKeyChecking {
|
||||
return ssh.InsecureIgnoreHostKey(), nil
|
||||
}
|
||||
if knownHostsPath == "" {
|
||||
return ssh.InsecureIgnoreHostKey(), nil
|
||||
}
|
||||
_, err := os.Stat(knownHostsPath)
|
||||
if os.IsNotExist(err) {
|
||||
return ssh.InsecureIgnoreHostKey(), nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return knownhosts.New(knownHostsPath)
|
||||
}
|
||||
|
||||
func EnsureKnownHosts(sshDir string) (string, error) {
|
||||
path := filepath.Join(sshDir, "known_hosts")
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644)
|
||||
@@ -27,6 +45,8 @@ func EnsureKnownHosts(sshDir string) (string, error) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// AddKnownHost stores the host key in standard ssh known_hosts format (hostname keytype base64key).
|
||||
// NOTE: Existing entries in known_hosts may need to be regenerated if they were stored in a different format.
|
||||
func AddKnownHost(sshDir, host string, port int, keyData []byte) error {
|
||||
path := filepath.Join(sshDir, "known_hosts")
|
||||
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -40,24 +39,17 @@ func dialSSH(ctx context.Context, host string, port int, user, privKeyPath, know
|
||||
|
||||
var capturedFingerprint string
|
||||
var capturedPubKey ssh.PublicKey
|
||||
|
||||
callback, err := NewKnownHostsCallback(knownHostsPath, strictHostKeyChecking)
|
||||
if err != nil {
|
||||
return nil, "", nil, fmt.Errorf("creating host key callback: %w", err)
|
||||
}
|
||||
|
||||
hostKeyCallback := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||
h := sha256.Sum256(key.Marshal())
|
||||
capturedFingerprint = "SHA256:" + base64.RawStdEncoding.EncodeToString(h[:])
|
||||
capturedPubKey = key
|
||||
if strictHostKeyChecking && knownHostsPath != "" {
|
||||
kh, err := GetKnownHost(filepath.Dir(knownHostsPath), host, port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("checking known_hosts: %w", err)
|
||||
}
|
||||
if kh == nil {
|
||||
return fmt.Errorf("host key not found in known_hosts: %s", hostname)
|
||||
}
|
||||
wantFP := kh.Fingerprint
|
||||
if capturedFingerprint != wantFP {
|
||||
return fmt.Errorf("host key mismatch: got %s, want %s", capturedFingerprint, wantFP)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return callback(hostname, remote, key)
|
||||
}
|
||||
|
||||
cfg := &ssh.ClientConfig{
|
||||
|
||||
Reference in New Issue
Block a user