46 changed files with 991 additions and 85 deletions
@ -0,0 +1,78 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.edge; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.HasTenantId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.entity.EntityDaoRegistry; |
|||
import org.thingsboard.server.dao.entity.EntityService; |
|||
import org.thingsboard.server.dao.exception.DataValidationException; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
/** |
|||
* Checks that entities referenced by edge messages belong to the edge's tenant. |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class EdgeEntityTenantChecker { |
|||
|
|||
private final EntityService entityService; |
|||
private final EntityDaoRegistry entityDaoRegistry; |
|||
|
|||
/** |
|||
* Returns true if the entity exists and belongs to the tenant. |
|||
* Uses the entity services, so cached and tenant-scoped lookups are used where available. |
|||
*/ |
|||
public boolean isTenantEntity(TenantId tenantId, EntityId entityId) { |
|||
if (entityId == null) { |
|||
return false; |
|||
} |
|||
try { |
|||
return entityService.fetchEntity(tenantId, entityId) |
|||
.map(entity -> entity instanceof HasTenantId hasTenantId && tenantId.equals(hasTenantId.getTenantId())) |
|||
.orElse(false); |
|||
} catch (IllegalArgumentException e) { |
|||
log.warn("[{}] Unsupported entity type of {}", tenantId, entityId); |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Loads the entity regardless of its tenant, so that an id of another tenant's entity is detected. |
|||
* Returns null if the entity doesn't exist, throws if it belongs to a different tenant. |
|||
*/ |
|||
@SuppressWarnings("unchecked") |
|||
public <T> T findTenantEntity(TenantId tenantId, EntityId entityId) { |
|||
Object entity = findEntity(tenantId, entityId); |
|||
if (entity instanceof HasTenantId hasTenantId && !tenantId.equals(hasTenantId.getTenantId())) { |
|||
throw new DataValidationException(entityId.getEntityType() + " " + entityId.getId() + " belongs to a different tenant!"); |
|||
} |
|||
return (T) entity; |
|||
} |
|||
|
|||
/** |
|||
* Returns the tenant of the entity regardless of the given tenant, or null if the entity doesn't exist. |
|||
*/ |
|||
public TenantId findEntityTenantId(TenantId tenantId, EntityId entityId) { |
|||
return findEntity(tenantId, entityId) instanceof HasTenantId hasTenantId ? hasTenantId.getTenantId() : null; |
|||
} |
|||
|
|||
private Object findEntity(TenantId tenantId, EntityId entityId) { |
|||
if (entityId == null) { |
|||
return null; |
|||
} |
|||
try { |
|||
return entityDaoRegistry.getDao(entityId.getEntityType()).findById(tenantId, entityId.getId()); |
|||
} catch (IllegalArgumentException e) { |
|||
log.warn("[{}] Unsupported entity type of {}", tenantId, entityId); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.edge; |
|||
|
|||
import com.google.protobuf.Descriptors.FieldDescriptor; |
|||
import com.google.protobuf.Message; |
|||
|
|||
import java.util.Collection; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.StringJoiner; |
|||
import java.util.UUID; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* Describes edge protobuf messages without their payloads, so they can be logged or put into notifications |
|||
* without exposing entity JSON, credentials, tokens or secrets. |
|||
*/ |
|||
public final class EdgeMsgSummaryUtils { |
|||
|
|||
private static final String MSB = "MSB"; |
|||
private static final String LSB = "LSB"; |
|||
|
|||
private EdgeMsgSummaryUtils() { |
|||
} |
|||
|
|||
// e.g. DownlinkMsg[downlinkMsgId=12, deviceUpdateMsg x2, deviceCredentialsUpdateMsg x1]
|
|||
// e.g. DeviceUpdateMsg[msgType=ENTITY_UPDATED_RPC_MESSAGE, id=1d6f1e2c-...]
|
|||
public static String toSummary(Message msg) { |
|||
if (msg == null) { |
|||
return "null"; |
|||
} |
|||
Map<FieldDescriptor, Object> fields = msg.getAllFields(); |
|||
StringJoiner summary = new StringJoiner(", ", msg.getDescriptorForType().getName() + "[", "]"); |
|||
fields.forEach((field, value) -> { |
|||
String name = field.getName(); |
|||
if (field.isRepeated()) { |
|||
summary.add(name + " x" + ((List<?>) value).size()); |
|||
return; |
|||
} |
|||
switch (field.getJavaType()) { |
|||
case INT, LONG -> { |
|||
FieldDescriptor lsbField = name.endsWith(MSB) ? findPairField(msg, name, MSB, LSB) : null; |
|||
if (lsbField != null && value instanceof Long msb && msg.getField(lsbField) instanceof Long lsb) { |
|||
summary.add(name.substring(0, name.length() - MSB.length()) + "=" + new UUID(msb, lsb)); |
|||
} else if (!isPrintedWithPair(msg, fields, name)) { |
|||
summary.add(name + "=" + value); |
|||
} |
|||
} |
|||
case BOOLEAN, ENUM -> summary.add(name + "=" + value); |
|||
case MESSAGE -> summary.add(name); |
|||
// strings and bytes may hold entity JSON, credentials or secrets
|
|||
default -> { |
|||
} |
|||
} |
|||
}); |
|||
return summary.toString(); |
|||
} |
|||
|
|||
// LSB part of an id is printed together with its MSB part
|
|||
private static boolean isPrintedWithPair(Message msg, Map<FieldDescriptor, Object> fields, String name) { |
|||
if (!name.endsWith(LSB)) { |
|||
return false; |
|||
} |
|||
FieldDescriptor msbField = findPairField(msg, name, LSB, MSB); |
|||
return msbField != null && fields.containsKey(msbField); |
|||
} |
|||
|
|||
private static FieldDescriptor findPairField(Message msg, String name, String suffix, String pairSuffix) { |
|||
return msg.getDescriptorForType().findFieldByName(name.substring(0, name.length() - suffix.length()) + pairSuffix); |
|||
} |
|||
|
|||
public static String toSummary(Collection<? extends Message> msgs) { |
|||
return msgs.stream().map(EdgeMsgSummaryUtils::toSummary).collect(Collectors.joining(", ", "[", "]")); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.edge; |
|||
|
|||
import org.junit.jupiter.api.Assertions; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.server.gen.edge.v1.DeviceCredentialsUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.gen.edge.v1.UplinkMsg; |
|||
|
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
|
|||
public class EdgeMsgSummaryUtilsTest { |
|||
|
|||
private static final String SECRET = "secret-access-token"; |
|||
|
|||
@Test |
|||
public void testDownlinkMsgSummaryHasNoPayload() { |
|||
DownlinkMsg downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(12) |
|||
.addDeviceUpdateMsg(DeviceUpdateMsg.newBuilder().setEntity("{\"name\":\"Device\"}").build()) |
|||
.addDeviceUpdateMsg(DeviceUpdateMsg.newBuilder().setEntity("{\"name\":\"Device 2\"}").build()) |
|||
.addDeviceCredentialsUpdateMsg(DeviceCredentialsUpdateMsg.newBuilder().setEntity("{\"credentialsId\":\"" + SECRET + "\"}").build()) |
|||
.build(); |
|||
|
|||
String summary = EdgeMsgSummaryUtils.toSummary(downlinkMsg); |
|||
|
|||
Assertions.assertEquals("DownlinkMsg[downlinkMsgId=12, deviceUpdateMsg x2, deviceCredentialsUpdateMsg x1]", summary); |
|||
} |
|||
|
|||
@Test |
|||
public void testUplinkMsgsSummaryHasNoPayload() { |
|||
UplinkMsg uplinkMsg = UplinkMsg.newBuilder() |
|||
.setUplinkMsgId(3) |
|||
.addDeviceCredentialsUpdateMsg(DeviceCredentialsUpdateMsg.newBuilder().setEntity("{\"credentialsId\":\"" + SECRET + "\"}").build()) |
|||
.build(); |
|||
|
|||
String summary = EdgeMsgSummaryUtils.toSummary(List.of(uplinkMsg, uplinkMsg)); |
|||
|
|||
Assertions.assertEquals("[UplinkMsg[uplinkMsgId=3, deviceCredentialsUpdateMsg x1], UplinkMsg[uplinkMsgId=3, deviceCredentialsUpdateMsg x1]]", summary); |
|||
Assertions.assertFalse(summary.contains(SECRET)); |
|||
} |
|||
|
|||
@Test |
|||
public void testEntityMsgSummaryHasTypeAndIdWithoutPayload() { |
|||
UUID deviceUuid = UUID.randomUUID(); |
|||
DeviceUpdateMsg deviceUpdateMsg = DeviceUpdateMsg.newBuilder() |
|||
.setMsgType(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE) |
|||
.setIdMSB(deviceUuid.getMostSignificantBits()) |
|||
.setIdLSB(deviceUuid.getLeastSignificantBits()) |
|||
.setEntity("{\"name\":\"" + SECRET + "\"}") |
|||
.build(); |
|||
|
|||
String summary = EdgeMsgSummaryUtils.toSummary(deviceUpdateMsg); |
|||
|
|||
Assertions.assertEquals("DeviceUpdateMsg[msgType=ENTITY_UPDATED_RPC_MESSAGE, id=" + deviceUuid + "]", summary); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue