Files
cacti/tests/tools/test_loadavg_script.py
T
Thomas Vincent eb2fa88a40 hardening: validate input and fail closed in data input scripts (#8447)
* fix(scripts): fail closed in data input scripts and keep cactigetOIDs secrets private

Rejected arguments now return U instead of an unfiltered count, the Motorola script is executable and reports errors on stderr, and cactigetOIDs reads credentials from a private defaults file.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(service): make cactid's environment file optional and widen the fail2ban dates

The unit failed on systems without /etc/sysconfig/cactid, and the fail2ban filter only parsed the default Y-m-d log date.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: guard log/.htaccess file rules for Apache 2.4 and trim the release archive

Bare Order/Deny blocks returned 500 without mod_access_compat, and git archive shipped dev-only configuration.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* hardening(scripts): validate arguments and bound remote input in data input scripts

The Motorola script accepts only a bare host and port, refuses redirects, caps response size and time and passes through only numeric values; diskfree.sh and the PO validator end option parsing before their arguments.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* hardening(contrib): skip invalid agent hostnames in cactigetOIDs

Hosts whose name starts with '-' or contains whitespace are skipped with a warning, and snmpget now receives '--' before the agent and OID.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(changelog): add fragment for #8447

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(scripts): fail closed on an invalid loadavg.pl selector

An argument that is not a number left the selector undefined, so the
script printed all three averages with exit 0 and warned three times on
stderr. Print U and exit 1 instead, as the other data input scripts do.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: enforce the modem fetch deadline during blocked reads

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-11-17 13:39:42 -05:00

42 lines
1.5 KiB
Python

"""Run the loadavg.pl data input script against valid and invalid selectors."""
from pathlib import Path
import re
import shutil
import subprocess
import unittest
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / 'scripts' / 'loadavg.pl'
PERL = shutil.which('perl')
@unittest.skipIf(PERL is None, 'perl is not installed')
class LoadavgScriptTest(unittest.TestCase):
def run_script(self, *args):
result = subprocess.run([PERL, str(SCRIPT), *args], capture_output=True, text=True,
timeout=30, check=False)
return result.returncode, result.stdout, result.stderr
def test_invalid_selector_prints_u_and_fails(self):
for arg in ('x', '5x', '-1', '1.5', ' 5'):
with self.subTest(arg=arg):
self.assertEqual(self.run_script(arg), (1, 'U', ''))
def test_selector_prints_one_average(self):
for arg in ('5', '15', '30'):
with self.subTest(arg=arg):
code, out, err = self.run_script(arg)
self.assertEqual((code, err), (0, ''))
self.assertRegex(out, r'^[0-9]+\.[0-9]+$')
def test_no_selector_prints_all_three_averages(self):
for args in ((), ('',)):
with self.subTest(args=args):
code, out, err = self.run_script(*args)
self.assertEqual((code, err), (0, ''))
self.assertEqual(len(re.findall(r'[0-9]+\.[0-9]+', out)), 3)
if __name__ == '__main__':
unittest.main()