Skip to content

armgit / core

Public Imported from GitHub
0

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

Improve access control and input validation on update parent endpoints (#2692)

Eric Bariaux38b9b55a84fc
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
index b4a6ef951..e8824363a 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
@@ -61,6 +61,7 @@ import static org.openremote.model.value.MetaItemType.*;
public class AssetResourceImpl extends ManagerWebResource implements AssetResource {
private static final Logger LOG = Logger.getLogger(AssetResourceImpl.class.getName());
+
protected final AssetStorageService assetStorageService;
protected final MessageBrokerService messageBrokerService;
protected final ClientEventService clientEventService;
@@ -625,31 +626,121 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
@Override
public void updateParent(RequestParams requestParams, String parentId, List<String> assetIds) {
- AssetQuery query = new AssetQuery();
- query.ids = assetIds.toArray(String[]::new);
+ try {
+ List<Asset<?>> validatedChildAssets = validateBatchParentMutation(assetIds, parentId, true);
- List<Asset<?>> assets = this.assetStorageService.findAll(query);
- LOG.fine("Updating parent for assets: count=" + assets.size() + ", newParentID=" + parentId);
+ String targetParentId = parentId;
+ LOG.fine("Updating parent for assets: count=" + validatedChildAssets.size() + ", newParentID=" + targetParentId);
- for (Asset<?> asset : assets) {
- asset.setParentId(parentId);
- LOG.fine("Updating asset parent: assetID=" + asset.getId() + ", newParentID=" + parentId);
- assetStorageService.merge(asset);
+ for (Asset<?> asset : validatedChildAssets) {
+ asset.setParentId(targetParentId);
+ LOG.fine("Updating asset parent: assetID=" + asset.getId() + ", newParentID=" + targetParentId);
+ assetStorageService.merge(asset);
+ }
+ } catch (IllegalStateException | ConstraintViolationException ex) {
+ throw new WebApplicationException(ex, BAD_REQUEST);
}
}
+ private List<Asset<?>> validateBatchParentMutation(List<String> assetIds, String parentId, boolean validateParent) {
+ if (isRestrictedUser()) {
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ List<String> normalizedAssetIds = normalizeBatchParentMutationAssetIds(assetIds);
+
+ if (validateParent) {
+ if (TextUtil.isNullOrEmpty(parentId)) {
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+
+ if (normalizedAssetIds.contains(parentId)) {
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+ }
+
+ List<Asset<?>> childAssets = assetStorageService.findAll(
+ new AssetQuery().ids(normalizedAssetIds.toArray(String[]::new))
+ );
+
+ if (childAssets.size() != normalizedAssetIds.size()) {
+ LOG.fine("Request to update parent of one or more invalid assets");
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+
+ String childRealm = null;
+
+ for (Asset<?> childAsset : childAssets) {
+ String childAssetRealm = childAsset.getRealm();
+
+ if (!isRealmActiveAndAccessible(childAssetRealm)) {
+ LOG.fine("Child asset in a nonexistent, inactive or inaccessible realm: username=" + getUsername() + ", assetID=" + childAsset.getId());
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ if (childRealm == null) {
+ childRealm = childAssetRealm;
+ } else if (validateParent && !childRealm.equals(childAssetRealm)) {
+ LOG.fine("Request to update parent for child assets in multiple realms: username=" + getUsername());
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+ }
+ Asset<?> parentAsset = null;
+
+ if (validateParent) {
+ parentAsset = assetStorageService.find(parentId, true);
+
+ if (parentAsset == null) {
+ LOG.fine("Parent asset not found: parentID=" + parentId);
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+
+ if (!isRealmActiveAndAccessible(parentAsset.getRealm())) {
+ LOG.fine("Parent asset in a nonexistent, inactive or inaccessible realm: username=" + getUsername() + ", parentID=" + parentId);
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ if (!childRealm.equals(parentAsset.getRealm())) {
+ LOG.fine("Parent asset realm does not match child asset realm: childRealm=" + childRealm + ", parentRealm=" + parentAsset.getRealm());
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+ }
+
+ return childAssets;
+ }
+
+ private List<String> normalizeBatchParentMutationAssetIds(List<String> assetIds) {
+ if (assetIds == null || assetIds.isEmpty()) {
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+
+ LinkedHashSet<String> normalizedAssetIds = new LinkedHashSet<>();
+
+ for (String assetId : assetIds) {
+ if (TextUtil.isNullOrEmpty(assetId)) {
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+
+ normalizedAssetIds.add(assetId);
+ }
+
+ return new ArrayList<>(normalizedAssetIds);
+ }
+
@Override
public void updateNoneParent(RequestParams requestParams, List<String> assetIds) {
- AssetQuery query = new AssetQuery();
- query.ids = assetIds.toArray(String[]::new);
+ try {
+ List<Asset<?>> validatedChildAssets = validateBatchParentMutation(assetIds, null, false);
- List<Asset<?>> assets = this.assetStorageService.findAll(query);
- LOG.fine("Updating parent for assets: count=" + assets.size() + ", newParentID=NONE");
+ LOG.fine("Updating parent for assets: count=" + validatedChildAssets.size() + ", newParentID=NONE");
- for (Asset<?> asset : assets) {
- asset.setParentId(null);
- LOG.fine("Updating asset parent: assetID=" + asset.getId() + ", newParentID=NONE");
- assetStorageService.merge(asset);
+ for (Asset<?> asset : validatedChildAssets) {
+ asset.setParentId(null);
+ LOG.fine("Updating asset parent: assetID=" + asset.getId() + ", newParentID=NONE");
+ assetStorageService.merge(asset);
+ }
+ } catch (IllegalStateException | ConstraintViolationException ex) {
+ throw new WebApplicationException(ex, BAD_REQUEST);
}
}
}
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
index 578322c88..c9eb9ca5b 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
@@ -3,10 +3,14 @@ package org.openremote.test.assets
import jakarta.ws.rs.WebApplicationException
import org.jboss.resteasy.api.validation.ViolationReport
import org.openremote.container.timer.TimerService
+import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.datapoint.AssetDatapointService
import org.openremote.manager.event.ClientEventService
+import org.openremote.manager.gateway.GatewayService
import org.openremote.manager.setup.SetupService
import org.openremote.model.asset.AssetResource
+import org.openremote.model.asset.impl.GatewayAsset
+import org.openremote.model.asset.impl.GroupAsset
import org.openremote.model.asset.impl.RoomAsset
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.Attribute
@@ -171,6 +175,295 @@ class AssetIntegrityTest extends Specification implements ManagerContainerTrait
ex.response.status == 400
}
+ def "Batch parent updates reject unknown child asset ids as superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there's an asset in the master realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def existingAsset = assetResource.create(null, new RoomAsset("Existing asset").setRealm(keycloakTestSetup.realmMaster.name))
+ def missingChildId = UniqueIdentifierGenerator.generateId()
+
+ when: "an updateParent call is made with child asset list containing an unknown child asset id"
+ assetResource.updateParent(null, managerTestSetup.groundFloorId, [existingAsset.id, missingChildId])
+
+ then: "the request should be bad and no asset should be moved"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, existingAsset.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent updates reject unknown parent asset ids as superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there's an asset in the master realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def existingAsset = assetResource.create(null, new RoomAsset("Existing asset").setRealm(keycloakTestSetup.realmMaster.name))
+ def missingParentId = UniqueIdentifierGenerator.generateId()
+
+ when: "an updateParent call is made with a non-existent parent asset id"
+ assetResource.updateParent(null, missingParentId, [existingAsset.id])
+
+ then: "the request should be bad and no asset should be moved"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, existingAsset.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent updates reject self-parent requests as superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there's an asset in the master realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def existingAsset = assetResource.create(null, new RoomAsset("Existing asset").setRealm(keycloakTestSetup.realmMaster.name))
+
+ when: "an updateParent call is made with the asset itself as the parent"
+ assetResource.updateParent(null, existingAsset.id, [existingAsset.id])
+
+ then: "the request should be bad and the asset should remain a root asset"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, existingAsset.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent updates reject mixed child realms as superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there are assets in the master and building realms"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def masterAssetForMixedRealm = assetResource.create(null, new RoomAsset("Master Mixed Realm").setRealm(keycloakTestSetup.realmMaster.name))
+ def buildingAssetForMixedRealm = assetResource.create(null, new RoomAsset("Building Mixed Realm").setRealm(keycloakTestSetup.realmBuilding.name))
+
+ when: "an updateParent call is made with child asset list containing child assets from different realms"
+ assetResource.updateParent(null, managerTestSetup.groundFloorId, [masterAssetForMixedRealm.id, buildingAssetForMixedRealm.id])
+
+ then: "the request should be bad and no asset should be moved"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, masterAssetForMixedRealm.id) as RoomAsset).parentId == null
+ (assetResource.get(null, buildingAssetForMixedRealm.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent update rejects null or empty child lists as superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there's an asset in the master realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def rootAsset = assetResource.create(null, new RoomAsset("Root Asset").setRealm(keycloakTestSetup.realmMaster.name))
+
+ when: "a parent update is requested with an empty child list"
+ assetResource.updateParent(null, managerTestSetup.groundFloorId, [] as List<String>)
+
+ then: "the request should be bad"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, rootAsset.id) as RoomAsset).parentId == null
+
+ when: "a parent clear is requested with a null child list"
+ assetResource.updateNoneParent(null, null)
+
+ then: "the request should be bad"
+ ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, rootAsset.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent updates reject moving an asset under its own descendant as superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there is an asset hierarchy in the master realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def rootAsset = assetResource.create(null, new RoomAsset("Root asset").setRealm(keycloakTestSetup.realmMaster.name))
+ def childAsset = assetResource.create(null, new RoomAsset("Child asset").setRealm(keycloakTestSetup.realmMaster.name).setParentId(rootAsset.id))
+ def grandChildAsset = assetResource.create(null, new RoomAsset("Grandchild asset").setRealm(keycloakTestSetup.realmMaster.name).setParentId(childAsset.id))
+
+ when: "an updateParent call tries to move the root asset under its own descendant"
+ assetResource.updateParent(null, grandChildAsset.id, [rootAsset.id])
+
+ then: "the request should be bad and the hierarchy should remain unchanged"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, rootAsset.id) as RoomAsset).parentId == null
+ (assetResource.get(null, childAsset.id) as RoomAsset).parentId == rootAsset.id
+ (assetResource.get(null, grandChildAsset.id) as RoomAsset).parentId == childAsset.id
+ }
+
+ def "Batch parent updates reject group child-type mismatches as superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there is a group parent that only accepts room assets"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def groupParent = assetResource.create(null, new GroupAsset("Rooms only", RoomAsset.class).setRealm(keycloakTestSetup.realmMaster.name))
+ def invalidChild = assetResource.create(null, new ThingAsset("Invalid child").setRealm(keycloakTestSetup.realmMaster.name))
+
+ when: "an updateParent call tries to move a mismatched asset type under the group"
+ assetResource.updateParent(null, groupParent.id, [invalidChild.id])
+
+ then: "the request should be bad and the asset should not be moved"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, invalidChild.id) as ThingAsset).parentId == null
+ }
+
+ def "Batch parent updates reject moving a local asset under a gateway asset as superuser"() {
+ given: "the server container is started"
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def gatewayService = container.getService(GatewayService.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there is a locally registered gateway asset in the building realm"
+ GatewayAsset gateway = assetStorageService.merge(new GatewayAsset("Test gateway")
+ .setRealm(managerTestSetup.realmBuildingName))
+ conditions.eventually {
+ assert gatewayService.isLocallyRegisteredGateway(gateway.id)
+ }
+
+ and: "there is a regular asset in the same realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def localAsset = assetResource.create(null, new RoomAsset("Local asset").setRealm(managerTestSetup.realmBuildingName))
+
+ when: "an updateParent call tries to move the local asset under the gateway asset"
+ assetResource.updateParent(null, gateway.id, [localAsset.id])
+
+ then: "the request should be bad and the asset should not be moved"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetResource.get(null, localAsset.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent clear rejects gateway descendant assets as superuser"() {
+ given: "the server container is started"
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def gatewayService = container.getService(GatewayService.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there is a locally registered gateway asset and a gateway descendant in the building realm"
+ GatewayAsset gateway = assetStorageService.merge(new GatewayAsset("Test gateway")
+ .setRealm(managerTestSetup.realmBuildingName))
+ conditions.eventually {
+ assert gatewayService.isLocallyRegisteredGateway(gateway.id)
+ }
+ def gatewayDescendant = assetStorageService.merge(
+ new RoomAsset("Gateway descendant")
+ .setId(UniqueIdentifierGenerator.generateId("GatewayDescendant"))
+ .setRealm(managerTestSetup.realmBuildingName)
+ .setParentId(gateway.id),
+ false,
+ gateway,
+ null
+ )
+
+ when: "an updateNoneParent call tries to clear the parent of the gateway descendant"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ assetResource.updateNoneParent(null, [gatewayDescendant.id])
+
+ then: "the request should be bad and the gateway descendant should remain attached to the gateway"
+ WebApplicationException ex = thrown()
+ ex.response.status == 400
+ (assetStorageService.find(gatewayDescendant.id, true) as RoomAsset).parentId == gateway.id
+ }
+
def "Test writing attributes with timestamps"() {
given: "the server container is started"
def container = startContainer(defaultConfig(), defaultServices())
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 f913662f4..75cc42e65 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
@@ -814,6 +814,175 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
assert testAsset.getAttribute("myCustomAttribute").get().getMetaItem(LABEL).get().getValue().get() == "My label update"
}
+ def "Batch parent updates within the authenticated realm succeed for testuser1"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated test user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ "testuser1",
+ "testuser1"
+ ).token
+
+ and: "there are assets in the authenticated realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def asset1 = assetResource.create(null, new RoomAsset("Batch Asset 1").setRealm(keycloakTestSetup.realmMaster.name))
+ def asset2 = assetResource.create(null, new RoomAsset("Batch Asset 2").setRealm(keycloakTestSetup.realmMaster.name))
+
+ when: "assets in the authenticated realm are moved under an authenticated parent"
+ assetResource.updateParent(null, managerTestSetup.groundFloorId, [asset1.id, asset2.id])
+
+ then: "the assets should be moved"
+ (assetResource.get(null, asset1.id) as RoomAsset).parentId == managerTestSetup.groundFloorId
+ (assetResource.get(null, asset2.id) as RoomAsset).parentId == managerTestSetup.groundFloorId
+
+ when: "the parent is cleared for assets in the authenticated realm"
+ assetResource.updateNoneParent(null, [asset1.id, asset2.id])
+
+ then: "the assets should become root assets again"
+ (assetResource.get(null, asset1.id) as RoomAsset).parentId == null
+ (assetResource.get(null, asset2.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent clear across accessible realms succeeds for superuser"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated admin user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "there are child assets with parents in different accessible realms"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def masterAsset = assetResource.create(null, new RoomAsset("Master batch clear").setRealm(keycloakTestSetup.realmMaster.name).setParentId(managerTestSetup.groundFloorId))
+ def buildingAsset = assetResource.create(null, new RoomAsset("Building batch clear").setRealm(keycloakTestSetup.realmBuilding.name).setParentId(managerTestSetup.smartBuildingId))
+
+ when: "the superuser clears the parent of both assets in one request"
+ assetResource.updateNoneParent(null, [masterAsset.id, buildingAsset.id])
+
+ then: "both assets should become root assets"
+ (assetResource.get(null, masterAsset.id) as RoomAsset).parentId == null
+ (assetResource.get(null, buildingAsset.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent updates reject foreign child assets for testuser1"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated test user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ "testuser1",
+ "testuser1"
+ ).token
+
+ and: "there is an asset in the authenticated realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def asset1 = assetResource.create(null, new RoomAsset("Batch Asset 1").setRealm(keycloakTestSetup.realmMaster.name))
+
+ when: "a parent update is requested with an asset from a foreign realm"
+ assetResource.updateParent(null, managerTestSetup.groundFloorId, [asset1.id, managerTestSetup.apartment2LivingroomId])
+
+ then: "the request should be forbidden and no asset should be moved"
+ WebApplicationException ex = thrown()
+ ex.response.status == 403
+ (assetResource.get(null, asset1.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent updates reject foreign parents for testuser1"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated test user will make the call"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ "testuser1",
+ "testuser1"
+ ).token
+
+ and: "there's an asset in the authenticated realm"
+ def assetResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetResource.class)
+ def asset1 = assetResource.create(null, new RoomAsset("Batch Asset 1").setRealm(keycloakTestSetup.realmMaster.name))
+
+ when: "a parent update is requested with the target parent in a foreign realm"
+ assetResource.updateParent(null, managerTestSetup.smartBuildingId, [asset1.id])
+
+ then: "the request should be forbidden and no asset should be moved"
+ WebApplicationException ex = thrown()
+ ex.response.status == 403
+ (assetResource.get(null, asset1.id) as RoomAsset).parentId == null
+ }
+
+ def "Batch parent updates are forbidden for restricted testuser3"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated restricted user will make the call"
+ def accessToken = authenticate(
+ container,
+ keycloakTestSetup.realmBuilding.name,
+ KEYCLOAK_CLIENT_ID,
+ "testuser3",
+ "testuser3"
+ ).token
+
+ when: "a restricted user tries to move a linked asset"
+ def assetResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.realmBuilding.name, accessToken).proxy(AssetResource.class)
+ assetResource.updateParent(null, managerTestSetup.apartment2Id, [managerTestSetup.apartment1LivingroomId])
+
+ then: "the request should be forbidden and the parent should remain unchanged"
+ WebApplicationException ex = thrown()
+ ex.response.status == 403
+ (assetResource.get(null, managerTestSetup.apartment1LivingroomId) as RoomAsset).parentId == managerTestSetup.apartment1Id
+ }
+
+ def "Batch parent clear is forbidden for restricted testuser3"() {
+ given: "the server container is started"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+
+ and: "an authenticated restricted user will make the call"
+ def accessToken = authenticate(
+ container,
+ keycloakTestSetup.realmBuilding.name,
+ KEYCLOAK_CLIENT_ID,
+ "testuser3",
+ "testuser3"
+ ).token
+
+ when: "a restricted user tries to clear the parent of a linked asset"
+ def assetResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.realmBuilding.name, accessToken).proxy(AssetResource.class)
+ assetResource.updateNoneParent(null, [managerTestSetup.apartment1LivingroomId])
+
+ then: "the request should be forbidden and the parent should remain unchanged"
+ WebApplicationException ex = thrown()
+ ex.response.status == 403
+ (assetResource.get(null, managerTestSetup.apartment1LivingroomId) as RoomAsset).parentId == managerTestSetup.apartment1Id
+ }
+
def "Access assets as anonymous user"() {
given: "the server container is started"
def container = startContainer(defaultConfig(), defaultServices())