2021-06-06 06:41:49 +00:00
|
|
|
import pytest
|
2021-07-04 05:56:34 +00:00
|
|
|
import logging
|
2021-06-06 06:41:49 +00:00
|
|
|
|
|
|
|
from amqtt.plugins.manager import BaseContext
|
2021-06-06 07:02:08 +00:00
|
|
|
from amqtt.plugins.topic_checking import (
|
|
|
|
BaseTopicPlugin,
|
|
|
|
TopicTabooPlugin,
|
|
|
|
TopicAccessControlListPlugin,
|
2021-07-04 05:24:43 +00:00
|
|
|
Action,
|
2021-06-06 07:02:08 +00:00
|
|
|
)
|
2021-06-06 07:00:41 +00:00
|
|
|
from amqtt.session import Session
|
2021-06-06 06:41:49 +00:00
|
|
|
|
|
|
|
|
2021-06-06 07:00:41 +00:00
|
|
|
# Base plug-in object
|
|
|
|
|
|
|
|
|
2021-06-06 06:41:49 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_base_no_config(logdog):
|
2021-06-06 06:41:49 +00:00
|
|
|
"""
|
|
|
|
Check BaseTopicPlugin returns false if no topic-check is present.
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {}
|
2021-06-06 06:41:49 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = BaseTopicPlugin(context)
|
|
|
|
authorised = plugin.topic_filtering()
|
|
|
|
assert authorised is False
|
2021-06-06 06:41:49 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should have printed a couple of warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 2
|
|
|
|
assert log_records[0].levelno == logging.WARN
|
|
|
|
assert (
|
|
|
|
log_records[0].message
|
|
|
|
== "'topic-check' section not found in context configuration"
|
|
|
|
)
|
2021-07-04 05:56:34 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
assert log_records[1].levelno == logging.WARN
|
2021-07-08 00:23:24 +00:00
|
|
|
assert log_records[1].message == "'auth' section not found in context configuration"
|
2021-07-07 21:52:01 +00:00
|
|
|
assert pile.is_empty()
|
2021-06-06 06:41:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_base_empty_config(logdog):
|
2021-06-06 06:41:49 +00:00
|
|
|
"""
|
|
|
|
Check BaseTopicPlugin returns false if topic-check is empty.
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {}}
|
2021-06-06 06:41:49 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = BaseTopicPlugin(context)
|
|
|
|
authorised = plugin.topic_filtering()
|
|
|
|
assert authorised is False
|
2021-06-06 06:41:49 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should have printed just one warning
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 1
|
|
|
|
assert log_records[0].levelno == logging.WARN
|
2021-07-08 00:23:24 +00:00
|
|
|
assert log_records[0].message == "'auth' section not found in context configuration"
|
2021-06-06 06:41:49 +00:00
|
|
|
|
|
|
|
|
2021-06-06 22:25:19 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_base_disabled_config(logdog):
|
2021-06-06 22:25:19 +00:00
|
|
|
"""
|
|
|
|
Check BaseTopicPlugin returns true if disabled. (it doesn't actually check)
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {"enabled": False}}
|
2021-06-06 22:25:19 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = BaseTopicPlugin(context)
|
|
|
|
authorised = plugin.topic_filtering()
|
|
|
|
assert authorised is True
|
2021-06-06 22:25:19 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should NOT have printed warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 0
|
2021-06-06 22:25:19 +00:00
|
|
|
|
|
|
|
|
2021-06-06 06:41:49 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_base_enabled_config(logdog):
|
2021-06-06 06:41:49 +00:00
|
|
|
"""
|
|
|
|
Check BaseTopicPlugin returns true if enabled.
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {"enabled": True}}
|
2021-06-06 06:41:49 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = BaseTopicPlugin(context)
|
|
|
|
authorised = plugin.topic_filtering()
|
|
|
|
assert authorised is True
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should NOT have printed warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 0
|
2021-06-06 07:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Taboo plug-in
|
|
|
|
|
2021-06-06 07:02:08 +00:00
|
|
|
|
2021-06-06 07:00:41 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taboo_empty_config(logdog):
|
2021-06-06 07:00:41 +00:00
|
|
|
"""
|
|
|
|
Check TopicTabooPlugin returns false if topic-check absent.
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {}
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = TopicTabooPlugin(context)
|
|
|
|
assert (await plugin.topic_filtering()) is False
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should have printed a couple of warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 2
|
|
|
|
assert log_records[0].levelno == logging.WARN
|
|
|
|
assert (
|
|
|
|
log_records[0].message
|
|
|
|
== "'topic-check' section not found in context configuration"
|
|
|
|
)
|
|
|
|
assert log_records[1].levelno == logging.WARN
|
2021-07-08 00:23:24 +00:00
|
|
|
assert log_records[1].message == "'auth' section not found in context configuration"
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-06-06 07:02:08 +00:00
|
|
|
|
2021-06-06 22:25:19 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-08 00:37:45 +00:00
|
|
|
async def test_taboo_disabled(logdog):
|
2021-06-06 22:25:19 +00:00
|
|
|
"""
|
|
|
|
Check TopicTabooPlugin returns true if checking disabled.
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {"enabled": False}}
|
2021-06-06 22:25:19 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "anybody"
|
2021-06-06 22:25:19 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = TopicTabooPlugin(context)
|
|
|
|
assert (
|
|
|
|
await plugin.topic_filtering(session=session, topic="not/prohibited")
|
|
|
|
) is True
|
2021-06-06 22:25:19 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should NOT have printed warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 0
|
2021-06-06 22:25:19 +00:00
|
|
|
|
|
|
|
|
2021-06-06 07:00:41 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taboo_not_taboo_topic(logdog):
|
2021-06-06 07:00:41 +00:00
|
|
|
"""
|
|
|
|
Check TopicTabooPlugin returns true if topic not taboo
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {"enabled": True}}
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "anybody"
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = TopicTabooPlugin(context)
|
|
|
|
assert (
|
|
|
|
await plugin.topic_filtering(session=session, topic="not/prohibited")
|
|
|
|
) is True
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should NOT have printed warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 0
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-06-06 07:02:08 +00:00
|
|
|
|
2021-06-06 07:00:41 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taboo_anon_taboo_topic(logdog):
|
2021-06-06 07:00:41 +00:00
|
|
|
"""
|
|
|
|
Check TopicTabooPlugin returns false if topic is taboo and session is anonymous.
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {"enabled": True}}
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = ""
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = TopicTabooPlugin(context)
|
2021-07-04 06:08:53 +00:00
|
|
|
assert (
|
|
|
|
await plugin.topic_filtering(session=session, topic="prohibited")
|
|
|
|
) is False
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should NOT have printed warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 0
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-06-06 07:02:08 +00:00
|
|
|
|
2021-06-06 07:00:41 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taboo_notadmin_taboo_topic(logdog):
|
2021-06-06 07:00:41 +00:00
|
|
|
"""
|
|
|
|
Check TopicTabooPlugin returns false if topic is taboo and user is not "admin".
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {"enabled": True}}
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "notadmin"
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = TopicTabooPlugin(context)
|
2021-07-04 06:08:53 +00:00
|
|
|
assert (
|
|
|
|
await plugin.topic_filtering(session=session, topic="prohibited")
|
|
|
|
) is False
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should NOT have printed warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 0
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-06-06 07:02:08 +00:00
|
|
|
|
2021-06-06 07:00:41 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taboo_admin_taboo_topic(logdog):
|
2021-06-06 07:00:41 +00:00
|
|
|
"""
|
|
|
|
Check TopicTabooPlugin returns true if topic is taboo and user is "admin".
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {"topic-check": {"enabled": True}}
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "admin"
|
2021-06-06 07:00:41 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = TopicTabooPlugin(context)
|
2021-07-04 06:08:53 +00:00
|
|
|
assert (
|
|
|
|
await plugin.topic_filtering(session=session, topic="prohibited")
|
|
|
|
) is True
|
2021-06-06 06:41:49 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should NOT have printed warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 0
|
2021-06-06 07:24:13 +00:00
|
|
|
|
|
|
|
|
2021-06-06 07:42:06 +00:00
|
|
|
# TopicAccessControlListPlugin tests
|
|
|
|
|
|
|
|
|
2021-06-06 07:24:13 +00:00
|
|
|
def test_topic_ac_not_match():
|
|
|
|
"""
|
|
|
|
Test TopicAccessControlListPlugin.topic_ac returns false if topics do not match.
|
|
|
|
"""
|
|
|
|
assert (
|
|
|
|
TopicAccessControlListPlugin.topic_ac("a/topic/to/match", "a/topic/to/notmatch")
|
|
|
|
is False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_topic_ac_not_match_longer_acl():
|
|
|
|
"""
|
|
|
|
Test TopicAccessControlListPlugin.topic_ac returns false if topics do not match and ACL topic is longer.
|
|
|
|
"""
|
2021-06-06 07:43:02 +00:00
|
|
|
assert TopicAccessControlListPlugin.topic_ac("topic", "topic/is/longer") is False
|
2021-06-06 07:24:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_topic_ac_not_match_longer_rq():
|
|
|
|
"""
|
|
|
|
Test TopicAccessControlListPlugin.topic_ac returns false if topics do not match and RQ topic is longer.
|
|
|
|
"""
|
2021-06-06 07:43:02 +00:00
|
|
|
assert TopicAccessControlListPlugin.topic_ac("topic/is/longer", "topic") is False
|
2021-06-06 07:24:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_topic_ac_match_exact():
|
|
|
|
"""
|
|
|
|
Test TopicAccessControlListPlugin.topic_ac returns true if topics match exactly.
|
|
|
|
"""
|
|
|
|
assert TopicAccessControlListPlugin.topic_ac("exact/topic", "exact/topic") is True
|
|
|
|
|
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
def test_topic_ac_match_plus():
|
2021-06-06 07:24:13 +00:00
|
|
|
"""
|
|
|
|
Test TopicAccessControlListPlugin.topic_ac correctly handles '+' wildcard.
|
|
|
|
"""
|
|
|
|
assert (
|
|
|
|
TopicAccessControlListPlugin.topic_ac(
|
|
|
|
"a/topic/anything/value", "a/topic/+/value"
|
|
|
|
)
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_topic_ac_match_hash():
|
|
|
|
"""
|
|
|
|
Test TopicAccessControlListPlugin.topic_ac correctly handles '#' wildcard.
|
|
|
|
"""
|
|
|
|
assert (
|
|
|
|
TopicAccessControlListPlugin.topic_ac(
|
|
|
|
"topic/prefix/and/suffix", "topic/prefix/#"
|
|
|
|
)
|
|
|
|
is True
|
|
|
|
)
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_empty_config(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns false if topic-check absent.
|
|
|
|
"""
|
2021-07-04 05:56:34 +00:00
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
2021-07-04 06:08:53 +00:00
|
|
|
context.logger = logging.getLogger("testlog")
|
2021-07-04 05:56:34 +00:00
|
|
|
context.config = {}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-04 05:56:34 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
assert (await plugin.topic_filtering()) is False
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-07 21:52:01 +00:00
|
|
|
# Should have printed a couple of warnings
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 2
|
|
|
|
assert (
|
|
|
|
log_records[0].message
|
|
|
|
== "'topic-check' section not found in context configuration"
|
|
|
|
)
|
2021-07-08 00:23:24 +00:00
|
|
|
assert log_records[1].message == "'auth' section not found in context configuration"
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
2021-06-06 22:25:19 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_true_disabled(logdog):
|
2021-06-06 22:25:19 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns true if topic checking is disabled.
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {"topic-check": {"enabled": False}}
|
2021-06-06 22:25:19 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "user"
|
2021-06-06 22:25:19 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.publish, session=session, topic="a/topic"
|
|
|
|
)
|
|
|
|
assert authorised is True
|
2021-06-06 22:25:19 +00:00
|
|
|
|
|
|
|
|
2021-06-06 07:42:06 +00:00
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_true_no_pub_acl(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns true if action=publish and no publish-acl given.
|
|
|
|
(This is for backward-compatibility with existing installations.)
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {"topic-check": {"enabled": True}}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "user"
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.publish, session=session, topic="a/topic"
|
|
|
|
)
|
|
|
|
assert authorised is True
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_false_sub_no_topic(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns false user there is no topic.
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {
|
|
|
|
"topic-check": {
|
|
|
|
"enabled": True,
|
|
|
|
"acl": {"anotheruser": ["allowed/topic", "another/allowed/topic/#"]},
|
2021-06-06 07:43:02 +00:00
|
|
|
}
|
2021-07-08 00:37:45 +00:00
|
|
|
}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "user"
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.subscribe, session=session, topic=""
|
|
|
|
)
|
|
|
|
assert authorised is False
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_false_sub_unknown_user(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns false user is not listed in ACL.
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {
|
|
|
|
"topic-check": {
|
|
|
|
"enabled": True,
|
|
|
|
"acl": {"anotheruser": ["allowed/topic", "another/allowed/topic/#"]},
|
2021-06-06 07:43:02 +00:00
|
|
|
}
|
2021-07-08 00:37:45 +00:00
|
|
|
}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "user"
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.subscribe, session=session, topic="allowed/topic"
|
|
|
|
)
|
|
|
|
assert authorised is False
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_false_sub_no_permission(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns false if "acl" does not list allowed topic.
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {
|
|
|
|
"topic-check": {
|
|
|
|
"enabled": True,
|
|
|
|
"acl": {"user": ["allowed/topic", "another/allowed/topic/#"]},
|
2021-06-06 07:43:02 +00:00
|
|
|
}
|
2021-07-08 00:37:45 +00:00
|
|
|
}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "user"
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.subscribe, session=session, topic="forbidden/topic"
|
|
|
|
)
|
|
|
|
assert authorised is False
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_true_sub_permission(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns true if "acl" lists allowed topic.
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {
|
|
|
|
"topic-check": {
|
|
|
|
"enabled": True,
|
|
|
|
"acl": {"user": ["allowed/topic", "another/allowed/topic/#"]},
|
2021-06-06 07:43:02 +00:00
|
|
|
}
|
2021-07-08 00:37:45 +00:00
|
|
|
}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "user"
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.subscribe, session=session, topic="allowed/topic"
|
|
|
|
)
|
|
|
|
assert authorised is True
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_true_pub_permission(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin returns true if "publish-acl" lists allowed topic for publish action.
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {
|
|
|
|
"topic-check": {
|
|
|
|
"enabled": True,
|
|
|
|
"publish-acl": {"user": ["allowed/topic", "another/allowed/topic/#"]},
|
2021-06-06 07:43:02 +00:00
|
|
|
}
|
2021-07-08 00:37:45 +00:00
|
|
|
}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = "user"
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.publish, session=session, topic="allowed/topic"
|
|
|
|
)
|
|
|
|
assert authorised is True
|
2021-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-07-04 05:56:34 +00:00
|
|
|
async def test_taclp_true_anon_sub_permission(logdog):
|
2021-06-06 07:42:06 +00:00
|
|
|
"""
|
|
|
|
Check TopicAccessControlListPlugin handles anonymous users.
|
|
|
|
"""
|
2021-07-08 00:37:45 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {
|
|
|
|
"topic-check": {
|
|
|
|
"enabled": True,
|
|
|
|
"acl": {"anonymous": ["allowed/topic", "another/allowed/topic/#"]},
|
2021-06-06 07:43:02 +00:00
|
|
|
}
|
2021-07-08 00:37:45 +00:00
|
|
|
}
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
session = Session()
|
|
|
|
session.username = None
|
2021-06-06 07:42:06 +00:00
|
|
|
|
2021-07-08 00:37:45 +00:00
|
|
|
plugin = TopicAccessControlListPlugin(context)
|
|
|
|
authorised = await plugin.topic_filtering(
|
|
|
|
action=Action.subscribe, session=session, topic="allowed/topic"
|
|
|
|
)
|
|
|
|
assert authorised is True
|