go-container/devenv/ssh_utils.go
Snider b19cdd91a5 refactor: replace os file ops and fmt.Errorf/errors.New with go-io/go-log equivalents
Replace all os.ReadFile/os.WriteFile/os.MkdirAll in production code with
coreio.Local equivalents (Read, Write, EnsureDir) from go-io. Replace all
fmt.Errorf and errors.New with coreerr.E() from go-log, adding structured
operation context to all error returns. Promote go-log from indirect to
direct dependency in go.mod.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-16 19:17:34 +00:00

70 lines
1.8 KiB
Go

package devenv
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
coreio "forge.lthn.ai/core/go-io"
coreerr "forge.lthn.ai/core/go-log"
)
// 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 {
return coreerr.E("ensureHostKey", "get home dir", err)
}
knownHostsPath := filepath.Join(home, ".core", "known_hosts")
// Ensure directory exists
if err := coreio.Local.EnsureDir(filepath.Dir(knownHostsPath)); err != nil {
return coreerr.E("ensureHostKey", "create known_hosts dir", err)
}
// 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 {
return coreerr.E("ensureHostKey", "ssh-keyscan failed", err)
}
if len(out) == 0 {
return coreerr.E("ensureHostKey", "ssh-keyscan returned no keys", nil)
}
// Read existing known_hosts to avoid duplicates
existingStr, _ := coreio.Local.Read(knownHostsPath)
// 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 {
return coreerr.E("ensureHostKey", "open known_hosts", err)
}
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 {
return coreerr.E("ensureHostKey", "write known_hosts", err)
}
}
}
return nil
}