Files
move-data-nas/internal/sshmanager/shutdown.go
T
darroyo 50f73cd656 Fix ApproveFingerprint: extract real host key via SSH instead of writing fingerprint SHA256 to known_hosts
The old ApproveFingerprint passed the SHA256 fingerprint string to
AddKnownHost which expected authorized_key format, causing known_hosts
entries to be corrupted and subsequent SSH connections (including shutdown)
to fail with "host key not found".

Changes:
- dialSSH now returns (conn, fingerprint, pubKey, error) with the raw
  ssh.PublicKey captured from the server
- New ConnectForApproval() wraps dialSSH with strictHostKeyChecking=false
  for the approval handshake
- ApproveFingerprint now opens an SSH connection to the host (non-strict),
  captures the real public key, and writes it in authorized_keys format
  to known_hosts via AddKnownHost
- shutdown.go updated to handle the new 4-value dialSSH return
- Supports optional host_key field in request body for direct key submission
2026-07-13 13:43:33 -04:00

60 lines
1.3 KiB
Go

package sshmanager
import (
"bytes"
"context"
"fmt"
"time"
)
type ShutdownResult struct {
Success bool
Output string
Error string
}
func RunRemoteCommand(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool, command string) (*ShutdownResult, error) {
conn, _, _, err := dialSSH(ctx, host, port, user, privKeyPath, knownHostsPath, strictHostKeyChecking)
if err != nil {
return &ShutdownResult{Success: false, Error: err.Error()}, nil
}
defer conn.Close()
session, err := conn.NewSession()
if err != nil {
return &ShutdownResult{Success: false, Error: fmt.Sprintf("session: %v", err)}, nil
}
defer session.Close()
var stdout, stderr bytes.Buffer
session.Stdout = &stdout
session.Stderr = &stderr
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
done := make(chan error, 1)
go func() {
done <- session.Run(command)
}()
select {
case err := <-done:
if err != nil {
return &ShutdownResult{
Success: false,
Error: fmt.Sprintf("exec: %v, stderr: %s", err, stderr.String()),
}, nil
}
return &ShutdownResult{
Success: true,
Output: stdout.String(),
}, nil
case <-ctx.Done():
return &ShutdownResult{
Success: false,
Error: "command timed out after 15 seconds (machine may be shutting down)",
}, nil
}
}