diff --git a/README.md b/README.md index 786ab0b..2e39654 100644 --- a/README.md +++ b/README.md @@ -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 (` ` 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 diff --git a/agent/agent.conf.example b/agent/agent.conf.example index a84cb2e..4627c2e 100644 --- a/agent/agent.conf.example +++ b/agent/agent.conf.example @@ -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 diff --git a/agent/install.sh b/agent/install.sh index 920afa1..8b4e923 100755 --- a/agent/install.sh +++ b/agent/install.sh @@ -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 ` 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") diff --git a/agent/kernelwatch-agent b/agent/kernelwatch-agent index 00b15ee..bd5fcef 100755 --- a/agent/kernelwatch-agent +++ b/agent/kernelwatch-agent @@ -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)")" diff --git a/server/kernelwatch/__init__.py b/server/kernelwatch/__init__.py index f9f78cc..9a79dd6 100644 --- a/server/kernelwatch/__init__.py +++ b/server/kernelwatch/__init__.py @@ -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" diff --git a/tests/test_agent.py b/tests/test_agent.py index 90fcc47..27bf6af 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -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"):