Skip to content

armgit / core

Public Imported from GitHub
0

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

Bugfix for public asset permissions (#2621)

Rich Turnerdbb367b498a9
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
index 08c24b750..3db1cf025 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
@@ -190,8 +190,8 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
}
} else {
- // Check attribute has public write flag for anonymous write
- if (attribute == null || !attribute.hasMeta(MetaItemType.ACCESS_PUBLIC_WRITE)) {
+ // Check attribute has public write flag set to true for anonymous write
+ if (attribute == null || !attribute.getMetaValue(MetaItemType.ACCESS_PUBLIC_WRITE).orElse(false)) {
LOG.log(System.Logger.Level.DEBUG, () -> "Asset doesn't support public write on '" + attributeEvent.getRef() + "': username=null");
return false;
}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetFilter.java b/model/src/main/java/org/openremote/model/asset/AssetFilter.java
index e9c286420..a7676556e 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetFilter.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetFilter.java
@@ -19,6 +19,7 @@
*/
package org.openremote.model.asset;
+import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.shared.EventFilter;
import org.openremote.model.event.shared.SharedEvent;
@@ -30,6 +31,7 @@ import org.openremote.model.value.MetaItemType;
import java.util.Arrays;
import java.util.List;
+import java.util.function.Predicate;
import java.util.stream.Collectors;
// TODO: Merge this with AssetQuery and use AssetQueryPredicate to resolve
@@ -196,7 +198,7 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> implements EventFilt
@Override
public T apply(T event) {
- MetaItemDescriptor<?> filterAttributesBy = null;
+ Predicate<Attribute<?>> attributeFilter = null;
// Only send attribute events where value has changed if requested
if (valueChanged && event instanceof AttributeEvent attributeEvent && !attributeEvent.valueChanged()) {
@@ -219,7 +221,7 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> implements EventFilt
return null;
}
} else {
- filterAttributesBy = MetaItemType.ACCESS_RESTRICTED_READ;
+ attributeFilter = (attr) -> attr.getMetaValue(MetaItemType.ACCESS_RESTRICTED_READ).orElse(false);
}
}
@@ -232,7 +234,7 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> implements EventFilt
if (!assetEvent.isAccessPublicRead()) {
return null;
} else {
- filterAttributesBy = MetaItemType.ACCESS_PUBLIC_READ;
+ attributeFilter = (attr) -> attr.getMetaValue(MetaItemType.ACCESS_PUBLIC_READ).orElse(false);
}
}
}
@@ -275,13 +277,12 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> implements EventFilt
}
}
- if (filterAttributesBy != null) {
+ if (attributeFilter != null) {
// Filter attributes before doing name match
AssetEvent assetEvent = (AssetEvent) event;
if (assetEvent.getAsset() != null) {
Asset<?> asset = ValueUtil.clone(assetEvent.getAsset());
- MetaItemDescriptor<?> finalFilterAttributesBy = filterAttributesBy;
- asset.setAttributes(asset.getAttributes().values().stream().filter(attribute -> attribute.hasMeta(finalFilterAttributesBy)).collect(Collectors.toList()));
+ asset.setAttributes(asset.getAttributes().values().stream().filter(attributeFilter).collect(Collectors.toList()));
event = (T) new AssetEvent(assetEvent.getCause(), asset, assetEvent.getUpdatedProperties());
}
}
diff --git a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
index a160115aa..d37dea654 100644
--- a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
+++ b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
@@ -223,6 +223,11 @@ public class ManagerTestSetup extends ManagerSetup {
new MetaItem<>(ACCESS_PUBLIC_READ),
new MetaItem<>(ACCESS_PUBLIC_WRITE)
));
+ apartment1.getAttribute(BuildingAsset.STREET).get().addOrReplaceMeta(
+ new MetaItem<>(ACCESS_PUBLIC_WRITE, false),
+ new MetaItem<>(ACCESS_PUBLIC_READ, false),
+ new MetaItem<>(ACCESS_RESTRICTED_READ)
+ );
apartment1 = assetStorageService.merge(apartment1);
apartment1Id = apartment1.getId();
@@ -234,6 +239,7 @@ public class ManagerTestSetup extends ManagerSetup {
/* ############################ ROOMS ############################## */
RoomAsset apartment1Livingroom = createDemoApartmentRoom(apartment1, "Living Room 1");
+ apartment1Livingroom.setAccessPublicRead(true);
apartment1Livingroom.getAttribute(Asset.NOTES).ifPresent(notes -> notes.addMeta(
new MetaItem<>(ACCESS_RESTRICTED_READ, true),
new MetaItem<>(ACCESS_RESTRICTED_WRITE, true)
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
index 69382c651..f913662f4 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
@@ -543,7 +543,7 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
apartment1.parentId == managerTestSetup.smartBuildingId
apartment1.path[0] == managerTestSetup.smartBuildingId
apartment1.path[1] == managerTestSetup.apartment1Id
- apartment1.attributes.size() == 7
+ apartment1.attributes.size() == 8
Asset apartment1Livingroom = assets[1]
apartment1Livingroom.id == managerTestSetup.apartment1LivingroomId
@@ -691,14 +691,14 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
then: "the update should be ignored"
assert testAsset.getParentId() == managerTestSetup.apartment1Id
- when: "an asset is made public"
+ when: "an asset is made private"
testAsset = assetResource.get(null, managerTestSetup.apartment1LivingroomId)
- testAsset.setAccessPublicRead(true)
+ testAsset.setAccessPublicRead(false)
assetResource.update(null, testAsset.id, testAsset)
testAsset = assetResource.get(null, managerTestSetup.apartment1LivingroomId)
then: "the update should be ignored"
- assert !testAsset.isAccessPublicRead()
+ assert testAsset.isAccessPublicRead()
when: "an asset is renamed"
testAsset = assetResource.get(null, managerTestSetup.apartment1LivingroomId)
@@ -827,8 +827,9 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
def assets = assetResource.queryAssets(null, new AssetQuery().recursive(true))
then: "only public assets should be returned"
- assets.size() == 2
+ assets.size() == 3
assets.find {it.id == managerTestSetup.apartment1Id } != null
+ assets.find {it.id == managerTestSetup.apartment1LivingroomId } != null
assets.find {it.id == managerTestSetup.apartment2LivingroomId } != null
when: "the public assets are retrieved"
@@ -836,16 +837,18 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
.realm(new RealmPredicate(keycloakTestSetup.realmBuilding.name)))
then: "the public assets should be retrieved"
- assert assets.size() == 2
+ assert assets.size() == 3
assert assets.find {it.id == managerTestSetup.apartment1Id} != null
+ assert assets.find {it.id == managerTestSetup.apartment1LivingroomId} != null
assert assets.find {it.id == managerTestSetup.apartment2LivingroomId} != null
when: "the public assets are retrieved without a query"
assets = assetResource.queryAssets(null, null)
then: "the public assets should be retrieved"
- assert assets.size() == 2
+ assert assets.size() == 3
assert assets.find {it.id == managerTestSetup.apartment1Id} != null
+ assert assets.find {it.id == managerTestSetup.apartment1LivingroomId} != null
assert assets.find {it.id == managerTestSetup.apartment2LivingroomId} != null
when: "an attribute with public write is written to and another with only public read"
@@ -858,5 +861,14 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
assert writeResults.size() == 2
assert writeResults.find {it.ref.id == managerTestSetup.apartment1Id && it.ref.name == Asset.LOCATION.name}.failure == null
assert writeResults.find {it.ref.id == managerTestSetup.apartment2LivingroomId && it.ref.name == Asset.LOCATION.name}.failure == AttributeWriteFailure.INSUFFICIENT_ACCESS
+
+ when: "an attribute with public write=false is written to anonymously"
+ def response = assetResource.writeAttributeValue(null, managerTestSetup.apartment1Id, BuildingAsset.STREET.name, '"Should fail"')
+
+ then: "the request should fail"
+ response.withCloseable { r ->
+ assert r.status == 403
+ true
+ }
}
}
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
index b07ff76ce..63d34cdfa 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
@@ -165,7 +165,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
apartment1.createdOn != null
apartment1.parentId == managerTestSetup.smartBuildingId
apartment1.realm == managerTestSetup.realmBuildingName
- apartment1.getAttributes().size() == 7
+ apartment1.getAttributes().size() == 8
apartment1.getAttribute("ventilationAuto").isPresent()
apartment1.getAttribute("ventilationLevel").isPresent()
apartment1.getAttribute("alarmEnabled").isPresent()
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
index 8f1d88e2a..cda8917b6 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
@@ -30,9 +30,14 @@ import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.setup.SetupService
+import org.openremote.model.asset.Asset
import org.openremote.model.asset.AssetEvent
+import org.openremote.model.asset.impl.BuildingAsset
import org.openremote.model.attribute.Attribute
+import org.openremote.model.attribute.MetaItem
import org.openremote.model.event.shared.SharedEvent
+import org.openremote.model.geo.GeoJSONPoint
+import org.openremote.model.value.MetaItemType
import org.openremote.model.value.ValueType
import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.setup.integration.ManagerTestSetup
@@ -129,11 +134,25 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
].toArray(new ChannelHandler[0])
})
+ and: "another Websocket client with no credentials"
+ def client3 = new WebsocketIOClient<String>(
+ new URIBuilder("ws://127.0.0.1:$serverPort/websocket/events?Realm=building").build(),
+ null, null)
+ client3.setEncoderDecoderProvider({
+ [
+ new StringEncoder(CharsetUtil.UTF_8),
+ new StringDecoder(CharsetUtil.UTF_8),
+ new AbstractNettyIOClient.MessageToMessageDecoder<String>(String.class, client3)
+ ].toArray(new ChannelHandler[0])
+ })
+
and: "we add callback consumers to the clients"
def connectionStatus = client.getConnectionStatus()
def connectionStatus2 = client2.getConnectionStatus()
+ def connectionStatus3 = client3.getConnectionStatus()
List<Object> receivedMessages = new CopyOnWriteArrayList<>()
List<Object> receivedMessages2 = new CopyOnWriteArrayList<>()
+ List<Object> receivedMessages3 = new CopyOnWriteArrayList<>()
client.addMessageConsumer({
message -> receivedMessages.add(messageFromString(message))
})
@@ -146,6 +165,12 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
client2.addConnectionStatusConsumer({
status -> connectionStatus2 = status
})
+ client3.addMessageConsumer({
+ message -> receivedMessages3.add(messageFromString(message))
+ })
+ client3.addConnectionStatusConsumer({
+ status -> connectionStatus3 = status
+ })
and: "the system settles down"
noEventProcessedIn(assetProcessingService, 500)
@@ -153,6 +178,7 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
when: "we call connect on the clients"
client.connect()
client2.connect()
+ client3.connect()
then: "the clients status should become CONNECTED"
conditions.eventually {
@@ -160,6 +186,8 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
assert connectionStatus == ConnectionStatus.CONNECTED
assert client2.connectionStatus == ConnectionStatus.CONNECTED
assert connectionStatus2 == ConnectionStatus.CONNECTED
+ assert client3.connectionStatus == ConnectionStatus.CONNECTED
+ assert connectionStatus3 == ConnectionStatus.CONNECTED
}
when: "we subscribe to attribute events produced by the server"
@@ -173,6 +201,11 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
AttributeEvent.class,
null,
"attributes2")))
+ client3.sendMessage(messageToString(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX,
+ new EventSubscription(
+ AttributeEvent.class,
+ null,
+ "attributes3")))
and: "we subscribe to asset events produced by the server"
client.sendMessage(messageToString(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX,
@@ -185,6 +218,11 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
AssetEvent.class,
null,
"assets2")))
+ client3.sendMessage(messageToString(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX,
+ new EventSubscription(
+ AssetEvent.class,
+ null,
+ "assets3")))
then: "the server should confirm the subscriptions"
conditions.eventually {
@@ -194,11 +232,15 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
assert receivedMessages2.size() == 2
assert receivedMessages2.any {it instanceof EventSubscription && it.subscriptionId == "attributes2"}
assert receivedMessages2.any {it instanceof EventSubscription && it.subscriptionId == "assets2"}
+ assert receivedMessages3.size() == 2
+ assert receivedMessages3.any {it instanceof EventSubscription && it.subscriptionId == "attributes3"}
+ assert receivedMessages3.any {it instanceof EventSubscription && it.subscriptionId == "assets3"}
}
when: "apartment 1 living room temp changes"
receivedMessages.clear()
receivedMessages2.clear()
+ receivedMessages3.clear()
assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1LivingroomId, "targetTemperature", 5))
then: "the testuser3 client receives the event"
@@ -217,6 +259,11 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
assert receivedMessages2.isEmpty()
}
+ and: "the anonymous user should not have received the event"
+ conditions.eventually {
+ assert receivedMessages3.isEmpty()
+ }
+
when: "apartment 2 living room temp changes"
receivedMessages.clear()
receivedMessages2.clear()
@@ -239,25 +286,101 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
when: "apartment 1 living room asset is modified"
receivedMessages.clear()
receivedMessages2.clear()
+ receivedMessages3.clear()
def apartment1Livingroom = assetStorageService.find(managerTestSetup.apartment1LivingroomId)
apartment1Livingroom.addAttributes(
- new Attribute<>("testAttribute", ValueType.BOOLEAN)
+ new Attribute<>("testAttribute", ValueType.BOOLEAN).addOrReplaceMeta(
+ new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_READ, false),
+ new MetaItem<>(MetaItemType.ACCESS_PUBLIC_READ, false)
+ )
+ )
+ apartment1Livingroom.getAttribute(Asset.NOTES).get().addOrReplaceMeta(
+ new MetaItem<>(MetaItemType.ACCESS_PUBLIC_READ)
)
apartment1Livingroom = assetStorageService.merge(apartment1Livingroom)
then: "the testuser3 client should be notified with only accessible attributes"
conditions.eventually {
- assert receivedMessages.size() == 1
- assert receivedMessages.get(0) instanceof TriggeredEventSubscription
- assert (receivedMessages.get(0) as TriggeredEventSubscription).subscriptionId == "assets"
- assert (receivedMessages.get(0) as TriggeredEventSubscription).events.size() == 1
- assert (receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) instanceof AssetEvent
- assert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).id == managerTestSetup.apartment1LivingroomId
- assert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).attributeNames.size() == 8
- assert !((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).attributeNames.contains("testAttribute")
+ assert receivedMessages.size() == 2
+ def attrSub = receivedMessages.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "attributes"} as TriggeredEventSubscription<AttributeEvent>
+ def assetSub = receivedMessages.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "assets"} as TriggeredEventSubscription<AssetEvent>
+ assert assetSub != null
+ assert assetSub.events.size() == 1
+ assert assetSub.events.get(0).id == managerTestSetup.apartment1LivingroomId
+ assert assetSub.events.get(0).attributeNames.size() == 8
+ assert !assetSub.events.get(0).attributeNames.contains("testAttribute")
+ assert attrSub != null
+ assert attrSub.events.size() == 1
+ assert attrSub.events.get(0).id == managerTestSetup.apartment1LivingroomId
+ assert attrSub.events.get(0).name == Asset.NOTES.name
}
- then: "the building user should not have received the event"
+ and: "the anonymous client should be notified with only accessible attributes"
+ conditions.eventually {
+ assert receivedMessages3.size() == 2
+ def attrSub = receivedMessages3.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "attributes3"} as TriggeredEventSubscription<AttributeEvent>
+ def assetSub = receivedMessages3.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "assets3"} as TriggeredEventSubscription<AssetEvent>
+ assert assetSub != null
+ assert assetSub.events.size() == 1
+ assert assetSub.events.get(0).id == managerTestSetup.apartment1LivingroomId
+ assert assetSub.events.get(0).attributeNames.size() == 1
+ assert assetSub.events.get(0).attributeNames.contains(Asset.NOTES.name)
+ assert attrSub != null
+ assert attrSub.events.size() == 1
+ assert attrSub.events.get(0).id == managerTestSetup.apartment1LivingroomId
+ assert attrSub.events.get(0).name == Asset.NOTES.name
+ }
+
+ and: "the building user should not have received the event"
+ assert receivedMessages2.isEmpty()
+
+ when: "apartment 1 asset is modified"
+ receivedMessages.clear()
+ receivedMessages2.clear()
+ receivedMessages3.clear()
+ def apartment1 = assetStorageService.find(managerTestSetup.apartment1Id)
+ apartment1.getAttribute(Asset.LOCATION).get().setValue(new GeoJSONPoint(10, 10))
+ apartment1.getAttribute(BuildingAsset.STREET).get().setValue("Test Street")
+ apartment1 = assetStorageService.merge(apartment1)
+
+ then: "the testuser3 client should be notified with only accessible attributes"
+ conditions.eventually {
+ assert receivedMessages.size() == 3
+ def attr1Sub = receivedMessages.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "attributes" && it.events.any {((AttributeEvent)it).ref.name==BuildingAsset.STREET.name}} as TriggeredEventSubscription<AttributeEvent>
+ def attr2Sub = receivedMessages.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "attributes" && it.events.any {((AttributeEvent)it).ref.name==Asset.LOCATION.name}} as TriggeredEventSubscription<AttributeEvent>
+ def assetSub = receivedMessages.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "assets"} as TriggeredEventSubscription<AssetEvent>
+ assert assetSub != null
+ assert assetSub.events.size() == 1
+ assert assetSub.events.get(0).id == managerTestSetup.apartment1Id
+ assert assetSub.events.get(0).attributeNames.size() == 8
+ assert assetSub.events.get(0).attributeNames.contains(BuildingAsset.STREET.name)
+ assert attr1Sub != null
+ assert attr1Sub.events.size() == 1
+ assert attr1Sub.events.get(0).id == managerTestSetup.apartment1Id
+ assert attr1Sub.events.get(0).name == BuildingAsset.STREET.name
+ assert attr2Sub != null
+ assert attr2Sub.events.size() == 1
+ assert attr2Sub.events.get(0).id == managerTestSetup.apartment1Id
+ assert attr2Sub.events.get(0).name == Asset.LOCATION.name
+ }
+
+ then: "the anonymous client should be notified with only accessible attributes"
+ conditions.eventually {
+ assert receivedMessages3.size() == 2
+ def attrSub = receivedMessages3.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "attributes3"} as TriggeredEventSubscription<AttributeEvent>
+ def assetSub = receivedMessages3.find{it instanceof TriggeredEventSubscription && it.subscriptionId == "assets3"} as TriggeredEventSubscription<AssetEvent>
+ assert assetSub != null
+ assert assetSub.events.size() == 1
+ assert assetSub.events.get(0).id == managerTestSetup.apartment1Id
+ assert assetSub.events.get(0).attributeNames.size() == 1
+ assert assetSub.events.get(0).attributeNames.contains(Asset.LOCATION.name)
+ assert attrSub != null
+ assert attrSub.events.size() == 1
+ assert attrSub.events.get(0).id == managerTestSetup.apartment1Id
+ assert attrSub.events.get(0).name == BuildingAsset.LOCATION.name
+ }
+
+ and: "the building user should not have received the event"
assert receivedMessages2.isEmpty()
cleanup: "the server should be stopped"
@@ -267,6 +390,9 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
if (client2 != null) {
client2.disconnect()
}
+ if (client3 != null) {
+ client3.disconnect()
+ }
}
def "Check client"() {