Răsfoiți Sursa

fix(agentbin): verify packed agents from a version stamp (#1914) (#1915)

Nacho 4 săptămâni în urmă
părinte
comite
c89aa3cf53

+ 2 - 0
.gitignore

@@ -39,6 +39,8 @@ bin/
 
 # Binarios de agente embebidos (construidos por CI)
 server-go/internal/agentbin/agents/netpulse-agent-*
+# Version stamp written by scripts/build-agents.sh (#1914).
+server-go/internal/agentbin/agents/agent.version
 .worktrees/
 
 # Rutas prerenderizadas por idioma (generadas, ver web/build-langs.mjs)

+ 3 - 0
CHANGELOG.md

@@ -7,6 +7,9 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 ## [Unreleased]
 
+### Fixed
+- Agent self-upgrade works again on servers built after the binaries were packed with UPX (#1914). The #1246 guard scanned the embedded bytes for the version string, and compression hides it, so every release since the packing change started with self-upgrade disabled and logged a mismatch that did not exist. The build now writes the agent version in a stamp file next to the binaries and the server reads that stamp first, keeping the byte scan for hand-copied or unpacked sets.
+
 ## [2.51.2] - 2026-11-06
 
 ### Added

+ 5 - 0
scripts/build-agents.sh

@@ -71,4 +71,9 @@ for arch in amd64 arm64 arm mipsle mips; do
 	"$UPX_BIN" -t "$bin" >/dev/null || { echo "build-agents: $bin fails the UPX integrity test" >&2; exit 1; }
 done
 
+# UPX compresses the version literal passed with -X main.Version, so the #1246
+# guard cannot find it by scanning the embedded bytes. The stamp keeps that
+# check working without unpacking at runtime (#1914).
+printf '%s\n' "$VERSION" > "$OUT/agent.version"
+
 ls -l "$OUT"/netpulse-agent-*

+ 24 - 2
server-go/internal/agentbin/agentbin.go

@@ -155,17 +155,39 @@ func VerifyEmbedded() (bool, string) {
 	return verifyFSVersion(agentsFS, EmbeddedAgentVersion)
 }
 
+// embeddedStampFile es el sello que build-agents.sh escribe junto a los
+// binarios embebidos. UPX comprime el literal de versión dentro del payload
+// (ver #1890), así que el escaneo de bytes no lo encuentra: el sello conserva
+// la comprobación sin descomprimir en runtime (#1914).
+const embeddedStampFile = "agents/agent.version"
+
+// embeddedArchs son las arquitecturas que el servidor embebe y sirve.
+var embeddedArchs = []string{"amd64", "arm64", "arm", "mips", "mipsle"}
+
 // verifyFSVersion es verifyEmbedded desacoplada del embed para tests.
 func verifyFSVersion(fsys fs.FS, version string) (bool, string) {
 	if version == "" || version == "0.1.0" {
 		return true, ""
 	}
-	var bad []string
-	for _, arch := range []string{"amd64", "arm64", "arm", "mips", "mipsle"} {
+	var present []string
+	for _, arch := range embeddedArchs {
 		b, err := fs.ReadFile(fsys, "agents/netpulse-agent-"+arch)
 		if err != nil || len(b) == 0 {
 			continue // sin binario para esa arquitectura: Open ya da 404
 		}
+		present = append(present, arch)
+	}
+
+	if stamp, err := fs.ReadFile(fsys, embeddedStampFile); err == nil {
+		if strings.TrimSpace(string(stamp)) == version {
+			return true, ""
+		}
+		return false, strings.Join(present, ",")
+	}
+
+	var bad []string
+	for _, arch := range present {
+		b, _ := fs.ReadFile(fsys, "agents/netpulse-agent-"+arch)
 		if !bytes.Contains(b, []byte(version)) {
 			bad = append(bad, arch)
 		}

+ 30 - 0
server-go/internal/agentbin/arch_test.go

@@ -49,6 +49,36 @@ func TestVerifyFSVersion(t *testing.T) {
 	}
 }
 
+// #1914: los agentes van comprimidos con UPX y el literal de versión no está
+// en claro en los bytes embebidos. El sello escrito por build-agents.sh
+// restaura la comprobación sin descomprimir.
+func TestVerifyFSVersionUsesStampForPackedBinaries(t *testing.T) {
+	packed := fstest.MapFS{
+		"agents/netpulse-agent-amd64": {Data: []byte("UPXD payload without the version")},
+		"agents/netpulse-agent-arm64": {Data: []byte("UPXD payload without the version")},
+	}
+	if ok, bad := verifyFSVersion(packed, "3.0.37"); ok || bad != "amd64,arm64" {
+		t.Fatalf("packed sin sello debe fallar: ok=%v bad=%q", ok, bad)
+	}
+
+	stamped := fstest.MapFS{
+		"agents/netpulse-agent-amd64": {Data: []byte("UPXD payload")},
+		"agents/netpulse-agent-arm64": {Data: []byte("UPXD payload")},
+		"agents/agent.version":        {Data: []byte("3.0.37\n")},
+	}
+	if ok, bad := verifyFSVersion(stamped, "3.0.37"); !ok || bad != "" {
+		t.Fatalf("sello correcto: ok=%v bad=%q", ok, bad)
+	}
+
+	stale := fstest.MapFS{
+		"agents/netpulse-agent-amd64": {Data: []byte("UPXD payload")},
+		"agents/agent.version":        {Data: []byte("3.0.36\n")},
+	}
+	if ok, bad := verifyFSVersion(stale, "3.0.37"); ok || bad != "amd64" {
+		t.Fatalf("sello viejo: ok=%v bad=%q", ok, bad)
+	}
+}
+
 func TestEmbeddedMismatchFlag(t *testing.T) {
 	SetEmbeddedMismatch(true)
 	if !EmbeddedMismatch() {