Browse Source

Tighten device claiming permission checks

pull/16435/head
Viacheslav Klimov 1 month ago
parent
commit
f8316b9fcb
Failed to extract signature
  1. 2
      application/src/main/java/org/thingsboard/server/service/security/permission/CustomerUserPermissions.java
  2. 96
      application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java

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

@ -227,7 +227,7 @@ public class CustomerUserPermissions extends AbstractPermissions {
}
}
} else {
if (operation == Operation.CLAIM_DEVICES) {
if (operation == Operation.CLAIM_DEVICES && user.getTenantId().equals(((HasOwnerId) entity).getOwnerId())) {
return user.getUserPermissions().hasGenericPermission(resource, operation);
}
if (entity.getEntityType() == EntityType.CUSTOMER && user.getCustomerId().equals(entityId) ||

96
application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java

@ -27,6 +27,7 @@ import org.testcontainers.shaded.org.awaitility.Awaitility;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.common.util.ThingsBoardExecutors;
import org.thingsboard.server.common.data.AttributeScope;
import org.thingsboard.server.common.data.ClaimRequest;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile;
@ -60,6 +61,8 @@ import org.thingsboard.server.common.data.sync.ie.importing.csv.BulkImportColumn
import org.thingsboard.server.common.data.sync.ie.importing.csv.BulkImportRequest;
import org.thingsboard.server.common.data.sync.ie.importing.csv.BulkImportResult;
import org.thingsboard.server.dao.device.DeviceDao;
import org.thingsboard.server.dao.device.claim.ClaimResponse;
import org.thingsboard.server.dao.device.claim.ClaimResult;
import org.thingsboard.server.dao.exception.DeviceCredentialsValidationException;
import org.thingsboard.server.dao.service.DaoSqlTest;
import org.thingsboard.server.exception.DataValidationException;
@ -1518,4 +1521,97 @@ public class DeviceControllerTest extends AbstractControllerTest {
assertThat(resavedSecondDevice.getExternalId()).isEqualTo(secondExternalId);
}
@Test
public void testReclaimDeviceByCustomerUser() throws Exception {
Device device = createTenantDevice("Customer device to reclaim", customerId);
loginCustomerAdminUser();
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class);
loginTenantAdmin();
Device reclaimedDevice = doGet("/api/device/" + device.getId().getId(), Device.class);
assertThat(reclaimedDevice.getOwnerId()).isEqualTo(tenantId);
}
@Test
public void testReclaimDeviceOfDifferentCustomerByCustomerUser() throws Exception {
createDifferentCustomer();
Device device = createTenantDevice("Different customer device", differentCustomerId);
loginCustomerAdminUser();
doDelete("/api/customer/device/" + device.getName() + "/claim").andExpect(status().isForbidden());
loginTenantAdmin();
Device foundDevice = doGet("/api/device/" + device.getId().getId(), Device.class);
assertThat(foundDevice.getOwnerId()).isEqualTo(differentCustomerId);
}
@Test
public void testReclaimSubCustomerDeviceByParentCustomerUser() throws Exception {
Device device = createTenantDevice("Sub-customer device to reclaim", subCustomerId);
loginCustomerAdminUser();
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class);
loginTenantAdmin();
Device reclaimedDevice = doGet("/api/device/" + device.getId().getId(), Device.class);
assertThat(reclaimedDevice.getOwnerId()).isEqualTo(tenantId);
}
@Test
public void testReclaimParentCustomerDeviceBySubCustomerUser() throws Exception {
Device device = createTenantDevice("Parent customer device", customerId);
loginSubCustomerAdminUser();
doDelete("/api/customer/device/" + device.getName() + "/claim").andExpect(status().isForbidden());
loginTenantAdmin();
Device foundDevice = doGet("/api/device/" + device.getId().getId(), Device.class);
assertThat(foundDevice.getOwnerId()).isEqualTo(customerId);
}
@Test
public void testReclaimDeviceByTenantAdmin() throws Exception {
Device device = createTenantDevice("Customer device to reclaim by tenant", customerId);
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class);
Device reclaimedDevice = doGet("/api/device/" + device.getId().getId(), Device.class);
assertThat(reclaimedDevice.getOwnerId()).isEqualTo(tenantId);
}
@Test
public void testClaimUnassignedDeviceByCustomerUser() throws Exception {
Device device = createTenantDevice("Unassigned device to claim", null);
claimDevicesService.registerClaimingInfo(tenantId, device.getId(), "secret", TimeUnit.MINUTES.toMillis(1)).get();
loginCustomerAdminUser();
ClaimResult claimResult = doPostClaimAsync("/api/customer/device/" + device.getName() + "/claim",
new ClaimRequest("secret"), ClaimResult.class, status().isOk());
assertThat(claimResult.getResponse()).isEqualTo(ClaimResponse.SUCCESS);
assertThat(claimResult.getDevice().getOwnerId()).isEqualTo(customerId);
}
@Test
public void testClaimDeviceOfDifferentCustomerByCustomerUser() throws Exception {
createDifferentCustomer();
Device device = createTenantDevice("Different customer device to claim", differentCustomerId);
loginCustomerAdminUser();
doPost("/api/customer/device/" + device.getName() + "/claim", new ClaimRequest(null)).andExpect(status().isForbidden());
loginTenantAdmin();
Device foundDevice = doGet("/api/device/" + device.getId().getId(), Device.class);
assertThat(foundDevice.getOwnerId()).isEqualTo(differentCustomerId);
}
private Device createTenantDevice(String name, CustomerId ownerId) throws Exception {
loginTenantAdmin();
Device device = new Device();
device.setName(name);
device.setType("default");
device.setCustomerId(ownerId);
return doPost("/api/device", device, Device.class);
}
}

Loading…
Cancel
Save