Explorar el Código

fix(reinstall): rotate the token in the UCI config too (#1816) (#1817)

Nacho hace 1 mes
padre
commit
7f28633dbf

+ 1 - 0
CHANGELOG.md

@@ -31,6 +31,7 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 ### Fixed
 
+- Rotating the agent token now reaches the configuration the agent actually reads (#1816). The packaged init prefers the UCI config and only falls back to the environment file when the UCI token is empty, while the rotation rewrote the environment file alone. A router that still carried a token from an older install kept pushing with it and got a 401 on every cycle, forever, while SSH polling made it look healthy. The hot rotation now writes server, slug and token through `uci` when the config file is present, before restarting the service.
 - 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.

+ 12 - 0
server-go/internal/reinstall/reinstall.go

@@ -508,6 +508,18 @@ fi
 chmod 600 "$ENV_FILE.tmp"
 # Swap atómico: el proceso vivo sigue leyendo el archivo íntegro.
 mv -f "$ENV_FILE.tmp" "$ENV_FILE"
+# #1816: el init del paquete lee UCI primero y solo cae al env si el token de
+# UCI está vacío. Rotar solo el env dejaba el token viejo en
+# /etc/config/netpulse-agent y el agente respondía 401 para siempre. Se
+# sincronizan también server y slug: si UCI apunta a un servidor antiguo, el
+# token nuevo tampoco sirve.
+UCI_CFG=/etc/config/netpulse-agent
+if [ -f "$UCI_CFG" ] && command -v uci >/dev/null 2>&1; then
+	uci -q set netpulse-agent.main.server="$SERVER" || true
+	uci -q set netpulse-agent.main.slug="$SLUG" || true
+	uci -q set netpulse-agent.main.token=` + token + ` || true
+	uci -q commit netpulse-agent || true
+fi
 # procd rearma solito por el file (procd_set_param file), pero por
 # determinismo reiniciamos el servicio explícitamente.
 [ -x "$INIT" ] && "$INIT" restart >/dev/null 2>&1 || true

+ 28 - 0
server-go/internal/reinstall/script_test.go

@@ -655,3 +655,31 @@ func TestEnvScriptDoesNotInstallPackage(t *testing.T) {
 		t.Fatal("el script ligero no debe instalar el paquete")
 	}
 }
+
+// #1816: la rotación en caliente del token escribía solo el env, y el init
+// del paquete lee UCI primero. El agente seguía con el token viejo y devolvía
+// 401 para siempre. El push debe dejar las dos copias coherentes.
+func TestTokenPushScriptSyncsUCIConfig(t *testing.T) {
+	token := strings.Repeat("ab", 32)
+	s := reinstall.TokenPushScript("test-router", token)
+	for _, want := range []string{
+		`UCI_CFG=/etc/config/netpulse-agent`,
+		`[ -f "$UCI_CFG" ] && command -v uci >/dev/null 2>&1`,
+		`uci -q set netpulse-agent.main.server="$SERVER"`,
+		`uci -q set netpulse-agent.main.slug="$SLUG"`,
+		`uci -q set netpulse-agent.main.token=` + token,
+		`uci -q commit netpulse-agent`,
+	} {
+		if !strings.Contains(s, want) {
+			t.Errorf("TokenPushScript no contiene %q", want)
+		}
+	}
+	// El bloque UCI va antes del restart para que el servicio arranque ya con
+	// el token nuevo en las dos fuentes.
+	if strings.Index(s, "uci -q commit netpulse-agent") > strings.Index(s, `"$INIT" restart`) {
+		t.Error("el bloque UCI debe ejecutarse antes del restart")
+	}
+	if err := exec.Command("sh", "-n", "-c", s).Run(); err != nil {
+		t.Fatalf("TokenPushScript no es shell válido: %v", err)
+	}
+}