Quellcode durchsuchen

fix(agentinstall): serve the installer with LF endings (#1812) (#1815)

Nacho vor 1 Monat
Ursprung
Commit
d45ec6c8f7

+ 7 - 0
.gitattributes

@@ -0,0 +1,7 @@
+# #1812: los scripts de shell viajan al router (go:embed) y se ejecutan con
+# busybox ash, que rechaza CRLF. Se fuerzan LF en el repositorio aunque el
+# checkout tenga core.autocrlf=true.
+*.sh    text eol=lf
+*.init  text eol=lf
+*.config text eol=lf
+*.defaults text eol=lf

+ 1 - 0
CHANGELOG.md

@@ -31,6 +31,7 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 ### Fixed
 
+- The served installer is always LF (#1812). A checkout with `core.autocrlf=true`, which is what Git for Windows picks by default, put CRLF in the embedded `install-agent.sh`, and the busybox ash on the router rejected the script with a syntax error. The embedded copy is normalised before it is served, and `.gitattributes` now forces LF on the shell files that travel to a router. The copy check compares both sides normalised, so it still catches a real divergence on a Windows checkout.
 - The pre-flight checks speak the language of the UI (#1811). Their detail text was written by the server in Spanish, so an English interface showed "3393 MB libres" next to a translated label. Each check now publishes a stable code with its numbers and the frontend translates it, leaving the server text as a fallback for older clients.
 - A resolved alert now shows what it resolved (#1466). The closing event used to list a bare internal id, because the original alert is removed from the live feed the moment it resolves and the lookup ran against that same list. The details are read from the persisted log instead, which keeps the title, the moment it fired, the type and the variables, so the block reads like the alert it closes.
 - A reinstall now keeps the UCI configuration of the packaged agent in sync (#1476). On a router that already carries the `.ipk`/`.apk`, `/etc/config/netpulse-agent` is the conffile the init reads and the one that survives a sysupgrade, but the SSH reinstall only rewrote the agent environment file, so the native config kept the old token and could point at a server that no longer exists. The script now sets server, slug, token, SPKI fingerprint, scan interval and both ping targets through `uci` when the config file is present, deletes the target keys when they resolve to nothing, and adds the config and its uci-defaults to the sysupgrade list. Routers without the package are untouched: they keep working from the environment file.

+ 17 - 1
server-go/internal/agentinstall/agentinstall.go

@@ -8,13 +8,29 @@
 package agentinstall
 
 import (
+	"bytes"
 	"crypto/sha256"
 	_ "embed"
 	"encoding/hex"
 )
 
 //go:embed install-agent.sh
-var script []byte
+var embedded []byte
+
+// script es el instalador que se sirve, siempre con saltos LF. Un checkout con
+// core.autocrlf=true (el default de Git en Windows) deja CRLF en el fichero
+// embebido, y el busybox ash del router rechaza ese script con un error de
+// sintaxis (#1812). La normalización es idempotente: un fichero LF se sirve
+// tal cual, sin copia nueva.
+var script = normalizeLF(embedded)
+
+// normalizeLF convierte los finales de línea CRLF en LF.
+func normalizeLF(b []byte) []byte {
+	if !bytes.Contains(b, []byte("\r\n")) {
+		return b
+	}
+	return bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n"))
+}
 
 // Script devuelve el instalador embebido. El llamante no debe modificarlo.
 func Script() []byte { return script }

+ 25 - 1
server-go/internal/agentinstall/agentinstall_test.go

@@ -19,7 +19,11 @@ func TestScriptMatchesRepoCopy(t *testing.T) {
 	if err != nil {
 		t.Fatalf("read %s: %v", repoScript, err)
 	}
-	if !bytes.Equal(want, Script()) {
+	// #1812: la comparación ignora los finales de línea. Un checkout con
+	// autocrlf deja CRLF en las dos copias y Script() los normaliza, así que
+	// comparar en crudo rompería en Windows sin que las copias se hayan
+	// separado de verdad.
+	if !bytes.Equal(normalizeLF(want), Script()) {
 		t.Fatalf("install-agent.sh embebido y el del repositorio difieren: copia de nuevo el fichero a internal/agentinstall/install-agent.sh")
 	}
 	if len(want) == 0 {
@@ -42,3 +46,23 @@ func TestDigestIsStable(t *testing.T) {
 		t.Fatal("Digest no es estable entre llamadas")
 	}
 }
+
+// #1812: el instalador servido nunca lleva CRLF, sea cual sea el checkout con
+// el que se compiló el servidor.
+func TestScriptServesLF(t *testing.T) {
+	if bytes.Contains(Script(), []byte("\r\n")) {
+		t.Fatal("el instalador embebido conserva CRLF: busybox ash lo rechaza")
+	}
+}
+
+// La normalización no cambia un fichero que ya está en LF.
+func TestNormalizeLFIsIdempotent(t *testing.T) {
+	lf := []byte("#!/bin/sh\necho ok\n")
+	if got := normalizeLF(lf); !bytes.Equal(got, lf) {
+		t.Fatalf("normalizeLF alteró un fichero LF: %q", got)
+	}
+	crlf := []byte("#!/bin/sh\r\necho ok\r\n")
+	if got := normalizeLF(crlf); bytes.Contains(got, []byte("\r\n")) {
+		t.Fatalf("normalizeLF dejó CRLF: %q", got)
+	}
+}