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 on asset import / discovery for agents (#2871)

Eric Bariauxb7e5d5990169
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java b/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java
index c922e4a5d..30eed5d48 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java
@@ -25,6 +25,7 @@ import org.openremote.container.util.CodecUtil;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebResource;
+import org.openremote.model.Constants;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetTreeNode;
import org.openremote.model.asset.agent.Agent;
@@ -108,6 +109,8 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
@Override
public AssetTreeNode[] doProtocolAssetDiscovery(RequestParams requestParams, String agentId, String realm) {
+ assertWriteAssetsAllowed();
+
if (!isSuperUser()) {
realm = getAuthenticatedRealmName();
}
@@ -148,6 +151,8 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
@Override
public AssetTreeNode[] doProtocolAssetImport(RequestParams requestParams, String agentId, String realm, FileInfo fileInfo) {
+ assertWriteAssetsAllowed();
+
if (!isSuperUser()) {
realm = getAuthenticatedRealmName();
}
@@ -193,6 +198,16 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
return foundAssetsArr;
}
+ protected void assertWriteAssetsAllowed() {
+ if (!isAuthenticated()) {
+ throw new NotAuthorizedException("Must be authenticated");
+ }
+
+ if (!hasResourceRoleOrIsSuperUser(Constants.WRITE_ASSETS_ROLE, Constants.KEYCLOAK_CLIENT_ID)) {
+ throw new ForbiddenException("User must have write asset permission");
+ }
+ }
+
// TODO: Allow user to select which assets/attributes are actually added to the DB
protected void persistAssets(AssetTreeNode[] assets, Asset<?> parentAsset, String realm) {
try {
diff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java b/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java
index d69499ad0..a7301e06f 100644
--- a/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java
+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java
@@ -74,6 +74,7 @@ public interface AgentResource {
@Path("assetDiscovery/{agentId}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
+ @RolesAllowed({Constants.WRITE_ASSETS_ROLE})
@Operation(operationId = "doProtocolAssetDiscovery", summary = "Do protocol asset discovery")
AssetTreeNode[] doProtocolAssetDiscovery(
@BeanParam RequestParams requestParams,
@@ -94,6 +95,7 @@ public interface AgentResource {
@Path("assetImport/{agentId}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
+ @RolesAllowed({Constants.WRITE_ASSETS_ROLE})
@Operation(operationId = "doProtocolAssetImport", summary = "Do protocol asset import")
// TODO: File upload should use standard multipart mechanism
AssetTreeNode[] doProtocolAssetImport(
diff --git a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
index cd565607e..25f279c63 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
@@ -20,16 +20,24 @@
package org.openremote.test.protocol
+import jakarta.ws.rs.ForbiddenException
+import jakarta.ws.rs.WebApplicationException
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.agent.AgentResource
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.*
+import org.openremote.model.file.FileInfo
+import org.openremote.model.query.AssetQuery
+import org.openremote.model.security.ClientRole
import org.openremote.model.value.MathExpressionValueFilter
import org.openremote.model.value.RegexValueFilter
import org.openremote.model.value.SubStringValueFilter
import org.openremote.model.value.ValueFilter
+import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@@ -37,8 +45,10 @@ import org.openremote.setup.integration.protocol.MockAgent
import org.openremote.setup.integration.protocol.MockAgentLink
import org.openremote.setup.integration.protocol.MockProtocol
+import java.util.UUID
import java.util.regex.Pattern
+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
import static org.openremote.model.Constants.MASTER_REALM
import static org.openremote.model.value.ValueType.*
import static org.openremote.model.value.MetaItemType.*
@@ -494,4 +504,193 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait {
assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.size() == 0
}
}
+
+ def "Protocol asset discovery requires asset write permission"() {
+
+ given: "the server container is started"
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.3, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def agentService = container.getService(AgentService.class)
+ MockAgent agent = null
+
+ and: "a master realm user with read-only asset permission"
+ def username = "agentdiscoveryreadonly-${UUID.randomUUID()}"
+ keycloakTestSetup.createUser(
+ MASTER_REALM,
+ username,
+ username,
+ "Agent Discovery",
+ "Read Only",
+ "${username}@openremote.local",
+ true,
+ [ClientRole.READ_ASSETS] as ClientRole[]
+ )
+ def accessToken = authenticate(container, MASTER_REALM, KEYCLOAK_CLIENT_ID, username, username)
+
+ and: "a running mock protocol agent"
+ agent = new MockAgent("Mock discovery agent")
+ .setRealm(MASTER_REALM)
+ .setRequired(true)
+ agent = assetStorageService.merge(agent)
+
+ conditions.eventually {
+ assert agentService.getProtocolInstance(agent.id) instanceof MockProtocol
+ }
+
+ and: "the agent resource"
+ def agentResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AgentResource.class)
+
+ when: "the read-only user discovers assets through the agent endpoint"
+ agentResource.doProtocolAssetDiscovery(null, agent.id, MASTER_REALM)
+
+ then: "asset discovery should require write permission"
+ ForbiddenException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+
+ and: "no discovered assets should be persisted"
+ conditions.eventually {
+ assert assetStorageService.findAll(new AssetQuery().parents(agent.id).recursive(true)).isEmpty()
+ }
+
+ cleanup: "remove agent and any discovered assets"
+ deleteAgentTree(assetStorageService, agent)
+ }
+
+ def "Protocol asset discovery allows asset write permission"() {
+
+ given: "the server container is started"
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.3, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def agentService = container.getService(AgentService.class)
+ MockAgent agent = null
+
+ and: "a master realm user with asset write permission"
+ def username = "agentdiscoverywriter-${UUID.randomUUID()}"
+ keycloakTestSetup.createUser(
+ MASTER_REALM,
+ username,
+ username,
+ "Agent Discovery",
+ "Writer",
+ "${username}@openremote.local",
+ true,
+ [ClientRole.WRITE_ASSETS] as ClientRole[]
+ )
+ def accessToken = authenticate(container, MASTER_REALM, KEYCLOAK_CLIENT_ID, username, username)
+
+ and: "a running mock protocol agent"
+ agent = new MockAgent("Mock discovery writer agent")
+ .setRealm(MASTER_REALM)
+ .setRequired(true)
+ agent = assetStorageService.merge(agent)
+
+ conditions.eventually {
+ assert agentService.getProtocolInstance(agent.id) instanceof MockProtocol
+ }
+
+ and: "the agent resource"
+ def agentResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AgentResource.class)
+
+ when: "the writer user discovers assets through the agent endpoint"
+ def assets = agentResource.doProtocolAssetDiscovery(null, agent.id, MASTER_REALM)
+
+ then: "discovered assets should be returned and persisted"
+ assert assets.length == 4
+ conditions.eventually {
+ assert assetStorageService.findAll(new AssetQuery().parents(agent.id).recursive(true)).size() == 4
+ }
+
+ cleanup: "remove agent and discovered assets"
+ deleteAgentTree(assetStorageService, agent)
+ }
+
+ def "Anonymous users cannot run protocol asset import"() {
+
+ given: "the server container is started"
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.3, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def agentService = container.getService(AgentService.class)
+ MockAgent agent = null
+
+ and: "a running mock protocol agent"
+ agent = new MockAgent("Mock import auth agent")
+ .setRealm(MASTER_REALM)
+ .setRequired(true)
+ agent = assetStorageService.merge(agent)
+
+ conditions.eventually {
+ assert agentService.getProtocolInstance(agent.id) instanceof MockProtocol
+ }
+
+ and: "an anonymous agent resource"
+ def agentResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM).proxy(AgentResource.class)
+
+ when: "an anonymous user imports assets through the agent endpoint"
+ agentResource.doProtocolAssetImport(null, agent.id, MASTER_REALM, new FileInfo("mock.txt", "ignored", false))
+
+ then: "anonymous asset import should be rejected"
+ WebApplicationException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+
+ cleanup: "remove agent and any imported assets"
+ deleteAgentTree(assetStorageService, agent)
+ }
+
+ def "Anonymous users cannot run protocol asset discovery"() {
+
+ given: "the server container is started"
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.3, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def agentService = container.getService(AgentService.class)
+ MockAgent agent = null
+
+ and: "a running mock protocol agent"
+ agent = new MockAgent("Mock discovery auth agent")
+ .setRealm(MASTER_REALM)
+ .setRequired(true)
+ agent = assetStorageService.merge(agent)
+
+ conditions.eventually {
+ assert agentService.getProtocolInstance(agent.id) instanceof MockProtocol
+ }
+
+ and: "an anonymous agent resource"
+ def agentResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM).proxy(AgentResource.class)
+
+ when: "an anonymous user discovers assets through the agent endpoint"
+ agentResource.doProtocolAssetDiscovery(null, agent.id, MASTER_REALM)
+
+ then: "anonymous asset discovery should be rejected"
+ WebApplicationException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+
+ cleanup: "remove agent and any discovered assets"
+ deleteAgentTree(assetStorageService, agent)
+ }
+
+ private void deleteAgentTree(AssetStorageService assetStorageService, MockAgent agent) {
+ if (assetStorageService == null || agent == null) {
+ return
+ }
+
+ def assetIds = assetStorageService.findAll(new AssetQuery().parents(agent.id).recursive(true))
+ .collect { it.id }
+ assetIds.add(agent.id)
+ assetStorageService.delete(assetIds, true)
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy
index 81beb4993..3f583eafd 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy
@@ -5,14 +5,17 @@ import org.openremote.agent.protocol.velbus.VelbusAgentLink
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.AssetTreeNode
import org.openremote.model.asset.agent.AgentResource
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.MetaItem
import org.openremote.model.file.FileInfo
+import org.openremote.model.security.ClientRole
import org.openremote.model.util.TextUtil
import org.openremote.model.util.ValueUtil
+import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.test.ManagerContainerTrait
import spock.lang.Shared
import spock.lang.Specification
@@ -269,4 +272,69 @@ class VelbusProtocolTest extends Specification implements ManagerContainerTrait
assetStorageService.delete([agent.id])
}
}
+
+ def "Protocol asset import requires asset write permission"() {
+
+ given: "the server container is started"
+ def conditions = new PollingConditions(timeout: 60, delay: 0.5)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def agentService = container.getService(AgentService.class)
+
+ and: "a VELBUS project file"
+ def velbusProjectFileResource = getClass().getResourceAsStream(
+ "/org/openremote/test/protocol/velbus/VelbusProject.vlp"
+ )
+ def velbusProjectFile = IOUtils.toString(velbusProjectFileResource, "UTF-8")
+
+ and: "a master realm user with read-only asset permission"
+ keycloakTestSetup.createUser(
+ MASTER_REALM,
+ "agentimportreadonly",
+ "agentimportreadonly",
+ "Agent Import",
+ "Read Only",
+ true,
+ [ClientRole.READ_ASSETS] as ClientRole[]
+ )
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ "agentimportreadonly",
+ "agentimportreadonly"
+ )
+
+ and: "the agent resource"
+ def agentResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AgentResource.class)
+
+ when: "a VELBUS agent is created"
+ def agent = new MockVelbusAgent("VELBUS")
+ .setRealm(MASTER_REALM)
+ agent = assetStorageService.merge(agent)
+
+ then: "the protocol instance for the agent should be created"
+ conditions.eventually {
+ assert agentService.getProtocolInstance(agent.id) != null
+ assert ((MockVelbusProtocol)agentService.getProtocolInstance(agent.id)).messageProcessor != null
+ }
+
+ when: "the read-only user imports assets through the agent endpoint"
+ def fileInfo = new FileInfo("VelbusProject.vlp", velbusProjectFile, false)
+ agentResource.doProtocolAssetImport(null, agent.getId(), MASTER_REALM, fileInfo)
+
+ then: "asset import should require write permission"
+ jakarta.ws.rs.ForbiddenException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+
+ cleanup: "remove the agent"
+ if (agent != null) {
+ assetStorageService.delete([agent.id])
+ }
+ }
}