100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added realm prefix for MQTT subscription and publish messages
Michael Rademakerbc092ae0d13c
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.javaindex 8bc762866..845f63b55 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java@@ -182,11 +182,11 @@ public class MqttBrokerService implements ContainerService {}public static boolean isAttributeTopic(List<String> tokens) {- return tokens.get(0).equals(ATTRIBUTE_TOPIC) || tokens.get(0).equals(ATTRIBUTE_VALUE_TOPIC);+ return tokens.get(1).equals(ATTRIBUTE_TOPIC) || tokens.get(1).equals(ATTRIBUTE_VALUE_TOPIC);}public static boolean isAssetTopic(List<String> tokens) {- return tokens.get(0).equals(ASSET_TOPIC);+ return tokens.get(1).equals(ASSET_TOPIC);}public static AssetFilter<?> buildAssetFilter(MqttConnection connection, List<String> topicTokens) {@@ -203,7 +203,7 @@ public class MqttBrokerService implements ContainerService {List<String> paths = new ArrayList<>();List<String> attributeNames = new ArrayList<>();- String assetId = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(1)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(1)) ? null : topicTokens.get(1);+ String assetId = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(2)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(2)) ? null : topicTokens.get(2);int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);@@ -211,55 +211,55 @@ public class MqttBrokerService implements ContainerService {return null;}- if (topicTokens.size() == 2) {- if (multiLevelIndex == 1) {- //.../#+ if (topicTokens.size() == 3) {+ if (multiLevelIndex == 2) {+ //realm/.../#// No asset filtering required- } else if (singleLevelIndex == 1) {- //.../++ } else if (singleLevelIndex == 2) {+ //realm/.../+parentIds.add(null);} else {- //.../assetId+ //realm/.../assetIdassetIds.add(assetId);}- } else if (topicTokens.size() == 3) {+ } else if (topicTokens.size() == 4) {if (isAssetTopic) {- if (multiLevelIndex == 2) {- //asset/assetId/#+ if (multiLevelIndex == 3) {+ //realm/asset/assetId/#paths.add(assetId);- } else if (singleLevelIndex == 2) {- //asset/assetId/++ } else if (singleLevelIndex == 3) {+ //realm/asset/assetId/+parentIds.add(assetId);} else {return null;}} else {if (assetId != null) {- if (multiLevelIndex == 2) {- //attribute/assetId/#+ if (multiLevelIndex == 3) {+ //realm/attribute/assetId/#paths.add(assetId);- } else if (singleLevelIndex == 2) {- //attribute/assetId/++ } else if (singleLevelIndex == 3) {+ //realm/attribute/assetId/+parentIds.add(assetId);} else {assetIds.add(assetId);- String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(2)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(2)) ? null : topicTokens.get(2);+ String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(3)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(3)) ? null : topicTokens.get(3);if (attributeName != null) {- //attribute/assetId/attributeName- attributeNames.add(topicTokens.get(2));+ //realm/attribute/assetId/attributeName+ attributeNames.add(topicTokens.get(3));} else {return null;}}} else {- String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(2)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(2)) ? null : topicTokens.get(2);+ String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(3)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(3)) ? null : topicTokens.get(3);if (attributeName != null) {attributeNames.add(attributeName);- if (multiLevelIndex == 2) {+ if (multiLevelIndex == 3) {return null; //no topic allowed after multilevel wildcard- } else if (singleLevelIndex == 2) {- //attribute/+/attributeName+ } else if (singleLevelIndex == 3) {+ //realm/attribute/+/attributeNameparentIds.add(null);} else {return null;@@ -269,17 +269,17 @@ public class MqttBrokerService implements ContainerService {}}}- } else if (topicTokens.size() == 4) {+ } else if (topicTokens.size() == 5) {if (isAssetTopic || assetId == null) {return null;}- String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(3)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(3)) ? null : topicTokens.get(3);+ String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(4)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(4)) ? null : topicTokens.get(4);if (attributeName != null) {attributeNames.add(attributeName);- if (multiLevelIndex == 2) {+ if (multiLevelIndex == 3) {return null; //no topic allowed after multilevel wildcard- } else if (singleLevelIndex == 2) {- //attribute/assetId/+/attributeName+ } else if (singleLevelIndex == 3) {+ //realm/attribute/assetId/+/attributeNameparentIds.add(assetId);} else {return null;diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.javaindex 749ddd9ac..c3ac88407 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java@@ -93,6 +93,11 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());+ if(!connection.realm.equals(topicTokens.get(0))) {+ LOG.warning("Topic should start with the realm of the connection: " + connection);+ return false;+ }+boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);@@ -101,8 +106,8 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {return false;}- if(topicTokens.size() > 1) {- String assetId = topicTokens.get(1);+ if(topicTokens.size() > 2) {+ String assetId = topicTokens.get(2);if (!SINGLE_LEVEL_WILDCARD.equals(assetId) && !MULTI_LEVEL_WILDCARD.equals(assetId)) {Asset<?> asset = assetStorageService.find(assetId);@@ -112,9 +117,9 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {return false;}- if (isAttributeTopic && topicTokens.size() > 2- && !(SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(2)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(2)))) {- String attributeName = topicTokens.get(2);+ if (isAttributeTopic && topicTokens.size() > 3+ && !(SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(3)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(3)))) {+ String attributeName = topicTokens.get(3);if (!asset.hasAttribute(attributeName)) {LOG.fine("Asset attribute not found for topic '" + topic + "': " + connection);@@ -140,12 +145,12 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {}- if (topicTokens.get(0).equals(ATTRIBUTE_VALUE_TOPIC) && topicTokens.size() != 3) {+ if (topicTokens.get(1).equals(ATTRIBUTE_VALUE_TOPIC) && topicTokens.size() != 4) {LOG.info("Write request to attribute value topic should contain asset ID and attribute name only, topic '" + topic + "': " + connection);return false;}- if (topicTokens.get(0).equals(ATTRIBUTE_TOPIC) && topicTokens.size() != 1) {+ if (topicTokens.get(1).equals(ATTRIBUTE_TOPIC) && topicTokens.size() != 2) {LOG.info("Write request to attribute topic should not contain any other tokens, topic '" + topic + "': " + connection);return false;}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.javaindex 044e87d5a..b98228e94 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java@@ -162,7 +162,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);- boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equals(topicTokens.get(0));+ boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equals(topicTokens.get(1));String subscriptionId = msg.getTopicFilter(); // Use topic as subscription IDAssetFilter filter = buildAssetFilter(connection, topicTokens);@@ -241,13 +241,13 @@ public class ORInterceptHandler extends AbstractInterceptHandler {}List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());- boolean isValueWrite = topicTokens.get(0).equals(ATTRIBUTE_VALUE_TOPIC);+ boolean isValueWrite = topicTokens.get(1).equals(ATTRIBUTE_VALUE_TOPIC);String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);AttributeEvent attributeEvent = null;if (isValueWrite) {- String assetId = topicTokens.get(1);- String attributeName = topicTokens.get(2);+ String assetId = topicTokens.get(2);+ String attributeName = topicTokens.get(3);Object value = ValueUtil.parse(payloadContent).orElse(null);attributeEvent = new AttributeEvent(assetId, attributeName, value);} else {diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 7c8a3cd9b..98ace3f36 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -145,7 +145,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset in another realm"- def topic = "attribute/" + managerTestSetup.thingId+ def topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.thingIdremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -166,7 +166,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an non existing asset"- topic = "attribute/dhajkdasjfgh"+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/dhajkdasjfgh"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -187,7 +187,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset"- topic = "attribute/" + managerTestSetup.apartment1HallwayId+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayIdremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -226,7 +226,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client publishes to an asset attribute which is readonly"- topic = "attribute/" + managerTestSetup.apartment1HallwayId+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayIddef payload = ValueUtil.asJSON(ValueUtil.createJsonObject().put("motionSensor", 70)).orElse(null)remainingLength = 2 + topic.size() + payload.length()@@ -245,7 +245,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client publishes to an asset attribute"- topic = "attributevalue/" + managerTestSetup.apartment1HallwayId + "/lights"+ topic = keycloakTestSetup.tenantBuilding.realm + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/lights"payload = ValueUtil.asJSON(false).orElse(null)remainingLength = 2 + topic.size() + payload.length()@@ -265,7 +265,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset"- topic = "attribute/" + managerTestSetup.apartment1HallwayId+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayIdremainingLength = 4 + topic.size()client@@ -295,7 +295,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset attribute"- topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -334,7 +334,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset attribute"- topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size()client@@ -355,7 +355,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes with multilevel on asset"- topic = "attribute/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARDremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -385,7 +385,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset attribute value"- topic = "attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"+ topic = keycloakTestSetup.tenantBuilding.realm + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -425,7 +425,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset attribute value"- topic = "attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"+ topic = keycloakTestSetup.tenantBuilding.realm + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size()client@@ -494,7 +494,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset in another realm"- topic = "asset/" + managerTestSetup.thingId+ topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.thingIdremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -515,7 +515,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an non existing asset"- topic = "asset/dhajkdasjfgh"+ topic = keycloakTestSetup.tenantBuilding.realm + "/asset/dhajkdasjfgh"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -536,7 +536,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset"- topic = "asset/" + managerTestSetup.apartment1HallwayId+ topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.apartment1HallwayIdremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -567,7 +567,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes for child assets of an asset"- topic = "asset/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD+ topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARDremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -599,7 +599,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset"- topic = "asset/" + managerTestSetup.apartment1HallwayId+ topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.apartment1HallwayIdremainingLength = 4 + topic.size()client