Skip to content

armgit / core

Public Imported from GitHub
0

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

Add happy flow tests

Ekhorn732b67f3275f
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 ac68a7e6f..489f0af43 100644
--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTunnelAuthorizationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTunnelAuthorizationTest.groovy
@@ -18,207 +18,429 @@
*/
package org.openremote.test.gateway
+import io.netty.channel.ChannelHandler
import jakarta.ws.rs.WebApplicationException
+import org.apache.http.client.utils.URIBuilder
+import org.openremote.agent.protocol.io.AbstractNettyIOClient
+import org.openremote.agent.protocol.websocket.WebsocketIOClient
import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.gateway.GatewayConnector
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.manager.system.VersionInfo
+import org.openremote.model.asset.AssetsEvent
import org.openremote.model.asset.UserAssetLink
-import org.openremote.model.asset.impl.ThingAsset
+import org.openremote.model.asset.impl.GatewayAsset
+import org.openremote.model.auth.OAuthClientCredentialsGrant
+import org.openremote.model.event.shared.SharedEvent
+import org.openremote.model.gateway.GatewayCapabilitiesRequestEvent
+import org.openremote.model.gateway.GatewayCapabilitiesResponseEvent
+import org.openremote.model.gateway.GatewayInitStartEvent
import org.openremote.model.gateway.GatewayServiceResource
import org.openremote.model.gateway.GatewayTunnelInfo
+import org.openremote.model.gateway.GatewayTunnelStartRequestEvent
+import org.openremote.model.gateway.GatewayTunnelStartResponseEvent
+import org.openremote.model.gateway.GatewayTunnelStopRequestEvent
+import org.openremote.model.gateway.GatewayTunnelStopResponseEvent
+import org.openremote.model.security.ClientRole
import org.openremote.model.security.User
+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
+import spock.util.concurrent.PollingConditions
-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
+import java.util.concurrent.CopyOnWriteArrayList
+
+import static org.openremote.manager.security.ManagerIdentityProvider.OR_ADMIN_PASSWORD
+import static org.openremote.manager.security.ManagerIdentityProvider.OR_ADMIN_PASSWORD_DEFAULT
+import static org.openremote.model.Constants.*
+import static org.openremote.model.util.MapAccess.getString
/**
- * 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.
+ * Covers the role gate on the gateway tunnel endpoints. A tunnel puts the manager on a host inside
+ * the gateway's own network, so every tunnel endpoint requires a tunnel role, and the admin roles do
+ * not confer one.
*
- * 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.
+ * <p>Reads run against tunnels placed directly in the gateway service. Writes run against a gateway
+ * simulated on the websocket that accepts every tunnel request, so a permitted write opens and closes
+ * a real tunnel.
*/
class GatewayTunnelAuthorizationTest extends Specification implements ManagerContainerTrait {
- @Shared
- static GatewayServiceResource noRoleTunnelResource
+ private static final String OTHER_REALM_GATEWAY_ID = "otherRealmGateway"
@Shared
- static GatewayServiceResource restrictedTunnelResource
+ static Map<String, GatewayServiceResource> resources = [:]
@Shared
- static GatewayServiceResource tunnelReaderResource
+ static GatewayService gatewayService
@Shared
- static GatewayService gatewayService
+ static AssetStorageService assetStorageService
@Shared
static ManagerKeycloakIdentityProvider identityProvider
@Shared
- static String buildingRealm
+ static String realm
+
+ @Shared
+ static String otherRealm
+
+ /** Connected through the simulated gateway */
+ @Shared
+ static GatewayAsset gateway
+
+ /** Never connected */
+ @Shared
+ static GatewayAsset otherGateway
+
+ @Shared
+ static WebsocketIOClient gatewayClient
+
+ /** Tunnels the gateway was asked to open */
+ @Shared
+ static List<GatewayTunnelInfo> openRequests = new CopyOnWriteArrayList<>()
+
+ /** Tunnels the gateway was asked to close */
+ @Shared
+ static List<GatewayTunnelInfo> closeRequests = new CopyOnWriteArrayList<>()
@Shared
- static String linkedGatewayId
+ static GatewayTunnelInfo httpsTunnel
@Shared
- static String unlinkedGatewayId
+ static GatewayTunnelInfo sshTunnel
+
+ @Shared
+ static GatewayTunnelInfo otherGatewayTunnel
+
+ @Shared
+ static GatewayTunnelInfo otherRealmTunnel
+
+ @Shared
+ static String originalSSHHostname
+
+ @Shared
+ static int originalSSHPort
@Shared
static List<String> createdUserIds = []
def setupSpec() {
- def container = startContainer(defaultConfig(), defaultServices())
+ startContainer(defaultConfig(), defaultServices())
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
- def assetStorageService = container.getService(AssetStorageService.class)
- buildingRealm = keycloakTestSetup.realmBuilding.name
+ realm = keycloakTestSetup.realmBuilding.name
+ otherRealm = keycloakTestSetup.realmCity.name
gatewayService = container.getService(GatewayService.class)
+ assetStorageService = container.getService(AssetStorageService.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)
+ // Tunnelling is not configured on the test container
+ originalSSHHostname = gatewayService.@tunnelSSHHostname
+ originalSSHPort = gatewayService.@tunnelSSHPort
+ gatewayService.@tunnelSSHHostname = "localhost"
+ gatewayService.@tunnelSSHPort = 2222
+
+ gateway = provisionGateway("Connected gateway")
+ otherGateway = provisionGateway("Other gateway")
+ gatewayClient = connectSimulatedGateway(gateway)
+
+ httpsTunnel = new GatewayTunnelInfo(realm, gateway.id, GatewayTunnelInfo.Type.HTTPS, "localhost", 443)
+ sshTunnel = new GatewayTunnelInfo(realm, gateway.id, GatewayTunnelInfo.Type.TCP, "10.0.0.1", 22)
+ otherGatewayTunnel = new GatewayTunnelInfo(realm, otherGateway.id, GatewayTunnelInfo.Type.TCP, "10.0.0.1", 22)
+ otherRealmTunnel = new GatewayTunnelInfo(otherRealm, OTHER_REALM_GATEWAY_ID, GatewayTunnelInfo.Type.TCP, "10.0.0.1", 22)
+
+ // testuser2 holds write:user, read:map and read:assets, and is not restricted
+ resources.noTunnelRoles = resourceFor(realm, "testuser2", "testuser2")
+ resources.adminWithoutTunnelRoles = resourceForNewUser("tunneladmin", [READ_ADMIN_ROLE, WRITE_ADMIN_ROLE])
+ resources.tunnelReader = resourceForNewUser("tunnelreader", [READ_TUNNELS_ROLE])
+ resources.tunnelWriter = resourceForNewUser("tunnelwriter", [READ_TUNNELS_ROLE, WRITE_TUNNELS_ROLE])
+ resources.readWriteComposites = resourceForNewUser("tunnelcomposites", [ClientRole.READ.value, ClientRole.WRITE.value])
+ resources.restrictedLinked = resourceForNewUser("tunnelrestrictedlinked", [READ_TUNNELS_ROLE, WRITE_TUNNELS_ROLE], true, gateway.id)
+ resources.restrictedUnlinked = resourceForNewUser("tunnelrestrictedunlinked", [READ_TUNNELS_ROLE, WRITE_TUNNELS_ROLE], true)
+ resources.superUser = resourceFor(MASTER_REALM, MASTER_REALM_ADMIN_USER, getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT))
+ }
+
+ def setup() {
+ // Every feature starts from the same active tunnels
+ removeTunnels()
+ [httpsTunnel, sshTunnel, otherGatewayTunnel, otherRealmTunnel].each {
+ [email protected](it.id, it)
}
+ openRequests.clear()
+ closeRequests.clear()
}
def cleanupSpec() {
+ gatewayClient?.disconnect()
+ gatewayClient?.removeAllMessageConsumers()
if (gatewayService != null) {
- [email protected]().removeIf { tunnel ->
- tunnel.gatewayId == linkedGatewayId || tunnel.gatewayId == unlinkedGatewayId
- }
+ removeTunnels()
+ gatewayService.@tunnelSSHHostname = originalSSHHostname
+ gatewayService.@tunnelSSHPort = originalSSHPort
}
- if (identityProvider != null) {
- createdUserIds.each { identityProvider.deleteUser(buildingRealm, it) }
- createdUserIds.clear()
+ def gatewayIds = [gateway, otherGateway].findAll()*.id
+ if (!gatewayIds.isEmpty()) {
+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+ assetStorageService.delete(gatewayIds)
+ conditions.eventually {
+ assert gatewayIds.every {
+ !gatewayService.gatewayConnectorMap.containsKey(it.toLowerCase(Locale.ROOT))
+ }
+ }
}
+ createdUserIds.each { identityProvider.deleteUser(realm, it) }
+ createdUserIds.clear()
+ resources.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) {
+ private void removeTunnels() {
+ def gatewayIds = [gateway?.id, otherGateway?.id, OTHER_REALM_GATEWAY_ID]
+ [email protected]().removeIf { it.gatewayId in gatewayIds }
+ }
- def user = identityProvider.createUpdateUser(
- buildingRealm, new User().setUsername(username).setEnabled(true), username, true)
- createdUserIds << user.id
+ private GatewayServiceResource resourceFor(String userRealm, String username, String password) {
+ def accessToken = authenticate(container, userRealm, KEYCLOAK_CLIENT_ID, username, password)
+ return getClientApiTarget(serverUri(serverPort), userRealm, accessToken).proxy(GatewayServiceResource.class)
+ }
- identityProvider.updateUserClientRoles(
- buildingRealm, user.id, KEYCLOAK_CLIENT_ID, READ_TUNNELS_ROLE)
+ /** Provisions a user in the realm and returns the tunnel resource authenticated as them. */
+ private GatewayServiceResource resourceForNewUser(String username, List<String> roles, boolean restricted = false, String linkedAssetId = null) {
+ def user = identityProvider.createUpdateUser(realm, new User().setUsername(username).setEnabled(true), username, true)
+ createdUserIds << user.id
+ identityProvider.updateUserClientRoles(realm, user.id, KEYCLOAK_CLIENT_ID, roles as String[])
if (restricted) {
identityProvider.updateUserRealmRoles(
- buildingRealm,
+ realm,
user.id,
- identityProvider.addUserRealmRoles(buildingRealm, user.id, RESTRICTED_USER_REALM_ROLE))
+ identityProvider.addUserRealmRoles(realm, user.id, RESTRICTED_USER_REALM_ROLE))
}
if (linkedAssetId != null) {
- assetStorageService.storeUserAssetLinks(
- [new UserAssetLink(buildingRealm, user.id, linkedAssetId)])
+ assetStorageService.storeUserAssetLinks([new UserAssetLink(realm, user.id, linkedAssetId)])
}
- def accessToken = authenticate(container, buildingRealm, KEYCLOAK_CLIENT_ID, username, username)
- return getClientApiTarget(serverUri(serverPort), buildingRealm, accessToken)
- .proxy(GatewayServiceResource.class)
+ return resourceFor(realm, username, username)
}
- def "A user holding no tunnel role cannot list a gateway's tunnels"() {
+ private GatewayAsset provisionGateway(String name) {
+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+ def asset = assetStorageService.merge(new GatewayAsset(name).setRealm(realm))
+ conditions.eventually {
+ asset = assetStorageService.find(asset.id, true) as GatewayAsset
+ assert asset.clientId.present
+ assert asset.clientSecret.present
+ }
+ return asset
+ }
- when: "a user with no administrative role lists the tunnels of an arbitrary gateway"
- noRoleTunnelResource.getGatewayActiveTunnelInfos(null, buildingRealm, "somegatewayid")
+ /** Connects as the gateway, which then accepts everything the manager asks of it. */
+ private WebsocketIOClient connectSimulatedGateway(GatewayAsset asset) {
+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+ def tokenUri = new URIBuilder(identityProvider.keycloakPublicUrl)
+ .setPath("auth/realms/${asset.realm}/protocol/openid-connect/token").build().toString()
+ def client = new WebsocketIOClient(
+ new URIBuilder("ws://127.0.0.1:$serverPort/websocket/events?Realm=${asset.realm}").build(),
+ null,
+ new OAuthClientCredentialsGrant(tokenUri, asset.clientId.get(), asset.clientSecret.get(), null)
+ .setBasicAuthHeader(true))
+ client.setEncoderDecoderProvider({
+ [new AbstractNettyIOClient.MessageToMessageDecoder<String>(String.class, client)].toArray(new ChannelHandler[0])
+ })
+ client.addMessageConsumer({ String message ->
+ if (message.startsWith(SharedEvent.MESSAGE_PREFIX)) {
+ def response = responseTo(ValueUtil.JSON.readValue(message.substring(SharedEvent.MESSAGE_PREFIX.length()), SharedEvent))
+ if (response != null) {
+ client.sendMessage(SharedEvent.MESSAGE_PREFIX + ValueUtil.asJSON(response).get())
+ }
+ }
+ })
+ client.connect()
+
+ conditions.eventually {
+ def connector = gatewayService.gatewayConnectorMap.get(asset.id.toLowerCase(Locale.ROOT))
+ assert connector != null
+ assert connector.isConnected()
+ assert !connector.isInitialSyncInProgress()
+ assert connector.isTunnellingSupported()
+ }
+ return client
+ }
- then: "the request is refused"
- WebApplicationException ex = thrown()
- ex.response.status == 403
+ private static SharedEvent responseTo(SharedEvent request) {
+ SharedEvent response
+ String messageId = request.messageID
+ if (request instanceof GatewayInitStartEvent) {
+ // The gateway has no assets of its own to synchronise
+ response = new AssetsEvent([])
+ messageId = GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL
+ } else if (request instanceof GatewayCapabilitiesRequestEvent) {
+ response = new GatewayCapabilitiesResponseEvent(VersionInfo.getGatewayApiVersion(), true)
+ } else if (request instanceof GatewayTunnelStartRequestEvent) {
+ openRequests << request.info
+ response = new GatewayTunnelStartResponseEvent(null)
+ } else if (request instanceof GatewayTunnelStopRequestEvent) {
+ closeRequests << request.info
+ response = new GatewayTunnelStopResponseEvent(null)
+ } else {
+ return null
+ }
+ response.messageID = messageId
+ return response
}
- def "A user holding no tunnel role cannot read a single tunnel"() {
+ /** A tunnel to a host on the gateway's network that is not already active. */
+ private GatewayTunnelInfo newTunnel(String gatewayId) {
+ return new GatewayTunnelInfo(realm, gatewayId, GatewayTunnelInfo.Type.TCP, "10.0.0.2", 5432)
+ }
- when: "a user with no administrative role reads one tunnel by endpoint"
- noRoleTunnelResource.getActiveTunnelInfo(null, buildingRealm, "somegatewayid", "10.0.0.1", 22)
+ private static Set<String> ids(GatewayTunnelInfo... tunnels) {
+ return tunnels.collect { it.id } as Set
+ }
- then: "the request is refused"
- WebApplicationException ex = thrown()
- ex.response.status == 403
+ /** The status the call was refused with, or null if it succeeded. */
+ private static Integer refusalOf(Closure<?> call) {
+ try {
+ call.call()
+ return null
+ } catch (WebApplicationException e) {
+ return e.response.status
+ }
}
- def "A user holding no tunnel role cannot start a tunnel"() {
+ def "#user is refused every tunnel endpoint"() {
- when: "a user with no administrative role opens a tunnel to a host on the gateway's network"
- noRoleTunnelResource.startTunnel(new GatewayTunnelInfo(
- buildingRealm,
- "somegatewayid",
- GatewayTunnelInfo.Type.TCP,
- "169.254.169.254",
- 80))
+ expect: "every tunnel endpoint is refused without a tunnel role, admin roles included"
+ refusalOf { resource.getAllActiveTunnelInfos(null, realm) } == 403
+ refusalOf { resource.getGatewayActiveTunnelInfos(null, realm, gateway.id) } == 403
+ refusalOf { resource.getActiveTunnelInfo(null, realm, gateway.id, "10.0.0.1", 22) } == 403
+ refusalOf { resource.startTunnel(newTunnel(gateway.id)) } == 403
+ refusalOf { resource.stopTunnel(sshTunnel) } == 403
- then: "the request is refused before the gateway is ever consulted"
- WebApplicationException ex = thrown()
- ex.response.status == 403
+ and: "the gateway was never asked to open or close a tunnel"
+ openRequests.isEmpty()
+ closeRequests.isEmpty()
+
+ where:
+ user << ["noTunnelRoles", "adminWithoutTunnelRoles"]
+ resource = resources[user]
}
- def "A user holding no tunnel role cannot stop a tunnel"() {
+ def "#user retrieves active tunnels by realm, gateway and endpoint"() {
+
+ expect: "the realm's tunnels are retrieved without those of other realms"
+ ids(resource.getAllActiveTunnelInfos(null, realm)) == ids(httpsTunnel, sshTunnel, otherGatewayTunnel)
- when: "a user with no administrative role stops a tunnel"
- noRoleTunnelResource.stopTunnel(new GatewayTunnelInfo(buildingRealm, "somegatewayid"))
+ and: "each gateway's tunnels are retrieved without those of other gateways"
+ ids(resource.getGatewayActiveTunnelInfos(null, realm, gateway.id)) == ids(httpsTunnel, sshTunnel)
+ ids(resource.getGatewayActiveTunnelInfos(null, realm, otherGateway.id)) == ids(otherGatewayTunnel)
- then: "the request is refused"
- WebApplicationException ex = thrown()
- ex.response.status == 403
+ and: "a tunnel is retrieved by gateway and endpoint, and an endpoint without one returns nothing"
+ resource.getActiveTunnelInfo(null, realm, gateway.id, "10.0.0.1", 22).id == sshTunnel.id
+ resource.getActiveTunnelInfo(null, realm, otherGateway.id, "10.0.0.1", 22).id == otherGatewayTunnel.id
+ resource.getActiveTunnelInfo(null, realm, gateway.id, "10.0.0.1", 23) == null
+
+ where:
+ user << ["tunnelReader", "readWriteComposites", "superUser"]
+ resource = resources[user]
}
- def "A user holding no tunnel role cannot list a realm's tunnels"() {
+ def "A user holding only the tunnel read role cannot open or close tunnels"() {
+
+ given: "a user holding only the tunnel read role"
+ def resource = resources.tunnelReader
- when: "a user with no administrative role lists every tunnel in the realm"
- noRoleTunnelResource.getAllActiveTunnelInfos(null, buildingRealm)
+ expect: "both writes are refused"
+ refusalOf { resource.startTunnel(newTunnel(gateway.id)) } == 403
+ refusalOf { resource.stopTunnel(sshTunnel) } == 403
- then: "the request is refused"
- WebApplicationException ex = thrown()
- ex.response.status == 403
+ and: "the gateway was never asked to open or close a tunnel"
+ openRequests.isEmpty()
+ closeRequests.isEmpty()
}
- def "A restricted user sees only the tunnels of the gateways linked to them"() {
+ def "A restricted user retrieves and reaches only the gateway linked to them"() {
+
+ given: "a restricted user holding both tunnel roles and linked to the connected gateway"
+ def resource = resources.restrictedLinked
+
+ expect: "the realm's tunnels are filtered down to the linked gateway's"
+ ids(resource.getAllActiveTunnelInfos(null, realm)) == ids(httpsTunnel, sshTunnel)
- when: "a restricted user holding the tunnel read role lists every tunnel in the realm"
- def tunnels = restrictedTunnelResource.getAllActiveTunnelInfos(null, buildingRealm)
+ and: "the linked gateway's tunnels are retrieved"
+ ids(resource.getGatewayActiveTunnelInfos(null, realm, gateway.id)) == ids(httpsTunnel, sshTunnel)
+ resource.getActiveTunnelInfo(null, realm, gateway.id, "10.0.0.1", 22).id == sshTunnel.id
- then: "the realm's other tunnels are filtered out rather than the request being refused"
- tunnels.length == 1
- tunnels[0].gatewayId == linkedGatewayId
+ and: "a gateway in the same realm that is not linked to them is refused"
+ refusalOf { resource.getGatewayActiveTunnelInfos(null, realm, otherGateway.id) } == 403
+ refusalOf { resource.getActiveTunnelInfo(null, realm, otherGateway.id, "10.0.0.1", 22) } == 403
+ refusalOf { resource.startTunnel(newTunnel(otherGateway.id)) } == 403
+ refusalOf { resource.stopTunnel(otherGatewayTunnel) } == 403
}
- def "A user holding the tunnel read role sees the whole realm"() {
+ def "A restricted user linked to no gateway retrieves no tunnels and reaches no gateway"() {
+
+ given: "a restricted user holding both tunnel roles and linked to nothing"
+ def resource = resources.restrictedUnlinked
+
+ expect: "the realm's tunnels are filtered down to none"
+ resource.getAllActiveTunnelInfos(null, realm).length == 0
+
+ and: "every gateway is refused"
+ refusalOf { resource.getGatewayActiveTunnelInfos(null, realm, gateway.id) } == 403
+ refusalOf { resource.getActiveTunnelInfo(null, realm, gateway.id, "10.0.0.1", 22) } == 403
+ refusalOf { resource.startTunnel(newTunnel(gateway.id)) } == 403
+ refusalOf { resource.stopTunnel(sshTunnel) } == 403
+
+ and: "the gateway was never asked to open or close a tunnel"
+ openRequests.isEmpty()
+ closeRequests.isEmpty()
+ }
+
+ def "A tunnel role does not reach another realm"() {
+
+ given: "a user holding both tunnel roles"
+ def resource = resources.tunnelWriter
+
+ expect: "a super user retrieves the other realm's tunnel"
+ ids(resources.superUser.getAllActiveTunnelInfos(null, otherRealm)) == ids(otherRealmTunnel)
+
+ and: "the user is refused every request naming the other realm"
+ refusalOf { resource.getAllActiveTunnelInfos(null, otherRealm) } == 403
+ refusalOf { resource.getGatewayActiveTunnelInfos(null, otherRealm, gateway.id) } == 403
+ refusalOf { resource.getActiveTunnelInfo(null, otherRealm, gateway.id, "10.0.0.1", 22) } == 403
+ refusalOf { resource.startTunnel(newTunnel(gateway.id).setRealm(otherRealm)) } == 403
+ refusalOf { resource.stopTunnel(otherRealmTunnel) } == 403
+ }
+
+ def "#user opens and closes a tunnel through the gateway"() {
+
+ given: "a tunnel to a host on the gateway's network"
+ def tunnel = newTunnel(gateway.id)
+
+ when: "the user opens it"
+ def opened = resource.startTunnel(tunnel)
+
+ then: "the gateway was asked to open it and it is active"
+ opened == tunnel
+ openRequests == [tunnel]
+ ids(resource.getGatewayActiveTunnelInfos(null, realm, gateway.id)) == ids(httpsTunnel, sshTunnel, tunnel)
+
+ when: "the user closes it"
+ resource.stopTunnel(opened)
- when: "an unrestricted user holding the tunnel read role lists every tunnel in the realm"
- def tunnels = tunnelReaderResource.getAllActiveTunnelInfos(null, buildingRealm)
+ then: "the gateway was asked to close it and it is no longer active"
+ closeRequests == [tunnel]
+ ids(resource.getGatewayActiveTunnelInfos(null, realm, gateway.id)) == ids(httpsTunnel, sshTunnel)
- then: "tunnels for gateways they are not linked to are returned too"
- tunnels.collect { it.gatewayId }.containsAll([linkedGatewayId, unlinkedGatewayId])
+ where:
+ user << ["tunnelWriter", "readWriteComposites", "restrictedLinked", "superUser"]
+ resource = resources[user]
}
}