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
Fix attribute link security issue and also disable attribute links for gateway descendants (#954)
Rich Turner841474e2199d
diff --git a/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.java b/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.javaindex 5fd3569a9..98385dfc3 100644--- a/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.java@@ -20,6 +20,7 @@package org.openremote.manager.asset;import org.openremote.manager.agent.AgentService;+import org.openremote.manager.gateway.GatewayService;import org.openremote.model.attribute.AttributeWriteFailure;import org.openremote.model.Container;import org.openremote.model.ContainerService;@@ -28,6 +29,7 @@ import org.openremote.model.attribute.*;import org.openremote.model.attribute.AttributeEvent.Source;import org.openremote.model.protocol.ProtocolUtil;import org.openremote.model.query.AssetQuery;+import org.openremote.model.query.filter.RealmPredicate;import org.openremote.model.util.Pair;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.MetaItemType;@@ -88,6 +90,7 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProprotected AssetProcessingService assetProcessingService;protected AssetStorageService assetStorageService;protected AgentService agentService;+ protected GatewayService gatewayService;@Overridepublic int getPriority() {@@ -99,6 +102,7 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProassetProcessingService = container.getService(AssetProcessingService.class);assetStorageService = container.getService(AssetStorageService.class);agentService = container.getService(AgentService.class);+ gatewayService = container.getService(GatewayService.class);}@Override@@ -119,10 +123,12 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProreturn false;}+ // If asset is a gateway descendant then don't action any linksattribute.getMetaValue(MetaItemType.ATTRIBUTE_LINKS)+ .map(attributeLinks -> gatewayService.getLocallyRegisteredGatewayId(asset.getId(), asset.getParentId()) != null ? null : attributeLinks).ifPresent(attributeLinks ->Arrays.stream(attributeLinks).forEach(attributeLink ->- processLinkedAttributeUpdate(em, attributeLink, new AttributeState(asset.getId(), attribute))));+ processLinkedAttributeUpdate(em, asset, attribute, attributeLink)));return false;}@@ -132,9 +138,7 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProassetProcessingService.sendAttributeEvent(attributeEvent, ATTRIBUTE_LINKING_SERVICE);}- protected void processLinkedAttributeUpdate(EntityManager em, AttributeLink attributeLink, AttributeState attributeState) {- if (attributeState == null)- return;+ protected void processLinkedAttributeUpdate(EntityManager em, Asset<?> asset, Attribute<?> attribute, AttributeLink attributeLink) {LOG.finest("Processing attribute state for linked attribute");if (attributeLink == null) {@@ -145,7 +149,8 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProPair<Boolean, Object> sendConvertedValue = convertValueForLinkedAttribute(em,assetStorageService,- attributeState.getValue().orElse(null),+ asset,+ attribute,attributeLink);@@ -157,16 +162,16 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProfinal Object[] value = {sendConvertedValue.value};// Get the attribute and try and coerce the value into the correct type- getAttribute(em, assetStorageService, attributeLink.getAttributeRef()).ifPresent(attribute -> {+ getAttribute(em, assetStorageService, asset.getRealm(), attributeLink.getAttributeRef()).ifPresent(attr -> {if (value[0] != null) {// Do basic value conversion- if (!attribute.getType().getType().isAssignableFrom(value[0].getClass())) {- Object val = ValueUtil.convert(value[0], attribute.getType().getType());+ if (!attr.getType().getType().isAssignableFrom(value[0].getClass())) {+ Object val = ValueUtil.convert(value[0], attr.getType().getType());if (val == null) {- LOG.warning("Failed to convert value: " + value[0].getClass() + " -> " + attribute.getType().getType());+ LOG.warning("Failed to convert value: " + value[0].getClass() + " -> " + attr.getType().getType());LOG.warning("Cannot send linked attribute update");return;}@@ -175,15 +180,17 @@ public class AttributeLinkingService implements ContainerService, AssetUpdatePro}sendAttributeEvent(new AttributeEvent(attributeLink.getAttributeRef(), value[0]));-});}protected Pair<Boolean, Object> convertValueForLinkedAttribute(EntityManager em,AssetStorageService assetStorageService,- Object originalValue,+ Asset<?> asset,+ Attribute<?> attribute,AttributeLink attributeLink) throws AssetProcessingException {+ Object originalValue = attribute.getValue().orElse(null);+// Filter the value firstif (attributeLink.getFilters() != null) {originalValue = ValueUtil.applyValueFilters(originalValue, attributeLink.getFilters());@@ -205,6 +212,7 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProspecialConverter -> doSpecialConversion(em,assetStorageService,+ asset,specialConverter,attributeLink.getAttributeRef())@@ -222,13 +230,14 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProprotected static Pair<Boolean, Object> doSpecialConversion(EntityManager em,AssetStorageService assetStorageService,+ Asset<?> asset,AttributeLink.ConverterType converter,AttributeRef linkedAttributeRef) throws AssetProcessingException {switch (converter) {case TOGGLE:// Look up current value of the linked attribute within the same database sessiontry {- Attribute<?> currentAttribute = getAttribute(em, assetStorageService, linkedAttributeRef).orElseThrow(+ Attribute<?> currentAttribute = getAttribute(em, assetStorageService, asset.getRealm(), linkedAttributeRef).orElseThrow(() -> new AssetProcessingException(AttributeWriteFailure.ATTRIBUTE_NOT_FOUND,"cannot toggle value as attribute cannot be found: " + linkedAttributeRef@@ -249,7 +258,7 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProcase DECREMENT:// Look up current value of the linked attribute within the same database sessiontry {- Attribute<?> currentAttribute = getAttribute(em, assetStorageService, linkedAttributeRef).orElseThrow(+ Attribute<?> currentAttribute = getAttribute(em, assetStorageService, asset.getRealm(), linkedAttributeRef).orElseThrow(() -> new AssetProcessingException(AttributeWriteFailure.ATTRIBUTE_NOT_FOUND,"cannot toggle value as attribute cannot be found: " + linkedAttributeRef@@ -277,11 +286,13 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProprotected static Optional<Attribute<?>> getAttribute(EntityManager em,AssetStorageService assetStorageService,+ String realm,AttributeRef attributeRef) {// Get the full asset as shared emAsset<?> asset = assetStorageService.find(em,new AssetQuery()+ .realm(new RealmPredicate(realm)).ids(attributeRef.getId()));diff --git a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.javaindex f4c7ce0d2..86fd4d987 100644--- a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java+++ b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java@@ -30,6 +30,8 @@ import org.openremote.model.asset.Asset;import org.openremote.model.asset.UserAssetLink;import org.openremote.model.asset.impl.*;import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeLink;+import org.openremote.model.attribute.AttributeRef;import org.openremote.model.attribute.MetaItem;import org.openremote.model.geo.GeoJSONPoint;import org.openremote.model.security.Realm;@@ -97,6 +99,8 @@ public class ManagerTestSetup extends ManagerSetup {public String electricityWindAssetId;public String electricitySupplierAssetId;public String electricityBatteryAssetId;+ public String light1Id;+ public String light2Id;public ManagerTestSetup(Container container) {super(container);@@ -613,6 +617,8 @@ public class ManagerTestSetup extends ManagerSetup {);smartCity = assetStorageService.merge(smartCity);++SimulatorAgent smartCityServiceAgent = new SimulatorAgent("Service Agent (Simulator)");smartCityServiceAgent.setParent(smartCity);smartCityServiceAgent = assetStorageService.merge(smartCityServiceAgent);@@ -643,9 +649,12 @@ public class ManagerTestSetup extends ManagerSetup {Asset<?> light1Asset = createDemoLightAsset("Light 1", assetArea1, new GeoJSONPoint(5.476111, 51.438492));light1Asset = assetStorageService.merge(light1Asset);+ light1Id = light1Asset.getId();Asset<?> light2Asset = createDemoLightAsset("Light 2", assetArea1, new GeoJSONPoint(5.477272, 51.439214));+ light2Asset.getAttribute(LightAsset.ON_OFF).get().addMeta(new MetaItem<>(ATTRIBUTE_LINKS, new AttributeLink[]{new AttributeLink(new AttributeRef(light1Asset.getId(), LightAsset.ON_OFF.getName()), null, null)}));light2Asset = assetStorageService.merge(light2Asset);+ light2Id = light2Asset.getId();// ################################ Realm B Area 2 ###################################diff --git a/test/src/test/groovy/org/openremote/test/assets/AttributeLinkingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AttributeLinkingTest.groovyindex 2e576f7d9..2bcaec374 100644--- a/test/src/test/groovy/org/openremote/test/assets/AttributeLinkingTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AttributeLinkingTest.groovy@@ -2,12 +2,15 @@ package org.openremote.test.assetsimport org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.setup.SetupServiceimport org.openremote.model.Constants+import org.openremote.model.asset.impl.BuildingAssetimport org.openremote.model.asset.impl.ThingAssetimport org.openremote.model.attribute.*import org.openremote.model.value.JsonPathFilterimport org.openremote.model.value.ValueFilterimport org.openremote.model.util.ValueUtil+import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.test.ManagerContainerTraitimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions@@ -28,6 +31,7 @@ class AttributeLinkingTest extends Specification implements ManagerContainerTraidef container = startContainer(defaultConfig(), defaultServices())def assetStorageService = container.getService(AssetStorageService.class)def assetProcessingService = container.getService(AssetProcessingService.class)+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)then: "the system should settle down"conditions.eventually {@@ -39,7 +43,8 @@ class AttributeLinkingTest extends Specification implements ManagerContainerTraiasset1.setRealm(Constants.MASTER_REALM)asset1.addOrReplaceAttributes(new Attribute<>("button", TEXT, "RELEASED"),- new Attribute<>("array", JSON_OBJECT.asArray(), null)+ new Attribute<>("array", JSON_OBJECT.asArray(), null),+ new Attribute<>("crossRealm", TEXT, null))asset1 = assetStorageService.merge(asset1)@@ -75,6 +80,7 @@ class AttributeLinkingTest extends Specification implements ManagerContainerTraiasset1.getAttribute("button").get().addMeta(new MetaItem<>(ATTRIBUTE_LINKS, [attributeLinkOnOff, attributeLinkCounter] as AttributeLink[]))asset1.getAttribute("array").get().addMeta(new MetaItem<>(ATTRIBUTE_LINKS, [attributeLinkProp] as AttributeLink[]))+ asset1.getAttribute("crossRealm").get().addMeta(new MetaItem<>(ATTRIBUTE_LINKS, [new AttributeLink(new AttributeRef(managerTestSetup.apartment1Id, BuildingAsset.STREET.name), null, null)] as AttributeLink[]))asset1 = assetStorageService.merge(asset1)and: "the button is pressed for a short period"@@ -203,5 +209,19 @@ class AttributeLinkingTest extends Specification implements ManagerContainerTraiasset2 = assetStorageService.find(asset2.id, true)assert !asset2.getAttribute("item2Prop1").flatMap{it.value}.orElse(true)}++ when: "the crossRealm attribute is written to"+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, "crossRealm", "Test"))++ then: "the source attribute should be updated"+ conditions.eventually {+ asset1 = assetStorageService.find(asset1.id, true)+ assert asset1.getAttribute("crossRealm").flatMap{it.value}.orElse(null) == "Test"+ }++ and: "the cross realm asset attribute should not have been updated after a short wait"+ Thread.sleep(1000)+ def apartment1 = assetStorageService.find(managerTestSetup.apartment1Id, true) as BuildingAsset+ assert apartment1.getStreet().orElse(null) == null}}diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovyindex 2cea5dc3a..3698ee615 100644--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy@@ -13,12 +13,14 @@ import org.openremote.container.util.UniqueIdentifierGeneratorimport org.openremote.manager.agent.AgentServiceimport org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.event.ClientEventServiceimport org.openremote.manager.gateway.GatewayClientServiceimport org.openremote.manager.gateway.GatewayConnectorimport org.openremote.manager.gateway.GatewayServiceimport org.openremote.manager.security.ManagerIdentityServiceimport org.openremote.manager.security.ManagerKeycloakIdentityProviderimport org.openremote.manager.setup.SetupService+import org.openremote.model.asset.impl.LightAssetimport org.openremote.model.security.Userimport org.openremote.setup.integration.ManagerTestSetupimport org.openremote.model.asset.*@@ -758,6 +760,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {def gatewayClientService = container.getService(GatewayClientService.class)def agentService = container.getService(AgentService.class)def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)+ def clientEventService = container.getService(ClientEventService.class)and: "an authenticated admin user"def accessToken = authenticate(@@ -906,6 +909,25 @@ class GatewayTest extends Specification implements ManagerContainerTrait {assert mirroredMicrophone2.getAttribute("test").flatMap{it.getValue()}.orElse("") == "newerValue"}+ when: "we subscribe to attribute events"+ List<AttributeEvent> attributeEvents = []+ clientEventService.addInternalSubscription(AttributeEvent.class, null, {attributeEvent ->+ attributeEvents.add(attributeEvent)+ })++ and: "an attribute with an attribute link is updated on the gateway"+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.light2Id, LightAsset.ON_OFF, true))++ then: "only four attribute events should have been generated, one for each of light1 and light2 and one for each of their mirrored assets below the gateway asset"+ Thread.sleep(1000)+ conditions.eventually {+ assert attributeEvents.size() == 4+ assert attributeEvents.any{it.assetId == managerTestSetup.light2Id && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}+ assert attributeEvents.any{it.assetId == managerTestSetup.light1Id && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}+ assert attributeEvents.any{it.assetId == mapAssetId(gateway.id, managerTestSetup.light2Id, false) && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}+ assert attributeEvents.any{it.assetId == mapAssetId(gateway.id, managerTestSetup.light1Id, false) && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}+ }+when: "a gateway client asset is deleted"def deleted = assetStorageService.delete(Collections.singletonList(managerTestSetup.microphone1Id))