Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

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.java
index 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/.../assetId
assetIds.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/+/attributeName
parentIds.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/+/attributeName
parentIds.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.java
index 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.java
index 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 ID
AssetFilter 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.groovy
index 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.thingId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -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 byte
client
@@ -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.apartment1HallwayId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -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.apartment1HallwayId
def 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.apartment1HallwayId
remainingLength = 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 byte
client
@@ -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_WILDCARD
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -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 byte
client
@@ -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.thingId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -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 byte
client
@@ -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.apartment1HallwayId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -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_WILDCARD
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -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.apartment1HallwayId
remainingLength = 4 + topic.size()
client