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 Bariaux9fad55e5a448
diff --git a/manager/src/main/java/org/openremote/manager/alarm/AlarmResourceImpl.java b/manager/src/main/java/org/openremote/manager/alarm/AlarmResourceImpl.java
index 1d6dc26ba..e89d1dccb 100644
--- a/manager/src/main/java/org/openremote/manager/alarm/AlarmResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/alarm/AlarmResourceImpl.java
@@ -73,10 +73,12 @@ public class AlarmResourceImpl extends ManagerWebResource implements AlarmResour
@Override
public void removeAlarms(RequestParams requestParams, List<Long> alarmIds) {
try {
- if (!isRealmActiveAndAccessible(getAuthenticatedRealmName())) {
- throw new ForbiddenException("Realm '" + getAuthenticatedRealmName() + "' is nonexistent, inactive or inaccessible");
+ List<SentAlarm> alarms = alarmService.getAlarms(alarmIds);
+ for (SentAlarm alarm : alarms) {
+ if (!isRealmActiveAndAccessible(alarm.getRealm())) {
+ throw new ForbiddenException("Realm '" + alarm.getRealm() + "' is nonexistent, inactive or inaccessible");
+ }
}
- List<SentAlarm> alarms = alarmService.getAlarms(alarmIds);;
alarmService.removeAlarms(alarms, alarmIds);
} catch (EntityNotFoundException e) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
diff --git a/manager/src/main/java/org/openremote/manager/alarm/AlarmService.java b/manager/src/main/java/org/openremote/manager/alarm/AlarmService.java
index 358702b8d..be6222eb4 100644
--- a/manager/src/main/java/org/openremote/manager/alarm/AlarmService.java
+++ b/manager/src/main/java/org/openremote/manager/alarm/AlarmService.java
@@ -167,7 +167,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Sends an alarm if the given user has access to the alarm realm.
+ * Sends an alarm. Callers are responsible for enforcing realm authorization.
*/
public SentAlarm sendAlarm(Alarm alarm, List<String> assetIds) {
Objects.requireNonNull(alarm, "Alarm cannot be null");
@@ -303,7 +303,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Updates an existing alarm if the given user has access to the alarm realm.
+ * Updates an existing alarm. Callers are responsible for enforcing realm authorization.
*/
public void updateAlarm(SentAlarm oldAlarm, SentAlarm newAlarm) {
String oldAssigneeId = oldAlarm.getAssigneeId();
@@ -338,7 +338,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Links multiple assets to existing alarms if the given user has access to the alarm realms.
+ * Links multiple assets to existing alarms. Callers are responsible for enforcing realm authorization.
*/
public void linkAssets(List<AlarmAssetLink> links) {
Set<Long> alarmIds = links.stream().map(link -> link.getId().getAlarmId()).collect(Collectors.toSet());
@@ -364,7 +364,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Returns the assets linked to an alarm if the given user has access to the alarm realm.
+ * Returns the assets linked to an alarm. Callers are responsible for enforcing realm authorization.
*/
public List<AlarmAssetLink> getAssetLinks(Long alarmId, String realm) throws IllegalArgumentException {
return persistenceService.doReturningTransaction(entityManager ->
@@ -380,7 +380,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Retrieves the details of an existing alarm if the given user has access to the alarm realm.
+ * Retrieves the details of an existing alarm. Callers are responsible for enforcing realm authorization.
*/
public SentAlarm getAlarm(Long alarmId) throws IllegalArgumentException {
validateAlarmId(alarmId);
@@ -401,7 +401,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Retrieves the details of existing alarms if the given user has access to the alarm realms.
+ * Retrieves the details of existing alarms. Callers are responsible for enforcing realm authorization.
*/
public List<SentAlarm> getAlarms(List<Long> alarmIds) throws IllegalArgumentException {
validateAlarmIds(alarmIds);
@@ -457,7 +457,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Removes an existing alarm if the given user has access to the alarm realm.
+ * Removes an existing alarm. Callers are responsible for enforcing realm authorization.
*/
public void removeAlarm(SentAlarm alarm) {
persistenceService.doTransaction(entityManager -> entityManager
@@ -469,7 +469,7 @@ public class AlarmService extends RouteBuilder implements ContainerService {
}
/**
- * Removes existing alarms if the given user has access to the alarm realms.
+ * Removes existing alarms. Callers are responsible for enforcing realm authorization.
*/
public void removeAlarms(List<SentAlarm> alarms, List<Long> alarmIds) throws IllegalArgumentException {
persistenceService.doTransaction(entityManager ->
diff --git a/test/src/test/groovy/org/openremote/test/alarm/AlarmResourceTest.groovy b/test/src/test/groovy/org/openremote/test/alarm/AlarmResourceTest.groovy
index 3412a44ec..73b409bf6 100644
--- a/test/src/test/groovy/org/openremote/test/alarm/AlarmResourceTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/alarm/AlarmResourceTest.groovy
@@ -63,6 +63,9 @@ class AlarmResourceTest extends Specification implements ManagerContainerTrait {
@Shared
static AlarmResource regularUserResource
+ @Shared
+ static AlarmResource buildingUserResource
+
@Shared
static KeycloakTestSetup keycloakTestSetup
@@ -116,6 +119,16 @@ class AlarmResourceTest extends Specification implements ManagerContainerTrait {
superAdminResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, superAdminAccessToken).proxy(AlarmResource.class)
+ def buildingUserAccessToken = authenticate(
+ container,
+ keycloakTestSetup.realmBuilding.name,
+ KEYCLOAK_CLIENT_ID,
+ "testuser3",
+ "testuser3"
+ )
+
+ buildingUserResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.realmBuilding.name, buildingUserAccessToken).proxy(AlarmResource.class)
+
def emailNotificationHandler = container.getService(EmailNotificationHandler.class)
def throwPushHandlerException = false
EmailNotificationHandler mockPushNotificationHandler = Spy(emailNotificationHandler)
@@ -137,9 +150,11 @@ class AlarmResourceTest extends Specification implements ManagerContainerTrait {
def cleanup() {
// Remove all alarms
- def alarms = adminResource.getAlarms(null, MASTER_REALM, null, null, null)
- if (alarms.length > 0) {
- adminResource.removeAlarms(null, (List<Long>) alarms.collect { it.id })
+ [MASTER_REALM, keycloakTestSetup.realmBuilding.name].each { realm ->
+ def alarms = superAdminResource.getAlarms(null, realm, null, null, null)
+ if (alarms.length > 0) {
+ superAdminResource.removeAlarms(null, (List<Long>) alarms.collect { it.id })
+ }
}
// Clear notifications
@@ -417,6 +432,67 @@ class AlarmResourceTest extends Specification implements ManagerContainerTrait {
}
}
+ // Delete alarms from another realm as non-super-user
+ def "should not bulk delete alarms from another realm"() {
+ given:
+ def masterAlarm = adminResource.createAlarm(null, new Alarm("Master alarm", "Master content", Severity.MEDIUM, null, MASTER_REALM), null)
+
+ when:
+ buildingUserResource.removeAlarms(null, [masterAlarm.id])
+
+ then:
+ WebApplicationException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+
+ and:
+ adminResource.getAlarm(null, masterAlarm.id) != null
+
+ when:
+ buildingUserResource.getAlarm(null, masterAlarm.id)
+
+ then:
+ ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+
+ when:
+ buildingUserResource.removeAlarm(null, masterAlarm.id)
+
+ then:
+ ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+ }
+
+ // Delete alarms in a mixed-realm bulk request as non-super-user
+ def "should not bulk delete any alarms when one alarm belongs to another realm"() {
+ given:
+ def buildingRealm = keycloakTestSetup.realmBuilding.name
+ def buildingAlarm = buildingUserResource.createAlarm(null, new Alarm("Building alarm", "Building content", Severity.MEDIUM, null, buildingRealm), null)
+ def masterAlarm = adminResource.createAlarm(null, new Alarm("Master alarm", "Master content", Severity.MEDIUM, null, MASTER_REALM), null)
+
+ when:
+ buildingUserResource.removeAlarms(null, [buildingAlarm.id, masterAlarm.id])
+
+ then:
+ WebApplicationException ex = thrown()
+ ex.response.withCloseable { r ->
+ assert r.status == 403
+ return true
+ }
+
+ and:
+ buildingUserResource.getAlarm(null, buildingAlarm.id) != null
+ adminResource.getAlarm(null, masterAlarm.id) != null
+ }
+
// Delete empty or null alarms
def "should not delete null or empty alarms"() {
when: