Skip to content

armgit / core

Public Imported from GitHub
0

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

Filter gateway tunnel list for restricted users

Ekhorn98d1d10e5687
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayServiceResourceImpl.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayServiceResourceImpl.java
index 014e1ec0c..fc44cbd26 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayServiceResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayServiceResourceImpl.java
@@ -21,6 +21,7 @@ package org.openremote.manager.gateway;
import jakarta.ws.rs.ForbiddenException;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
+import java.util.stream.Stream;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.security.ManagerIdentityService;
@@ -56,13 +57,18 @@ public class GatewayServiceResourceImpl extends ManagerWebResource
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
+ Stream<GatewayTunnelInfo> tunnels =
+ this.gatewayService.getTunnelInfos().stream()
+ .filter(tunnel -> tunnel.getRealm().equals(realm));
+
+ // A restricted user only sees tunnels for the gateways linked to them
if (isRestrictedUser()) {
- throw new WebApplicationException(Response.Status.FORBIDDEN);
+ String userId = getUserId();
+ tunnels =
+ tunnels.filter(tunnel -> assetStorageService.isUserAsset(userId, tunnel.getGatewayId()));
}
- return this.gatewayService.getTunnelInfos().stream()
- .filter(tunnel -> tunnel.getRealm().equals(realm))
- .toArray(GatewayTunnelInfo[]::new);
+ return tunnels.toArray(GatewayTunnelInfo[]::new);
}
@Override
diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTunnelAuthorizationTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTunnelAuthorizationTest.groovy
index 70e7e4ff8..57f6f01d4 100644
--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTunnelAuthorizationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTunnelAuthorizationTest.groovy
@@ -19,39 +19,133 @@
package org.openremote.test.gateway
import jakarta.ws.rs.WebApplicationException
+import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.gateway.GatewayService
+import org.openremote.manager.security.ManagerIdentityService
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider
import org.openremote.manager.setup.SetupService
+import org.openremote.model.asset.UserAssetLink
+import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.gateway.GatewayServiceResource
import org.openremote.model.gateway.GatewayTunnelInfo
+import org.openremote.model.security.User
import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.test.ManagerContainerTrait
import spock.lang.Shared
import spock.lang.Specification
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
+import static org.openremote.model.Constants.READ_TUNNELS_ROLE
+import static org.openremote.model.Constants.RESTRICTED_USER_REALM_ROLE
/**
* Covers the role gate on the gateway tunnel endpoints. Opening a tunnel puts the manager on a
* host inside the gateway's own network, so it has to require a role of its own rather than
* accepting any account that happens to be signed in.
+ *
+ * The restricted user cases use plain assets in place of gateway assets. The filter keys on the
+ * user asset link and not on the asset type, so this exercises the same query while leaving the
+ * gateway connector map alone.
*/
class GatewayTunnelAuthorizationTest extends Specification implements ManagerContainerTrait {
@Shared
static GatewayServiceResource noRoleTunnelResource
+ @Shared
+ static GatewayServiceResource restrictedTunnelResource
+
+ @Shared
+ static GatewayServiceResource tunnelReaderResource
+
+ @Shared
+ static GatewayService gatewayService
+
+ @Shared
+ static ManagerKeycloakIdentityProvider identityProvider
+
@Shared
static String buildingRealm
+ @Shared
+ static String linkedGatewayId
+
+ @Shared
+ static String unlinkedGatewayId
+
+ @Shared
+ static List<String> createdUserIds = []
+
def setupSpec() {
def container = startContainer(defaultConfig(), defaultServices())
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
buildingRealm = keycloakTestSetup.realmBuilding.name
+ gatewayService = container.getService(GatewayService.class)
+ identityProvider = container.getService(ManagerIdentityService.class).identityProvider as ManagerKeycloakIdentityProvider
// testuser2 holds write:user, read:map and read:assets and nothing else. It is the least
// privileged account the API will accept, and it is not a restricted user.
def accessToken = authenticate(container, buildingRealm, KEYCLOAK_CLIENT_ID, "testuser2", "testuser2")
noRoleTunnelResource = getClientApiTarget(serverUri(serverPort), buildingRealm, accessToken)
.proxy(GatewayServiceResource.class)
+
+ linkedGatewayId = assetStorageService.merge(
+ new ThingAsset("Linked gateway").setRealm(buildingRealm)).id
+ unlinkedGatewayId = assetStorageService.merge(
+ new ThingAsset("Unlinked gateway").setRealm(buildingRealm)).id
+
+ restrictedTunnelResource = tunnelResourceFor(
+ container, assetStorageService, "tunnelrestricted", true, linkedGatewayId)
+ tunnelReaderResource = tunnelResourceFor(
+ container, assetStorageService, "tunnelreader", false, null)
+
+ [linkedGatewayId, unlinkedGatewayId].each { gatewayId ->
+ def tunnel = new GatewayTunnelInfo(
+ buildingRealm, gatewayId, GatewayTunnelInfo.Type.HTTPS, "localhost", 443)
+ [email protected](tunnel.id, tunnel)
+ }
+ }
+
+ def cleanupSpec() {
+ if (gatewayService != null) {
+ [email protected]().removeIf { tunnel ->
+ tunnel.gatewayId == linkedGatewayId || tunnel.gatewayId == unlinkedGatewayId
+ }
+ }
+ if (identityProvider != null) {
+ createdUserIds.each { identityProvider.deleteUser(buildingRealm, it) }
+ createdUserIds.clear()
+ }
+ }
+
+ /** Creates a user holding read:tunnels and returns the tunnel resource authenticated as them. */
+ private GatewayServiceResource tunnelResourceFor(
+ container, AssetStorageService assetStorageService, String username, boolean restricted,
+ String linkedAssetId) {
+
+ def user = identityProvider.createUpdateUser(
+ buildingRealm, new User().setUsername(username).setEnabled(true), username, true)
+ createdUserIds << user.id
+
+ identityProvider.updateUserClientRoles(
+ buildingRealm, user.id, KEYCLOAK_CLIENT_ID, READ_TUNNELS_ROLE)
+
+ if (restricted) {
+ identityProvider.updateUserRealmRoles(
+ buildingRealm,
+ user.id,
+ identityProvider.addUserRealmRoles(buildingRealm, user.id, RESTRICTED_USER_REALM_ROLE))
+ }
+
+ if (linkedAssetId != null) {
+ assetStorageService.storeUserAssetLinks(
+ [new UserAssetLink(buildingRealm, user.id, linkedAssetId)])
+ }
+
+ def accessToken = authenticate(container, buildingRealm, KEYCLOAK_CLIENT_ID, username, username)
+ return getClientApiTarget(serverUri(serverPort), buildingRealm, accessToken)
+ .proxy(GatewayServiceResource.class)
}
def "A user holding no tunnel role cannot list a gateway's tunnels"() {
@@ -108,4 +202,23 @@ class GatewayTunnelAuthorizationTest extends Specification implements ManagerCon
WebApplicationException ex = thrown()
ex.response.status == 403
}
+
+ def "A restricted user sees only the tunnels of the gateways linked to them"() {
+
+ when: "a restricted user holding the tunnel read role lists every tunnel in the realm"
+ def tunnels = restrictedTunnelResource.getAllActiveTunnelInfos(null, buildingRealm)
+
+ then: "the realm's other tunnels are filtered out rather than the request being refused"
+ tunnels.length == 1
+ tunnels[0].gatewayId == linkedGatewayId
+ }
+
+ def "A user holding the tunnel read role sees the whole realm"() {
+
+ when: "an unrestricted user holding the tunnel read role lists every tunnel in the realm"
+ def tunnels = tunnelReaderResource.getAllActiveTunnelInfos(null, buildingRealm)
+
+ then: "tunnels for gateways they are not linked to are returned too"
+ tunnels.collect { it.gatewayId }.containsAll([linkedGatewayId, unlinkedGatewayId])
+ }
}