lwm2m: fix bug redis if NoSec mode - do not decode

This commit is contained in:
nick 2024-04-22 19:24:17 +03:00
parent 1012b47775
commit d6b75e29af
2 changed files with 31 additions and 3 deletions

View File

@ -90,7 +90,7 @@ public class TbLwM2mRedisRegistrationStore implements RegistrationStore, Startab
private static final Logger LOG = LoggerFactory.getLogger(RedisRegistrationStore.class); private static final Logger LOG = LoggerFactory.getLogger(RedisRegistrationStore.class);
// Redis key prefixes // Redis key prefixes
private static final String REG_EP = "REG:EP:"; // (Endpoint => Registration) public static final String REG_EP = "REG:EP:"; // (Endpoint => Registration)
private static final String REG_EP_REGID_IDX = "EP:REGID:"; // secondary index key (Registration ID => Endpoint) private static final String REG_EP_REGID_IDX = "EP:REGID:"; // secondary index key (Registration ID => Endpoint)
private static final String REG_EP_ADDR_IDX = "EP:ADDR:"; // secondary index key (Socket Address => Endpoint) private static final String REG_EP_ADDR_IDX = "EP:ADDR:"; // secondary index key (Socket Address => Endpoint)
private static final String REG_EP_IDENTITY = "EP:IDENTITY:"; // secondary index key (Identity => Endpoint) private static final String REG_EP_IDENTITY = "EP:IDENTITY:"; // secondary index key (Identity => Endpoint)

View File

@ -15,17 +15,24 @@
*/ */
package org.thingsboard.server.transport.lwm2m.server.store; package org.thingsboard.server.transport.lwm2m.server.store;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.leshan.core.SecurityMode; import org.eclipse.leshan.core.SecurityMode;
import org.eclipse.leshan.core.peer.OscoreIdentity; import org.eclipse.leshan.core.peer.OscoreIdentity;
import org.eclipse.leshan.server.security.NonUniqueSecurityInfoException; import org.eclipse.leshan.server.security.NonUniqueSecurityInfoException;
import org.eclipse.leshan.server.security.SecurityInfo; import org.eclipse.leshan.server.security.SecurityInfo;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry; import org.springframework.integration.redis.util.RedisLockRegistry;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.JavaSerDesUtil; import org.thingsboard.server.common.data.JavaSerDesUtil;
import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo; import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import static org.thingsboard.server.transport.lwm2m.server.store.TbLwM2mRedisRegistrationStore.REG_EP;
@Slf4j
public class TbLwM2mRedisSecurityStore implements TbEditableSecurityStore { public class TbLwM2mRedisSecurityStore implements TbEditableSecurityStore {
private static final String SEC_EP = "SEC#EP#"; private static final String SEC_EP = "SEC#EP#";
private static final String LOCK_EP = "LOCK#EP#"; private static final String LOCK_EP = "LOCK#EP#";
@ -49,11 +56,19 @@ public class TbLwM2mRedisSecurityStore implements TbEditableSecurityStore {
if (data == null || data.length == 0) { if (data == null || data.length == 0) {
return null; return null;
} else { } else {
if (SecurityMode.NO_SEC.equals(((TbLwM2MSecurityInfo) JavaSerDesUtil.decode(data)).getSecurityMode())) { TbLwM2MSecurityInfo tbLwM2MSecurityInfo = JavaSerDesUtil.decode(data);
if (tbLwM2MSecurityInfo != null) {
if (SecurityMode.NO_SEC.equals(tbLwM2MSecurityInfo.getSecurityMode())){
return SecurityInfo.newPreSharedKeyInfo(SecurityMode.NO_SEC.toString(), SecurityMode.NO_SEC.toString(),
SecurityMode.NO_SEC.toString().getBytes());
} else {
return tbLwM2MSecurityInfo.getSecurityInfo();
}
} else if (SecurityMode.NO_SEC.equals(getSecurityModeByRegistration (connection, endpoint))){
return SecurityInfo.newPreSharedKeyInfo(SecurityMode.NO_SEC.toString(), SecurityMode.NO_SEC.toString(), return SecurityInfo.newPreSharedKeyInfo(SecurityMode.NO_SEC.toString(), SecurityMode.NO_SEC.toString(),
SecurityMode.NO_SEC.toString().getBytes()); SecurityMode.NO_SEC.toString().getBytes());
} else { } else {
return ((TbLwM2MSecurityInfo) JavaSerDesUtil.decode(data)).getSecurityInfo(); return null;
} }
} }
} finally { } finally {
@ -168,4 +183,17 @@ public class TbLwM2mRedisSecurityStore implements TbEditableSecurityStore {
private String toLockKey(String endpoint) { private String toLockKey(String endpoint) {
return LOCK_EP + endpoint; return LOCK_EP + endpoint;
} }
private SecurityMode getSecurityModeByRegistration (RedisConnection connection, String endpoint) {
try {
byte[] data = connection.get((REG_EP + endpoint).getBytes());
JsonNode registrationNode = JacksonUtil.fromString(new String(data != null ? data : new byte[0]), JsonNode.class);
String typeModeStr = registrationNode.get("transportdata").get("identity").get("type").asText();
return "unsecure".equals(typeModeStr) ? SecurityMode.NO_SEC : null;
} catch (Exception e) {
log.error("Redis: Failed get SecurityMode by Registration, endpoint: [{}]", endpoint);
return null;
}
}
} }