Collect Ubuntu Livepatch status in the agent
CI / tests (Python 3.10) (pull_request) Successful in 22s
CI / tests (Python 3.11) (pull_request) Successful in 22s
CI / tests (Python 3.12) (pull_request) Successful in 21s
CI / tests (Python 3.13) (pull_request) Successful in 21s
CI / lint (pull_request) Successful in 2s
CI / tests (Python 3.10) (pull_request) Successful in 22s
CI / tests (Python 3.11) (pull_request) Successful in 22s
CI / tests (Python 3.12) (pull_request) Successful in 21s
CI / tests (Python 3.13) (pull_request) Successful in 21s
CI / lint (pull_request) Successful in 2s
kernelwatch-livepatch.service runs canonical-livepatch as root with no network and a read-only filesystem, and a small python3 helper keeps the running kernel's state, check state, support, end-of-support date and fix count in /run/kernelwatch/livepatch. The agent (0.4.0) reads that file the way it reads needrestart's and never runs the snap itself. NoNewPrivileges=yes can't be used: snap-confine fails under it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4fa86de11f
commit
b59336d7e2
11 files changed
+273
-8
No files matched your search
@@ -0,0 +1,99 @@
|
||||
"""kernelwatch-livepatch: flattening real `canonical-livepatch status --format json` output."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from helpers import REPO
|
||||
|
||||
SCRIPT = os.path.join(REPO, "agent", "kernelwatch-livepatch")
|
||||
|
||||
# Captured from Ubuntu 22.04 (HWE 6.8) and 24.04 hosts, client v11.1.0.
|
||||
APPLIED = {
|
||||
"Client-Version": "v11.1.0",
|
||||
"Status": [{
|
||||
"Kernel": "6.8.0-138.138~22.04.1-generic",
|
||||
"Running": True,
|
||||
"Livepatch": {
|
||||
"CheckState": "checked", "State": "applied", "Version": "122.1",
|
||||
"Fixes": [
|
||||
{"Name": "cve-2025-37944", "Description": "", "Bug": "", "Patched": True},
|
||||
{"Name": "cve-2025-71089", "Description": "", "Bug": "", "Patched": True},
|
||||
],
|
||||
},
|
||||
"Supported": "supported",
|
||||
"UpgradeRequiredDate": "2027-09-07",
|
||||
}],
|
||||
"tier": "updates",
|
||||
"Fixed-CVEs": {"Patched-CVEs": ["UBUNTU-CVE-2025-37944", "UBUNTU-CVE-2025-71089"],
|
||||
"Kernel-Upgrade": {"Status": "same-kernel"}},
|
||||
}
|
||||
NOTHING = {
|
||||
"Status": [{
|
||||
"Kernel": "7.0.0-38.38~24.04.4-generic",
|
||||
"Running": True,
|
||||
"Livepatch": {"CheckState": "checked", "State": "nothing-to-apply", "Version": ""},
|
||||
"Supported": "supported",
|
||||
"UpgradeRequiredDate": "2027-10-14",
|
||||
}],
|
||||
"Fixed-CVEs": {"Patched-CVEs": []},
|
||||
}
|
||||
|
||||
|
||||
def flatten(stdin):
|
||||
if not isinstance(stdin, str):
|
||||
stdin = json.dumps(stdin)
|
||||
return subprocess.run([sys.executable, "-I", SCRIPT], input=stdin, capture_output=True,
|
||||
text=True, timeout=30)
|
||||
|
||||
|
||||
class LivepatchHelperTest(unittest.TestCase):
|
||||
def test_applied_patches(self):
|
||||
proc = flatten(APPLIED)
|
||||
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||
self.assertEqual(proc.stdout.splitlines(), [
|
||||
"LIVEPATCH-STATE: applied",
|
||||
"LIVEPATCH-CHECK: checked",
|
||||
"LIVEPATCH-SUPPORTED: supported",
|
||||
"LIVEPATCH-UPGRADE-REQUIRED: 2027-09-07",
|
||||
"LIVEPATCH-FIXES: 2",
|
||||
])
|
||||
|
||||
def test_nothing_to_apply_has_no_fixes_key_in_input(self):
|
||||
proc = flatten(NOTHING)
|
||||
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||
self.assertIn("LIVEPATCH-STATE: nothing-to-apply", proc.stdout)
|
||||
self.assertIn("LIVEPATCH-FIXES: 0", proc.stdout)
|
||||
|
||||
def test_only_the_running_kernel_counts(self):
|
||||
data = json.loads(json.dumps(APPLIED))
|
||||
other = json.loads(json.dumps(NOTHING["Status"][0]))
|
||||
other["Running"] = False
|
||||
data["Status"].insert(0, other)
|
||||
self.assertIn("LIVEPATCH-STATE: applied", flatten(data).stdout)
|
||||
|
||||
def test_unpatched_fixes_are_not_counted(self):
|
||||
data = json.loads(json.dumps(APPLIED))
|
||||
data["Status"][0]["Livepatch"]["Fixes"][1]["Patched"] = False
|
||||
self.assertIn("LIVEPATCH-FIXES: 1", flatten(data).stdout)
|
||||
|
||||
def test_not_livepatch_status_prints_nothing_and_fails(self):
|
||||
no_running = {"Status": [dict(NOTHING["Status"][0], Running=False)]}
|
||||
for stdin in ("", "Error: livepatch is not enabled\n", "[]", "null", '{"Status": 3}',
|
||||
'{"Status": [null]}', json.dumps(no_running), '{"error": "x"}'):
|
||||
with self.subTest(stdin=stdin[:30]):
|
||||
proc = flatten(stdin)
|
||||
self.assertEqual((proc.returncode, proc.stdout), (1, ""), proc.stderr)
|
||||
|
||||
def test_values_cannot_inject_lines(self):
|
||||
data = json.loads(json.dumps(NOTHING))
|
||||
data["Status"][0]["Supported"] = "supported\nLIVEPATCH-STATE: applied"
|
||||
out = flatten(data).stdout.splitlines()
|
||||
self.assertEqual([l for l in out if l.startswith("LIVEPATCH-STATE")],
|
||||
["LIVEPATCH-STATE: nothing-to-apply"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user