mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
Revalidate installed identity under the unattended update lock (#4395)
Release discovery does not reserve the installation. Re-read the actual Community executable while holding the existing transaction lock, without handing that lock descriptor to the probe. Refuse unknown or Pro identity, skip equal or newer versions without claiming installation, and preserve signed installation and recovery for an older Community version. Cover real lock/identity admission and the main result consumer; keep sequential lock-release fixtures consistent with no repeated installation. Contract-Neutral: Enforce the existing Community-only non-downgrading updater admission under its existing lock; no installer, channel, authority or recovery contract changes. --- Fleet candidate `20261114T170850Z-delivery-trust` from the `delivery-trust` lane. Reviewed tip `bcf67873d8c87597230bb0da8320283aca60e511` on `main`. [How Pulse handles triage](https://github.com/rcourtman/Pulse/blob/main/docs/AI_TRANSPARENCY.md)
This commit is contained in:
@@ -2,6 +2,7 @@ package installtests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -10,6 +11,229 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// A caller's discovery can become stale before it takes the installation lock.
|
||||
// Exercise the real entry and executable parser with private shell binaries and
|
||||
// a real flock. The service-state sentinel ends admitted cases before mutation;
|
||||
// refused/skipped cases must not even reach it, temporary files or transport.
|
||||
func TestAutoUpdateRevalidatesInstalledIdentityUnderLock(t *testing.T) {
|
||||
for _, tool := range []string{"bash", "flock", "timeout", "python3"} {
|
||||
if _, err := exec.LookPath(tool); err != nil {
|
||||
t.Fatalf("locked updater admission requires %s: %v", tool, err)
|
||||
}
|
||||
}
|
||||
updater := os.Getenv("PULSE_AUTO_UPDATE_IDENTITY_SOURCE")
|
||||
if updater == "" {
|
||||
updater = repoFile("scripts", "pulse-auto-update.sh")
|
||||
}
|
||||
updater, err := filepath.Abs(updater)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, identity, layout string
|
||||
probeExit, status int
|
||||
admitted, noProbe bool
|
||||
}{
|
||||
{name: "older_community", identity: "Pulse v6.7.0", status: 1, admitted: true},
|
||||
{name: "same_release_preview", identity: "Pulse v6.8.0-rc.2", status: 1, admitted: true},
|
||||
{name: "already_target", identity: "Pulse v6.8.0", status: 3},
|
||||
{name: "newer_stable", identity: "Pulse v6.9.0", status: 3},
|
||||
{name: "newer_preview", identity: "Pulse v6.9.0-rc.1", status: 3},
|
||||
{name: "newly_pro", identity: "Pulse Pro v6.9.0", status: 1},
|
||||
{name: "unknown_identity", identity: "unidentified fixture", status: 1},
|
||||
{name: "failed_probe", identity: "Pulse v6.7.0", probeExit: 19, status: 1},
|
||||
{name: "missing_binary", layout: "missing", status: 1, noProbe: true},
|
||||
{name: "broken_primary_does_not_borrow_legacy", identity: "Pulse v6.7.0", layout: "both", probeExit: 19, status: 1},
|
||||
{name: "unusable_primary_does_not_borrow_legacy", identity: "Pulse v6.7.0", layout: "unusable", status: 1, noProbe: true},
|
||||
{name: "newer_legacy", identity: "Pulse v6.9.0", layout: "legacy", status: 3},
|
||||
{name: "older_legacy", identity: "Pulse v6.7.0", layout: "legacy", status: 1, admitted: true},
|
||||
{name: "busy_lock_precedes_identity_probe", identity: "Pulse v6.7.0", layout: "busy", status: 1, noProbe: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
install := filepath.Join(dir, "install")
|
||||
config := filepath.Join(dir, "config")
|
||||
for _, path := range []string{filepath.Join(install, "bin"), config} {
|
||||
if err := os.MkdirAll(path, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Each real --version execution independently attempts this lock.
|
||||
// Its failure must be contention, not an unavailable flock command.
|
||||
binary := []byte(`#!/bin/bash
|
||||
set -eu
|
||||
[[ "$*" == --version ]] || exit 97
|
||||
status=0
|
||||
flock --exclusive --nonblock "$PULSE_INSTALL_DIR" true || status=$?
|
||||
printf '%s\n' "$status" >> "$FIXTURE/probes"
|
||||
python3 - "$PULSE_INSTALL_DIR" <<'PY'
|
||||
import os
|
||||
import sys
|
||||
target = os.stat(sys.argv[1])
|
||||
for name in os.listdir('/dev/fd'):
|
||||
try:
|
||||
entry = os.fstat(int(name))
|
||||
except OSError:
|
||||
continue
|
||||
if (entry.st_dev, entry.st_ino) == (target.st_dev, target.st_ino):
|
||||
sys.exit('version probe inherited the transaction descriptor')
|
||||
PY
|
||||
printf '%s\n' "$FIXTURE_IDENTITY"
|
||||
exit "$FIXTURE_PROBE_EXIT"
|
||||
`)
|
||||
primary := filepath.Join(install, "bin", "pulse")
|
||||
if tc.layout == "legacy" {
|
||||
primary = filepath.Join(install, "pulse")
|
||||
}
|
||||
if tc.layout != "missing" {
|
||||
if err := os.WriteFile(primary, binary, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if tc.layout == "both" || tc.layout == "unusable" {
|
||||
if err := os.WriteFile(filepath.Join(install, "pulse"), []byte("#!/bin/sh\necho legacy >> \"$FIXTURE/unexpected\"\necho 'Pulse v6.7.0'\n"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if tc.layout == "unusable" {
|
||||
if err := os.Chmod(primary, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// A stale sidecar is not an edition/version admission source. Keep
|
||||
// both binary layouts, settings and identity byte-for-byte intact.
|
||||
for name, data := range map[string]string{
|
||||
filepath.Join(install, "VERSION"): "v6.7.0\n",
|
||||
filepath.Join(config, "system.json"): "{\"autoUpdateEnabled\":true}\n",
|
||||
filepath.Join(config, "agent-id"): "saved-fixture-identity\n",
|
||||
} {
|
||||
if err := os.WriteFile(name, []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
before := make(map[string]string)
|
||||
if err := filepath.WalkDir(install, func(path string, entry os.DirEntry, walkErr error) error {
|
||||
if walkErr != nil || entry.IsDir() {
|
||||
return walkErr
|
||||
}
|
||||
data, readErr := os.ReadFile(path)
|
||||
before[path] = string(data)
|
||||
return readErr
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"system.json", "agent-id"} {
|
||||
path := filepath.Join(config, name)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before[path] = string(data)
|
||||
}
|
||||
script := `source "$UPDATER_UNDER_TEST"
|
||||
log() { printf '[%s] %s\n' "$1" "${*:2}"; }
|
||||
detect_service_name() { echo pulse; }
|
||||
read_update_service_state() { echo service >> "$FIXTURE/events"; return 67; }
|
||||
mktemp() { echo temporary >> "$FIXTURE/unexpected"; return 71; }
|
||||
curl() { echo transport >> "$FIXTURE/unexpected"; return 72; }
|
||||
systemctl() { echo manager >> "$FIXTURE/unexpected"; return 73; }
|
||||
if [[ "$FIXTURE_LAYOUT" == busy ]]; then
|
||||
exec {held}< "$INSTALL_DIR"
|
||||
flock --exclusive --nonblock "$held"
|
||||
fi
|
||||
status=0
|
||||
perform_update v6.8.0 || status=$?
|
||||
printf 'RESULT=%s\n' "$status"
|
||||
[[ -z "$(trap -p RETURN)" ]] || { echo 'leaked RETURN trap'; exit 95; }
|
||||
if [[ "$FIXTURE_LAYOUT" == busy ]]; then exec {held}<&-; fi
|
||||
flock --exclusive --nonblock "$INSTALL_DIR" true || { echo 'leaked update lock'; exit 96; }
|
||||
`
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "bash", "-c", script)
|
||||
cmd.Env = append(os.Environ(), "UPDATER_UNDER_TEST="+updater,
|
||||
"FIXTURE="+dir, "PULSE_INSTALL_DIR="+install, "PULSE_CONFIG_DIR="+config,
|
||||
"FIXTURE_IDENTITY="+tc.identity, fmt.Sprintf("FIXTURE_PROBE_EXIT=%d", tc.probeExit), "FIXTURE_LAYOUT="+tc.layout)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil || ctx.Err() != nil || !strings.Contains(string(out), fmt.Sprintf("RESULT=%d\n", tc.status)) {
|
||||
t.Fatalf("locked admission: command=%v context=%v want status=%d\n%s", err, ctx.Err(), tc.status, out)
|
||||
}
|
||||
probes, readErr := os.ReadFile(filepath.Join(dir, "probes"))
|
||||
if tc.noProbe {
|
||||
if !os.IsNotExist(readErr) {
|
||||
t.Fatalf("identity unexpectedly executed: %q, %v", probes, readErr)
|
||||
}
|
||||
} else if readErr != nil || string(probes) != "1\n" {
|
||||
t.Fatalf("identity was not read exactly once under the held lock: %q, %v\n%s", probes, readErr, out)
|
||||
}
|
||||
events, readErr := os.ReadFile(filepath.Join(dir, "events"))
|
||||
if tc.admitted {
|
||||
if readErr != nil || string(events) != "service\n" {
|
||||
t.Fatalf("older community did not reach the unchanged transaction admission: %q, %v\n%s", events, readErr, out)
|
||||
}
|
||||
} else if !os.IsNotExist(readErr) {
|
||||
t.Fatalf("stale or unknown admission reached transaction state: %q, %v\n%s", events, readErr, out)
|
||||
}
|
||||
if data, readErr := os.ReadFile(filepath.Join(dir, "unexpected")); !os.IsNotExist(readErr) {
|
||||
t.Fatalf("refusal/sentinel made an unexpected call: %q, %v", data, readErr)
|
||||
}
|
||||
for path, want := range before {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil || string(data) != want {
|
||||
t.Fatalf("saved file changed at %s: %v", filepath.Base(path), err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Consume the transaction's distinct no-change result through real main. An
|
||||
// earlier "new version available" observation is not an installed update.
|
||||
func TestAutoUpdateMainRetainsLockedAdmissionOutcome(t *testing.T) {
|
||||
if _, err := os.Stat("/.dockerenv"); err == nil {
|
||||
t.Fatal("updater main-flow proof requires a non-Docker environment")
|
||||
}
|
||||
updater := os.Getenv("PULSE_AUTO_UPDATE_IDENTITY_SOURCE")
|
||||
if updater == "" {
|
||||
updater = repoFile("scripts", "pulse-auto-update.sh")
|
||||
}
|
||||
updater, err := filepath.Abs(updater)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, result, message string
|
||||
success, installed bool
|
||||
}{
|
||||
{name: "no_change", result: "3", message: "No update needed after locked installed-version check", success: true},
|
||||
{name: "refused", result: "1", message: "Update failed"},
|
||||
{name: "other_failure", result: "17", message: "Update failed"},
|
||||
{name: "installed", result: "0", message: "Update completed successfully to v6.8.0", success: true, installed: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "bash", "-c", `source "$UPDATER_UNDER_TEST"
|
||||
log() { printf '[%s] %s\n' "$1" "${*:2}"; }
|
||||
check_auto_updates_enabled() { :; }
|
||||
installed_binary_is_pulse_pro() { return 1; }
|
||||
get_current_version() { echo v6.7.0; }
|
||||
get_latest_stable_version() { echo v6.8.0; }
|
||||
perform_update() { echo "TRANSACTION:$*"; return "$FIXTURE_RESULT"; }
|
||||
main
|
||||
`)
|
||||
cmd.Env = append(os.Environ(), "UPDATER_UNDER_TEST="+updater, "FIXTURE_RESULT="+tc.result)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if ctx.Err() != nil || (err == nil) != tc.success || !strings.Contains(string(out), tc.message) ||
|
||||
strings.Count(string(out), "TRANSACTION:v6.8.0\n") != 1 ||
|
||||
strings.Contains(string(out), "Update completed successfully to") != tc.installed ||
|
||||
strings.Contains(string(out), "Auto-update check completed") != tc.success {
|
||||
t.Fatalf("main lost locked admission outcome: command=%v context=%v\n%s", err, ctx.Err(), out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Exercise the real main entry after a controlled installer result. Discovery,
|
||||
// installation and logging are private doubles: no service, provider, journal
|
||||
// or notification destination is contacted. The transaction tests separately
|
||||
|
||||
@@ -311,7 +311,7 @@ func TestAutoUpdateRunningExecutableIdentity(t *testing.T) {
|
||||
func extractAutoUpdateServiceFunctions(t *testing.T) string {
|
||||
t.Helper()
|
||||
var functions []string
|
||||
for _, name := range []string{"read_update_service_state", "start_update_service_if_stopped", "stop_update_service_for_rollback", "wait_for_service_active", "ensure_service_restarted"} {
|
||||
for _, name := range []string{"version_greater_than", "read_update_service_state", "start_update_service_if_stopped", "stop_update_service_for_rollback", "wait_for_service_active", "ensure_service_restarted"} {
|
||||
functions = append(functions, extractAutoUpdateFunction(t, name))
|
||||
}
|
||||
// These pure-shell controls have simulated service state and script
|
||||
|
||||
@@ -649,6 +649,24 @@ perform_update() {
|
||||
# transaction trap below takes over only after that observation succeeds.
|
||||
trap 'trap - RETURN; exec {update_lock_fd}<&-' RETURN
|
||||
|
||||
# main's edition/version checks precede remote release discovery. Another
|
||||
# transaction can finish in that interval and release this same lock before
|
||||
# we acquire it. Revalidate the real community executable under the lock:
|
||||
# stale discovery must not downgrade it or replace a newly installed Pro
|
||||
# binary. Unknown identity grants no backup, installer or recovery action.
|
||||
local locked_version=""
|
||||
# The parent retains ownership; a version-probe child must not inherit the
|
||||
# transaction descriptor and keep the installation locked after we return.
|
||||
locked_version=$(get_current_version {update_lock_fd}<&-) || locked_version=unknown
|
||||
if [[ -z "$locked_version" || "$locked_version" == unknown ]]; then
|
||||
log error "Cannot verify installed community version after acquiring update lock; no update attempted"
|
||||
return 1
|
||||
fi
|
||||
if ! version_greater_than "$new_version" "$locked_version"; then
|
||||
log info "Update no longer needed: installed $locked_version is not older than $new_version; no installer run"
|
||||
return 3 # Distinct no-change result; main must not claim installation.
|
||||
fi
|
||||
|
||||
local installer_tmp=""
|
||||
local signature_tmp=""
|
||||
local backup_dir=""
|
||||
@@ -956,8 +974,13 @@ main() {
|
||||
# notification endpoint; a best-effort loopback POST cannot prove
|
||||
# delivery and must not hold this successful oneshot open.
|
||||
else
|
||||
log error "Update failed"
|
||||
exit 1
|
||||
local update_status=$?
|
||||
if [[ "$update_status" == 3 ]]; then
|
||||
log info "No update needed after locked installed-version check"
|
||||
else
|
||||
log error "Update failed"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
log info "Already running latest version"
|
||||
|
||||
@@ -1418,10 +1418,18 @@ with patch.object(Path, 'iterdir', entries), patch.object(Path, 'stat', descript
|
||||
self.wait_marker(f / "returned-first")
|
||||
self.assertIsNone(first.poll())
|
||||
self.assertEqual((f / "install/VERSION").read_text(), "v6.6.0\n" if status == 0 else "v6.5.0\n")
|
||||
self.finish(self.start(f, "next"), 0)
|
||||
# A released lock admits a fresh observation, not another
|
||||
# installation of the version the first call just installed.
|
||||
# After rollback, the still-older version must install normally.
|
||||
output = self.finish(self.start(f, "next"), 3 if status == 0 else 0)
|
||||
if status == 0:
|
||||
self.assertIn("Update no longer needed", output)
|
||||
self.assertFalse((f / "manager-next").exists())
|
||||
self.assertFalse((f / "download-next").exists())
|
||||
(f / "release-return-first").touch()
|
||||
self.finish(first, 0 if status == 0 else 1)
|
||||
self.assertEqual((f / "entered").read_text().splitlines(), ["first", "next"])
|
||||
self.assertEqual((f / "entered").read_text().splitlines(),
|
||||
["first"] if status == 0 else ["first", "next"])
|
||||
self.assert_saved(f)
|
||||
|
||||
def test_service_observation_refusal_releases_the_early_lock(self):
|
||||
|
||||
Reference in New Issue
Block a user