Skip to content

armgit / core

Public Imported from GitHub
0

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

Finished clientId in topic for MQTT connections

Michael Rademaker41173076baa9
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 513bac178..3a53377d0 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -184,11 +184,11 @@ public class MqttBrokerService implements ContainerService {
}
public static boolean isAttributeTopic(List<String> tokens) {
- return tokens.get(1).equals(ATTRIBUTE_TOPIC) || tokens.get(1).equals(ATTRIBUTE_VALUE_TOPIC);
+ return tokens.get(2).equals(ATTRIBUTE_TOPIC) || tokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC);
}
public static boolean isAssetTopic(List<String> tokens) {
- return tokens.get(1).equals(ASSET_TOPIC);
+ return tokens.get(2).equals(ASSET_TOPIC);
}
public static AssetFilter<?> buildAssetFilter(MqttConnection connection, List<String> topicTokens) {
@@ -205,7 +205,7 @@ public class MqttBrokerService implements ContainerService {
List<String> paths = new ArrayList<>();
List<String> attributeNames = new ArrayList<>();
- String assetId = Pattern.matches(Constants.ASSET_ID_REGEXP, topicTokens.get(2)) ? topicTokens.get(2) : null;
+ String assetId = Pattern.matches(Constants.ASSET_ID_REGEXP, topicTokens.get(3)) ? topicTokens.get(3) : null;
int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);
int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);
@@ -213,64 +213,65 @@ public class MqttBrokerService implements ContainerService {
return null;
}
- if (topicTokens.size() == 3) {
+ if (topicTokens.size() == 4) {
if (isAssetTopic) {
- if (multiLevelIndex == 2) {
- //realm/.../#
+ if (multiLevelIndex == 3) {
+ //realm/clientId/.../#
// No asset filtering required
- } else if (singleLevelIndex == 2) {
- //realm/.../+
+ } else if (singleLevelIndex == 3) {
+ //realm/clientId/.../+
parentIds.add(null);
} else {
- //realm/.../assetId
+ //realm/clientId/.../assetId
assetIds.add(assetId);
}
} else {
- //realm/attribute/attributeName
if(assetId != null) {
+ //realm/clientId/attribute/assetId
assetIds.add(assetId);
} else {
- attributeNames.add(topicTokens.get(2));
+ //realm/clientId/attribute/attributeName
+ attributeNames.add(topicTokens.get(3));
}
}
- } else if (topicTokens.size() == 4) {
+ } else if (topicTokens.size() == 5) {
if (isAssetTopic) {
- if (multiLevelIndex == 3) {
- //realm/asset/assetId/#
+ if (multiLevelIndex == 4) {
+ //realm/clientId/asset/assetId/#
paths.add(assetId);
- } else if (singleLevelIndex == 3) {
- //realm/asset/assetId/+
+ } else if (singleLevelIndex == 4) {
+ //realm/clientId/asset/assetId/+
parentIds.add(assetId);
} else {
return null;
}
} else {
if (assetId != null) {
- if (multiLevelIndex == 3) {
- //realm/attribute/assetId/#
+ if (multiLevelIndex == 4) {
+ //realm/clientId/attribute/assetId/#
paths.add(assetId);
- } else if (singleLevelIndex == 3) {
- //realm/attribute/assetId/+
+ } else if (singleLevelIndex == 4) {
+ //realm/clientId/attribute/assetId/+
parentIds.add(assetId);
} else {
assetIds.add(assetId);
- 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) {
- //realm/attribute/assetId/attributeName
- attributeNames.add(topicTokens.get(3));
+ //realm/clientId/attribute/assetId/attributeName
+ attributeNames.add(topicTokens.get(4));
} else {
return null;
}
}
} else {
- 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 == 3) {
+ if (multiLevelIndex == 4) {
return null; //no topic allowed after multilevel wildcard
- } else if (singleLevelIndex == 3) {
- //realm/attribute/+/attributeName
+ } else if (singleLevelIndex == 4) {
+ //realm/clientId/attribute/+/attributeName
parentIds.add(null);
} else {
return null;
@@ -280,17 +281,17 @@ public class MqttBrokerService implements ContainerService {
}
}
}
- } else if (topicTokens.size() == 5) {
+ } else if (topicTokens.size() == 6) {
if (isAssetTopic || assetId == null) {
return null;
}
- String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(4)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(4)) ? null : topicTokens.get(4);
+ String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(5)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(5)) ? null : topicTokens.get(5);
if (attributeName != null) {
attributeNames.add(attributeName);
- if (multiLevelIndex == 3) {
+ if (multiLevelIndex == 4) {
return null; //no topic allowed after multilevel wildcard
- } else if (singleLevelIndex == 3) {
- //realm/attribute/assetId/+/attributeName
+ } else if (singleLevelIndex == 4) {
+ //realm/clientId/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 f8c7912e8..891358e87 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
@@ -103,6 +103,11 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
return false;
}
+ if (!connection.sessionId.equals(topicTokens.get(1))) {
+ LOG.warning("Second topic level should match clientId: " + connection);
+ return false;
+ }
+
boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);
boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);
@@ -111,8 +116,8 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
return false;
}
- if(topicTokens.size() > 2) {
- String assetId = topicTokens.get(2);
+ if(topicTokens.size() > 3) {
+ String assetId = topicTokens.get(3);
if (Pattern.matches(Constants.ASSET_ID_REGEXP, assetId)) {
Asset<?> asset;
@@ -128,9 +133,9 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
return false;
}
- 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 (isAttributeTopic && topicTokens.size() > 4
+ && !(SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(4)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(4)))) {
+ String attributeName = topicTokens.get(4);
if (!asset.hasAttribute(attributeName)) {
LOG.fine("Asset attribute not found for topic '" + topic + "': " + connection);
@@ -156,12 +161,12 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
}
- 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);
+ if (topicTokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC) && topicTokens.size() != 5) {
+ LOG.info("Write request to attribute value topic should contain clientId, asset ID and attribute name only, topic '" + topic + "': " + connection);
return false;
}
- if (topicTokens.get(1).equals(ATTRIBUTE_TOPIC) && topicTokens.size() != 2) {
+ if (topicTokens.get(2).equals(ATTRIBUTE_TOPIC) && topicTokens.size() != 3) {
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 b98228e94..17a99cd93 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(1));
+ boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equals(topicTokens.get(2));
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(1).equals(ATTRIBUTE_VALUE_TOPIC);
+ boolean isValueWrite = topicTokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC);
String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);
AttributeEvent attributeEvent = null;
if (isValueWrite) {
- String assetId = topicTokens.get(2);
- String attributeName = topicTokens.get(3);
+ String assetId = topicTokens.get(3);
+ String attributeName = topicTokens.get(4);
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 6681529d8..3fc5d330b 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 = keycloakTestSetup.tenantCity.realm + "/attribute/" + managerTestSetup.thingId
+ def topic = keycloakTestSetup.tenantCity.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.thingId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -166,7 +166,49 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes to an non existing asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/2F6uvhLm8w110jug2caukV"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/2F6uvhLm8w110jug2caukV"
+ remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
+
+ client
+ // SUBSCRIBE
+ .write(0x82) // MQTT Control Packet type(8) with QoS level 1
+ .write(remainingLength.byteValue()) // Remaining Length
+ .write(0x00, 0x10) // MessageId
+
+ // Payload
+ .write(0x00, topic.size().byteValue()) // Topic Length
+ .write(topic) // Topic
+ .write(0x01) // QoS level 1
+ .flush()
+
+ then: "No subscription should exist"
+ conditions.eventually {
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
+ }
+
+ when: "a mqtt client subscribes with clientId missing"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId
+ remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
+
+ client
+ // SUBSCRIBE
+ .write(0x82) // MQTT Control Packet type(8) with QoS level 1
+ .write(remainingLength.byteValue()) // Remaining Length
+ .write(0x00, 0x10) // MessageId
+
+ // Payload
+ .write(0x00, topic.size().byteValue()) // Topic Length
+ .write(topic) // Topic
+ .write(0x01) // QoS level 1
+ .flush()
+
+ then: "No subscription should exist"
+ conditions.eventually {
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
+ }
+
+ when: "a mqtt client subscribes with different clientId"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + UniqueIdentifierGenerator.generateId() + "/attribute/" + managerTestSetup.apartment1HallwayId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -187,7 +229,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -226,7 +268,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client publishes to an asset attribute which is readonly"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId
def payload = ValueUtil.asJSON(ValueUtil.createJsonObject().put("motionSensor", 70)).orElse(null)
remainingLength = 2 + topic.size() + payload.length()
@@ -245,7 +287,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client publishes to an asset attribute"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/lights"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/lights"
payload = ValueUtil.asJSON(false).orElse(null)
remainingLength = 2 + topic.size() + payload.length()
@@ -265,7 +307,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client unsubscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId
remainingLength = 4 + topic.size()
client
@@ -295,7 +337,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes to an asset attribute"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -334,7 +376,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client unsubscribes to an asset attribute"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
remainingLength = 4 + topic.size()
client
@@ -355,7 +397,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes with multilevel on asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -385,7 +427,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes to an asset attribute value"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -425,7 +467,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client unsubscribes to an asset attribute value"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
remainingLength = 4 + topic.size()
client
@@ -494,7 +536,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes to an asset in another realm"
- topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.thingId
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.thingId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -515,7 +557,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes to an non existing asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/asset/2F6uvhLm8w110jug2caukV"
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/2F6uvhLm8w110jug2caukV"
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -536,7 +578,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.apartment1HallwayId
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.apartment1HallwayId
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -567,7 +609,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes for child assets of an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -599,7 +641,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client unsubscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/asset/" + managerTestSetup.apartment1HallwayId
+ topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.apartment1HallwayId
remainingLength = 4 + topic.size()
client