integrations_pin_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package httpapi
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "errors"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "github.com/gnacho/netpulse/server-go/internal/reinstall"
  11. "github.com/gnacho/netpulse/server-go/internal/rintegration"
  12. )
  13. func acceptAny(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }
  14. func TestPusherPinEnv(t *testing.T) {
  15. tlsSrv := httptest.NewTLSServer(http.HandlerFunc(acceptAny))
  16. defer tlsSrv.Close()
  17. sum := sha256.Sum256(tlsSrv.Certificate().RawSubjectPublicKeyInfo)
  18. want := hex.EncodeToString(sum[:])
  19. if got := pusherPinEnv(tlsSrv.URL); got[envServerFP] != want {
  20. t.Fatalf("https pin = %q, want %q", got[envServerFP], want)
  21. }
  22. plain := httptest.NewServer(http.HandlerFunc(acceptAny))
  23. defer plain.Close()
  24. if got := pusherPinEnv(plain.URL); got != nil {
  25. t.Fatalf("http must not carry a pin, got %v", got)
  26. }
  27. // Sin handshake no hay pin: se omite la variable en lugar de inventarla.
  28. if got := pusherPinEnv("https://127.0.0.1:1"); got != nil {
  29. t.Fatalf("unreachable https must not carry a pin, got %v", got)
  30. }
  31. }
  32. func TestPusherEnvCarriesServerPin(t *testing.T) {
  33. tlsSrv := httptest.NewTLSServer(http.HandlerFunc(acceptAny))
  34. defer tlsSrv.Close()
  35. plain := httptest.NewServer(http.HandlerFunc(acceptAny))
  36. defer plain.Close()
  37. var s *server // pusherEnv no toca estado del servidor.
  38. row := rintegration.Row{Slug: "salon", Host: "192.0.2.10", Username: "admin", IntervalS: 30}
  39. env := s.pusherEnv(rintegration.Vendor{ID: "asuswrt", Mode: "local"}, row, "pw", tlsSrv.URL, "tok")
  40. if env[envServerFP] == "" {
  41. t.Fatalf("https server URL must ship %s, env=%v", envServerFP, env)
  42. }
  43. if env["NETPULSE_SERVER"] != tlsSrv.URL || env["NETPULSE_TOKEN"] != "tok" || env["NETPULSE_SLUG"] != "salon" {
  44. t.Fatalf("unexpected env: %v", env)
  45. }
  46. // En modo ssh el pusher corre en el router: el host del vendor cambia, el
  47. // pin del server NO (es la misma URL pública la que va a verificar).
  48. sshEnv := s.pusherEnv(rintegration.Vendor{ID: "asuswrt", Mode: "ssh"}, row, "pw", tlsSrv.URL, "tok")
  49. if sshEnv[envServerFP] != env[envServerFP] {
  50. t.Fatalf("ssh env pin = %q, want %q", sshEnv[envServerFP], env[envServerFP])
  51. }
  52. if sshEnv["ASUSWRT_HOST"] != "127.0.0.1" {
  53. t.Fatalf("ssh env host = %q, want 127.0.0.1", sshEnv["ASUSWRT_HOST"])
  54. }
  55. httpEnv := s.pusherEnv(rintegration.Vendor{ID: "asuswrt", Mode: "local"}, row, "pw", plain.URL, "tok")
  56. if _, present := httpEnv[envServerFP]; present {
  57. t.Fatalf("http server must not ship a pin: %v", httpEnv)
  58. }
  59. }
  60. // stubServerFP sustituye el derivado del pin y lo devuelve al terminar.
  61. func stubServerFP(t *testing.T, fp string, err error) {
  62. t.Helper()
  63. orig := serverFPWithError
  64. serverFPWithError = func(string) (string, error) { return fp, err }
  65. t.Cleanup(func() { serverFPWithError = orig })
  66. }
  67. // Un https sin pin derivable no puede escribirse: el pusher se negaría a
  68. // arrancar y el alta mentiría con un 201.
  69. func TestRequirePusherPinRejectsUndiaableHttps(t *testing.T) {
  70. cause := errors.New("dial tcp 203.0.113.10:443: connect: connection refused")
  71. stubServerFP(t, "", cause)
  72. err := requirePusherPin("https://netpulse.example:3443")
  73. if err == nil {
  74. t.Fatal("https sin pin derivable debe rechazar el alta")
  75. }
  76. for _, want := range []string{"cannot derive the server fingerprint", cause.Error(), "NETPULSE_AGENT_URL", envServerFP} {
  77. if !strings.Contains(err.Error(), want) {
  78. t.Errorf("el mensaje debe contener %q; es: %q", want, err)
  79. }
  80. }
  81. // El mismo fallo sin causa concreta (handshake que no presenta cert) sigue
  82. // siendo un rechazo, no un env incompleto.
  83. stubServerFP(t, "", nil)
  84. if err := requirePusherPin("https://netpulse.example:3443"); err == nil {
  85. t.Fatal("un https sin cert también debe rechazarse")
  86. }
  87. }
  88. func TestRequirePusherPinAllowsWhatCanStart(t *testing.T) {
  89. stubServerFP(t, "aabb", nil)
  90. if err := requirePusherPin("https://netpulse.example:3443"); err != nil {
  91. t.Fatalf("con pin derivable no hay que rechazar: %v", err)
  92. }
  93. // http: no hay nada que verificar, así que la ausencia de pin es el estado
  94. // correcto, no un error.
  95. if err := requirePusherPin("http://192.168.1.5:3000"); err != nil {
  96. t.Fatalf("http no necesita pin: %v", err)
  97. }
  98. // Sin stub: un TLS real del todo debe dar el pin por sí solo.
  99. tlsSrv := httptest.NewTLSServer(http.HandlerFunc(acceptAny))
  100. defer tlsSrv.Close()
  101. restore := serverFPWithError
  102. serverFPWithError = reinstall.ServerFPWithError
  103. t.Cleanup(func() { serverFPWithError = restore })
  104. if err := requirePusherPin(tlsSrv.URL); err != nil {
  105. t.Fatalf("pin derivable de un TLS real: %v", err)
  106. }
  107. }