| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- """Hermetic tests for the node-exporter wrapper in the connector cloud-init.
- The script is extracted from modules/twingate-bootstrap/cloud-init.yaml.tftpl
- (same extraction as the host-firewall tests) and run with a fake `ip` on a PATH
- that holds nothing but that fake and awk, inside a temporary directory. The
- exporter binary it `exec`s is replaced by a fake that records its arguments.
- Nothing here touches this machine's network or services. Documentation and
- private ranges only.
- """
- import re
- import shutil
- import subprocess
- from pathlib import Path
- import pytest
- from test_host_firewall_script import TEMPLATE, extract_file
- BASH = shutil.which("bash")
- AWK = shutil.which("awk")
- SCRIPT_PATH = "/usr/local/sbin/cah-node-exporter-run"
- EXPORTER = "/usr/bin/prometheus-node-exporter"
- DROPIN_PATH = "/etc/systemd/system/prometheus-node-exporter.service.d/10-cah-private-bind.conf"
- pytestmark = pytest.mark.skipif(BASH is None or AWK is None, reason="bash and awk are required")
- # `ip -4 -o addr show scope global` on a DigitalOcean-like machine: the public
- # address on eth0 (documentation range) and the VPC address on eth1.
- DO_LIKE = (
- "2: eth0 inet 203.0.113.10/20 brd 203.0.113.255 scope global eth0\\ valid_lft forever\n"
- "3: eth1 inet 10.118.0.2/20 brd 10.118.15.255 scope global eth1\\ valid_lft forever\n"
- )
- PUBLIC_ONLY = "2: eth0 inet 203.0.113.10/20 brd 203.0.113.255 scope global eth0\n"
- def extract(port: str = "9100") -> str:
- return extract_file(
- TEMPLATE.read_text(encoding="utf-8"), SCRIPT_PATH, {"node_exporter_port": port}
- )
- def run_script(tmp: Path, ip_out: str, port: str = "9100"):
- bindir = tmp / "bin"
- bindir.mkdir()
- (tmp / "ip.out").write_text(ip_out)
- fake_ip = bindir / "ip"
- fake_ip.write_text(
- f"#!{BASH}\n"
- 'while IFS= read -r l || [ -n "$l" ]; do printf \'%s\\n\' "$l"; done '
- f'< "{tmp / "ip.out"}"\n'
- )
- fake_ip.chmod(0o755)
- (bindir / "awk").symlink_to(AWK)
- args_file = tmp / "exporter.args"
- fake_exporter = tmp / "fake-exporter"
- fake_exporter.write_text(f'#!{BASH}\nprintf \'%s\\n\' "$@" > "{args_file}"\n')
- fake_exporter.chmod(0o755)
- script = tmp / "run.sh"
- text = extract(port)
- assert EXPORTER in text
- script.write_text(text.replace(EXPORTER, str(fake_exporter)))
- proc = subprocess.run(
- [BASH, str(script)],
- env={"PATH": str(bindir), "HOME": str(tmp)},
- capture_output=True,
- text=True,
- timeout=30,
- check=False,
- )
- return proc, (args_file.read_text() if args_file.exists() else None)
- def test_extraction_finds_the_script():
- assert extract().startswith("#!/usr/bin/env bash\n")
- def test_picks_the_private_address_not_the_public_one(tmp_path):
- proc, args = run_script(tmp_path, DO_LIKE)
- assert proc.returncode == 0, proc.stderr
- assert args == "--web.listen-address=10.118.0.2:9100\n"
- def test_uses_the_configured_port(tmp_path):
- proc, args = run_script(tmp_path, DO_LIKE, port="9200")
- assert proc.returncode == 0, proc.stderr
- assert args == "--web.listen-address=10.118.0.2:9200\n"
- @pytest.mark.parametrize("addr", ["10.0.0.5", "172.16.0.5", "172.31.255.1", "192.168.1.1"])
- def test_every_rfc1918_block_is_accepted(tmp_path, addr):
- line = f"2: eth0 inet {addr}/24 brd 0.0.0.0 scope global eth0\n"
- proc, args = run_script(tmp_path, line)
- assert proc.returncode == 0, proc.stderr
- assert args == f"--web.listen-address={addr}:9100\n"
- # DigitalOcean for real: eth0 holds the public address AND a legacy private
- # 10.x address; the VPC address is on eth1. Documentation range for the public
- # address. The exporter must bind the VPC address (#340).
- DO_REAL = (
- "2: eth0 inet 203.0.113.10/20 brd 203.0.113.255 scope global eth0\\ valid_lft forever\n"
- "2: eth0 inet 10.20.0.5/16 brd 10.20.255.255 scope global eth0\\ valid_lft forever\n"
- "3: eth1 inet 10.78.0.4/24 brd 10.78.0.255 scope global eth1\\ valid_lft forever\n"
- )
- # AWS: one interface, one private address.
- AWS_LIKE = "2: ens5 inet 10.0.1.23/24 metric 100 brd 10.0.1.255 scope global dynamic ens5\\ valid_lft 3000sec\n"
- def test_digitalocean_binds_the_vpc_address_not_the_legacy_private_one(tmp_path):
- proc, args = run_script(tmp_path, DO_REAL)
- assert proc.returncode == 0, proc.stderr
- assert args == "--web.listen-address=10.78.0.4:9100\n"
- def test_aws_single_interface_single_private_address(tmp_path):
- proc, args = run_script(tmp_path, AWS_LIKE)
- assert proc.returncode == 0, proc.stderr
- assert args == "--web.listen-address=10.0.1.23:9100\n"
- def test_private_address_on_a_public_interface_alone_is_refused(tmp_path):
- out = DO_REAL.splitlines(keepends=True)[:2]
- proc, args = run_script(tmp_path, "".join(out))
- assert proc.returncode != 0
- assert args is None
- assert "refusing to start" in proc.stderr
- def test_old_first_private_logic_fails_the_digitalocean_case(tmp_path):
- # Mutation check: put the old selection back into the extracted script and
- # the DigitalOcean case must bind the wrong address, so the test bites.
- old = (
- 'addr=""; while read -r cand; do case "$cand" in '
- '10.*|192.168.*|172.1[6-9].*|172.2[0-9].*|172.3[01].*) addr="$cand"; break ;; esac; '
- "done < <(ip -4 -o addr show scope global | awk '{ split($4, a, \"/\"); print a[1] }')\n"
- )
- text = extract()
- start = text.index('addr="$(ip -4')
- end = text.index('if [ -z "$addr" ]')
- mutated = text[:start] + old + text[end:]
- bindir = tmp_path / "bin"
- bindir.mkdir()
- (tmp_path / "ip.out").write_text(DO_REAL)
- fake_ip = bindir / "ip"
- fake_ip.write_text(f'#!{BASH}\ncat "{tmp_path / "ip.out"}"\n')
- fake_ip.chmod(0o755)
- (bindir / "awk").symlink_to(AWK)
- (bindir / "cat").symlink_to(shutil.which("cat"))
- args_file = tmp_path / "args"
- fake_exp = tmp_path / "fake-exporter"
- fake_exp.write_text(f'#!{BASH}\nprintf \'%s\\n\' "$@" > "{args_file}"\n')
- fake_exp.chmod(0o755)
- script = tmp_path / "old.sh"
- script.write_text(mutated.replace(EXPORTER, str(fake_exp)))
- subprocess.run([BASH, str(script)], env={"PATH": str(bindir)}, check=False, timeout=30)
- assert args_file.read_text() == "--web.listen-address=10.20.0.5:9100\n"
- # Negative cases: the guard bites. With no private address the script fails and
- # never execs the exporter, so the unit fails and there is no wildcard fallback.
- @pytest.mark.parametrize(
- "ip_out",
- [
- PUBLIC_ONLY,
- "",
- "2: eth0 inet 172.32.0.1/24 scope global eth0\n",
- "2: eth0 inet 172.15.0.1/24 scope global eth0\n",
- "2: eth0 inet 100.64.0.1/10 scope global eth0\n",
- ],
- ids=["public-only", "no-address", "172.32", "172.15", "cgnat"],
- )
- def test_no_private_address_fails_closed(tmp_path, ip_out):
- proc, args = run_script(tmp_path, ip_out)
- assert proc.returncode != 0
- assert args is None
- assert "refusing to start" in proc.stderr
- def test_the_script_never_writes_a_wildcard_address():
- text = extract()
- assert "0.0.0.0" not in text
- assert not re.search(r"listen-address=:", text)
- assert "exec " + EXPORTER in text
- def test_dropin_replaces_exec_start_with_the_wrapper_and_nothing_order_dependent():
- text = extract_file(TEMPLATE.read_text(encoding="utf-8"), DROPIN_PATH)
- lines = text.splitlines()
- # An empty ExecStart= clears the package's own command line before ours.
- first = lines.index("ExecStart=")
- assert lines[first + 1] == "ExecStart=/usr/local/sbin/cah-node-exporter-run"
- # systemd reads EnvironmentFile= before ExecStartPre= runs, so neither may
- # carry the address.
- assert "EnvironmentFile" not in text and "ExecStartPre" not in text
- def test_extraction_negative_case_missing_path_is_an_error():
- with pytest.raises(AssertionError):
- extract_file(TEMPLATE.read_text(encoding="utf-8"), "/etc/not-written")
- def test_template_gates_every_node_exporter_piece_on_the_flag():
- # Every mention of the exporter must sit inside a node_exporter conditional,
- # so the default render is unchanged.
- text = TEMPLATE.read_text(encoding="utf-8")
- needles = (
- "- prometheus-node-exporter",
- SCRIPT_PATH,
- "prometheus-node-exporter.service.d",
- "systemctl restart prometheus-node-exporter",
- "node_exporter_rule",
- "host_firewall_node_exporter",
- )
- for needle in needles:
- found = list(re.finditer(re.escape(needle), text))
- assert found, f"{needle} is not in the template"
- for m in found:
- before = text[: m.start()]
- assert before.rfind("%{ if node_exporter ~}") > before.rfind("%{ endif ~}"), (
- f"{needle} is outside a node_exporter conditional"
- )
|