Merge pull request #10613 from thingsboard/fix_bug_lwm2m_redis
fix_bug_redis_decode
This commit is contained in:
commit
009d355927
@ -67,6 +67,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_CONNECTION_ID_LENGTH;
|
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_CONNECTION_ID_LENGTH;
|
||||||
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY;
|
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY;
|
||||||
@ -147,9 +148,9 @@ public class LwM2MTestClient {
|
|||||||
initializer.setClassForObject(SECURITY, Security.class);
|
initializer.setClassForObject(SECURITY, Security.class);
|
||||||
initializer.setInstancesForObject(SECURITY, instances);
|
initializer.setInstancesForObject(SECURITY, instances);
|
||||||
// SERVER
|
// SERVER
|
||||||
Server lwm2mServer = new Server(shortServerId, 300);
|
Server lwm2mServer = new Server(shortServerId, TimeUnit.MINUTES.toSeconds(60));
|
||||||
lwm2mServer.setId(serverId);
|
lwm2mServer.setId(serverId);
|
||||||
Server serverBs = new Server(shortServerIdBs0, 300);
|
Server serverBs = new Server(shortServerIdBs0, TimeUnit.MINUTES.toSeconds(60));
|
||||||
serverBs.setId(serverIdBs);
|
serverBs.setId(serverIdBs);
|
||||||
instances = new LwM2mInstanceEnabler[]{serverBs, lwm2mServer};
|
instances = new LwM2mInstanceEnabler[]{serverBs, lwm2mServer};
|
||||||
initializer.setClassForObject(SERVER, Server.class);
|
initializer.setClassForObject(SERVER, Server.class);
|
||||||
@ -163,7 +164,7 @@ public class LwM2MTestClient {
|
|||||||
// SECURITY
|
// SECURITY
|
||||||
initializer.setInstancesForObject(SECURITY, security);
|
initializer.setInstancesForObject(SECURITY, security);
|
||||||
// SERVER
|
// SERVER
|
||||||
Server lwm2mServer = new Server(shortServerId, 300);
|
Server lwm2mServer = new Server(shortServerId, TimeUnit.MINUTES.toSeconds(60));
|
||||||
lwm2mServer.setId(serverId);
|
lwm2mServer.setId(serverId);
|
||||||
initializer.setInstancesForObject(SERVER, lwm2mServer );
|
initializer.setInstancesForObject(SERVER, lwm2mServer );
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -61,6 +61,7 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_CONNECTION_ID_LENGTH;
|
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_CONNECTION_ID_LENGTH;
|
||||||
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY;
|
import static org.eclipse.californium.scandium.config.DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY;
|
||||||
@ -118,7 +119,7 @@ public class LwM2MTestClient {
|
|||||||
// SECURITY
|
// SECURITY
|
||||||
initializer.setInstancesForObject(SECURITY, security);
|
initializer.setInstancesForObject(SECURITY, security);
|
||||||
// SERVER
|
// SERVER
|
||||||
Server lwm2mServer = new Server(shortServerId, 300);
|
Server lwm2mServer = new Server(shortServerId, TimeUnit.MINUTES.toSeconds(60));
|
||||||
lwm2mServer.setId(serverId);
|
lwm2mServer.setId(serverId);
|
||||||
initializer.setInstancesForObject(SERVER, lwm2mServer);
|
initializer.setInstancesForObject(SERVER, lwm2mServer);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user