Browse Source

Answer entity group requests of other tenants as not found

(cherry picked from commit 7ccb975993bcc1e126d282df0152534f775f55c5)
pull/16844/head
Viacheslav Klimov 3 weeks ago
parent
commit
a7ac43daeb
Failed to extract signature
  1. 2
      application/src/main/java/org/thingsboard/server/controller/BaseController.java
  2. 15
      application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/security/AccessValidator.java
  4. 125
      application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java

2
application/src/main/java/org/thingsboard/server/controller/BaseController.java

@ -1244,7 +1244,7 @@ public abstract class BaseController {
protected EntityGroupInfo checkEntityGroupId(EntityGroupId entityGroupId, Operation operation) throws ThingsboardException { protected EntityGroupInfo checkEntityGroupId(EntityGroupId entityGroupId, Operation operation) throws ThingsboardException {
try { try {
validateId(entityGroupId, id -> "Incorrect entityGroupId " + id); validateId(entityGroupId, id -> "Incorrect entityGroupId " + id);
EntityGroupInfo entityGroup = entityGroupService.findEntityGroupInfoById(getTenantId(), entityGroupId); EntityGroupInfo entityGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), entityGroupId);
checkNotNull(entityGroup, "Entity group with id [" + entityGroupId + "] is not found"); checkNotNull(entityGroup, "Entity group with id [" + entityGroupId + "] is not found");
accessControlService.checkEntityGroupInfoPermission(getCurrentUser(), operation, entityGroup); accessControlService.checkEntityGroupInfoPermission(getCurrentUser(), operation, entityGroup);
return entityGroup; return entityGroup;

15
application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java

@ -964,7 +964,11 @@ public class EntityGroupController extends AutoCommitController {
for (String strEntityGroupId : strEntityGroupIds) { for (String strEntityGroupId : strEntityGroupIds) {
entityGroupIds.add(new EntityGroupId(toUUID(strEntityGroupId))); entityGroupIds.add(new EntityGroupId(toUUID(strEntityGroupId)));
} }
return checkNotNull(entityGroupService.findEntityGroupEntityInfosByIds(tenantId, entityGroupIds, new PageLink(Integer.MAX_VALUE)).getData()); List<EntityGroupInfo> entityGroups = checkNotNull(
entityGroupService.findEntityGroupInfosByIds(tenantId, entityGroupIds, new PageLink(Integer.MAX_VALUE)).getData());
return filterEntityGroupsByReadPermission(entityGroups).stream()
.map(entityGroup -> new EntityInfo(entityGroup.getId(), entityGroup.getName()))
.toList();
} }
@ApiOperation(value = "Get Owners (getOwners)", @ApiOperation(value = "Get Owners (getOwners)",
@ -1170,7 +1174,8 @@ public class EntityGroupController extends AutoCommitController {
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
} }
} else { } else {
userGroup = entityGroupService.findEntityGroupInfoById(getTenantId(), shareGroupRequest.getUserGroupId()); userGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), shareGroupRequest.getUserGroupId());
checkNotNull(userGroup, "Entity group with id [" + shareGroupRequest.getUserGroupId() + "] is not found");
} }
accessControlService.checkEntityGroupInfoPermission(getCurrentUser(), Operation.WRITE, userGroup); accessControlService.checkEntityGroupInfoPermission(getCurrentUser(), Operation.WRITE, userGroup);
@ -1243,7 +1248,7 @@ public class EntityGroupController extends AutoCommitController {
checkParameter("userGroupId", strUserGroupId); checkParameter("userGroupId", strUserGroupId);
checkParameter("roleId", strRoleId); checkParameter("roleId", strRoleId);
EntityGroupId userGroupId = new EntityGroupId(toUUID(strUserGroupId)); EntityGroupId userGroupId = new EntityGroupId(toUUID(strUserGroupId));
EntityGroup userGroup = entityGroupService.findEntityGroupById(getTenantId(), userGroupId); EntityGroup userGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), userGroupId);
if (userGroup == null) { if (userGroup == null) {
throw new ThingsboardException("User group with requested id: " + userGroupId + " wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); throw new ThingsboardException("User group with requested id: " + userGroupId + " wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
} }
@ -1296,7 +1301,7 @@ public class EntityGroupController extends AutoCommitController {
private EntityGroup resolveEntityGroup(UUID entityGroupUuid) throws ThingsboardException { private EntityGroup resolveEntityGroup(UUID entityGroupUuid) throws ThingsboardException {
EntityGroupId entityGroupId = new EntityGroupId(entityGroupUuid); EntityGroupId entityGroupId = new EntityGroupId(entityGroupUuid);
EntityGroup entityGroup = entityGroupService.findEntityGroupById(getTenantId(), entityGroupId); EntityGroup entityGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), entityGroupId);
if (entityGroup == null) { if (entityGroup == null) {
throw new ThingsboardException("Entity group with requested id: " + entityGroupId + " wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); throw new ThingsboardException("Entity group with requested id: " + entityGroupId + " wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
} }
@ -1314,7 +1319,7 @@ public class EntityGroupController extends AutoCommitController {
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
} }
} else { } else {
userGroup = entityGroupService.findEntityGroupInfoById(getTenantId(), shareGroupRequest.getUserGroupId()); userGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), shareGroupRequest.getUserGroupId());
} }
if (userGroup == null) { if (userGroup == null) {
throw new ThingsboardException("Specified user group wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); throw new ThingsboardException("Specified user group wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);

2
application/src/main/java/org/thingsboard/server/service/security/AccessValidator.java

@ -642,7 +642,7 @@ public class AccessValidator {
if (currentUser.isSystemAdmin()) { if (currentUser.isSystemAdmin()) {
callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION));
} else { } else {
ListenableFuture<EntityGroupInfo> entityGroupFuture = entityGroupService.findEntityGroupInfoByIdAsync(currentUser.getTenantId(), new EntityGroupId(entityId.getId())); ListenableFuture<EntityGroupInfo> entityGroupFuture = entityGroupService.findEntityGroupInfoByTenantIdAndIdAsync(currentUser.getTenantId(), new EntityGroupId(entityId.getId()));
Futures.addCallback(entityGroupFuture, getCallback(callback, entityGroup -> { Futures.addCallback(entityGroupFuture, getCallback(callback, entityGroup -> {
if (entityGroup == null) { if (entityGroup == null) {
return ValidationResult.entityNotFound("Entity group with requested id wasn't found!"); return ValidationResult.entityNotFound("Entity group with requested id wasn't found!");

125
application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java

@ -12,6 +12,7 @@ import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityInfo;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
@ -23,6 +24,7 @@ import org.thingsboard.server.common.data.id.IdBased;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.permission.GroupPermissionInfo; import org.thingsboard.server.common.data.permission.GroupPermissionInfo;
import org.thingsboard.server.common.data.permission.Operation;
import org.thingsboard.server.common.data.permission.ShareGroupRequest; import org.thingsboard.server.common.data.permission.ShareGroupRequest;
import org.thingsboard.server.common.data.role.Role; import org.thingsboard.server.common.data.role.Role;
import org.thingsboard.server.common.data.role.RoleType; import org.thingsboard.server.common.data.role.RoleType;
@ -38,6 +40,7 @@ import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@DaoSqlTest @DaoSqlTest
@ -167,11 +170,131 @@ public class EntityGroupControllerTest extends AbstractControllerTest {
loginCustomerAdminUser(); loginCustomerAdminUser();
otherTenantEntityGroup.setOwnerId(customerId); otherTenantEntityGroup.setOwnerId(customerId);
doPost("/api/entityGroup", otherTenantEntityGroup) doPost("/api/entityGroup", otherTenantEntityGroup)
.andExpect(status().isForbidden()); .andExpect(status().isNotFound());
assertThat(entityGroupService.findEntityGroupById(differentTenantId, otherTenantEntityGroup.getId()).getOwnerId()).isEqualTo(differentTenantId); assertThat(entityGroupService.findEntityGroupById(differentTenantId, otherTenantEntityGroup.getId()).getOwnerId()).isEqualTo(differentTenantId);
} }
@Test
public void testGetEntityGroupInfosByIdsReturnsOnlyOwnTenantGroups() throws Exception {
loginTenantAdmin();
EntityGroup ownGroup = new EntityGroup();
ownGroup.setName("Own Device Group");
ownGroup.setType(EntityType.DEVICE);
ownGroup = doPost("/api/entityGroup", ownGroup, EntityGroup.class);
loginDifferentTenant();
EntityGroup otherTenantGroup = new EntityGroup();
otherTenantGroup.setName("Other Tenant Device Group");
otherTenantGroup.setType(EntityType.DEVICE);
otherTenantGroup = doPost("/api/entityGroup", otherTenantGroup, EntityGroup.class);
loginTenantAdmin();
List<EntityInfo> entityInfos = doGetTyped("/api/entityGroupInfos?entityGroupIds=" + ownGroup.getId().getId() + "," + otherTenantGroup.getId().getId(),
new TypeReference<>() {});
assertThat(entityInfos).extracting(EntityInfo::getId).containsExactly(ownGroup.getId());
}
@Test
public void testGetEntityGroupInfosByIdsReturnsOnlyReadableGroups() throws Exception {
loginTenantAdmin();
EntityGroup tenantGroup = new EntityGroup();
tenantGroup.setName("Tenant Device Group");
tenantGroup.setType(EntityType.DEVICE);
tenantGroup = doPost("/api/entityGroup", tenantGroup, EntityGroup.class);
loginCustomerAdminUser();
EntityGroup customerGroup = new EntityGroup();
customerGroup.setName("Customer Device Group");
customerGroup.setType(EntityType.DEVICE);
customerGroup.setOwnerId(customerId);
customerGroup = doPost("/api/entityGroup", customerGroup, EntityGroup.class);
List<EntityInfo> entityInfos = doGetTyped("/api/entityGroupInfos?entityGroupIds=" + customerGroup.getId().getId() + "," + tenantGroup.getId().getId(),
new TypeReference<>() {});
assertThat(entityInfos).extracting(EntityInfo::getId).containsExactly(customerGroup.getId());
}
@Test
public void testShareGroupWithUserGroupOfOtherTenantReturnsNotFound() throws Exception {
loginDifferentTenant();
EntityGroup otherTenantUserGroup = new EntityGroup();
otherTenantUserGroup.setName("Other Tenant User Group");
otherTenantUserGroup.setType(EntityType.USER);
otherTenantUserGroup = doPost("/api/entityGroup", otherTenantUserGroup, EntityGroup.class);
loginTenantAdmin();
EntityGroup deviceGroup = new EntityGroup();
deviceGroup.setName("Shared Device Group");
deviceGroup.setType(EntityType.DEVICE);
deviceGroup = doPost("/api/entityGroup", deviceGroup, EntityGroup.class);
ShareGroupRequest shareGroupRequest = new ShareGroupRequest(tenantId, false, otherTenantUserGroup.getId(), true, null);
doPost("/api/entityGroup/" + deviceGroup.getId().getId() + "/share", shareGroupRequest)
.andExpect(status().isNotFound())
.andExpect(statusReason(not(containsString(otherTenantUserGroup.getName()))));
}
@Test
public void testShareGroupToChildOwnerUserGroupWithGroupsOfOtherTenantReturnsNotFound() throws Exception {
loginDifferentTenant();
EntityGroup otherTenantUserGroup = new EntityGroup();
otherTenantUserGroup.setName("Other Tenant User Group");
otherTenantUserGroup.setType(EntityType.USER);
otherTenantUserGroup = doPost("/api/entityGroup", otherTenantUserGroup, EntityGroup.class);
EntityGroup otherTenantDeviceGroup = new EntityGroup();
otherTenantDeviceGroup.setName("Other Tenant Device Group");
otherTenantDeviceGroup.setType(EntityType.DEVICE);
otherTenantDeviceGroup = doPost("/api/entityGroup", otherTenantDeviceGroup, EntityGroup.class);
loginTenantAdmin();
EntityGroup deviceGroup = new EntityGroup();
deviceGroup.setName("Shared Device Group");
deviceGroup.setType(EntityType.DEVICE);
deviceGroup = doPost("/api/entityGroup", deviceGroup, EntityGroup.class);
EntityGroup userGroup = new EntityGroup();
userGroup.setName("Own User Group");
userGroup.setType(EntityType.USER);
userGroup = doPost("/api/entityGroup", userGroup, EntityGroup.class);
Role role = new Role();
role.setName("Read Group Role");
role.setType(RoleType.GROUP);
role.setPermissions(JacksonUtil.valueToTree(List.of(Operation.READ)));
role = doPost("/api/role", role, Role.class);
doPost("/api/entityGroup/" + deviceGroup.getId().getId() + "/" + otherTenantUserGroup.getId().getId() + "/" + role.getId().getId() + "/share")
.andExpect(status().isNotFound());
doPost("/api/entityGroup/" + otherTenantDeviceGroup.getId().getId() + "/" + userGroup.getId().getId() + "/" + role.getId().getId() + "/share")
.andExpect(status().isNotFound());
}
@Test
public void testGetAttributesOfEntityGroupOfOtherTenantReturnsNotFound() throws Exception {
loginDifferentTenant();
EntityGroup otherTenantGroup = new EntityGroup();
otherTenantGroup.setName("Other Tenant Asset Group");
otherTenantGroup.setType(EntityType.ASSET);
otherTenantGroup = doPost("/api/entityGroup", otherTenantGroup, EntityGroup.class);
loginTenantAdmin();
doGetAsync("/api/plugins/telemetry/ENTITY_GROUP/" + otherTenantGroup.getId().getId() + "/values/attributes")
.andExpect(status().isNotFound());
}
@Test
public void testGetEntityGroupOfOtherTenantReturnsNotFound() throws Exception {
loginDifferentTenant();
EntityGroup otherTenantGroup = new EntityGroup();
otherTenantGroup.setName("Other Tenant Asset Group");
otherTenantGroup.setType(EntityType.ASSET);
otherTenantGroup = doPost("/api/entityGroup", otherTenantGroup, EntityGroup.class);
loginTenantAdmin();
doGet("/api/entityGroup/" + otherTenantGroup.getId().getId())
.andExpect(status().isNotFound())
.andExpect(statusReason(not(containsString(otherTenantGroup.getName()))));
}
@Test @Test
public void testShouldNotUpdateNonExistingEntityGroup() throws Exception { public void testShouldNotUpdateNonExistingEntityGroup() throws Exception {
loginTenantAdmin(); loginTenantAdmin();

Loading…
Cancel
Save