Browse Source

Merge pull request #16436 from thingsboard/fix/device-claiming-checks-43

Tightened device claiming permission checks
pull/16097/merge
Viacheslav Klimov 4 weeks ago
committed by GitHub
parent
commit
cff4ea56da
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      application/src/main/java/org/thingsboard/server/service/security/permission/CustomerUserPermissions.java
  2. 18
      application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java
  3. 57
      application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java

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

@ -9,6 +9,7 @@ import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.TbResourceInfo;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.ApiKeyId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TbResourceId;
@ -71,7 +72,11 @@ public class CustomerUserPermissions extends AbstractPermissions {
if (!(entity instanceof HasCustomerId)) {
return false;
}
return operation.equals(Operation.CLAIM_DEVICES) || user.getCustomerId().equals(((HasCustomerId) entity).getCustomerId());
CustomerId customerId = ((HasCustomerId) entity).getCustomerId();
if (operation.equals(Operation.CLAIM_DEVICES) && (customerId == null || customerId.isNullUid())) {
return true;
}
return user.getCustomerId().equals(customerId);
}
};

18
application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java

@ -533,10 +533,12 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest {
}
protected void loginDifferentCustomer() throws Exception {
if (savedDifferentCustomer != null) {
login(savedDifferentCustomer.getEmail(), CUSTOMER_USER_PASSWORD);
if (differentCustomerUser != null) {
login(DIFFERENT_CUSTOMER_USER_EMAIL, DIFFERENT_CUSTOMER_USER_PASSWORD);
} else {
createDifferentCustomer();
if (savedDifferentCustomer == null) {
createDifferentCustomer();
}
loginTenantAdmin();
differentCustomerUser = new User();
@ -551,10 +553,12 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest {
}
protected void loginDifferentTenantCustomer() throws Exception {
if (savedDifferentTenantCustomer != null) {
login(savedDifferentTenantCustomer.getEmail(), CUSTOMER_USER_PASSWORD);
if (differentTenantCustomerUser != null) {
login(DIFFERENT_TENANT_CUSTOMER_USER_EMAIL, DIFFERENT_CUSTOMER_USER_PASSWORD);
} else {
createDifferentTenantCustomer();
if (savedDifferentTenantCustomer == null) {
createDifferentTenantCustomer();
}
loginDifferentTenant();
differentTenantCustomerUser = new User();
@ -597,6 +601,8 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest {
loginSysAdmin();
deleteTenant(savedDifferentTenant.getId());
savedDifferentTenant = null;
savedDifferentTenantCustomer = null;
differentTenantCustomerUser = null;
}
}

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

@ -26,6 +26,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.DeviceInfo;
@ -1695,6 +1696,62 @@ public class DeviceControllerTest extends AbstractControllerTest {
assertThat(device.getVersion()).isEqualTo(3);
}
@Test
public void testReclaimDeviceByCustomerUser() throws Exception {
Device device = createCustomerDevice("Reclaim own device");
loginCustomerUser();
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class);
loginTenantAdmin();
Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class);
Assert.assertEquals(NULL_UUID, foundDevice.getCustomerId().getId());
}
@Test
public void testReclaimDeviceOfDifferentCustomerByCustomerUser() throws Exception {
Device device = createCustomerDevice("Reclaim device of different customer");
loginDifferentCustomer();
doDelete("/api/customer/device/" + device.getName() + "/claim")
.andExpect(status().isForbidden())
.andExpect(statusReason(containsString(msgErrorPermission)));
loginTenantAdmin();
Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class);
Assert.assertEquals(customerId, foundDevice.getCustomerId());
}
@Test
public void testClaimDeviceOfDifferentCustomerByCustomerUser() throws Exception {
Device device = createCustomerDevice("Claim device of different customer");
loginDifferentCustomer();
doPost("/api/customer/device/" + device.getName() + "/claim", new ClaimRequest(null))
.andExpect(status().isForbidden())
.andExpect(statusReason(containsString(msgErrorPermission)));
loginTenantAdmin();
Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class);
Assert.assertEquals(customerId, foundDevice.getCustomerId());
}
@Test
public void testReclaimDeviceByTenantAdmin() throws Exception {
Device device = createCustomerDevice("Reclaim device by tenant admin");
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class);
Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class);
Assert.assertEquals(NULL_UUID, foundDevice.getCustomerId().getId());
}
private Device createCustomerDevice(String name) throws Exception {
loginTenantAdmin();
Device device = createDevice(name);
return doPost("/api/customer/" + customerId.getId() + "/device/" + device.getUuidId(), Device.class);
}
@Test
public void testSaveDeviceWithUniquifyStrategy() throws Exception {
Device device = new Device();

Loading…
Cancel
Save