Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix: proxy agent binary from GitHub instead of redirecting

rcourtman502766b9b7fc
diff --git a/internal/api/unified_agent.go b/internal/api/unified_agent.go
index 47f1d8c209..6c85e363d8 100644
--- a/internal/api/unified_agent.go
+++ b/internal/api/unified_agent.go
@@ -1,6 +1,8 @@
package api
import (
+ "crypto/sha256"
+ "encoding/hex"
"io"
"net/http"
"os"
@@ -174,18 +176,12 @@ func (r *Router) handleDownloadUnifiedAgent(w http.ResponseWriter, req *http.Req
return
}
- // Fallback: redirect to GitHub releases for the binary
- // This handles LXC/barebone installations that don't have agent binaries locally
+ // Fallback: proxy from GitHub releases for the binary
+ // This handles LXC/barebone installations that don't have agent binaries locally.
+ // We proxy instead of redirecting because agents require the X-Checksum-Sha256 header,
+ // which GitHub doesn't provide.
if normalized != "" {
- // Map architecture to GitHub release asset name
- binaryName := "pulse-agent-" + normalized
- if strings.HasPrefix(normalized, "windows-") {
- binaryName += ".exe"
- }
- githubURL := "https://github.com/rcourtman/Pulse/releases/latest/download/" + binaryName
- log.Info().Str("arch", normalized).Str("redirect", githubURL).Msg("Local agent binary not found, redirecting to GitHub releases")
- w.Header().Set("X-Served-From", "github-redirect")
- http.Redirect(w, req, githubURL, http.StatusTemporaryRedirect)
+ r.proxyAgentBinaryFromGitHub(w, req, normalized)
return
}
@@ -193,6 +189,66 @@ func (r *Router) handleDownloadUnifiedAgent(w http.ResponseWriter, req *http.Req
http.Error(w, "Agent binary not found. Specify ?arch=linux-amd64 (or your architecture)", http.StatusNotFound)
}
+// proxyAgentBinaryFromGitHub downloads an agent binary from GitHub releases and serves
+// it to the requesting agent with the X-Checksum-Sha256 header. This is used when the
+// binary isn't available locally (e.g., LXC/bare-metal installations updated via web UI).
+// We must proxy instead of redirecting because the agent requires the checksum header
+// for security verification, and GitHub doesn't provide it.
+func (r *Router) proxyAgentBinaryFromGitHub(w http.ResponseWriter, req *http.Request, normalized string) {
+ binaryName := "pulse-agent-" + normalized
+ if strings.HasPrefix(normalized, "windows-") {
+ binaryName += ".exe"
+ }
+ githubURL := "https://github.com/rcourtman/Pulse/releases/latest/download/" + binaryName
+
+ log.Info().Str("arch", normalized).Str("url", githubURL).Msg("Local agent binary not found, proxying from GitHub releases")
+
+ client := r.installScriptClient
+ if client == nil {
+ client = &http.Client{
+ Timeout: 5 * time.Minute,
+ }
+ }
+
+ resp, err := client.Get(githubURL)
+ if err != nil {
+ log.Error().Err(err).Str("url", githubURL).Msg("Failed to fetch agent binary from GitHub")
+ http.Error(w, "Failed to fetch agent binary", http.StatusServiceUnavailable)
+ return
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ log.Error().Int("status", resp.StatusCode).Str("url", githubURL).Msg("GitHub returned non-200 status for agent binary")
+ http.Error(w, "Agent binary not found on GitHub", http.StatusNotFound)
+ return
+ }
+
+ // Read the binary with size limit (100 MB)
+ const maxAgentBinarySize = 100 * 1024 * 1024
+ limitedReader := io.LimitReader(resp.Body, maxAgentBinarySize+1)
+
+ hasher := sha256.New()
+ content, err := io.ReadAll(io.TeeReader(limitedReader, hasher))
+ if err != nil {
+ log.Error().Err(err).Msg("Failed to read agent binary from GitHub")
+ http.Error(w, "Failed to read agent binary", http.StatusInternalServerError)
+ return
+ }
+ if int64(len(content)) > maxAgentBinarySize {
+ log.Error().Int64("size", int64(len(content))).Msg("Agent binary from GitHub exceeds size limit")
+ http.Error(w, "Agent binary too large", http.StatusInternalServerError)
+ return
+ }
+
+ checksum := hex.EncodeToString(hasher.Sum(nil))
+
+ w.Header().Set("X-Checksum-Sha256", checksum)
+ w.Header().Set("X-Served-From", "github-proxy")
+ w.Header().Set("Content-Type", "application/octet-stream")
+ w.Write(content)
+}
+
// proxyInstallScriptFromGitHub fetches an install script from GitHub releases
// This is used as a fallback when scripts aren't available locally (e.g., LXC updates)
func (r *Router) proxyInstallScriptFromGitHub(w http.ResponseWriter, req *http.Request, scriptName string) {
diff --git a/internal/api/unified_agent_test.go b/internal/api/unified_agent_test.go
index 476c796a19..fea227531b 100644
--- a/internal/api/unified_agent_test.go
+++ b/internal/api/unified_agent_test.go
@@ -3,6 +3,7 @@ package api
import (
"crypto/sha256"
"encoding/hex"
+ "errors"
"net/http"
"net/http/httptest"
"os"
@@ -123,35 +124,74 @@ func TestDownloadUnifiedAgent_Local_SpecificArch(t *testing.T) {
assert.Equal(t, binContent, w.Body.String())
}
-func TestDownloadUnifiedAgent_Redirect(t *testing.T) {
+func TestDownloadUnifiedAgent_ProxyFromGitHub(t *testing.T) {
router, _ := setupUnifiedAgentRouter(t)
// Ensure NO local files exist (temp dir is empty of binaries)
+ // Set up a mock HTTP client to simulate GitHub response
+ binaryContent := "fake binary content for proxy test"
+ expectedURL := "https://github.com/rcourtman/Pulse/releases/latest/download/pulse-agent-linux-amd64"
+ router.installScriptClient = newTestInstallScriptClient(t, expectedURL, http.StatusOK, binaryContent, nil)
req := httptest.NewRequest(http.MethodGet, "/api/install/agent?arch=linux-amd64", nil)
w := httptest.NewRecorder()
router.handleDownloadUnifiedAgent(w, req)
- // Since local binary is missing, it should redirect to GitHub
- require.Equal(t, http.StatusTemporaryRedirect, w.Code)
- location := w.Header().Get("Location")
- assert.Contains(t, location, "github.com")
- assert.Contains(t, location, "pulse-agent-linux-amd64")
- assert.Equal(t, "github-redirect", w.Header().Get("X-Served-From"))
+ // Should proxy the binary with checksum header instead of redirecting
+ require.Equal(t, http.StatusOK, w.Code)
+ assert.Equal(t, binaryContent, w.Body.String())
+ assert.Equal(t, "github-proxy", w.Header().Get("X-Served-From"))
+
+ // Verify checksum header is present and correct
+ hash := sha256.Sum256([]byte(binaryContent))
+ expectedChecksum := hex.EncodeToString(hash[:])
+ assert.Equal(t, expectedChecksum, w.Header().Get("X-Checksum-Sha256"))
}
-func TestDownloadUnifiedAgent_Redirect_Windows(t *testing.T) {
+func TestDownloadUnifiedAgent_ProxyFromGitHub_Windows(t *testing.T) {
router, _ := setupUnifiedAgentRouter(t)
+ binaryContent := "MZ fake windows binary"
+ expectedURL := "https://github.com/rcourtman/Pulse/releases/latest/download/pulse-agent-windows-amd64.exe"
+ router.installScriptClient = newTestInstallScriptClient(t, expectedURL, http.StatusOK, binaryContent, nil)
+
req := httptest.NewRequest(http.MethodGet, "/api/install/agent?arch=windows-amd64", nil)
w := httptest.NewRecorder()
router.handleDownloadUnifiedAgent(w, req)
- require.Equal(t, http.StatusTemporaryRedirect, w.Code)
- location := w.Header().Get("Location")
- assert.Contains(t, location, "pulse-agent-windows-amd64.exe")
+ require.Equal(t, http.StatusOK, w.Code)
+ assert.Equal(t, binaryContent, w.Body.String())
+ assert.NotEmpty(t, w.Header().Get("X-Checksum-Sha256"))
+}
+
+func TestDownloadUnifiedAgent_ProxyFromGitHub_NotFound(t *testing.T) {
+ router, _ := setupUnifiedAgentRouter(t)
+
+ // GitHub returns 404 for the binary
+ router.installScriptClient = newTestInstallScriptClient(t, "", http.StatusNotFound, "", nil)
+
+ req := httptest.NewRequest(http.MethodGet, "/api/install/agent?arch=linux-amd64", nil)
+ w := httptest.NewRecorder()
+
+ router.handleDownloadUnifiedAgent(w, req)
+
+ require.Equal(t, http.StatusNotFound, w.Code)
+}
+
+func TestDownloadUnifiedAgent_ProxyFromGitHub_Error(t *testing.T) {
+ router, _ := setupUnifiedAgentRouter(t)
+
+ // GitHub is unreachable
+ router.installScriptClient = newTestInstallScriptClient(t, "", 0, "", errors.New("connection refused"))
+
+ req := httptest.NewRequest(http.MethodGet, "/api/install/agent?arch=linux-amd64", nil)
+ w := httptest.NewRecorder()
+
+ router.handleDownloadUnifiedAgent(w, req)
+
+ require.Equal(t, http.StatusServiceUnavailable, w.Code)
}
func TestNormalizeUnifiedAgentArch(t *testing.T) {