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.

Exercise credential-file handoffs through the real installer parser

pulse-triage[bot]f1beea2e8420
diff --git a/scripts/tests/agent_installer_test_support.py b/scripts/tests/agent_installer_test_support.py
new file mode 100644
index 0000000000..c874611cee
--- /dev/null
+++ b/scripts/tests/agent_installer_test_support.py
@@ -0,0 +1,34 @@
+"""Inert documented-command fixture using the real agent argument parser."""
+
+from pathlib import Path
+
+
+INSTALLER = Path(__file__).resolve().parents[2] / "scripts/install.sh"
+
+
+def credential_path_parser_fixture():
+ # Keep the actual source through argument parsing, but never include the
+ # subsequent saved-state, credential-reader or installation operations.
+ prefix, boundary, _ = INSTALLER.read_text().partition(
+ 'if [[ "$RETARGET_ONLY" == "true" && -z "$PULSE_URL" ]]; then'
+ )
+ if not boundary:
+ raise AssertionError("agent parser boundary missing; refusing to build fixture")
+ return '#!/usr/bin/env bash\nDOCS_INSTALLER_ARGS=("$@")\n' + prefix + r'''
+python3 - "$TOKEN_FILE_PATH" "${DOCS_INSTALLER_ARGS[@]}" <<'PY'
+import json, os, stat, sys
+from pathlib import Path
+parsed_path = sys.argv[1]
+args = sys.argv[2:]
+assert args.count('--token-file') == 1
+assert parsed_path == args[args.index('--token-file') + 1], "token path changed during parsing"
+path = Path(parsed_path)
+assert stat.S_IMODE(path.stat().st_mode) == 0o600
+assert path.read_text().strip() == os.environ['EXPECTED_TOKEN']
+assert '--token' not in args
+assert os.environ['EXPECTED_TOKEN'] not in ' '.join(args)
+Path(os.environ['INSTALL_RECEIPT']).write_text(json.dumps(args))
+PY
+}
+main "$@"
+'''
diff --git a/scripts/tests/test_agent_docs.py b/scripts/tests/test_agent_docs.py
index 60fad5d758..b448b3d559 100644
--- a/scripts/tests/test_agent_docs.py
+++ b/scripts/tests/test_agent_docs.py
@@ -16,6 +16,7 @@ import subprocess
import tempfile
import unittest
+from agent_installer_test_support import credential_path_parser_fixture
from test_pbs_docs import certificate, fixture_environment, server
@@ -322,20 +323,7 @@ Path(os.environ['HOME'], '.config/pulse/agent-install.sh').write_text('other sav
for name in NAMES:
profiles.extend(block for block in blocks(name) if 'bash "$HOME/.config/pulse/agent-install.sh"' in block and "--token-file" in block)
self.assertEqual(len(profiles), 10)
- fixture = '''#!/bin/bash
-python3 - "$@" <<'PY'
-import json, os, stat, sys
-from pathlib import Path
-args = sys.argv[1:]
-token = Path(args[args.index('--token-file') + 1])
-assert args.count('--token-file') == 1
-assert token.read_text().strip() == os.environ['EXPECTED_TOKEN']
-assert stat.S_IMODE(token.stat().st_mode) == 0o600
-assert os.environ['EXPECTED_TOKEN'] not in ' '.join(args)
-assert '--token' not in args
-Path(os.environ['INSTALL_RECEIPT']).write_text(json.dumps(args))
-PY
-'''
+ fixture = credential_path_parser_fixture()
with tempfile.TemporaryDirectory() as temporary:
home = Path(temporary)
private = home / ".config/pulse"
@@ -370,7 +358,6 @@ PY
# Compare the actual argv tokens, retaining the type's value.
self.assertIn(" ".join(expected), [" ".join(a) for a in actual_profiles])
source = (ROOT / "scripts/install.sh").read_text()
- self.assertIn('--token-file) TOKEN_FILE_PATH="$2"; shift 2 ;;', source)
self.assertIn('read_collector_token_file_safely "$TOKEN_FILE_PATH" true', source)
def test_skipped_or_failed_recipe_cannot_reuse_a_previous_receipt(self):
diff --git a/scripts/tests/test_agent_installer_credential_paths.py b/scripts/tests/test_agent_installer_credential_paths.py
index 4023d32667..96321fff46 100644
--- a/scripts/tests/test_agent_installer_credential_paths.py
+++ b/scripts/tests/test_agent_installer_credential_paths.py
@@ -8,6 +8,9 @@ import shutil
import subprocess
import tempfile
import unittest
+from unittest.mock import patch
+
+import agent_installer_test_support
ROOT = Path(__file__).resolve().parents[2]
@@ -130,6 +133,32 @@ main "$@"
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertEqual(json.loads(result.stdout.removeprefix("PARSER_ACCEPTED=")), ["", "", ""])
+ def test_documented_command_fixture_rejects_dropped_or_changed_file_paths(self):
+ source = INSTALLER.read_text()
+ assignment = ' TOKEN_FILE_PATH="$2"; shift 2 ;;'
+ self.assertEqual(source.count(assignment), 1)
+ token = self.home / "documented token"
+ token.write_text(TOKEN + "\n")
+ token.chmod(0o600)
+ receipt = self.home / "documented-command.json"
+ self.env.update(EXPECTED_TOKEN=TOKEN, INSTALL_RECEIPT=str(receipt))
+ for replacement in ('shift 2 ;;', 'TOKEN_FILE_PATH="${2}/changed"; shift 2 ;;'):
+ with self.subTest(replacement=replacement):
+ changed = self.home / "changed-installer.sh"
+ changed.write_text(source.replace(assignment, replacement))
+ with patch.object(agent_installer_test_support, "INSTALLER", changed):
+ self.parser.write_text(agent_installer_test_support.credential_path_parser_fixture())
+ result = self.run_script(["--token-file", str(token)], parser=True)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertFalse(receipt.exists(), "changed handoff produced passing evidence")
+
+ def test_documented_command_fixture_refuses_a_missing_parser_boundary(self):
+ changed = self.home / "changed-installer.sh"
+ changed.write_text("#!/bin/bash\nexit 0\n")
+ with patch.object(agent_installer_test_support, "INSTALLER", changed):
+ with self.assertRaisesRegex(AssertionError, "parser boundary missing"):
+ agent_installer_test_support.credential_path_parser_fixture()
+
if __name__ == "__main__":
unittest.main()
diff --git a/scripts/tests/test_pbs_docs.py b/scripts/tests/test_pbs_docs.py
index 80bacd552b..c15da9da1e 100644
--- a/scripts/tests/test_pbs_docs.py
+++ b/scripts/tests/test_pbs_docs.py
@@ -19,6 +19,8 @@ import tempfile
import threading
import unittest
+from agent_installer_test_support import credential_path_parser_fixture
+
ROOT = Path(__file__).resolve().parents[2]
DOC = ROOT / "docs/PBS.md"
@@ -366,18 +368,7 @@ class PBSDocsTest(unittest.TestCase):
def test_download_and_agent_file_handoff(self):
# The downloaded fixture deliberately cannot install or change a
# service. It verifies only the documented argument/file handoff.
- installer = b'''#!/bin/bash
-python3 - "$@" <<'PY'
-import json, os, stat, sys
-from pathlib import Path
-args = sys.argv[1:]
-path = Path(args[args.index('--token-file') + 1])
-assert stat.S_IMODE(path.stat().st_mode) == 0o600
-assert path.read_text().strip() == os.environ['EXPECTED_AGENT_TOKEN']
-assert os.environ['EXPECTED_AGENT_TOKEN'] not in ' '.join(args)
-Path(os.environ['AGENT_RECEIPT']).write_text(json.dumps(args))
-PY
-'''
+ installer = credential_path_parser_fixture().encode()
with tempfile.TemporaryDirectory() as temporary, private_server(self.cert, self.key, installer=installer) as (port, requests):
home = Path(temporary)
result, argv = self.run_curl(home, recipe(DOWNLOAD_SELECTOR), port, self.cert)
@@ -396,7 +387,7 @@ PY
token.chmod(0o600)
command = recipe('bash "$HOME/.config/pulse/pbs-agent-install.sh"')
env = fixture_environment(home)
- env.update(EXPECTED_AGENT_TOKEN=AGENT_TOKEN, AGENT_RECEIPT=str(home / "agent-argv.json"))
+ env.update(EXPECTED_TOKEN=AGENT_TOKEN, INSTALL_RECEIPT=str(home / "agent-argv.json"))
result = subprocess.run(["bash", "-eu", "-c", command], env=env, capture_output=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stderr.decode())
self.assertEqual(json.loads((home / "agent-argv.json").read_text()), [
@@ -405,7 +396,6 @@ PY
])
self.assertNotIn(AGENT_TOKEN.encode(), result.stdout + result.stderr)
actual = (ROOT / "scripts/install.sh").read_text()
- self.assertIn('--token-file) TOKEN_FILE_PATH="$2"; shift 2 ;;', actual)
self.assertIn('read_collector_token_file_safely "$TOKEN_FILE_PATH" true', actual)
def test_failed_installer_download_is_nonzero(self):