Commit 944b2f75720a3e61c901fafd6cd52e2470c199da

Authored by Andrew Shvayka
Committed by GitHub
2 parents 4dd4b4e0 b84c6abd

Merge pull request #5453 from YevhenBondarenko/lwm2m-improvements

[3.3.2] process deleting device and updating credential events for LwM2M
... ... @@ -719,7 +719,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
719 719 void processCredentialsUpdate(TbActorMsg msg) {
720 720 if (((DeviceCredentialsUpdateNotificationMsg) msg).getDeviceCredentials().getCredentialsType() == DeviceCredentialsType.LWM2M_CREDENTIALS) {
721 721 sessions.forEach((k, v) -> {
722   - notifyTransportAboutProfileUpdate(k, v, ((DeviceCredentialsUpdateNotificationMsg) msg).getDeviceCredentials());
  722 + notifyTransportAboutDeviceCredentialsUpdate(k, v, ((DeviceCredentialsUpdateNotificationMsg) msg).getDeviceCredentials());
723 723 });
724 724 } else {
725 725 sessions.forEach((sessionId, sessionMd) -> notifyTransportAboutClosedSession(sessionId, sessionMd, "device credentials updated!"));
... ... @@ -747,7 +747,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
747 747 systemContext.getTbCoreToTransportService().process(sessionMd.getSessionInfo().getNodeId(), msg);
748 748 }
749 749
750   - void notifyTransportAboutProfileUpdate(UUID sessionId, SessionInfoMetaData sessionMd, DeviceCredentials deviceCredentials) {
  750 + void notifyTransportAboutDeviceCredentialsUpdate(UUID sessionId, SessionInfoMetaData sessionMd, DeviceCredentials deviceCredentials) {
751 751 ToTransportUpdateCredentialsProto.Builder notification = ToTransportUpdateCredentialsProto.newBuilder();
752 752 notification.addCredentialsId(deviceCredentials.getCredentialsId());
753 753 notification.addCredentialsValue(deviceCredentials.getCredentialsValue());
... ...
... ... @@ -28,7 +28,7 @@ import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
28 28 import org.thingsboard.server.transport.lwm2m.server.client.LwM2MAuthException;
29 29 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext;
30 30 import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MDtlsSessionStore;
31   -import org.thingsboard.server.transport.lwm2m.server.store.TbSecurityStore;
  31 +import org.thingsboard.server.transport.lwm2m.server.store.TbMainSecurityStore;
32 32
33 33 @Component
34 34 @RequiredArgsConstructor
... ... @@ -37,7 +37,7 @@ import org.thingsboard.server.transport.lwm2m.server.store.TbSecurityStore;
37 37 public class TbLwM2MAuthorizer implements Authorizer {
38 38
39 39 private final TbLwM2MDtlsSessionStore sessionStorage;
40   - private final TbSecurityStore securityStore;
  40 + private final TbMainSecurityStore securityStore;
41 41 private final SecurityChecker securityChecker = new SecurityChecker();
42 42 private final LwM2mClientContext clientContext;
43 43
... ... @@ -58,17 +58,16 @@ public class TbLwM2MAuthorizer implements Authorizer {
58 58 // If session info is not found, this may be the trusted certificate, so we still need to check all other options below.
59 59 }
60 60 SecurityInfo expectedSecurityInfo = null;
61   - if (securityStore != null) {
62   - try {
63   - expectedSecurityInfo = securityStore.getByEndpoint(registration.getEndpoint());
64   - } catch (LwM2MAuthException e) {
65   - log.info("Registration failed: FORBIDDEN, endpointId: [{}]", registration.getEndpoint());
66   - return null;
67   - }
  61 + try {
  62 + expectedSecurityInfo = securityStore.getByEndpoint(registration.getEndpoint());
  63 + } catch (LwM2MAuthException e) {
  64 + log.info("Registration failed: FORBIDDEN, endpointId: [{}]", registration.getEndpoint());
  65 + return null;
68 66 }
69 67 if (securityChecker.checkSecurityInfo(registration.getEndpoint(), senderIdentity, expectedSecurityInfo)) {
70 68 return registration;
71 69 } else {
  70 + securityStore.remove(registration.getEndpoint(), registration.getId());
72 71 return null;
73 72 }
74 73 }
... ...
... ... @@ -25,7 +25,6 @@ import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
25 25 import org.eclipse.leshan.server.californium.LeshanServer;
26 26 import org.eclipse.leshan.server.californium.LeshanServerBuilder;
27 27 import org.eclipse.leshan.server.californium.registration.CaliforniumRegistrationStore;
28   -import org.eclipse.leshan.server.model.LwM2mModelProvider;
29 28 import org.springframework.stereotype.Component;
30 29 import org.thingsboard.server.cache.ota.OtaPackageDataCache;
31 30 import org.thingsboard.server.common.data.DataConstants;
... ... @@ -34,7 +33,6 @@ import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
34 33 import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig;
35 34 import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MAuthorizer;
36 35 import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MDtlsCertificateVerifier;
37   -import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext;
38 36 import org.thingsboard.server.transport.lwm2m.server.store.TbSecurityStore;
39 37 import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2MUplinkMsgHandler;
40 38 import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl;
... ...
... ... @@ -19,13 +19,12 @@ import io.netty.util.concurrent.Future;
19 19 import io.netty.util.concurrent.GenericFutureListener;
20 20 import lombok.RequiredArgsConstructor;
21 21 import lombok.extern.slf4j.Slf4j;
22   -import org.jetbrains.annotations.NotNull;
23 22 import org.thingsboard.server.common.data.Device;
24 23 import org.thingsboard.server.common.data.DeviceProfile;
25 24 import org.thingsboard.server.common.data.ResourceType;
  25 +import org.thingsboard.server.common.data.id.DeviceId;
26 26 import org.thingsboard.server.common.transport.SessionMsgListener;
27 27 import org.thingsboard.server.common.transport.TransportService;
28   -import org.thingsboard.server.common.transport.TransportServiceCallback;
29 28 import org.thingsboard.server.gen.transport.TransportProtos;
30 29 import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg;
31 30 import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg;
... ... @@ -109,4 +108,10 @@ public class LwM2mSessionMsgListener implements GenericFutureListener<Future<? s
109 108 this.handler.onResourceDelete(resourceDeleteMsgOpt);
110 109 }
111 110 }
  111 +
  112 + @Override
  113 + public void onDeviceDeleted(DeviceId deviceId) {
  114 + log.trace("[{}] Device on delete", deviceId);
  115 + this.handler.onDeviceDelete(deviceId);
  116 + }
112 117 }
... ...
... ... @@ -57,8 +57,6 @@ public interface LwM2mClientContext {
57 57
58 58 void update(LwM2mClient lwM2MClient);
59 59
60   - void removeCredentials(TransportProtos.SessionInfoProto sessionInfo);
61   -
62 60 void sendMsgsAfterSleeping(LwM2mClient lwM2MClient);
63 61
64 62 boolean isComposite(LwM2mClient client);
... ...
... ... @@ -329,11 +329,6 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
329 329 }
330 330
331 331 @Override
332   - public void removeCredentials(TransportProtos.SessionInfoProto sessionInfo) {
333   - //TODO: implement
334   - }
335   -
336   - @Override
337 332 public void sendMsgsAfterSleeping(LwM2mClient lwM2MClient) {
338 333 if (LwM2MClientState.REGISTERED.equals(lwM2MClient.getState())) {
339 334 PowerMode powerMode = getPowerMode(lwM2MClient);
... ...
... ... @@ -36,6 +36,7 @@ import org.eclipse.leshan.core.response.ObserveResponse;
36 36 import org.eclipse.leshan.core.response.ReadCompositeResponse;
37 37 import org.eclipse.leshan.core.response.ReadResponse;
38 38 import org.eclipse.leshan.server.registration.Registration;
  39 +import org.eclipse.leshan.server.registration.RegistrationStore;
39 40 import org.springframework.context.annotation.Lazy;
40 41 import org.springframework.stereotype.Service;
41 42 import org.thingsboard.common.util.DonAsynchron;
... ... @@ -46,6 +47,7 @@ import org.thingsboard.server.common.data.device.data.lwm2m.ObjectAttributes;
46 47 import org.thingsboard.server.common.data.device.data.lwm2m.OtherConfiguration;
47 48 import org.thingsboard.server.common.data.device.data.lwm2m.TelemetryMappingConfiguration;
48 49 import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration;
  50 +import org.thingsboard.server.common.data.id.DeviceId;
49 51 import org.thingsboard.server.common.data.id.TenantId;
50 52 import org.thingsboard.server.common.data.ota.OtaPackageUtil;
51 53 import org.thingsboard.server.common.transport.TransportService;
... ... @@ -82,9 +84,9 @@ import org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttrib
82 84 import org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttributesRequest;
83 85 import org.thingsboard.server.transport.lwm2m.server.log.LwM2MTelemetryLogService;
84 86 import org.thingsboard.server.transport.lwm2m.server.ota.LwM2MOtaUpdateService;
85   -import org.thingsboard.server.transport.lwm2m.server.rpc.LwM2MRpcRequestHandler;
86 87 import org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager;
87 88 import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MDtlsSessionStore;
  89 +import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2mSecurityStore;
88 90 import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl;
89 91
90 92 import javax.annotation.PostConstruct;
... ... @@ -143,6 +145,8 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl
143 145 private final LwM2mClientContext clientContext;
144 146 private final LwM2mDownlinkMsgHandler defaultLwM2MDownlinkMsgHandler;
145 147 private final LwM2mVersionedModelProvider modelProvider;
  148 + private final RegistrationStore registrationStore;
  149 + private final TbLwM2mSecurityStore securityStore;
146 150
147 151 public DefaultLwM2MUplinkMsgHandler(TransportService transportService,
148 152 LwM2MTransportServerConfig config,
... ... @@ -155,7 +159,9 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl
155 159 @Lazy LwM2mDownlinkMsgHandler defaultLwM2MDownlinkMsgHandler,
156 160 LwM2mTransportContext context,
157 161 TbLwM2MDtlsSessionStore sessionStore,
158   - LwM2mVersionedModelProvider modelProvider) {
  162 + LwM2mVersionedModelProvider modelProvider,
  163 + RegistrationStore registrationStore,
  164 + TbLwM2mSecurityStore securityStore) {
159 165 this.transportService = transportService;
160 166 this.sessionManager = sessionManager;
161 167 this.attributesService = attributesService;
... ... @@ -168,6 +174,8 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl
168 174 this.context = context;
169 175 this.sessionStore = sessionStore;
170 176 this.modelProvider = modelProvider;
  177 + this.registrationStore = registrationStore;
  178 + this.securityStore = securityStore;
171 179 }
172 180
173 181 @PostConstruct
... ... @@ -273,26 +281,27 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl
273 281 * @param observations - !!! Warn: if have not finishing unReg, then this operation will be finished on next Client`s connect
274 282 */
275 283 public void unReg(Registration registration, Collection<Observation> observations) {
276   - executor.submit(() -> {
277   - LwM2mClient client = clientContext.getClientByEndpoint(registration.getEndpoint());
278   - try {
279   - logService.log(client, LOG_LWM2M_INFO + ": Client unRegistration");
280   - clientContext.unregister(client, registration);
281   - SessionInfoProto sessionInfo = client.getSession();
282   - if (sessionInfo != null) {
283   - sessionManager.deregister(sessionInfo);
284   - sessionStore.remove(registration.getEndpoint());
285   - log.info("Client close session: [{}] unReg [{}] name [{}] profile ", registration.getId(), registration.getEndpoint(), sessionInfo.getDeviceType());
286   - } else {
287   - log.error("Client close session: [{}] unReg [{}] name [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null);
288   - }
289   - } catch (LwM2MClientStateException stateException) {
290   - log.info("[{}] delete registration: [{}] {}.", registration.getEndpoint(), stateException.getState(), stateException.getMessage());
291   - } catch (Throwable t) {
292   - log.error("[{}] endpoint [{}] error Unable un registration.", registration.getEndpoint(), t);
293   - logService.log(client, LOG_LWM2M_ERROR + String.format(": Client Unable un Registration, %s", t.getMessage()));
  284 + executor.submit(() -> doUnReg(registration, clientContext.getClientByEndpoint(registration.getEndpoint())));
  285 + }
  286 +
  287 + private void doUnReg(Registration registration, LwM2mClient client) {
  288 + try {
  289 + logService.log(client, LOG_LWM2M_INFO + ": Client unRegistration");
  290 + clientContext.unregister(client, registration);
  291 + SessionInfoProto sessionInfo = client.getSession();
  292 + if (sessionInfo != null) {
  293 + sessionManager.deregister(sessionInfo);
  294 + sessionStore.remove(registration.getEndpoint());
  295 + log.info("Client close session: [{}] unReg [{}] name [{}] profile ", registration.getId(), registration.getEndpoint(), sessionInfo.getDeviceType());
  296 + } else {
  297 + log.error("Client close session: [{}] unReg [{}] name [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null);
294 298 }
295   - });
  299 + } catch (LwM2MClientStateException stateException) {
  300 + log.info("[{}] delete registration: [{}] {}.", registration.getEndpoint(), stateException.getState(), stateException.getMessage());
  301 + } catch (Throwable t) {
  302 + log.error("[{}] endpoint [{}] error Unable un registration.", registration.getEndpoint(), t);
  303 + logService.log(client, LOG_LWM2M_ERROR + String.format(": Client Unable un Registration, %s", t.getMessage()));
  304 + }
296 305 }
297 306
298 307 @Override
... ... @@ -391,6 +400,11 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl
391 400 }
392 401
393 402 @Override
  403 + public void onDeviceDelete(DeviceId deviceId) {
  404 + clearAndUnregister(clientContext.getClientByDeviceId(deviceId.getId()));
  405 + }
  406 +
  407 + @Override
394 408 public void onResourceUpdate(TransportProtos.ResourceUpdateMsg resourceUpdateMsgOpt) {
395 409 String idVer = resourceUpdateMsgOpt.getResourceKey();
396 410 TenantId tenantId = new TenantId(new UUID(resourceUpdateMsgOpt.getTenantIdMSB(), resourceUpdateMsgOpt.getTenantIdLSB()));
... ... @@ -886,8 +900,8 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl
886 900 */
887 901 @Override
888 902 public void onToTransportUpdateCredentials(SessionInfoProto sessionInfo, TransportProtos.ToTransportUpdateCredentialsProto updateCredentials) {
889   - log.info("[{}] idList [{}] valueList updateCredentials", updateCredentials.getCredentialsIdList(), updateCredentials.getCredentialsValueList());
890   - this.clientContext.removeCredentials(sessionInfo);
  903 + log.info("[{}] updateCredentials", sessionInfo);
  904 + clearAndUnregister(clientContext.getClientBySessionInfo(sessionInfo));
891 905 }
892 906
893 907 /**
... ... @@ -964,4 +978,16 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl
964 978 .setLastActivityTime(System.currentTimeMillis())
965 979 .build(), TransportServiceCallback.EMPTY);
966 980 }
  981 +
  982 + private void clearAndUnregister(LwM2mClient client) {
  983 + client.lock();
  984 + try {
  985 + Registration registration = client.getRegistration();
  986 + doUnReg(registration, client);
  987 + securityStore.remove(registration.getEndpoint(), registration.getId());
  988 + registrationStore.removeRegistration(registration.getId());
  989 + } finally {
  990 + client.unlock();
  991 + }
  992 + }
967 993 }
... ...
... ... @@ -23,6 +23,7 @@ import org.eclipse.leshan.core.response.ReadResponse;
23 23 import org.eclipse.leshan.server.registration.Registration;
24 24 import org.thingsboard.server.common.data.Device;
25 25 import org.thingsboard.server.common.data.DeviceProfile;
  26 +import org.thingsboard.server.common.data.id.DeviceId;
26 27 import org.thingsboard.server.gen.transport.TransportProtos;
27 28 import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig;
28 29 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient;
... ... @@ -48,6 +49,8 @@ public interface LwM2mUplinkMsgHandler {
48 49
49 50 void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt);
50 51
  52 + void onDeviceDelete(DeviceId deviceId);
  53 +
51 54 void onResourceUpdate(TransportProtos.ResourceUpdateMsg resourceUpdateMsgOpt);
52 55
53 56 void onResourceDelete(TransportProtos.ResourceDeleteMsg resourceDeleteMsgOpt);
... ...