Jelajahi Sumber

fix(integrations): heal pre-pin pusher environments on update and edit (#1699)

* fix(integrations): heal pre-pin pusher environments on update and edit

* test(integrations): stub the fingerprint derivation in the pin heal test
Nacho 1 bulan lalu
induk
melakukan
58e38b96b4

+ 19 - 0
server-go/internal/httpapi/integrations.go

@@ -328,6 +328,17 @@ func (s *server) pusherEnv(v rintegration.Vendor, row rintegration.Row, password
 	return env
 }
 
+// healPusherPin merges the current server pin into a pusher environment,
+// healing files written before the pin requirement and refreshing the pin
+// after a certificate reissue. It is a no-op when the server URL needs no
+// pin (plain HTTP).
+func healPusherPin(env map[string]string, serverURL string) map[string]string {
+	for k, v := range pusherPinEnv(serverURL) {
+		env[k] = v
+	}
+	return env
+}
+
 func (s *server) runSSHInstall(v rintegration.Vendor, row rintegration.Row, env map[string]string, assetURL, sha256 string) error {
 	_, err := s.pool.Run(row.Host, sshInstallScript(v, env, assetURL, sha256), 300*time.Second)
 	return err
@@ -457,6 +468,12 @@ func (s *server) handleIntegrationUpdate(w http.ResponseWriter, r *http.Request)
 			writeError(w, http.StatusBadGateway, "download_failed", err.Error())
 			return
 		}
+		// Heal environments created before the pin requirement: the
+		// refreshed binary refuses to start without it. Best effort, on
+		// error the old file is reused and the start below reports it.
+		if env, err := s.integrations.Env(slug); err == nil {
+			_ = s.integrations.WriteEnv(slug, healPusherPin(env, s.agentBaseURL(r)))
+		}
 		if err := s.integrations.Start(slug, nil, filepath.Join(installDir, vendor.Binary)); err != nil {
 			_ = store.SetState(slug, "error", err.Error())
 			s.broadcastIntegrations()
@@ -576,6 +593,7 @@ func (s *server) handleIntegrationEdit(w http.ResponseWriter, r *http.Request) {
 			env[prefix+"PASS"] = password
 		}
 		env["NETPULSE_INTERVAL"] = strconv.Itoa(row.IntervalS) + "s"
+		healPusherPin(env, s.agentBaseURL(r))
 		if err := s.integrations.WriteEnv(slug, env); err != nil {
 			_ = store.SetState(slug, "error", err.Error())
 			s.broadcastIntegrations()
@@ -607,6 +625,7 @@ func (s *server) handleIntegrationEdit(w http.ResponseWriter, r *http.Request) {
 			env[prefix+"PASS"] = password
 		}
 		env["NETPULSE_INTERVAL"] = strconv.Itoa(row.IntervalS) + "s"
+		healPusherPin(env, s.agentBaseURL(r))
 		if _, err := s.pool.Run(row.Host, sshEnvWriteScript(vendor, env), 60*time.Second); err != nil {
 			_ = store.SetState(slug, "error", err.Error())
 			s.broadcastIntegrations()

+ 29 - 0
server-go/internal/httpapi/integrations_pinheal_test.go

@@ -0,0 +1,29 @@
+package httpapi
+
+import "testing"
+
+func TestHealPusherPinAddsPinForHTTPS(t *testing.T) {
+	orig := serverFPWithError
+	serverFPWithError = func(string) (string, error) { return "sha256-test-pin", nil }
+	defer func() { serverFPWithError = orig }()
+
+	env := map[string]string{
+		"NETPULSE_SERVER":   "https://np.example:3000",
+		"NETPULSE_INTERVAL": "45s",
+	}
+	out := healPusherPin(env, "https://np.example:3000")
+	if out["NETPULSE_SERVER_FP"] != "sha256-test-pin" {
+		t.Fatalf("pin not merged: got %q", out["NETPULSE_SERVER_FP"])
+	}
+	if out["NETPULSE_INTERVAL"] != "45s" {
+		t.Fatalf("existing keys must be preserved, got %q", out["NETPULSE_INTERVAL"])
+	}
+}
+
+func TestHealPusherPinNoopForHTTP(t *testing.T) {
+	env := map[string]string{"NETPULSE_SERVER": "http://127.0.0.1:3000"}
+	out := healPusherPin(env, "http://127.0.0.1:3000")
+	if _, ok := out["NETPULSE_SERVER_FP"]; ok {
+		t.Fatal("pin must not be added for a plain http server URL")
+	}
+}