Răsfoiți Sursa

fix(agent): run the install one-liner from a downloaded file (#1577)

The generated one-liner piped curl into sh, so a truncated download (12 KiB of 29343 bytes in the report) reached the shell as an incomplete script and died with a syntax error on valid code. Download to /tmp first and run only after curl succeeds; same for the two installers advertised in the README. Regression test rejects a piped one-liner.
Nacho 1 lună în urmă
părinte
comite
92edc4a3d3

+ 2 - 2
README.es.md

@@ -175,7 +175,7 @@ sección plegada de más abajo).
 **1. Instala el servidor** en la caja:
 **1. Instala el servidor** en la caja:
 
 
 ```bash
 ```bash
-curl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install.sh | sh
+curl -fsSL --retry 3 -o /tmp/netpulse-install.sh https://raw.githubusercontent.com/gnacho/netpulse/main/install.sh && sh /tmp/netpulse-install.sh
 ```
 ```
 
 
 El instalador es shell plano y legible ([inspecciónalo primero](install.sh)):
 El instalador es shell plano y legible ([inspecciónalo primero](install.sh)):
@@ -278,7 +278,7 @@ no está.
 opcionales:
 opcionales:
 
 
 ```bash
 ```bash
-curl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install-collector.sh | sh
+curl -fsSL --retry 3 -o /tmp/netpulse-install-collector.sh https://raw.githubusercontent.com/gnacho/netpulse/main/install-collector.sh && sh /tmp/netpulse-install-collector.sh
 ```
 ```
 
 
 **Modo on-box.** ¿Sin caja aparte? El servidor corre en el propio router
 **Modo on-box.** ¿Sin caja aparte? El servidor corre en el propio router

+ 2 - 2
README.md

@@ -167,7 +167,7 @@ folded section below).
 **1. Install the server** on the box:
 **1. Install the server** on the box:
 
 
 ```bash
 ```bash
-curl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install.sh | sh
+curl -fsSL --retry 3 -o /tmp/netpulse-install.sh https://raw.githubusercontent.com/gnacho/netpulse/main/install.sh && sh /tmp/netpulse-install.sh
 ```
 ```
 
 
 The installer is plain, readable shell ([inspect it first](install.sh)): it
 The installer is plain, readable shell ([inspect it first](install.sh)): it
@@ -268,7 +268,7 @@ absent.
 **Latency sidecar.** Optional long-term TCP latency probes per router:
 **Latency sidecar.** Optional long-term TCP latency probes per router:
 
 
 ```bash
 ```bash
-curl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install-collector.sh | sh
+curl -fsSL --retry 3 -o /tmp/netpulse-install-collector.sh https://raw.githubusercontent.com/gnacho/netpulse/main/install-collector.sh && sh /tmp/netpulse-install-collector.sh
 ```
 ```
 
 
 **On-box mode.** No spare box? The server itself runs on an OpenWrt router,
 **On-box mode.** No spare box? The server itself runs on an OpenWrt router,

+ 6 - 1
server-go/internal/httpapi/agent.go

@@ -518,8 +518,13 @@ func (s *server) agentInstallLine(r *http.Request, slug, token string) string {
 	} else if tr.ServerFP != "" {
 	} else if tr.ServerFP != "" {
 		fpArg = " --server-fp=" + tr.ServerFP
 		fpArg = " --server-fp=" + tr.ServerFP
 	}
 	}
+	// #1577: descarga a fichero y ejecuta SOLO si curl terminó bien. Con
+	// `curl | sh` el código de salida de curl se perdía: una descarga
+	// truncada (proxy, corte de red) llegaba al shell como script
+	// incompleto y moría con un "Syntax error: Unterminated quoted string"
+	// en una línea sin relación con la causa.
 	return fmt.Sprintf(
 	return fmt.Sprintf(
-		"%scurl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install-agent.sh | sh -s -- --host=%s --server=%s --slug=%s --token=%s%s%s%s",
+		"%scurl -fsSL --retry 3 -o /tmp/netpulse-install-agent.sh https://raw.githubusercontent.com/gnacho/netpulse/main/install-agent.sh && sh /tmp/netpulse-install-agent.sh --host=%s --server=%s --slug=%s --token=%s%s%s%s",
 		caArg, host, server, slug, token, caFileArg, fpArg, sshKeyArg)
 		caArg, host, server, slug, token, caFileArg, fpArg, sshKeyArg)
 }
 }
 
 

+ 9 - 0
server-go/internal/httpapi/agent_api_test.go

@@ -149,6 +149,15 @@ func TestIngestTokenValidoEInvalido(t *testing.T) {
 	if !strings.Contains(install, "install-agent.sh") || !strings.Contains(install, token) || !strings.Contains(install, "--slug=patio") || !strings.Contains(install, "--ssh-key=") {
 	if !strings.Contains(install, "install-agent.sh") || !strings.Contains(install, token) || !strings.Contains(install, "--slug=patio") || !strings.Contains(install, "--ssh-key=") {
 		t.Fatalf("one-liner: %q", install)
 		t.Fatalf("one-liner: %q", install)
 	}
 	}
+	// #1577: el one-liner descarga a fichero y lo ejecuta tras `&&`. Con
+	// `curl | sh` un curl a medias (descarga truncada) alimentaba al shell con
+	// un script incompleto y el error real (exit de curl) se perdía.
+	if strings.Contains(install, "| sh") {
+		t.Fatalf("one-liner no debe encadenar curl a sh: %q", install)
+	}
+	if !strings.Contains(install, "-o /tmp/netpulse-install-agent.sh") || !strings.Contains(install, "&& sh /tmp/netpulse-install-agent.sh") {
+		t.Fatalf("one-liner debe descargar a fichero y ejecutarlo: %q", install)
+	}
 	// kv guarda sha256, NUNCA el token en claro
 	// kv guarda sha256, NUNCA el token en claro
 	var stored string
 	var stored string
 	if err := ts.db.QueryRow("SELECT value FROM kv WHERE key = 'agent.token.patio'").Scan(&stored); err != nil {
 	if err := ts.db.QueryRow("SELECT value FROM kv WHERE key = 'agent.token.patio'").Scan(&stored); err != nil {