agent_binary_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // agent_binary_test.go — #1367: el endpoint de binario del agente no debe
  2. // defaultear a arm64 en silencio cuando falta ?arch=, y el one-liner de
  3. // instalación no debe pre-descargar el binario (la detección de arquitectura
  4. // la hace install-agent.sh contra el router, que es quien la sabe).
  5. package httpapi_test
  6. import (
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/gnacho/netpulse/server-go/internal/agentbin"
  12. )
  13. // getBinary hace GET /api/agents/<slug>/binary autenticado con el token del
  14. // agente; query se añade tal cual ("" o "?arch=mipsle").
  15. func getBinary(t *testing.T, ts *agentTestServer, slug, token, query string) *http.Response {
  16. t.Helper()
  17. url := ts.URL + "/api/agents/" + slug + "/binary" + query
  18. req, _ := http.NewRequest("GET", url, nil)
  19. req.Header.Set("Authorization", "Bearer "+token)
  20. res, err := http.DefaultClient.Do(req)
  21. if err != nil {
  22. t.Fatalf("GET %s: %v", url, err)
  23. }
  24. return res
  25. }
  26. func createTokenFor(t *testing.T, ts *agentTestServer, slug string) string {
  27. t.Helper()
  28. status, token, _ := createAgentToken(t, ts, slug)
  29. if status != 201 {
  30. t.Fatalf("create token %s: %d", slug, status)
  31. }
  32. return token
  33. }
  34. // #1367: sin ?arch= el server servía arm64 en silencio; un router MIPS que
  35. // instalara con el one-liner recibía un binario aarch64 (exec format error).
  36. // El default silencioso es la causa del reporte: ahora se exige la arch.
  37. func TestAgentBinaryRequiresArch(t *testing.T) {
  38. ts := makeAgentTestServer(t)
  39. token := createTokenFor(t, ts, "mipsbox")
  40. res := getBinary(t, ts, "mipsbox", token, "")
  41. defer res.Body.Close()
  42. if res.StatusCode != http.StatusBadRequest {
  43. body, _ := io.ReadAll(res.Body)
  44. t.Fatalf("sin ?arch= quiero 400 pidiendo la arch, tengo %d: %s", res.StatusCode, body)
  45. }
  46. }
  47. // Con ?arch= explícita el binario servido debe ser el de ESA arquitectura
  48. // (Content-Disposition y sha256 presentes), no el default.
  49. func TestAgentBinaryServesRequestedArch(t *testing.T) {
  50. if !agentbin.HasBinaries() {
  51. t.Skip("sin binarios embebidos (árbol dev sin agentbin/agents)")
  52. }
  53. ts := makeAgentTestServer(t)
  54. token := createTokenFor(t, ts, "mipsbox")
  55. res := getBinary(t, ts, "mipsbox", token, "?arch=mipsle")
  56. defer res.Body.Close()
  57. if res.StatusCode != http.StatusOK {
  58. body, _ := io.ReadAll(res.Body)
  59. t.Fatalf("?arch=mipsle quiero 200, tengo %d: %s", res.StatusCode, body)
  60. }
  61. if cd := res.Header.Get("Content-Disposition"); !strings.Contains(cd, "mipsle") {
  62. t.Fatalf("Content-Disposition = %q, quiero mipsle", cd)
  63. }
  64. if res.Header.Get("X-Checksum-Sha256") == "" {
  65. t.Fatal("falta la cabecera X-Checksum-Sha256")
  66. }
  67. head := make([]byte, 4)
  68. if _, err := io.ReadFull(res.Body, head); err != nil {
  69. t.Fatalf("leer body: %v", err)
  70. }
  71. if string(head) != "\x7fELF" {
  72. t.Fatalf("el body no es un ELF: %x", head)
  73. }
  74. }
  75. // Arch desconocida: 404 (comportamiento preexistente, se fija como contrato).
  76. func TestAgentBinaryUnknownArch(t *testing.T) {
  77. ts := makeAgentTestServer(t)
  78. token := createTokenFor(t, ts, "mipsbox")
  79. res := getBinary(t, ts, "mipsbox", token, "?arch=sparc")
  80. defer res.Body.Close()
  81. if res.StatusCode != http.StatusNotFound {
  82. t.Fatalf("?arch=sparc quiero 404, tengo %d", res.StatusCode)
  83. }
  84. }
  85. // #1367: el one-liner NO debe pre-descargar el binario (sin arch no puede
  86. // acertar en MIPS); debe delegar en install-agent.sh, que detecta la arch
  87. // del router por SSH y descarga del propio server con ?arch=.
  88. func TestInstallLineDelegatesBinaryToInstaller(t *testing.T) {
  89. ts := makeAgentTestServer(t)
  90. _, _, install := createAgentToken(t, ts, "rt1")
  91. if strings.Contains(install, "/api/agents/") {
  92. t.Fatalf("el one-liner no debe descargar el binario: %s", install)
  93. }
  94. if !strings.Contains(install, "install-agent.sh") {
  95. t.Fatalf("el one-liner debe invocar install-agent.sh: %s", install)
  96. }
  97. for _, want := range []string{"--slug=rt1", "--token=", "--server="} {
  98. if !strings.Contains(install, want) {
  99. t.Fatalf("falta %s en el one-liner: %s", want, install)
  100. }
  101. }
  102. }