2026-03-09 11:42:51 +00:00
|
|
|
package devenv
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
2026-03-16 19:15:01 +00:00
|
|
|
|
|
|
|
|
coreio "forge.lthn.ai/core/go-io"
|
|
|
|
|
coreerr "forge.lthn.ai/core/go-log"
|
2026-03-09 11:42:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ensureHostKey ensures that the host key for the dev environment is in the known hosts file.
|
|
|
|
|
// This is used after boot to allow StrictHostKeyChecking=yes to work.
|
|
|
|
|
func ensureHostKey(ctx context.Context, port int) error {
|
|
|
|
|
// Skip if requested (used in tests)
|
|
|
|
|
if os.Getenv("CORE_SKIP_SSH_SCAN") == "true" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
2026-03-16 19:15:01 +00:00
|
|
|
return coreerr.E("ensureHostKey", "get home dir", err)
|
2026-03-09 11:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
knownHostsPath := filepath.Join(home, ".core", "known_hosts")
|
|
|
|
|
|
|
|
|
|
// Ensure directory exists
|
2026-03-16 19:15:01 +00:00
|
|
|
if err := coreio.Local.EnsureDir(filepath.Dir(knownHostsPath)); err != nil {
|
|
|
|
|
return coreerr.E("ensureHostKey", "create known_hosts dir", err)
|
2026-03-09 11:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get host key using ssh-keyscan
|
|
|
|
|
cmd := exec.CommandContext(ctx, "ssh-keyscan", "-p", fmt.Sprintf("%d", port), "localhost")
|
|
|
|
|
out, err := cmd.Output()
|
|
|
|
|
if err != nil {
|
2026-03-16 19:15:01 +00:00
|
|
|
return coreerr.E("ensureHostKey", "ssh-keyscan failed", err)
|
2026-03-09 11:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(out) == 0 {
|
2026-03-16 19:15:01 +00:00
|
|
|
return coreerr.E("ensureHostKey", "ssh-keyscan returned no keys", nil)
|
2026-03-09 11:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read existing known_hosts to avoid duplicates
|
2026-03-16 19:15:01 +00:00
|
|
|
existingStr, _ := coreio.Local.Read(knownHostsPath)
|
2026-03-09 11:42:51 +00:00
|
|
|
|
|
|
|
|
// Append new keys that aren't already there
|
|
|
|
|
f, err := os.OpenFile(knownHostsPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
|
if err != nil {
|
2026-03-16 19:15:01 +00:00
|
|
|
return coreerr.E("ensureHostKey", "open known_hosts", err)
|
2026-03-09 11:42:51 +00:00
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(existingStr, line) {
|
|
|
|
|
if _, err := f.WriteString(line + "\n"); err != nil {
|
2026-03-16 19:15:01 +00:00
|
|
|
return coreerr.E("ensureHostKey", "write known_hosts", err)
|
2026-03-09 11:42:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|