Browse Source

Check entity group edge assignment and write permission

pull/16453/head
Nikita Mazurenko 1 month ago
parent
commit
fe97edcc11
  1. 2
      application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java
  2. 89
      application/src/main/java/org/thingsboard/server/service/edge/rpc/sync/DefaultEdgeRequestsService.java
  3. 36
      application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java
  4. 15
      application/src/test/java/org/thingsboard/server/edge/EntityGroupEdgeTest.java

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

@ -1389,7 +1389,7 @@ public class EntityGroupController extends AutoCommitController {
Edge edge = checkEdgeId(edgeId, Operation.WRITE);
EntityGroupId entityGroupId = new EntityGroupId(toUUID(strEntityGroupId));
checkEntityGroupId(entityGroupId, Operation.READ);
checkEntityGroupId(entityGroupId, Operation.WRITE);
EntityType groupType = checkStrEntityGroupType("groupType", strGroupType);
EntityGroup savedEntityGroup = checkNotNull(entityGroupService.assignEntityGroupToEdge(getCurrentUser().getTenantId(), entityGroupId, edgeId, groupType));

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

@ -26,6 +26,7 @@ import org.thingsboard.rule.engine.api.SmsService;
import org.thingsboard.rule.engine.api.notification.FirebaseService;
import org.thingsboard.rule.engine.api.notification.SlackService;
import org.thingsboard.server.common.data.AttributeScope;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.DashboardInfo;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device;
@ -41,6 +42,7 @@ 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.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EdgeId;
@ -566,34 +568,79 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService {
if (entityGroup == null || !isTenantEntity(tenantId, entityGroup.getOwnerId())) {
return Futures.immediateFuture(null);
}
ListenableFuture<List<EntityId>> entityIdsFuture;
try {
// TODO: refactor this to pagination
entityIdsFuture = entityGroupService.findAllEntityIdsAsync(edge.getTenantId(), entityGroupId, new PageLink(Integer.MAX_VALUE));
} catch (IncorrectParameterException e) {
log.warn("[{}] Entity group not found to process entityGroupEntitiesRequestMsg {}", tenantId, entityGroupEntitiesRequestMsg, e);
return Futures.immediateFuture(null);
}
return Futures.transformAsync(entityIdsFuture, entityIds -> {
if (entityIds != null && !entityIds.isEmpty()) {
EntityType groupType = EntityType.valueOf(entityGroupEntitiesRequestMsg.getType());
return switch (groupType) {
case DEVICE -> syncDevices(edge, entityIds, entityGroupId);
case ASSET -> syncAssets(edge, entityIds, entityGroupId);
case ENTITY_VIEW -> syncEntityViews(edge, entityIds, entityGroupId);
case DASHBOARD -> syncDashboards(edge, entityIds, entityGroupId);
case USER -> syncUsers(edge, entityIds, entityGroupId);
default -> Futures.immediateFuture(null);
};
} else {
log.trace("No entities found for the requested entity group {} [{}]", entityGroupId.getId(), entityGroupEntitiesRequestMsg.getType());
ListenableFuture<Boolean> assignedFuture =
entityGroupService.checkEntityGroupAssignedToEdgeAsync(tenantId, edge.getId(), entityGroupId, entityGroup.getType());
return Futures.transformAsync(assignedFuture, assigned -> {
if (!Boolean.TRUE.equals(assigned) && !isEdgePublicUsersGroup(tenantId, edge, entityGroup)) {
log.warn("[{}][{}] Entity group {} is not assigned to edge, skipping request", tenantId, edge.getId(), entityGroupId);
return Futures.immediateFuture(null);
}
return syncEntityGroupEntities(tenantId, edge, entityGroupId, entityGroupEntitiesRequestMsg);
}, dbCallbackExecutorService);
}
return Futures.immediateFuture(null);
}
// Public Users groups are sent to the edge on sync without an edge assignment
private boolean isEdgePublicUsersGroup(TenantId tenantId, Edge edge, EntityGroup entityGroup) {
if (!EntityType.USER.equals(entityGroup.getType()) || !EntityGroup.GROUP_PUBLIC_USERS_NAME.equals(entityGroup.getName())
|| !EntityType.CUSTOMER.equals(entityGroup.getOwnerId().getEntityType())) {
return false;
}
Customer publicCustomer = findCustomer(tenantId, new CustomerId(entityGroup.getOwnerId().getId()));
if (publicCustomer == null || !publicCustomer.isPublic()) {
return false;
}
EntityId publicCustomerOwnerId = publicCustomer.getOwnerId();
if (tenantId.equals(publicCustomerOwnerId)) {
return true;
}
EntityId ownerId = edge.getOwnerId();
while (ownerId != null && EntityType.CUSTOMER.equals(ownerId.getEntityType())) {
if (ownerId.equals(publicCustomerOwnerId)) {
return true;
}
Customer customer = findCustomer(tenantId, new CustomerId(ownerId.getId()));
ownerId = customer != null ? customer.getOwnerId() : null;
}
return false;
}
private Customer findCustomer(TenantId tenantId, CustomerId customerId) {
return entityService.fetchEntity(tenantId, customerId)
.filter(Customer.class::isInstance)
.map(Customer.class::cast)
.filter(customer -> tenantId.equals(customer.getTenantId()))
.orElse(null);
}
private ListenableFuture<Void> syncEntityGroupEntities(TenantId tenantId, Edge edge, EntityGroupId entityGroupId, EntityGroupRequestMsg entityGroupEntitiesRequestMsg) {
ListenableFuture<List<EntityId>> entityIdsFuture;
try {
// TODO: refactor this to pagination
entityIdsFuture = entityGroupService.findAllEntityIdsAsync(edge.getTenantId(), entityGroupId, new PageLink(Integer.MAX_VALUE));
} catch (IncorrectParameterException e) {
log.warn("[{}] Entity group not found to process entityGroupEntitiesRequestMsg {}", tenantId, entityGroupEntitiesRequestMsg, e);
return Futures.immediateFuture(null);
}
return Futures.transformAsync(entityIdsFuture, entityIds -> {
if (entityIds != null && !entityIds.isEmpty()) {
EntityType groupType = EntityType.valueOf(entityGroupEntitiesRequestMsg.getType());
return switch (groupType) {
case DEVICE -> syncDevices(edge, entityIds, entityGroupId);
case ASSET -> syncAssets(edge, entityIds, entityGroupId);
case ENTITY_VIEW -> syncEntityViews(edge, entityIds, entityGroupId);
case DASHBOARD -> syncDashboards(edge, entityIds, entityGroupId);
case USER -> syncUsers(edge, entityIds, entityGroupId);
default -> Futures.immediateFuture(null);
};
} else {
log.trace("No entities found for the requested entity group {} [{}]", entityGroupId.getId(), entityGroupEntitiesRequestMsg.getType());
return Futures.immediateFuture(null);
}
}, dbCallbackExecutorService);
}
@Override
public ListenableFuture<Void> processEntityGroupPermissionsRequest(TenantId tenantId, Edge edge, EntityGroupRequestMsg entityGroupEntitiesRequestMsg) {
log.trace("[{}] processEntityGroupPermissionsRequest [{}][{}]", tenantId, edge.getName(), entityGroupEntitiesRequestMsg);

36
application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java

@ -69,6 +69,7 @@ import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.page.TimePageLink;
import org.thingsboard.server.common.data.permission.GroupPermission;
import org.thingsboard.server.common.data.permission.ShareGroupRequest;
import org.thingsboard.server.common.data.queue.Queue;
import org.thingsboard.server.common.data.role.Role;
import org.thingsboard.server.common.data.role.RoleType;
@ -282,6 +283,41 @@ public class EdgeControllerTest extends AbstractControllerTest {
Assert.assertEquals(savedEdge.getSecret(), foundEdge.getSecret());
}
@Test
public void testAssignReadOnlyEntityGroupToEdgeByCustomerUser() throws Exception {
Edge edge = doPost("/api/edge", constructEdge("Customer edge", "default"), Edge.class);
doPost("/api/owner/CUSTOMER/" + customerId.getId() + "/EDGE/" + edge.getId().getId())
.andExpect(status().isOk());
EntityGroup readOnlyGroup = new EntityGroup();
readOnlyGroup.setType(EntityType.DEVICE);
readOnlyGroup.setName("Read only device group");
readOnlyGroup = doPost("/api/entityGroup", readOnlyGroup, EntityGroup.class);
doPost("/api/entityGroup/" + readOnlyGroup.getId().getId() + "/share",
new ShareGroupRequest(customerId, true, null, true, null))
.andExpect(status().isOk());
EntityGroup customerGroup = new EntityGroup();
customerGroup.setType(EntityType.DEVICE);
customerGroup.setName("Customer device group");
customerGroup.setOwnerId(customerId);
customerGroup = doPost("/api/entityGroup", customerGroup, EntityGroup.class);
loginCustomerAdminUser();
doGet("/api/entityGroup/" + readOnlyGroup.getId().getId())
.andExpect(status().isOk());
doPost("/api/edge/" + edge.getId().getId() + "/entityGroup/" + readOnlyGroup.getId().getId() + "/DEVICE")
.andExpect(status().isForbidden());
doPost("/api/edge/" + edge.getId().getId() + "/entityGroup/" + customerGroup.getId().getId() + "/DEVICE")
.andExpect(status().isOk());
loginTenantAdmin();
EntityGroupId readOnlyGroupId = readOnlyGroup.getId();
PageData<EntityGroupInfo> edgeGroups = doGetTypedWithPageLink("/api/entityGroups/edge/" + edge.getId().getId() + "/DEVICE?",
new TypeReference<>() {}, new PageLink(100));
Assert.assertTrue(edgeGroups.getData().stream().noneMatch(g -> g.getId().equals(readOnlyGroupId)));
}
@Test
public void testFindEdgeById() throws Exception {
Edge edge = constructEdge("My edge", "default");

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

@ -214,6 +214,21 @@ public class EntityGroupEdgeTest extends AbstractEdgeTest {
.build()));
}
@Test
public void testSendEntityGroupEntitiesRequestForGroupNotAssignedToEdgeToCloud() throws Exception {
EntityGroup entityGroup = new EntityGroup();
entityGroup.setName("Device Group Not Assigned To Edge");
entityGroup.setType(EntityType.DEVICE);
entityGroup = doPost("/api/entityGroup", entityGroup, EntityGroup.class);
saveDevice("Device Not Assigned To Edge", "default", entityGroup.getId());
EntityGroupId entityGroupId = entityGroup.getId();
sendUplinkMsgAndVerifyNoDownlink(uplinkMsgBuilder -> uplinkMsgBuilder.addEntityGroupEntitiesRequestMsg(EntityGroupRequestMsg.newBuilder()
.setEntityGroupIdMSB(entityGroupId.getId().getMostSignificantBits())
.setEntityGroupIdLSB(entityGroupId.getId().getLeastSignificantBits())
.setType(EntityType.DEVICE.name()).build()));
}
private EntityGroup saveDifferentTenantGroup(EntityType groupType, EntityId entityId) throws Exception {
EntityGroup entityGroup = new EntityGroup();
entityGroup.setType(groupType);

Loading…
Cancel
Save