hostkey.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package httpapi
  2. // hostkey.go — confirmación explícita de re-onboard SSH tras una host key
  3. // cambiada (issue #603). La conexión a un host con clave distinta se rechaza
  4. // (posible MITM); este endpoint, admin-only, borra la entrada known_hosts y
  5. // fuerza un sondeo para que la nueva clave se registre con TOFU.
  6. import (
  7. "net/http"
  8. )
  9. // handleAcceptHostKey: POST /api/routers/{id}/accept-host-key (admin).
  10. func (s *server) handleAcceptHostKey(w http.ResponseWriter, r *http.Request) {
  11. id := r.PathValue("id")
  12. if id == "" {
  13. writeError(w, http.StatusNotFound, "not_found")
  14. return
  15. }
  16. acc, ok := s.adapter.(interface {
  17. AcceptHostKey(routerID string) error
  18. })
  19. if !ok {
  20. writeError(w, http.StatusServiceUnavailable, "unavailable", "el adapter no soporta aceptar host key")
  21. return
  22. }
  23. if err := acc.AcceptHostKey(id); err != nil {
  24. writeError(w, http.StatusNotFound, "not_found", err.Error())
  25. return
  26. }
  27. // Sondeo inmediato: al no quedar entrada, el dial vuelve a hacer TOFU y el
  28. // router recupera el estado online si la clave nueva es legítima.
  29. if s.pollNow != nil {
  30. s.pollNow()
  31. }
  32. writeJSON(w, http.StatusAccepted, map[string]any{"ok": true})
  33. }