Skip to content

armgit / core

Public Imported from GitHub
0

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

Merge commit from fork

Eric Bariaux583dbbfb9607
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java
index 286b58a88..7456426b4 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java
@@ -83,16 +83,16 @@ public class AssetPredictedDatapointResourceImpl extends ManagerWebResource impl
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
+ Attribute<?> attribute = asset.getAttribute(attributeName).orElseThrow(() ->
+ new WebApplicationException(Response.Status.NOT_FOUND)
+ );
+
// If logged in, user should have READ ASSETS role
if(isAuthenticated() && !hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
LOG.info("Forbidden access for user '" + getUsername() + "': " + asset.getRealm());
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
- Attribute<?> attribute = asset.getAttribute(attributeName).orElseThrow(() ->
- new WebApplicationException(Response.Status.NOT_FOUND)
- );
-
// If restricted, the attribute should also be restricted
if(isRestrictedUser()) {
attribute.getMeta().getValue(MetaItemType.ACCESS_RESTRICTED_READ).ifPresentOrElse((v) -> {
@@ -136,14 +136,9 @@ public class AssetPredictedDatapointResourceImpl extends ManagerWebResource impl
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
- // If not logged in, asset should be PUBLIC READ
- if(!isAuthenticated() && !asset.isAccessPublicRead()) {
- throw new WebApplicationException(Response.Status.FORBIDDEN);
- }
-
- // If logged in, user should have READ ASSETS role
- if(isAuthenticated() && !hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
- LOG.info("Forbidden access for user '" + getUsername() + "': " + asset.getRealm());
+ // If logged in, user should have WRITE ATTRIBUTES role
+ if(isAuthenticated() && !hasResourceRole(ClientRole.WRITE_ATTRIBUTES.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
+ LOG.info("Forbidden write access for user '" + getUsername() + "': " + asset.getRealm());
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
@@ -151,18 +146,18 @@ public class AssetPredictedDatapointResourceImpl extends ManagerWebResource impl
new WebApplicationException(Response.Status.NOT_FOUND)
);
- // If restricted, the attribute should also be restricted
+ // If restricted, the attribute should be writable by restricted users
if(isRestrictedUser()) {
- attribute.getMeta().getValue(MetaItemType.ACCESS_RESTRICTED_READ).ifPresentOrElse((v) -> {
+ attribute.getMeta().getValue(MetaItemType.ACCESS_RESTRICTED_WRITE).ifPresentOrElse((v) -> {
if(!v) { throw new WebApplicationException(Response.Status.FORBIDDEN); }
}, () -> {
throw new WebApplicationException(Response.Status.FORBIDDEN);
});
}
- // If not logged in, attribute should be PUBLIC READ
+ // If not logged in, attribute should be PUBLIC WRITE
if(!isAuthenticated()) {
- attribute.getMeta().getValue(MetaItemType.ACCESS_PUBLIC_READ).ifPresentOrElse((v) -> {
+ attribute.getMeta().getValue(MetaItemType.ACCESS_PUBLIC_WRITE).ifPresentOrElse((v) -> {
if(!v) { throw new WebApplicationException(Response.Status.FORBIDDEN); }
}, () -> {
throw new WebApplicationException(Response.Status.FORBIDDEN);
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
index b740dae59..f0036de03 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
@@ -1,5 +1,6 @@
package org.openremote.test.assets
+import jakarta.ws.rs.WebApplicationException
import io.netty.channel.ChannelHandler
import io.netty.handler.codec.string.StringDecoder
import io.netty.handler.codec.string.StringEncoder
@@ -16,8 +17,10 @@ import org.openremote.manager.event.ClientEventService
import org.openremote.manager.security.ManagerIdentityService
import org.openremote.manager.setup.SetupService
import org.openremote.model.asset.agent.ConnectionStatus
+import org.openremote.model.asset.impl.BuildingAsset
import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.AttributeRef
+import org.openremote.model.attribute.MetaItem
import org.openremote.model.auth.OAuthPasswordGrant
import org.openremote.model.datapoint.AssetPredictedDatapointEvent
import org.openremote.model.datapoint.AssetPredictedDatapointResource
@@ -46,6 +49,7 @@ import java.util.function.Consumer
import static java.util.concurrent.TimeUnit.*
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
import static org.openremote.model.Constants.MASTER_REALM
+import static org.openremote.model.value.MetaItemType.ACCESS_PUBLIC_WRITE
import static org.openremote.setup.integration.ManagerTestSetup.thingLightToggleAttributeName
import static spock.util.matcher.HamcrestMatchers.closeTo
@@ -418,6 +422,109 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
assert predictedData.count {it.value == false} == 2
}
+ def "Test anonymous predicted datapoint writes are allowed for public write attributes"() {
+ given: "expected conditions"
+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+
+ and: "the manager container and test setup are available"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+
+ and: "a public-write-only attribute exists on a public asset"
+ def attributeName = "predictedPublicWrite"
+ def apartment1 = assetStorageService.find(managerTestSetup.apartment1Id, true)
+ apartment1.getAttributes().addOrReplace(
+ new Attribute<>(attributeName, ValueType.NUMBER, 0d)
+ .addMeta(new MetaItem<>(ACCESS_PUBLIC_WRITE, true))
+ )
+ assetStorageService.merge(apartment1)
+
+ and: "an anonymous predicted datapoint API client"
+ def predictedDatapointResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.realmBuilding.name).proxy(AssetPredictedDatapointResource.class)
+
+ when: "an anonymous predicted datapoint write is made"
+ predictedDatapointResource.writePredictedDatapoints(
+ null,
+ managerTestSetup.apartment1Id,
+ attributeName,
+ [new ValueDatapoint<>(getClockTimeOf(container) + 60000, 12d)] as ValueDatapoint<?>[]
+ )
+
+ then: "the predicted datapoint should be stored"
+ conditions.eventually {
+ def predictedData = assetPredictedDatapointService.getDatapoints(new AttributeRef(managerTestSetup.apartment1Id, attributeName))
+ assert predictedData.size() == 1
+ assert predictedData[0].value == 12d
+ }
+ }
+
+ def "Test predicted datapoint writes are denied without WRITE_ATTRIBUTES role"() {
+ given: "the manager container and test setup are available"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "a building user with READ_ASSETS but without WRITE_ATTRIBUTES"
+ def accessToken = authenticate(
+ container,
+ keycloakTestSetup.realmBuilding.name,
+ KEYCLOAK_CLIENT_ID,
+ "testuser2",
+ "testuser2"
+ )
+ def predictedDatapointResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.realmBuilding.name, accessToken).proxy(AssetPredictedDatapointResource.class)
+
+ when: "the user writes predicted datapoints"
+ predictedDatapointResource.writePredictedDatapoints(
+ null,
+ managerTestSetup.apartment1LivingroomId,
+ "targetTemperature",
+ [new ValueDatapoint<>(getClockTimeOf(container) + 60000, 19d)] as ValueDatapoint<?>[]
+ )
+
+ then: "access should be forbidden"
+ WebApplicationException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+ }
+
+ def "Test restricted predicted datapoint writes require restricted write meta"() {
+ given: "the manager container and test setup are available"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "a restricted user with linked assets"
+ def accessToken = authenticate(
+ container,
+ keycloakTestSetup.realmBuilding.name,
+ KEYCLOAK_CLIENT_ID,
+ "testuser3",
+ "testuser3"
+ )
+ def predictedDatapointResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.realmBuilding.name, accessToken).proxy(AssetPredictedDatapointResource.class)
+
+ when: "the restricted user writes a predicted datapoint for an attribute without restricted write access"
+ predictedDatapointResource.writePredictedDatapoints(
+ null,
+ managerTestSetup.apartment1Id,
+ BuildingAsset.STREET.name,
+ [new ValueDatapoint<>(getClockTimeOf(container) + 60000, "should fail")] as ValueDatapoint<?>[]
+ )
+
+ then: "access should be forbidden"
+ WebApplicationException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+ }
+
def "Test predicted datapoint change events are received via websocket API"() {
given: "expected conditions"
@@ -606,8 +713,21 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
and: "the manager container and setup are available"
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def identityService = container.getService(ManagerIdentityService.class)
- and: "a building realm predicted datapoint API client"
+ and: "the limited building user has attribute write permission for producing predicted datapoint events"
+ identityService.getIdentityProvider().updateUserClientRoles(
+ keycloakTestSetup.realmBuilding.name,
+ keycloakTestSetup.testuser2Id,
+ KEYCLOAK_CLIENT_ID,
+ ClientRole.WRITE_USER.value,
+ ClientRole.READ_MAP.value,
+ ClientRole.READ_ASSETS.value,
+ ClientRole.WRITE_ATTRIBUTES.value
+ )
+
+ and: "a limited building realm predicted datapoint API client"
def accessToken = authenticate(
container,
"building",
@@ -669,7 +789,15 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
!(it instanceof TriggeredEventSubscription) || it.subscriptionId != "predicted-restricted-unlinked"
}
- cleanup: "the websocket client is disconnected"
+ cleanup: "the websocket client is disconnected and original roles are restored"
+ identityService.getIdentityProvider().updateUserClientRoles(
+ keycloakTestSetup.realmBuilding.name,
+ keycloakTestSetup.testuser2Id,
+ KEYCLOAK_CLIENT_ID,
+ ClientRole.WRITE_USER.value,
+ ClientRole.READ_MAP.value,
+ ClientRole.READ_ASSETS.value
+ )
if (client != null) {
client.disconnect()
}
@@ -684,13 +812,26 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def assetStorageService = container.getService(AssetStorageService.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def identityService = container.getService(ManagerIdentityService.class)
and: "a non-restricted attribute exists on an asset linked to the restricted user"
def apartment1 = assetStorageService.find(managerTestSetup.apartment1Id, true)
apartment1.getAttributes().addOrReplace(new Attribute<>("predictedNonRestricted", ValueType.NUMBER, 0d))
assetStorageService.merge(apartment1)
- and: "a building realm predicted datapoint API client"
+ and: "the limited building user has attribute write permission for producing predicted datapoint events"
+ identityService.getIdentityProvider().updateUserClientRoles(
+ keycloakTestSetup.realmBuilding.name,
+ keycloakTestSetup.testuser2Id,
+ KEYCLOAK_CLIENT_ID,
+ ClientRole.WRITE_USER.value,
+ ClientRole.READ_MAP.value,
+ ClientRole.READ_ASSETS.value,
+ ClientRole.WRITE_ATTRIBUTES.value
+ )
+
+ and: "a limited building realm predicted datapoint API client"
def accessToken = authenticate(
container,
"building",
@@ -752,7 +893,15 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
!(it instanceof TriggeredEventSubscription) || it.subscriptionId != "predicted-restricted-attr"
}
- cleanup: "the websocket client is disconnected"
+ cleanup: "the websocket client is disconnected and original roles are restored"
+ identityService.getIdentityProvider().updateUserClientRoles(
+ keycloakTestSetup.realmBuilding.name,
+ keycloakTestSetup.testuser2Id,
+ KEYCLOAK_CLIENT_ID,
+ ClientRole.WRITE_USER.value,
+ ClientRole.READ_MAP.value,
+ ClientRole.READ_ASSETS.value
+ )
if (client != null) {
client.disconnect()
}