Skip to content

Commit 8515e5b

Browse files
committed
ignore PKI for testmsg as well
1 parent bfa24f8 commit 8515e5b

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed

bridger/cogs/testmsg.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
from datetime import datetime
44
from functools import partial
5+
from typing import Optional
56

67
import aiomqtt
78
from aiocache import SimpleMemoryCache
@@ -17,6 +18,7 @@
1718
from bridger.influx.interfaces import InfluxReader
1819
from bridger.log import logger
1920
from bridger.mqtt import PBPacketProcessor
21+
from bridger.utils import should_ignore_pki_message
2022

2123
MQTT_TEST_CHANNEL_MESHTASTIC = os.getenv("MQTT_TEST_CHANNEL", "+")
2224
MQTT_TEST_CHANNEL_DISCORD = int(os.getenv("MQTT_TEST_CHANNEL_ID", 1253788609316913265))
@@ -43,7 +45,7 @@ async def on_ready(self):
4345
logger.info(f"TestMsg cog is ready and channel is: {self.discord_channel}")
4446

4547
@staticmethod
46-
def format_node_name(node_id: int, node_info: dict = None) -> str:
48+
def format_node_name(node_id: int, node_info: Optional[dict] = None) -> str:
4749
"""Format a consistent node name based on available info"""
4850
if not node_info:
4951
return f"**{node_id}**"
@@ -71,14 +73,17 @@ def create_embed(self, service_envelope: ServiceEnvelope):
7173

7274
embed = Embed(color=color)
7375
# Try to get node info for the gateway hex ID
76+
gateway_id = None
77+
node_info = None
78+
7479
try:
7580
gateway_id = int(gateway.strip("!"), 16)
7681
node_info = self.influx_reader.get_node_info(gateway_id)
7782
except (ValueError, TypeError) as e:
7883
logger.error(f"Failed to parse gateway ID '{gateway}': {e}")
79-
node_info = None
8084

81-
gateway_name = self.format_node_name(gateway_id if "gateway_id" in locals() else gateway, node_info)
85+
node_id = gateway_id if gateway_id is not None else int(gateway, 16)
86+
gateway_name = self.format_node_name(node_id, node_info)
8287
embed.description = f"Heard by {gateway_name} - `{gateway}` at {formatted_time}"
8388
embed.add_field(name="SNR", value=snr, inline=True)
8489
embed.add_field(name="RSSI", value=rssi, inline=True)
@@ -112,7 +117,7 @@ async def run_mqtt(self):
112117
logger.info(f"Attempting to connect to MQTT broker at {MQTT_BROKER}:{MQTT_PORT}")
113118
async with aiomqtt.Client(
114119
MQTT_BROKER,
115-
MQTT_PORT,
120+
int(MQTT_PORT),
116121
username=MQTT_USER,
117122
password=MQTT_PASS,
118123
clean_session=True,
@@ -121,9 +126,16 @@ async def run_mqtt(self):
121126
logger.info(f"Subscribed to {full_topic}")
122127
await logger.complete()
123128

124-
async for message in client.messages:
129+
async for mqtt_message in client.messages:
130+
# Ignoring PKI messages for now as we cannot decrypt them without storing keys somewhere
131+
if should_ignore_pki_message(str(mqtt_message.topic)):
132+
logger.bind(topic=topic, channel=channel, *mqtt_message.properties).debug(
133+
f"Ignoring PKI message on topic {mqtt_message.topic}"
134+
) # noqa: E501
135+
continue
136+
125137
try:
126-
service_envelope = ServiceEnvelope.FromString(message.payload)
138+
service_envelope = ServiceEnvelope.FromString(mqtt_message.payload)
127139
except Exception:
128140
logger.exception("Failed to decode MQTT message")
129141
continue

bridger/mqtt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from bridger.influx.interfaces import InfluxWriter
1212
from bridger.log import logger
1313
from bridger.mesh import PacketProcessorError, PBPacketProcessor
14+
from bridger.utils import should_ignore_pki_message
1415

1516

1617
class BridgerMQTT(Client):
@@ -45,9 +46,8 @@ def on_message(self, client, userdata, message):
4546
add_breadcrumb(level="info", data=breadcrumb_data, category="mqtt", message="Received message")
4647

4748
# Ignoring PKI messages for now as we cannot decrypt them without storing keys somewhere
48-
pki_topic = MQTT_TOPIC.removesuffix("/#") + "/PKI/"
49-
if message.topic.startswith(pki_topic):
50-
logger.bind(**breadcrumb_data).debug("Ignoring PKI message")
49+
if should_ignore_pki_message(message.topic):
50+
logger.bind(**breadcrumb_data).debug(f"Ignoring PKI message on topic {message.topic}")
5151
return
5252

5353
try:

bridger/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from bridger.config import MQTT_TOPIC
2+
3+
4+
def should_ignore_pki_message(topic: str) -> bool:
5+
pki_topic = MQTT_TOPIC.removesuffix("/#") + "/PKI/"
6+
return topic.startswith(pki_topic)

tests/test_testmsg.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,38 @@ def test_integration_deduplication_prevents_true_duplicates(self, testmsg_cog):
169169

170170
should_process_second = testmsg_cog.deduplicator.should_process(envelope2)
171171
assert not should_process_second
172+
173+
174+
class TestFormatNodeName:
175+
def test_format_node_name_no_node_info(self):
176+
result = TestMsg.format_node_name(123456789, None)
177+
assert result == "**123456789**"
178+
179+
def test_format_node_name_empty_node_info(self):
180+
result = TestMsg.format_node_name(123456789, {})
181+
assert result == "**123456789**"
182+
183+
def test_format_node_name_with_short_and_long(self):
184+
node_info = {"short_name": "ABC", "long_name": "Station ABC"}
185+
result = TestMsg.format_node_name(123456789, node_info)
186+
assert result == "**ABC** - Station ABC"
187+
188+
def test_format_node_name_with_short_only(self):
189+
node_info = {"short_name": "ABC"}
190+
result = TestMsg.format_node_name(123456789, node_info)
191+
assert result == "**123456789**"
192+
193+
def test_format_node_name_with_long_only(self):
194+
node_info = {"long_name": "Station ABC"}
195+
result = TestMsg.format_node_name(123456789, node_info)
196+
assert result == "**123456789**"
197+
198+
def test_format_node_name_with_empty_strings(self):
199+
node_info = {"short_name": "", "long_name": ""}
200+
result = TestMsg.format_node_name(123456789, node_info)
201+
assert result == "**123456789**"
202+
203+
def test_format_node_name_with_none_values(self):
204+
node_info = {"short_name": None, "long_name": None}
205+
result = TestMsg.format_node_name(123456789, node_info)
206+
assert result == "**123456789**"

tests/test_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from unittest.mock import patch
2+
3+
from bridger.utils import should_ignore_pki_message
4+
5+
6+
class TestShouldIgnorePkiMessage:
7+
8+
@patch("bridger.utils.MQTT_TOPIC", "msh/US/2/e/LongFast/#")
9+
def test_should_ignore_pki_message_true(self):
10+
pki_topic = "msh/US/2/e/LongFast/PKI/some/subtopic"
11+
assert should_ignore_pki_message(pki_topic) is True
12+
13+
@patch("bridger.utils.MQTT_TOPIC", "msh/US/2/e/LongFast/#")
14+
def test_should_ignore_pki_message_false_regular_topic(self):
15+
regular_topic = "msh/US/2/e/LongFast/MQTT/some/subtopic"
16+
assert should_ignore_pki_message(regular_topic) is False
17+
18+
@patch("bridger.utils.MQTT_TOPIC", "msh/US/2/e/LongFast/#")
19+
def test_should_ignore_pki_message_false_different_path(self):
20+
different_topic = "msh/US/2/e/DifferentChannel/PKI/subtopic"
21+
assert should_ignore_pki_message(different_topic) is False
22+
23+
@patch("bridger.utils.MQTT_TOPIC", "msh/US/2/e/LongFast/#")
24+
def test_should_ignore_pki_message_false_partial_match(self):
25+
partial_topic = "msh/US/2/e/LongFast/PKI"
26+
assert should_ignore_pki_message(partial_topic) is False
27+
28+
@patch("bridger.utils.MQTT_TOPIC", "simple/topic/#")
29+
def test_should_ignore_pki_message_simple_topic(self):
30+
pki_topic = "simple/topic/PKI/test"
31+
regular_topic = "simple/topic/regular/test"
32+
33+
assert should_ignore_pki_message(pki_topic) is True
34+
assert should_ignore_pki_message(regular_topic) is False
35+
36+
@patch("bridger.utils.MQTT_TOPIC", "no/wildcard")
37+
def test_should_ignore_pki_message_no_wildcard(self):
38+
pki_topic = "no/wildcard/PKI/test"
39+
regular_topic = "no/wildcard/regular/test"
40+
41+
assert should_ignore_pki_message(pki_topic) is True
42+
assert should_ignore_pki_message(regular_topic) is False

0 commit comments

Comments
 (0)