5 changed files with 105 additions and 14 deletions
@ -0,0 +1,44 @@ |
|||||
|
// 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.StringJoiner; |
||||
|
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 EdgeMsgSummaryUtils() { |
||||
|
} |
||||
|
|
||||
|
// e.g. DownlinkMsg[downlinkMsgId=12, deviceUpdateMsg x2, deviceCredentialsUpdateMsg x1]
|
||||
|
public static String toSummary(Message msg) { |
||||
|
if (msg == null) { |
||||
|
return "null"; |
||||
|
} |
||||
|
StringJoiner summary = new StringJoiner(", ", msg.getDescriptorForType().getName() + "[", "]"); |
||||
|
msg.getAllFields().forEach((field, value) -> { |
||||
|
if (field.isRepeated()) { |
||||
|
summary.add(field.getName() + " x" + ((List<?>) value).size()); |
||||
|
} else if (field.getJavaType() == FieldDescriptor.JavaType.INT || field.getJavaType() == FieldDescriptor.JavaType.LONG) { |
||||
|
summary.add(field.getName() + "=" + value); |
||||
|
} else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { |
||||
|
summary.add(field.getName()); |
||||
|
} |
||||
|
}); |
||||
|
return summary.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String toSummary(Collection<? extends Message> msgs) { |
||||
|
return msgs.stream().map(EdgeMsgSummaryUtils::toSummary).collect(Collectors.joining(", ", "[", "]")); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
// 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.UplinkMsg; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
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)); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue