sshkey.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Package sshkey — clave SSH propia de NetPulse (paridad src/sshkey.js):
  2. // par ed25519 en DATA_DIR/.ssh generado la primera vez, pública expuesta vía
  3. // /api/config/sshkey para autorizarla a mano en cada router. known_hosts
  4. // JUNTO a la clave (imprescindible con systemd ProtectSystem=strict).
  5. package sshkey
  6. import (
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "regexp"
  11. "strings"
  12. )
  13. // BaseArgs replica sshBaseArgs (src/sshkey.js:16-24): los args SSH comunes.
  14. // known_hosts junto a la clave (dentro de DATA_DIR).
  15. func BaseArgs(keyPath string) []string {
  16. return []string{
  17. "-i", keyPath,
  18. "-o", "BatchMode=yes",
  19. "-o", "ConnectTimeout=4",
  20. "-o", "StrictHostKeyChecking=accept-new",
  21. "-o", "UserKnownHostsFile=" + filepath.Join(filepath.Dir(keyPath), "known_hosts"),
  22. }
  23. }
  24. // KnownHostsPath devuelve la ruta del known_hosts propio (junto a la clave).
  25. func KnownHostsPath(keyPath string) string {
  26. return filepath.Join(filepath.Dir(keyPath), "known_hosts")
  27. }
  28. // EnsureKeypair garantiza que existe el par de claves en keyPath
  29. // (ssh-keygen -t ed25519 -N '' -C netpulse); best-effort en permisos.
  30. func EnsureKeypair(keyPath string) error {
  31. if _, err := os.Stat(keyPath); err == nil {
  32. if _, err := os.Stat(keyPath + ".pub"); err == nil {
  33. return nil
  34. }
  35. }
  36. if err := os.MkdirAll(filepath.Dir(keyPath), 0o700); err != nil {
  37. return err
  38. }
  39. cmd := exec.Command("ssh-keygen", "-t", "ed25519", "-f", keyPath, "-N", "", "-C", "netpulse", "-q")
  40. if out, err := cmd.CombinedOutput(); err != nil {
  41. return &ExecError{What: "ssh-keygen", Err: err, Out: string(out)}
  42. }
  43. _ = os.Chmod(filepath.Dir(keyPath), 0o700)
  44. _ = os.Chmod(keyPath, 0o600)
  45. return nil
  46. }
  47. // ExecError envuelve un fallo de proceso externo.
  48. type ExecError struct {
  49. What string
  50. Err error
  51. Out string
  52. }
  53. func (e *ExecError) Error() string { return e.What + ": " + e.Err.Error() + " " + strings.TrimSpace(e.Out) }
  54. func (e *ExecError) Unwrap() error { return e.Err }
  55. // PublicKey es la respuesta de /api/config/sshkey.
  56. type PublicKey struct {
  57. PublicKey string `json:"publicKey"`
  58. Fingerprint string `json:"fingerprint"`
  59. }
  60. var fpRe = regexp.MustCompile(`^\d+\s+(\S+)`)
  61. // GetPublicKey lee la clave pública y su fingerprint (ssh-keygen -lf, 2º
  62. // campo). Devuelve (nil, nil) si no se puede leer (→ 500 no_key).
  63. func GetPublicKey(keyPath string) *PublicKey {
  64. data, err := os.ReadFile(keyPath + ".pub")
  65. if err != nil {
  66. return nil
  67. }
  68. pub := strings.TrimSpace(string(data))
  69. out, err := exec.Command("ssh-keygen", "-lf", keyPath+".pub").Output()
  70. fp := ""
  71. if err == nil {
  72. if m := fpRe.FindStringSubmatch(strings.TrimSpace(string(out))); m != nil {
  73. fp = m[1]
  74. }
  75. }
  76. return &PublicKey{PublicKey: pub, Fingerprint: fp}
  77. }