Merge pull request 'Let an agent report a chosen hostname' (#15) from feat/report-hostname into main
CI / tests (Python 3.10) (push) Successful in 24s
CI / tests (Python 3.11) (push) Successful in 22s
CI / tests (Python 3.12) (push) Successful in 21s
CI / tests (Python 3.13) (push) Successful in 22s
CI / lint (push) Successful in 4s

Reviewed-on: #15
This commit was merged in pull request #15.
This commit is contained in:
patrick committed 2026-10-30 02:17:15 +00:00
commit e799f88f8e
6 files changed
+57 -6

No files matched your search

+6 -2
View File
@@ -78,8 +78,12 @@ How the agent works it out:
the row gets a note: "registered as web01 but reports itself as db01". This
catches a token copied to the wrong machine. It never changes the status. A
host you registered under a custom label (`<ssh target> <name>` in
`agent/hosts`) will always show the note; register it under its `hostname -s`
to clear it.
`agent/hosts`) shows the note until you tell its agent which name to report:
`agent/deploy.sh iris -- --report-hostname iris` (or `install.sh
--report-hostname iris` on the host, which sets `REPORT_HOSTNAME` in
`agent.conf`). Upgrades and `--rotate` keep it;
`--default-report-hostname` removes it. That's the host's own say-so, so it
only silences the note for hosts you've deliberately named.
- **Available updates**: a simulated `apt-get dist-upgrade`. It is read-only, takes
no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel
packages pulled in by the meta-package show up. Lines whose origin contains
+5
View File
@@ -12,3 +12,8 @@ TOKEN_FILE=/etc/kernelwatch/agent.token
# curl timeout in seconds.
#TIMEOUT=30
# Hostname to report instead of the machine's own (`hostname -f`). Set this when
# the host is registered under a different name, so the dashboard doesn't note
# the mismatch. Usually set with: install.sh --report-hostname NAME
#REPORT_HOSTNAME=web01
+24 -1
View File
@@ -3,6 +3,7 @@
#
# sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem]
# [--with-needrestart] [--with-apt-update | --without-apt-update]
# [--report-hostname NAME | --default-report-hostname]
#
# --with-needrestart installs needrestart (Ubuntu has it already) so the
# dashboard can show services needing a restart, and sets it to only list
@@ -12,6 +13,11 @@
# hourly report, so new kernel and security updates show up within the hour.
# --without-apt-update turns that off again. Without either, it stays as it was.
#
# --report-hostname makes the agent report NAME as its hostname instead of its
# own, for a host registered under a different name (the dashboard notes a
# mismatch). --default-report-hostname goes back to the machine's own. Without
# either, it stays as it was.
#
# The host token (from `kernelwatch add-host <name>` on the server) is read
# from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt,
# so it never lands in shell history. Re-running without --server or a token
@@ -24,6 +30,7 @@ ca=
token_file=
with_needrestart=0
apt_update=
report_hostname=keep
while (($#)); do
case $1 in
@@ -33,7 +40,12 @@ while (($#)); do
--with-needrestart) with_needrestart=1; shift ;;
--with-apt-update) apt_update=on; shift ;;
--without-apt-update) apt_update=off; shift ;;
-h | --help) sed -n '2,19s/^# \{0,1\}//p' "$0"; exit 0 ;;
--report-hostname)
report_hostname=${2:?--report-hostname needs a name}; shift 2
[[ $report_hostname =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$ ]] ||
{ echo "invalid --report-hostname '$report_hostname'" >&2; exit 2; } ;;
--default-report-hostname) report_hostname=off; shift ;;
-h | --help) sed -n '2,25s/^# \{0,1\}//p' "$0"; exit 0 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
done
@@ -78,6 +90,9 @@ if [[ -n $ca ]]; then
install -m 0644 "$ca" /etc/kernelwatch/ca.pem
fi
# Writing the config below replaces it, so remember a REPORT_HOSTNAME to keep.
prev_report_hostname=$(sed -n 's/^REPORT_HOSTNAME=//p' /etc/kernelwatch/agent.conf 2>/dev/null | tail -n1 || true)
if [[ -n $server ]]; then
{
echo "SERVER_URL=$server"
@@ -90,6 +105,14 @@ elif [[ ! -e /etc/kernelwatch/agent.conf ]]; then
exit 1
fi
case $report_hostname in
keep) report_hostname=$prev_report_hostname ;;
off) report_hostname= ;;
esac
sed -i '/^REPORT_HOSTNAME=/d' /etc/kernelwatch/agent.conf
[[ -z $report_hostname ]] || echo "REPORT_HOSTNAME=$report_hostname" >>/etc/kernelwatch/agent.conf
chmod 0644 /etc/kernelwatch/agent.conf
token=
if [[ -n $token_file ]]; then
token=$(tr -d '[:space:]' <"$token_file")
+5 -2
View File
@@ -6,7 +6,7 @@
# rules can change without redeploying agents.
set -euo pipefail
AGENT_VERSION=0.4.0
AGENT_VERSION=0.4.1
# Prefix for every file the agent reads. Only the test suite sets this.
ROOT=${KERNELWATCH_ROOT:-}
@@ -68,6 +68,8 @@ SERVER_URL=
TOKEN_FILE=$ROOT/etc/kernelwatch/agent.token
CA_FILE=
TIMEOUT=30
# Name to report instead of `hostname -f`, for a host registered under another name.
REPORT_HOSTNAME=
load_config() {
[[ -r $CONF ]] || return 0
@@ -84,6 +86,7 @@ load_config() {
TOKEN_FILE) TOKEN_FILE=$value ;;
CA_FILE) CA_FILE=$value ;;
TIMEOUT) TIMEOUT=$value ;;
REPORT_HOSTNAME) REPORT_HOSTNAME=$value ;;
*) echo "kernelwatch-agent: ignoring unknown config key '$key'" >&2 ;;
esac
done <"$CONF"
@@ -248,7 +251,7 @@ build_report() {
printf '{'
printf '"agent_version":%s,' "$(json_str "$AGENT_VERSION")"
printf '"reported_hostname":%s,' "$(json_str "$(hostname -f 2>/dev/null || hostname)")"
printf '"reported_hostname":%s,' "$(json_str "${REPORT_HOSTNAME:-$(hostname -f 2>/dev/null || hostname)}")"
printf '"os_id":%s,' "$(json_str "$(os_release_field ID)")"
printf '"os_version_id":%s,' "$(json_str "$(os_release_field VERSION_ID)")"
printf '"os_pretty_name":%s,' "$(json_str "$(os_release_field PRETTY_NAME)")"
+1 -1
View File
@@ -4,4 +4,4 @@ __version__ = "0.4.0"
# Version of agent/kernelwatch-agent shipped alongside this server; older agents
# are noted on the dashboard. tests/test_status.py checks the two stay in sync.
AGENT_VERSION = "0.4.0"
AGENT_VERSION = "0.4.1"
+16
View File
@@ -139,6 +139,22 @@ class AgentReportTest(AgentCase):
self.write_livepatch("LIVEPATCH-STATE: applied\nLIVEPATCH-FIXES: 2\n")
self.assertEqual(sorted(set(self.report()) - set(FIELDS)), [])
def write_conf(self, text):
with open(os.path.join(self.root, "etc/kernelwatch/agent.conf"), "w") as f:
f.write(text)
def test_report_hostname_overrides_the_machines_own(self):
self.write_conf("SERVER_URL=http://x\nREPORT_HOSTNAME=iris\n")
self.assertEqual(self.report()["reported_hostname"], "iris")
def test_blank_report_hostname_uses_the_machines_own(self):
self.write_conf("REPORT_HOSTNAME=\n")
self.assertEqual(self.report()["reported_hostname"], "web01.example")
def test_report_hostname_is_json_escaped(self):
self.write_conf('REPORT_HOSTNAME=a"b\\c\n')
self.assertEqual(self.report()["reported_hostname"], 'a"b\\c')
def test_no_livepatch_file_reports_nulls(self):
r = self.report()
for key in ("state", "check_state", "supported", "upgrade_required", "fix_count"):