Browse Source

Check entity group and owner tenant in edge msgs processing

pull/16453/head
Nikita Mazurenko 1 month ago
parent
commit
db90d151e9
  1. 12
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/BaseEdgeProcessor.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/AssetEdgeProcessor.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/dashboard/DashboardEdgeProcessor.java
  4. 2
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceEdgeProcessor.java
  5. 2
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/entityview/EntityViewEdgeProcessor.java
  6. 2
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/user/UserEdgeProcessor.java
  7. 5
      application/src/main/java/org/thingsboard/server/service/edge/rpc/sync/DefaultEdgeRequestsService.java
  8. 18
      application/src/test/java/org/thingsboard/server/edge/DeviceEdgeTest.java
  9. 79
      application/src/test/java/org/thingsboard/server/edge/EntityGroupEdgeTest.java

12
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/BaseEdgeProcessor.java

@ -503,6 +503,9 @@ public abstract class BaseEdgeProcessor implements EdgeProcessor {
protected void changeOwnerIfRequired(TenantId tenantId, CustomerId customerId, EntityId entityId) throws ThingsboardException {
EntityId newOwnerId = getOwnerId(tenantId, customerId);
if (!isEntityExists(tenantId, newOwnerId)) {
throw new DataValidationException("Owner " + newOwnerId + " belongs to a different tenant!");
}
EntityId currentOwnerId;
switch (entityId.getEntityType()) {
case DEVICE -> {
@ -564,12 +567,19 @@ public abstract class BaseEdgeProcessor implements EdgeProcessor {
protected void safeAddEntityToGroup(TenantId tenantId, EntityGroupId entityGroupId, EntityId entityId) {
if (entityGroupId != null && !ModelConstants.NULL_UUID.equals(entityGroupId.getId())) {
EntityGroup entityGroup = edgeCtx.getEntityGroupService().findEntityGroupById(tenantId, entityGroupId);
if (entityGroup != null) {
if (entityGroup != null && isEntityExists(tenantId, entityGroup.getOwnerId())) {
edgeCtx.getEntityGroupService().addEntityToEntityGroup(tenantId, entityGroupId, entityId);
}
}
}
protected void safeRemoveEntityFromGroup(TenantId tenantId, EntityGroupId entityGroupId, EntityId entityId) {
EntityGroup entityGroup = edgeCtx.getEntityGroupService().findEntityGroupById(tenantId, entityGroupId);
if (entityGroup != null && isEntityExists(tenantId, entityGroup.getOwnerId())) {
edgeCtx.getEntityGroupService().removeEntityFromEntityGroup(tenantId, entityGroupId, entityId);
}
}
protected boolean isSaveRequired(HasVersion current, HasVersion updated) {
if (current != null) {
current.setVersion(null);

2
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/AssetEdgeProcessor.java

@ -55,7 +55,7 @@ public class AssetEdgeProcessor extends BaseAssetProcessor implements AssetProce
if (assetUpdateMsg.hasEntityGroupIdMSB() && assetUpdateMsg.hasEntityGroupIdLSB()) {
EntityGroupId entityGroupId = new EntityGroupId(
new UUID(assetUpdateMsg.getEntityGroupIdMSB(), assetUpdateMsg.getEntityGroupIdLSB()));
edgeCtx.getEntityGroupService().removeEntityFromEntityGroup(tenantId, entityGroupId, assetId);
safeRemoveEntityFromGroup(tenantId, entityGroupId, assetId);
} else if (EdgeVersionComparator.INSTANCE.compare(edgeVersion, EdgeVersion.V_4_3_0) >= 0) {
deleteAsset(tenantId, edge, assetId);
} else {

2
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/dashboard/DashboardEdgeProcessor.java

@ -51,7 +51,7 @@ public class DashboardEdgeProcessor extends BaseDashboardProcessor implements Da
if (dashboardUpdateMsg.hasEntityGroupIdMSB() && dashboardUpdateMsg.hasEntityGroupIdLSB()) {
EntityGroupId entityGroupId = new EntityGroupId(
new UUID(dashboardUpdateMsg.getEntityGroupIdMSB(), dashboardUpdateMsg.getEntityGroupIdLSB()));
edgeCtx.getEntityGroupService().removeEntityFromEntityGroup(tenantId, entityGroupId, dashboardId);
safeRemoveEntityFromGroup(tenantId, entityGroupId, dashboardId);
} else if (EdgeVersionComparator.INSTANCE.compare(edgeVersion, EdgeVersion.V_4_3_0) >= 0) {
deleteDashboard(tenantId, edge, dashboardId);
} else {

2
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceEdgeProcessor.java

@ -76,7 +76,7 @@ public class DeviceEdgeProcessor extends BaseDeviceProcessor implements DevicePr
if (deviceUpdateMsg.hasEntityGroupIdMSB() && deviceUpdateMsg.hasEntityGroupIdLSB()) {
EntityGroupId entityGroupId = new EntityGroupId(
new UUID(deviceUpdateMsg.getEntityGroupIdMSB(), deviceUpdateMsg.getEntityGroupIdLSB()));
edgeCtx.getEntityGroupService().removeEntityFromEntityGroup(tenantId, entityGroupId, deviceId);
safeRemoveEntityFromGroup(tenantId, entityGroupId, deviceId);
} else if (EdgeVersionComparator.INSTANCE.compare(edgeVersion, EdgeVersion.V_4_3_0) >= 0) {
deleteDevice(tenantId, edge, deviceId);
} else {

2
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/entityview/EntityViewEdgeProcessor.java

@ -54,7 +54,7 @@ public class EntityViewEdgeProcessor extends BaseEntityViewProcessor implements
if (entityViewUpdateMsg.hasEntityGroupIdMSB() && entityViewUpdateMsg.hasEntityGroupIdLSB()) {
EntityGroupId entityGroupId = new EntityGroupId(
new UUID(entityViewUpdateMsg.getEntityGroupIdMSB(), entityViewUpdateMsg.getEntityGroupIdLSB()));
edgeCtx.getEntityGroupService().removeEntityFromEntityGroup(tenantId, entityGroupId, entityViewId);
safeRemoveEntityFromGroup(tenantId, entityGroupId, entityViewId);
} else if (EdgeVersionComparator.INSTANCE.compare(edgeVersion, EdgeVersion.V_4_3_0) >= 0) {
deleteEntityView(tenantId, edge, entityViewId);
} else {

2
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/user/UserEdgeProcessor.java

@ -63,7 +63,7 @@ public class UserEdgeProcessor extends BaseUserProcessor implements UserProcesso
if (userUpdateMsg.hasEntityGroupIdMSB() && userUpdateMsg.hasEntityGroupIdLSB()) {
EntityGroupId entityGroupId = new EntityGroupId(
new UUID(userUpdateMsg.getEntityGroupIdMSB(), userUpdateMsg.getEntityGroupIdLSB()));
edgeCtx.getEntityGroupService().removeEntityFromEntityGroup(tenantId, entityGroupId, userId);
safeRemoveEntityFromGroup(tenantId, entityGroupId, userId);
yield Futures.immediateFuture(null);
} else if (EdgeVersionComparator.INSTANCE.compare(edgeVersion, EdgeVersion.V_4_3_0) >= 0) {
deleteUserAndPushEntityDeletedEventToRuleEngine(tenantId, userId, edge);

5
application/src/main/java/org/thingsboard/server/service/edge/rpc/sync/DefaultEdgeRequestsService.java

@ -39,6 +39,7 @@ import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.edge.EdgeEvent;
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
import org.thingsboard.server.common.data.edge.EdgeEventType;
import org.thingsboard.server.common.data.group.EntityGroup;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.DeviceId;
@ -561,6 +562,10 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService {
log.trace("[{}] processEntityGroupEntitiesRequest [{}][{}]", tenantId, edge.getName(), entityGroupEntitiesRequestMsg);
if (entityGroupEntitiesRequestMsg.getEntityGroupIdMSB() != 0 && entityGroupEntitiesRequestMsg.getEntityGroupIdLSB() != 0) {
EntityGroupId entityGroupId = new EntityGroupId(new UUID(entityGroupEntitiesRequestMsg.getEntityGroupIdMSB(), entityGroupEntitiesRequestMsg.getEntityGroupIdLSB()));
EntityGroup entityGroup = entityGroupService.findEntityGroupById(tenantId, entityGroupId);
if (entityGroup == null || !isTenantEntity(tenantId, entityGroup.getOwnerId())) {
return Futures.immediateFuture(null);
}
ListenableFuture<List<EntityId>> entityIdsFuture;
try {
// TODO: refactor this to pagination

18
application/src/test/java/org/thingsboard/server/edge/DeviceEdgeTest.java

@ -20,6 +20,7 @@ import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.TestPropertySource;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.adaptor.JsonConverter;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceInfo;
@ -1136,4 +1137,21 @@ public class DeviceEdgeTest extends AbstractEdgeTest {
Assert.assertEquals(tenantId, doGet("/api/device/" + device.getUuidId(), Device.class).getTenantId());
}
@Test
public void testSendDeviceUpdateWithCustomerOfDifferentTenantToCloud() throws Exception {
loginDifferentTenant();
Customer customer = new Customer();
customer.setTitle("Different Tenant Customer");
Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
loginTenantAdmin();
Device device = saveDeviceOnCloudAndVerifyDeliveryToEdge();
Device deviceUpdate = new Device(device);
deviceUpdate.setCustomerId(savedCustomer.getId());
sendUplinkMsgAndWaitForResponse(uplinkMsgBuilder -> uplinkMsgBuilder.addDeviceUpdateMsg(
EdgeMsgConstructorUtils.constructDeviceUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, deviceUpdate, null)));
Assert.assertEquals(tenantId, doGet("/api/device/" + device.getUuidId(), Device.class).getOwnerId());
}
}

79
application/src/test/java/org/thingsboard/server/edge/EntityGroupEdgeTest.java

@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.TextNode;
import org.awaitility.Awaitility;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard;
@ -21,12 +22,15 @@ import org.thingsboard.server.common.data.group.EntityGroupInfo;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityGroupId;
import org.thingsboard.server.common.data.id.EntityViewId;
import org.thingsboard.server.common.data.id.RoleId;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.permission.GroupPermission;
import org.thingsboard.server.common.data.role.Role;
import org.thingsboard.server.common.data.role.RoleType;
import org.thingsboard.server.dao.group.EntityGroupService;
import org.thingsboard.server.dao.service.DaoSqlTest;
import org.thingsboard.server.gen.edge.v1.AssetUpdateMsg;
import org.thingsboard.server.gen.edge.v1.DashboardUpdateMsg;
@ -35,6 +39,7 @@ import org.thingsboard.server.gen.edge.v1.EntityGroupRequestMsg;
import org.thingsboard.server.gen.edge.v1.EntityViewUpdateMsg;
import org.thingsboard.server.gen.edge.v1.GroupPermissionProto;
import org.thingsboard.server.gen.edge.v1.UpdateMsgType;
import org.thingsboard.server.gen.edge.v1.UserUpdateMsg;
import org.thingsboard.server.gen.edge.v1.UplinkMsg;
import java.util.List;
@ -47,6 +52,9 @@ import static org.thingsboard.server.dao.customer.CustomerServiceImpl.PUBLIC_CUS
@DaoSqlTest
public class EntityGroupEdgeTest extends AbstractEdgeTest {
@Autowired
private EntityGroupService entityGroupService;
@Test
public void testAllEdgeEntityGroup_autoAssignedToEdgeAfterUnAssign() throws Exception {
sendAssetCreateMsgToCloud("Asset From Edge");
@ -149,6 +157,77 @@ public class EntityGroupEdgeTest extends AbstractEdgeTest {
}
}
@Test
public void testSendDeviceDeleteFromGroupOfDifferentTenantToCloud() throws Exception {
loginDifferentTenant();
Device device = new Device();
device.setName("Different Tenant Group Device");
device.setType("default");
Device savedDevice = doPost("/api/device", device, Device.class);
EntityGroup deviceGroup = saveDifferentTenantGroup(EntityType.DEVICE, savedDevice.getId());
loginTenantAdmin();
DeviceUpdateMsg deviceDeleteMsg = DeviceUpdateMsg.newBuilder()
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE)
.setIdMSB(savedDevice.getUuidId().getMostSignificantBits())
.setIdLSB(savedDevice.getUuidId().getLeastSignificantBits())
.setEntityGroupIdMSB(deviceGroup.getUuidId().getMostSignificantBits())
.setEntityGroupIdLSB(deviceGroup.getUuidId().getLeastSignificantBits())
.build();
sendUplinkMsgAndWaitForResponse(uplinkMsgBuilder -> uplinkMsgBuilder.addDeviceUpdateMsg(deviceDeleteMsg));
Assert.assertTrue(findGroupEntityIds(deviceGroup).contains(savedDevice.getId()));
}
@Test
public void testSendUserDeleteFromGroupOfDifferentTenantToCloud() throws Exception {
loginDifferentTenant();
EntityGroup userGroup = saveDifferentTenantGroup(EntityType.USER, savedDifferentTenantUser.getId());
loginTenantAdmin();
UserUpdateMsg userDeleteMsg = UserUpdateMsg.newBuilder()
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE)
.setIdMSB(savedDifferentTenantUser.getUuidId().getMostSignificantBits())
.setIdLSB(savedDifferentTenantUser.getUuidId().getLeastSignificantBits())
.setEntityGroupIdMSB(userGroup.getUuidId().getMostSignificantBits())
.setEntityGroupIdLSB(userGroup.getUuidId().getLeastSignificantBits())
.build();
sendUplinkMsgAndWaitForResponse(uplinkMsgBuilder -> uplinkMsgBuilder.addUserUpdateMsg(userDeleteMsg));
Assert.assertTrue(findGroupEntityIds(userGroup).contains(savedDifferentTenantUser.getId()));
}
@Test
public void testSendEntityGroupEntitiesRequestForGroupOfDifferentTenantToCloud() throws Exception {
loginDifferentTenant();
Device device = new Device();
device.setName("Different Tenant Requested Device");
device.setType("default");
Device savedDevice = doPost("/api/device", device, Device.class);
EntityGroup deviceGroup = saveDifferentTenantGroup(EntityType.DEVICE, savedDevice.getId());
loginTenantAdmin();
sendUplinkMsgAndVerifyNoDownlink(uplinkMsgBuilder -> uplinkMsgBuilder.addEntityGroupEntitiesRequestMsg(EntityGroupRequestMsg.newBuilder()
.setEntityGroupIdMSB(deviceGroup.getUuidId().getMostSignificantBits())
.setEntityGroupIdLSB(deviceGroup.getUuidId().getLeastSignificantBits())
.setType(EntityType.DEVICE.name())
.build()));
}
private EntityGroup saveDifferentTenantGroup(EntityType groupType, EntityId entityId) throws Exception {
EntityGroup entityGroup = new EntityGroup();
entityGroup.setType(groupType);
entityGroup.setName("Different Tenant " + groupType.name() + " Group");
entityGroup.setOwnerId(differentTenantId);
EntityGroup savedGroup = doPost("/api/entityGroup", entityGroup, EntityGroup.class);
addEntitiesToEntityGroup(List.of(entityId), savedGroup.getId());
return savedGroup;
}
private List<EntityId> findGroupEntityIds(EntityGroup entityGroup) throws Exception {
return entityGroupService.findAllEntityIdsAsync(differentTenantId, entityGroup.getId(), new PageLink(100)).get();
}
private EntityGroup createUserGroup() {
EntityGroup entityGroup = new EntityGroup();
entityGroup.setType(EntityType.USER);

Loading…
Cancel
Save