Files
napalm-ubiquiti/tests/test_sanitize.py
T

64 lines
2.0 KiB
Python

"""The fixture sanitiser: no secret of a recorded device reaches the repository."""
from __future__ import annotations
import json
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).parents[1] / "tools"))
from sanitize import REDACTED, anonymise_macs, redact, redact_cfg, sanitize # noqa: E402
def test_controller_keys_are_redacted_at_any_depth() -> None:
record = {"x_authkey": "abc", "vap_table": [{"x_passphrase": "pw", "essid": "Lab"}]}
assert redact(record) == {
"x_authkey": REDACTED,
"vap_table": [{"x_passphrase": REDACTED, "essid": "Lab"}],
}
def test_device_tokens_are_redacted() -> None:
assert redact({"guest_token": "t", "anon_id": "a", "model": "US24"}) == {
"guest_token": REDACTED,
"anon_id": REDACTED,
"model": "US24",
}
def test_running_cfg_secret_lines_are_redacted() -> None:
text = "unifi.key=55da\nusers.1.password=x\nswitch.port.2.pvid=11\n# comment\n"
assert (
redact_cfg(text)
== "unifi.key=REDACTED\nusers.1.password=REDACTED\nswitch.port.2.pvid=11\n# comment\n"
)
def test_replacements_apply_after_redaction_in_both_formats() -> None:
cfg = sanitize("resolv.host.1.name=usw-real\n", {"usw-real": "usw-1"})
data = json.loads(sanitize('{"mac": "64:f8:46:45:57:f0"}', {"45:57:f0": "00:00:10"}))
assert cfg == "resolv.host.1.name=usw-1\n"
assert data == {"mac": "64:f8:46:00:00:10"}
def test_other_macs_map_consistently_to_local_ones() -> None:
text = "aa:bb:cc:00:00:01 AA:BB:CC:00:00:01 f0:9f:c2:00:00:01 aa:bb:cc:00:00:02"
out = anonymise_macs(text, keep={"f0:9f:c2:00:00:01"})
assert out == "02:00:00:00:00:01 02:00:00:00:00:01 f0:9f:c2:00:00:01 02:00:00:00:00:02"
def test_sanitize_keeps_the_replaced_macs() -> None:
out = sanitize(
'{"a": "64:f8:46:45:57:f0", "b": "1c:a8:46:7b:2f:55"}',
{"64:f8:46:45:57:f0": "64:f8:46:00:00:10"},
macs=True,
)
assert json.loads(out) == {"a": "64:f8:46:00:00:10", "b": "02:00:00:00:00:01"}