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
114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
package sshmanager
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
type ConnResult struct {
|
|
Success bool
|
|
Output string
|
|
Error string
|
|
Fingerprint string
|
|
}
|
|
|
|
func dialSSH(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool) (*ssh.Client, string, ssh.PublicKey, error) {
|
|
addr := fmt.Sprintf("%s:%d", host, port)
|
|
|
|
auths := []ssh.AuthMethod{}
|
|
if privKeyPath != "" {
|
|
key, err := os.ReadFile(privKeyPath)
|
|
if err != nil {
|
|
return nil, "", nil, fmt.Errorf("reading private key: %w", err)
|
|
}
|
|
signer, err := ssh.ParsePrivateKey(key)
|
|
if err != nil {
|
|
return nil, "", nil, fmt.Errorf("parsing private key: %w", err)
|
|
}
|
|
auths = append(auths, ssh.PublicKeys(signer))
|
|
}
|
|
|
|
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
|
|
return callback(hostname, remote, key)
|
|
}
|
|
|
|
cfg := &ssh.ClientConfig{
|
|
User: user,
|
|
Auth: auths,
|
|
HostKeyCallback: hostKeyCallback,
|
|
Timeout: 10 * time.Second,
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
|
|
conn, err := ssh.Dial("tcp", addr, cfg)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "known_hosts") || strings.Contains(err.Error(), "host key") {
|
|
return nil, capturedFingerprint, capturedPubKey, fmt.Errorf("host key verification failed: %v", err)
|
|
}
|
|
return nil, capturedFingerprint, capturedPubKey, fmt.Errorf("connection failed: %v", err)
|
|
}
|
|
return conn, capturedFingerprint, capturedPubKey, nil
|
|
}
|
|
|
|
func TestSSHConnection(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool) (*ConnResult, error) {
|
|
conn, fingerprint, _, err := dialSSH(ctx, host, port, user, privKeyPath, knownHostsPath, strictHostKeyChecking)
|
|
if err != nil {
|
|
return &ConnResult{
|
|
Success: false,
|
|
Error: err.Error(),
|
|
Fingerprint: fingerprint,
|
|
}, nil
|
|
}
|
|
defer conn.Close()
|
|
|
|
session, err := conn.NewSession()
|
|
if err != nil {
|
|
return &ConnResult{Success: false, Error: fmt.Sprintf("session: %v", err), Fingerprint: fingerprint}, nil
|
|
}
|
|
defer session.Close()
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
session.Stdout = &stdout
|
|
session.Stderr = &stderr
|
|
|
|
if err := session.Run("echo ok && uname -a"); err != nil {
|
|
return &ConnResult{
|
|
Success: false,
|
|
Error: fmt.Sprintf("exec: %v, stderr: %s", err, stderr.String()),
|
|
Fingerprint: fingerprint,
|
|
}, nil
|
|
}
|
|
|
|
return &ConnResult{
|
|
Success: true,
|
|
Output: stdout.String(),
|
|
Fingerprint: fingerprint,
|
|
}, nil
|
|
}
|
|
|
|
func ConnectForApproval(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string) (*ssh.Client, string, ssh.PublicKey, error) {
|
|
return dialSSH(ctx, host, port, user, privKeyPath, knownHostsPath, false)
|
|
}
|