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
Limit auto provisioning response topic subscription for a device to single subscription (#1608)
Rich Turner227f5e5fa387
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.javaindex 613c5708e..a4a47a4fd 100644--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java@@ -25,7 +25,6 @@ import com.hivemq.client.mqtt.MqttClientSslConfigBuilder;import com.hivemq.client.mqtt.MqttClientState;import com.hivemq.client.mqtt.exceptions.ConnectionClosedException;import com.hivemq.client.mqtt.exceptions.ConnectionFailedException;-import com.hivemq.client.mqtt.exceptions.MqttClientStateException;import com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource;import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient;import com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder;@@ -37,10 +36,10 @@ import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck;import com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck;import org.openremote.agent.protocol.io.IOClient;import org.openremote.container.Container;-import org.openremote.model.util.UniqueIdentifierGenerator;import org.openremote.model.asset.agent.ConnectionStatus;import org.openremote.model.auth.UsernamePassword;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.UniqueIdentifierGenerator;import org.openremote.model.util.ValueUtil;import javax.net.ssl.KeyManagerFactory;@@ -95,7 +94,7 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<SMqtt3ClientBuilder builder = MqttClient.builder().useMqttVersion3().identifier(clientId)- .addConnectedListener(context -> onConnectionStatusChanged())+ .addConnectedListener(context -> onConnectionStatusChanged(null)).addDisconnectedListener(context -> {boolean userClosed = context.getSource() == MqttDisconnectSource.USER;if (disconnected) {@@ -119,7 +118,7 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S} else if (context.getCause() instanceof ConnectionFailedException) {LOG.log(Level.INFO, "Connection failed '" + getClientUri() + "': initiator=" + context.getSource(), context.getCause());}- onConnectionStatusChanged();+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);}).automaticReconnect().initialDelay(RECONNECT_DELAY_INITIAL_MILLIS, TimeUnit.MILLISECONDS)@@ -399,12 +398,15 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S});}- protected void onConnectionStatusChanged() {- executorService.submit(() -> {- ConnectionStatus status = getConnectionStatus();- LOG.info("Client '" + getClientUri() + "' connection status changed: " + status);+ /**+ * Allows an override to be provided as {@link #getConnectionStatus} doesn't always return the correct status when a+ * server initiated disconnect occurs (some sort of timing issue).+ */+ protected void onConnectionStatusChanged(ConnectionStatus statusOverride) {+ ConnectionStatus status = statusOverride != null ? statusOverride : getConnectionStatus();+ LOG.info("Client '" + getClientUri() + "' connection status changed: " + status);- connectionStatusConsumers.forEach(+ connectionStatusConsumers.forEach(consumer -> {try {consumer.accept(status);@@ -412,7 +414,6 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<SLOG.log(Level.WARNING, "Connection status change handler threw an exception: " + getClientUri(), e);}});- });}@Override@@ -434,19 +435,14 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<Sclient.disconnect().whenComplete((unused, throwable) -> {connected.set(false);- if (throwable instanceof MqttClientStateException) {- // Likely already disconnected- return;- }if (throwable != null) {- LOG.log(Level.WARNING, "Failed to disconnect: " + getClientUri(), throwable);- return;+ LOG.log(Level.INFO, "Disconnect error '" + getClientUri() + "':" + throwable.getMessage());}if (this.cleanSession) {removeAllMessageConsumers();}- onConnectionStatusChanged();+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);});}diff --git a/container/src/main/java/org/openremote/container/util/package-info.java b/container/src/main/java/org/openremote/container/util/package-info.javaindex 36105d892..1d799822b 100644--- a/container/src/main/java/org/openremote/container/util/package-info.java+++ b/container/src/main/java/org/openremote/container/util/package-info.java@@ -1,7 +1,7 @@@org.hibernate.annotations.GenericGenerators({@org.hibernate.annotations.GenericGenerator(name = Constants.PERSISTENCE_UNIQUE_ID_GENERATOR,- strategy = "org.openremote.container.util.HibernateUniqueIdentifierGenerator"+ type = org.openremote.container.util.HibernateUniqueIdentifierGenerator.class)})diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex 8bc3c784b..cb9adb8d1 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -371,15 +371,15 @@ public class ClientEventService extends RouteBuilder implements ContainerService.routeId("ClientPublishToSubscribers").routeConfigurationId(ATTRIBUTE_EVENT_ROUTE_CONFIG_ID).threads().executorService(executorService)- .filter(body().isInstanceOf(SharedEvent.class))+ .filter(body().isInstanceOf(Event.class)).process(exchange -> {- SharedEvent event = exchange.getIn().getBody(SharedEvent.class);+ Event event = exchange.getIn().getBody(Event.class);sendToSubscribers(event);});}@SuppressWarnings("unchecked")- protected <T extends SharedEvent> void sendToSubscribers(T event) {+ protected <T extends Event> void sendToSubscribers(T event) {eventSubscriptions.forEach(eventSubscriptionConsumerPair -> {EventSubscription<?> subscription = eventSubscriptionConsumerPair.getKey();diff --git a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.javaindex 727c699ff..1dd905f4b 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java@@ -60,6 +60,7 @@ import java.util.logging.Logger;import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;+import static org.openremote.manager.mqtt.MQTTBrokerService.connectionToString;import static org.openremote.model.Constants.RESTRICTED_USER_REALM_ROLE;import static org.openremote.model.syslog.SyslogCategory.API;@@ -116,6 +117,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {protected boolean isKeycloak;protected final ConcurrentMap<Long, Set<RemotingConnection>> provisioningConfigAuthenticatedConnectionMap = new ConcurrentHashMap<>();protected Timer provisioningTimer;+ protected final Map<String, RemotingConnection> responseSubscribedConnections = new ConcurrentHashMap<>();@Overridepublic void init(Container container, Configuration serverConfiguration) throws Exception {@@ -203,19 +205,30 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {return false;}- return isResponseTopic(topic)+ boolean allowed = isResponseTopic(topic)&& !TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 1))&& !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 1));++ if (allowed) {+ // Only allow if there is no existing subscription for this topic+ RemotingConnection existingConnection = responseSubscribedConnections.get(topic.getString());+ if (existingConnection != null) {+ LOG.warning("Subscription already exists possible eavesdropping");+ allowed = false;+ }+ }++ return allowed;}@Overridepublic void onSubscribe(RemotingConnection connection, Topic topic) {- // Nothing to do here as we'll publish to this topic in response to client messages+ responseSubscribedConnections.put(topic.getString(), connection);}@Overridepublic void onUnsubscribe(RemotingConnection connection, Topic topic) {-+ responseSubscribedConnections.remove(topic.getString());}@Override@@ -398,7 +411,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {provisioningConfigAuthenticatedConnectionMap.compute(matchingConfig.getId(), (id, connections) -> {if (connections == null) {- connections = new HashSet<>();+ connections = ConcurrentHashMap.newKeySet();}connections.add(connection);return connections;diff --git a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovyindex 7df3ccdba..a78c602ec 100644--- a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy@@ -105,12 +105,14 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTrThingAsset asset = nulldef attributeEvents = new CopyOnWriteArrayList<AttributeEvent>()Consumer<AttributeEvent> eventConsumer = it -> {+ LOG.debug("Attribute event received: " + it)attributeEvents.add(it)}Consumer<OutdatedAttributeEvent> outdatedEventConsumer = it -> {+ LOG.debug("Outdated attribute event received: " + it)attributeEvents.add(it.event)}- clientEventService.addSubscription(AttributeEvent.class, null, eventConsumer)+ clientEventService.addSubscription(AttributeEvent.class, null, eventConsumer)clientEventService.addSubscription(OutdatedAttributeEvent.class, null, outdatedEventConsumer)and: "some mail messages exist"diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovyindex 57fead9e4..cef7f7bed 100644--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy@@ -76,6 +76,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContagiven: "expected conditions"def conditions = new PollingConditions(timeout: 10, delay: 0.2)MQTT_IOClient device1Client+ MQTT_IOClient device1SnoopClientMQTT_IOClient deviceNClientand: "the container starts"@@ -154,10 +155,12 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaRemotingConnection connection = nulldef device1UniqueId = "device1"def mqttDevice1ClientId = UniqueIdentifierGenerator.generateId("device1")+ def mqttDevice1SnoopClientId = UniqueIdentifierGenerator.generateId("device1snoop")List<String> subscribeFailures = new CopyOnWriteArrayList<>()List<ConnectionStatus> connectionStatuses = new CopyOnWriteArrayList<>()Consumer<String> subscribeFailureCallback = {String topic -> subscribeFailures.add(topic)}device1Client = new MQTT_IOClient(mqttDevice1ClientId, mqttHost, mqttPort, false, false, null, null, null)+ device1SnoopClient = new MQTT_IOClient(mqttDevice1SnoopClientId, mqttHost, mqttPort, false, false, null, null, null)device1Client.setTopicSubscribeFailureConsumer(subscribeFailureCallback)device1Client.addConnectionStatusConsumer({connectionStatus ->connectionStatuses.add(connectionStatus)})@@ -183,7 +186,34 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaassert device1Client.topicConsumerMap.get(device1ResponseTopic).size() == 1}+ when: "an eavesdropping client connects"+ device1SnoopClient.setTopicSubscribeFailureConsumer(subscribeFailureCallback)+ device1SnoopClient.connect()++ then: "it should be connected"+ conditions.eventually {+ assert device1SnoopClient.getConnectionStatus() == ConnectionStatus.CONNECTED+ }++ when: "the eavesdropping client subscribes to the provisioning response topic"+ def result = device1SnoopClient.addMessageConsumer(device1ResponseTopic, device1MessageConsumer)++ then: "the subscription should have failed"+ conditions.eventually {+ assert !result+ assert device1SnoopClient.topicConsumerMap.get(device1ResponseTopic) == null+ assert subscribeFailures.size() == 1+ }++ and: "the actual client should still be connected and subscribed"+ conditions.eventually {+ assert device1Client.getConnectionStatus() == ConnectionStatus.CONNECTED+ assert device1Client.topicConsumerMap.get(device1ResponseTopic) != null+ assert device1Client.topicConsumerMap.get(device1ResponseTopic).size() == 1+ }+when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config"+ subscribeFailures.clear()def existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)device1Client.sendMessage(new MQTTMessage<String>(device1RequestTopic, ValueUtil.asJSON(@@ -356,6 +386,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconditions.eventually {assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))assert device1Client.getConnectionStatus() == ConnectionStatus.DISCONNECTED+ assert userAssetProvisioningMQTTHandler.responseSubscribedConnections.isEmpty()}and: "the connected attribute of the provisioned asset should show as not connected"@@ -591,9 +622,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "already connected client that was authenticated should be disconnected, then reconnect and should fail to re-subscribe to asset and attribute events"conditions.eventually {- assert connectionStatuses.size() == 2- assert connectionStatuses.get(0) == ConnectionStatus.WAITING- assert connectionStatuses.get(1) == ConnectionStatus.CONNECTED+ assert connectionStatuses.size() >= 1+ assert connectionStatuses.get(0) == ConnectionStatus.DISCONNECTEDassert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != nullassert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != existingConnectionconnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)@@ -780,6 +810,9 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaif (device1Client != null) {device1Client.disconnect()}+ if (device1SnoopClient != null) {+ device1SnoopClient.disconnect()+ }if (deviceNClient != null) {deviceNClient.disconnect()}diff --git a/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovy b/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovyindex c698d0684..df12cd024 100644--- a/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovy@@ -190,7 +190,9 @@ Value: 6000assetLink.parentAssetName == "Apartment 2"and: "the expected number e-mail notifications matches"- notificationMessages.size() == emailNotifications+ conditions.eventually {+ notificationMessages.size() == emailNotifications+ }where:severity | assigneeId | emailNotifications