sshpool_2014_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package adapters
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "sync"
  7. "testing"
  8. "github.com/gnacho/netpulse/server-go/internal/sshkey"
  9. "golang.org/x/crypto/ssh/knownhosts"
  10. )
  11. // TestHostKeyCallbackRecoversFromCorruptKnownHosts: una línea corrupta en el
  12. // known_hosts deja hoy sin SSH a TODAS las unidades, porque knownhosts.New
  13. // rechaza el fichero entero (#2014). El callback debe sanear antes de cargar.
  14. func TestHostKeyCallbackRecoversFromCorruptKnownHosts(t *testing.T) {
  15. pool := newTestPool(t)
  16. _, pub := genTestKey(t)
  17. good := knownhosts.Line([]string{knownhosts.Normalize("host-a")}, pub)
  18. content := good + "\nhost-corrupt ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAbc\n"
  19. if err := os.WriteFile(pool.khPath, []byte(content), 0o600); err != nil {
  20. t.Fatal(err)
  21. }
  22. // Premisa: el fichero de partida es inválido para la librería.
  23. if _, err := knownhosts.New(pool.khPath); err == nil {
  24. t.Fatal("premisa rota: el fichero de partida debería ser inválido")
  25. }
  26. cb, err := pool.hostKeyCallback()
  27. if err != nil {
  28. t.Fatalf("el callback debería recuperarse de una línea corrupta, obtuve: %v", err)
  29. }
  30. if cb == nil {
  31. t.Fatal("callback nil")
  32. }
  33. data, _ := os.ReadFile(pool.khPath)
  34. if strings.Contains(string(data), "host-corrupt") {
  35. t.Fatal("la línea corrupta sigue en el fichero")
  36. }
  37. if !strings.Contains(string(data), good) {
  38. t.Fatal("el saneado se comió la entrada válida")
  39. }
  40. }
  41. // TestRemoveHostKeyConcurrentWithAppends: la carrera del issue consistía en
  42. // reescribir el fichero (truncate en sitio) mientras otro escritor hacía
  43. // append. Con la reescritura atómica el fichero siempre queda legible.
  44. func TestRemoveHostKeyConcurrentWithAppends(t *testing.T) {
  45. pool := newTestPool(t)
  46. const n = 40
  47. lines := make([]string, n)
  48. for i := 0; i < n; i++ {
  49. _, pub := genTestKey(t)
  50. lines[i] = knownhosts.Line([]string{knownhosts.Normalize(fmt.Sprintf("host-%d", i))}, pub)
  51. }
  52. if err := os.WriteFile(pool.khPath, []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {
  53. t.Fatal(err)
  54. }
  55. var wg sync.WaitGroup
  56. for i := 0; i < n; i++ {
  57. wg.Add(2)
  58. go func(i int) {
  59. defer wg.Done()
  60. if err := sshkey.AppendKnownHosts(pool.khPath, []string{lines[i]}); err != nil {
  61. t.Errorf("append %d: %v", i, err)
  62. }
  63. }(i)
  64. go func(i int) {
  65. defer wg.Done()
  66. if err := pool.RemoveHostKey(fmt.Sprintf("host-%d", i)); err != nil {
  67. t.Errorf("remove %d: %v", i, err)
  68. }
  69. }(i)
  70. }
  71. wg.Wait()
  72. if _, err := knownhosts.New(pool.khPath); err != nil {
  73. t.Fatalf("known_hosts ilegible tras la concurrencia: %v", err)
  74. }
  75. }
  76. // TestRemoveHostKeyKeepsOtherHostsAndSkipsNoop: sin coincidencias no se
  77. // reescribe; con coincidencias se quita solo ese host.
  78. func TestRemoveHostKeyKeepsOtherHostsAndSkipsNoop(t *testing.T) {
  79. pool := newTestPool(t)
  80. _, pubA := genTestKey(t)
  81. _, pubB := genTestKey(t)
  82. a := knownhosts.Line([]string{knownhosts.Normalize("host-a")}, pubA)
  83. b := knownhosts.Line([]string{knownhosts.Normalize("host-b")}, pubB)
  84. if err := os.WriteFile(pool.khPath, []byte(a+"\n"+b+"\n"), 0o600); err != nil {
  85. t.Fatal(err)
  86. }
  87. before, _ := os.Stat(pool.khPath)
  88. if err := pool.RemoveHostKey("host-inexistente"); err != nil {
  89. t.Fatal(err)
  90. }
  91. after, _ := os.Stat(pool.khPath)
  92. if !os.SameFile(before, after) {
  93. t.Fatal("un RemoveHostKey sin coincidencias no debería reescribir el fichero")
  94. }
  95. if err := pool.RemoveHostKey("host-a"); err != nil {
  96. t.Fatal(err)
  97. }
  98. data, _ := os.ReadFile(pool.khPath)
  99. s := string(data)
  100. if strings.Contains(s, "host-a") {
  101. t.Fatal("no se quitó host-a")
  102. }
  103. if !strings.Contains(s, b) {
  104. t.Fatal("se perdió host-b")
  105. }
  106. }