Closes the cred-coverage gap for 7 services that already had the data
on the wire but never landed it in the Credential table:
- SNMP — community string lands as secret_kind="snmp_community",
principal=None (v1/v2c has no per-user identity, the community IS
the auth).
- SIP — Digest response hash, previously buried in the auth= header
dump, now classify_authorization()-extracted.
- HTTP / HTTPS — Authorization header was in the headers JSON but
never extracted. Now Basic decodes to plaintext, Bearer →
http_bearer (principal=None), Digest → http_digest_md5.
- K8s — already extracted Authorization but didn't normalize. Service-
account JWTs flow through as Bearer.
- Docker API — headers absent entirely. Adds the headers JSON dump
and runs Authorization through the classifier.
- Elasticsearch — five distinct request handlers; each gains a
per-handler _cred_fields() helper.
Adds canonical templates/syslog_bridge.py:classify_authorization().
Recognised: Basic / Bearer / Token / Digest. Unknown schemes (NTLM,
AWS4-HMAC, Negotiate) return None; the header still rides in the
ambient SD-block but isn't normalized as a credential. The SD shape
on the wire collapses sip_digest_md5 into http_digest_md5 — same
algorithm, so cross-protocol reuse correlates correctly when (rare)
nonce collisions allow.
Drive-by repair of tests/core/test_fingerprinting.py:
- The pre-existing `test_http_useragent_extracted` asserted both that
add_bounty was called exactly once AND that the UA payload carried
`path` and `method` fields. Both wrong since this session opened:
the http_quirks fingerprint added later fires too, and the UA
payload never actually included path/method despite the assertion.
- Adds `path`/`method` to the UA fingerprint payload (real operator
value: "Nikto hit /admin" beats "Nikto seen on this decky").
- Replaces `assert_awaited_once` with a `_find_ua_bounty()` helper
that filters add_bounty calls by `fingerprint_type`. New fingerprint
families landing later won't retroactively break old tests.
- Updates the two credential-bearing tests to use the post-DEBT-039
native shape (`secret_b64` / `principal`) and `upsert_credential`,
not the deleted legacy `username+password` adapter.
Also rebuilds the per-service fake `syslog_bridge` modules in
tests/service_testing/{conftest,test_imap,test_pop3,test_snmp,test_mqtt,test_smtp}.py
to expose `encode_secret` + `classify_authorization`. Service templates
that import either now no longer fail at test collection.
173 tests pass in the touched scope. Phases 2-7 still pending.
203 lines
7.2 KiB
Python
203 lines
7.2 KiB
Python
"""
|
|
Tests for decnet/templates/mqtt/server.py
|
|
|
|
Exercises behavior with MQTT_ACCEPT_ALL=1 and customizable topics.
|
|
Uses asyncio transport/protocol directly.
|
|
"""
|
|
|
|
import importlib.util
|
|
import json
|
|
import sys
|
|
from types import ModuleType
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
# ── Helpers ───────────────────────────────────────────────────────────────────
|
|
|
|
def _make_fake_syslog_bridge() -> ModuleType:
|
|
mod = ModuleType("syslog_bridge")
|
|
mod.syslog_line = MagicMock(return_value="")
|
|
mod.write_syslog_file = MagicMock()
|
|
mod.forward_syslog = MagicMock()
|
|
mod.SEVERITY_WARNING = 4
|
|
mod.SEVERITY_INFO = 6
|
|
mod.encode_secret = MagicMock(return_value={"secret_printable": "", "secret_b64": ""})
|
|
mod.classify_authorization = MagicMock(return_value=None)
|
|
return mod
|
|
|
|
|
|
def _load_mqtt(accept_all: bool = True, custom_topics: str = "", persona: str = "water_plant"):
|
|
env = {
|
|
"MQTT_ACCEPT_ALL": "1" if accept_all else "0",
|
|
"NODE_NAME": "testhost",
|
|
"MQTT_PERSONA": persona,
|
|
"MQTT_CUSTOM_TOPICS": custom_topics,
|
|
}
|
|
for key in ("mqtt_server", "syslog_bridge", "instance_seed"):
|
|
sys.modules.pop(key, None)
|
|
|
|
sys.modules["syslog_bridge"] = _make_fake_syslog_bridge()
|
|
|
|
spec = importlib.util.spec_from_file_location("mqtt_server", "decnet/templates/mqtt/server.py")
|
|
mod = importlib.util.module_from_spec(spec)
|
|
with patch.dict("os.environ", env, clear=False):
|
|
from .conftest import load_real_instance_seed
|
|
sys.modules["instance_seed"] = load_real_instance_seed()
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
def _make_protocol(mod):
|
|
proto = mod.MQTTProtocol()
|
|
transport = MagicMock()
|
|
written: list[bytes] = []
|
|
transport.write.side_effect = written.append
|
|
proto.connection_made(transport)
|
|
written.clear()
|
|
return proto, transport, written
|
|
|
|
|
|
def _send(proto, data: bytes) -> None:
|
|
proto.data_received(data)
|
|
|
|
|
|
# ── Fixtures ──────────────────────────────────────────────────────────────────
|
|
|
|
@pytest.fixture
|
|
def mqtt_mod():
|
|
return _load_mqtt()
|
|
|
|
@pytest.fixture
|
|
def mqtt_no_auth_mod():
|
|
return _load_mqtt(accept_all=False)
|
|
|
|
|
|
# ── Packet Helpers ────────────────────────────────────────────────────────────
|
|
|
|
def _connect_packet() -> bytes:
|
|
# 0x10, len 14, 00 04 MQTT 04 02 00 3c 00 02 id
|
|
return b"\x10\x0e\x00\x04MQTT\x04\x02\x00\x3c\x00\x02id"
|
|
|
|
def _subscribe_packet(topic: str, pid: int = 1) -> bytes:
|
|
topic_bytes = topic.encode()
|
|
payload = pid.to_bytes(2, "big") + len(topic_bytes).to_bytes(2, "big") + topic_bytes + b"\x01" # qos 1
|
|
return bytes([0x82, len(payload)]) + payload
|
|
|
|
def _publish_packet(topic: str, payload: str, qos: int = 1, pid: int = 1) -> bytes:
|
|
topic_bytes = topic.encode()
|
|
payload_bytes = payload.encode()
|
|
flags = qos << 1
|
|
byte0 = 0x30 | flags
|
|
if qos > 0:
|
|
packet_payload = len(topic_bytes).to_bytes(2, "big") + topic_bytes + pid.to_bytes(2, "big") + payload_bytes
|
|
else:
|
|
packet_payload = len(topic_bytes).to_bytes(2, "big") + topic_bytes + payload_bytes
|
|
|
|
return bytes([byte0, len(packet_payload)]) + packet_payload
|
|
|
|
def _pingreq_packet() -> bytes:
|
|
return b"\xc0\x00"
|
|
|
|
def _disconnect_packet() -> bytes:
|
|
return b"\xe0\x00"
|
|
|
|
|
|
# ── Tests ─────────────────────────────────────────────────────────────────────
|
|
|
|
def test_connect_accept(mqtt_mod):
|
|
proto, transport, written = _make_protocol(mqtt_mod)
|
|
_send(proto, _connect_packet())
|
|
assert len(written) == 1
|
|
assert written[0] == b"\x20\x02\x00\x00"
|
|
assert proto._auth is True
|
|
|
|
def test_connect_reject(mqtt_no_auth_mod):
|
|
proto, transport, written = _make_protocol(mqtt_no_auth_mod)
|
|
_send(proto, _connect_packet())
|
|
assert len(written) == 1
|
|
assert written[0] == b"\x20\x02\x00\x05"
|
|
assert transport.close.called
|
|
|
|
def test_pingreq(mqtt_mod):
|
|
proto, _, written = _make_protocol(mqtt_mod)
|
|
_send(proto, _pingreq_packet())
|
|
assert written[0] == b"\xd0\x00"
|
|
|
|
def test_subscribe_wildcard_retained(mqtt_mod):
|
|
proto, _, written = _make_protocol(mqtt_mod)
|
|
_send(proto, _connect_packet())
|
|
written.clear()
|
|
|
|
# The water_plant persona now picks a per-instance site prefix (north,
|
|
# south, plant-a, etc.) instead of hardcoding "plant/". Use the top-level
|
|
# wildcard so the test doesn't depend on which site this decky rolled.
|
|
_send(proto, _subscribe_packet("#"))
|
|
|
|
assert len(written) >= 2 # At least SUBACK + some publishes
|
|
assert written[0].startswith(b"\x90") # SUBACK
|
|
|
|
combined = b"".join(written[1:])
|
|
# The identifying tail of water-plant topics is stable regardless of
|
|
# which site prefix was chosen at instance boot.
|
|
assert b"water/tank1/level" in combined
|
|
|
|
def test_publish_qos1_returns_puback(mqtt_mod):
|
|
proto, _, written = _make_protocol(mqtt_mod)
|
|
_send(proto, _connect_packet())
|
|
written.clear()
|
|
|
|
_send(proto, _publish_packet("target/topic", "malicious_payload", qos=1, pid=42))
|
|
assert len(written) == 1
|
|
# PUBACK (0x40), len=2, pid=42
|
|
assert written[0] == b"\x40\x02\x00\x2a"
|
|
|
|
def test_custom_topics():
|
|
custom = {"custom/1": "val1", "custom/2": "val2"}
|
|
mod = _load_mqtt(custom_topics=json.dumps(custom))
|
|
proto, _, written = _make_protocol(mod)
|
|
_send(proto, _connect_packet())
|
|
written.clear()
|
|
|
|
_send(proto, _subscribe_packet("custom/1"))
|
|
assert len(written) > 1
|
|
combined = b"".join(written[1:])
|
|
assert b"custom/1" in combined
|
|
assert b"val1" in combined
|
|
|
|
# ── Negative Tests ────────────────────────────────────────────────────────────
|
|
|
|
def test_subscribe_before_auth_closes(mqtt_mod):
|
|
proto, transport, written = _make_protocol(mqtt_mod)
|
|
_send(proto, _subscribe_packet("plant/#"))
|
|
assert transport.close.called
|
|
|
|
def test_publish_before_auth_closes(mqtt_mod):
|
|
proto, transport, written = _make_protocol(mqtt_mod)
|
|
_send(proto, _publish_packet("test", "test", qos=0))
|
|
assert transport.close.called
|
|
|
|
def test_malformed_connect_len(mqtt_mod):
|
|
proto, transport, _ = _make_protocol(mqtt_mod)
|
|
_send(proto, b"\x10\x05\x00\x04MQT")
|
|
# buffer handles it
|
|
_send(proto, b"\x10\x02\x00\x04")
|
|
# No crash
|
|
|
|
def test_bad_packet_type_closer(mqtt_mod):
|
|
proto, transport, _ = _make_protocol(mqtt_mod)
|
|
_send(proto, b"\xf0\x00") # Reserved type 15
|
|
assert transport.close.called
|
|
|
|
def test_invalid_json_config():
|
|
mod = _load_mqtt(custom_topics="{invalid: json}")
|
|
proto, _, _ = _make_protocol(mod)
|
|
assert len(proto._topics) > 0 # fell back to persona
|
|
|
|
def test_disconnect_packet(mqtt_mod):
|
|
proto, transport, _ = _make_protocol(mqtt_mod)
|
|
_send(proto, _connect_packet())
|
|
_send(proto, _disconnect_packet())
|
|
assert transport.close.called
|