-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DA] Add protection for SensorWAN "direct" addressing scheme
By defining a `direct_channel_allowed_networks` setting on the application configuration, the direct access to the corresponding channel will be restricted to the specified networks/owners.
- Loading branch information
Showing
9 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# (c) 2015-2021 Andreas Motl, <[email protected]> | ||
from munch import Munch | ||
|
||
from kotori.daq.decoder import MessageType | ||
|
||
|
||
class StrategyBase: | ||
|
||
def __init__(self, channel_settings=None): | ||
channel_settings = channel_settings or Munch() | ||
self.channel_settings = channel_settings | ||
|
||
@staticmethod | ||
def sanitize_db_identifier(value): | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ | |
# (c) 2015-2023 Andreas Motl, <[email protected]> | ||
import re | ||
|
||
from kotori.daq.exception import ChannelAccessDenied | ||
from kotori.daq.strategy import StrategyBase | ||
from kotori.util.common import SmartMunch | ||
from kotori.util.configuration import read_list | ||
|
||
|
||
class WanBusStrategy(StrategyBase): | ||
|
@@ -72,6 +74,12 @@ def topic_to_topology(self, topic): | |
|
||
# Try to match the per-device pattern with dashed topology encoding for topics. | ||
if address is None: | ||
|
||
# Decode permission setting from channel configuration object. | ||
direct_channel_allowed_networks = None | ||
if "direct_channel_allowed_networks" in self.channel_settings: | ||
direct_channel_allowed_networks = read_list(self.channel_settings.direct_channel_allowed_networks) | ||
|
||
m = self.direct_channel_matcher.match(topic) | ||
if m: | ||
address = SmartMunch(m.groupdict()) | ||
|
@@ -98,6 +106,10 @@ def topic_to_topology(self, topic): | |
# dissolved, or it was propagated into the `node` slot. | ||
del address.channel | ||
|
||
# Evaluate permissions. | ||
if direct_channel_allowed_networks and address.network not in direct_channel_allowed_networks: | ||
raise ChannelAccessDenied(f"Rejected access to SensorWAN network: {address.network}") | ||
|
||
# Try to match the classic path-based WAN topic encoding scheme. | ||
if address is None: | ||
m = self.wide_channel_matcher.match(topic) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# (c) 2020-2021 Andreas Motl <[email protected]> | ||
import logging | ||
import re | ||
|
||
import pytest | ||
import pytest_twisted | ||
|
@@ -123,7 +124,7 @@ def test_mqtt_to_influxdb_json_wan_device(machinery, device_create_influxdb, dev | |
@pytest_twisted.inlineCallbacks | ||
@pytest.mark.mqtt | ||
@pytest.mark.device | ||
def test_mqtt_to_influxdb_json_wan_channel(machinery, create_influxdb, reset_influxdb): | ||
def test_mqtt_to_influxdb_json_wan_channel_success(machinery, create_influxdb, reset_influxdb): | ||
""" | ||
Run MQTT data acquisition with per-device dashed-topo addressing. | ||
|
@@ -146,3 +147,32 @@ def test_mqtt_to_influxdb_json_wan_channel(machinery, create_influxdb, reset_inf | |
del record['time'] | ||
assert record == {u'humidity': 83.1, u'temperature': 42.84} | ||
yield record | ||
|
||
|
||
@pytest_twisted.inlineCallbacks | ||
@pytest.mark.mqtt | ||
@pytest.mark.device | ||
def test_mqtt_to_influxdb_json_wan_channel_access_denied(machinery, create_influxdb, reset_influxdb): | ||
""" | ||
Run MQTT data acquisition with per-device dashed-topo addressing. | ||
Addressing: Per-device WAN, with dashed topology decoding | ||
Example: mqttkit-1/channel/network-gateway-node | ||
""" | ||
|
||
# Submit a single measurement, without timestamp. | ||
data = { | ||
'temperature': 42.84, | ||
'humidity': 83.1, | ||
} | ||
yield threads.deferToThread(mqtt_json_sensor, settings.direct_mqtt_topic_channel_denied, data) | ||
|
||
# Wait for some time to process the message. | ||
yield sleep(PROCESS_DELAY_MQTT) | ||
|
||
# Proof that no data arrived in InfluxDB. | ||
with pytest.raises(AssertionError) as ex: | ||
influx_sensors.get_first_record() | ||
assert ex.match(re.escape("No data in database: len(result) = 0")) | ||
|
||
# FIXME: How to find `"Rejected access to SensorWAN network: another"` within log output? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
# (c) 2023 Andreas Motl <[email protected]> | ||
import pytest | ||
from munch import munchify | ||
|
||
from kotori.daq.exception import ChannelAccessDenied | ||
from kotori.daq.strategy.wan import WanBusStrategy | ||
from kotori.util.common import SmartMunch | ||
|
||
|
@@ -43,7 +45,7 @@ def test_wan_strategy_device_generic_success(): | |
|
||
|
||
@pytest.mark.strategy | ||
def test_wan_strategy_device_dashed_topo_basic(): | ||
def test_wan_strategy_device_dashed_topo_basic_success(): | ||
""" | ||
Verify the per-device WAN topology decoding, using a dashed device identifier, which translates to the topology. | ||
""" | ||
|
@@ -60,6 +62,17 @@ def test_wan_strategy_device_dashed_topo_basic(): | |
) | ||
|
||
|
||
@pytest.mark.strategy | ||
def test_wan_strategy_device_dashed_topo_basic_access_denied(): | ||
""" | ||
Verify the per-device WAN topology decoding, using a dashed device identifier, which translates to the topology. | ||
""" | ||
strategy = WanBusStrategy(channel_settings=munchify({"direct_channel_allowed_networks": "foo, bar"})) | ||
with pytest.raises(ChannelAccessDenied) as ex: | ||
strategy.topic_to_topology("myrealm/channel/baz-qux-eui70b3d57ed005dac6/data.json") | ||
assert ex.match("Rejected access to SensorWAN network: baz") | ||
|
||
|
||
@pytest.mark.strategy | ||
def test_wan_strategy_device_dashed_topo_too_few_components(): | ||
""" | ||
|