Commit 4457b5a11aad26acdfca36140a387842dfee6ff2
Committed by
Andrew Shvayka
1 parent
30971564
Lwm2m: fix bug Bootstrap + Tests NoSec - ok
Showing
4 changed files
with
62 additions
and
17 deletions
@@ -19,6 +19,9 @@ import lombok.RequiredArgsConstructor; | @@ -19,6 +19,9 @@ import lombok.RequiredArgsConstructor; | ||
19 | import lombok.extern.slf4j.Slf4j; | 19 | import lombok.extern.slf4j.Slf4j; |
20 | import org.eclipse.californium.elements.util.SslContextUtil; | 20 | import org.eclipse.californium.elements.util.SslContextUtil; |
21 | import org.eclipse.californium.scandium.config.DtlsConnectorConfig; | 21 | import org.eclipse.californium.scandium.config.DtlsConnectorConfig; |
22 | +import org.eclipse.leshan.core.model.ObjectLoader; | ||
23 | +import org.eclipse.leshan.core.model.ObjectModel; | ||
24 | +import org.eclipse.leshan.core.model.StaticModel; | ||
22 | import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager; | 25 | import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager; |
23 | import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServer; | 26 | import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServer; |
24 | import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServerBuilder; | 27 | import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServerBuilder; |
@@ -26,6 +29,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | @@ -26,6 +29,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
26 | import org.springframework.stereotype.Component; | 29 | import org.springframework.stereotype.Component; |
27 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MBootstrapSecurityStore; | 30 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MBootstrapSecurityStore; |
28 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MInMemoryBootstrapConfigStore; | 31 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MInMemoryBootstrapConfigStore; |
32 | +import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MInMemoryBootstrapConfigurationAdapter; | ||
29 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2mDefaultBootstrapSessionManager; | 33 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2mDefaultBootstrapSessionManager; |
30 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportBootstrapConfig; | 34 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportBootstrapConfig; |
31 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; | 35 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; |
@@ -38,6 +42,7 @@ import java.security.KeyStoreException; | @@ -38,6 +42,7 @@ import java.security.KeyStoreException; | ||
38 | import java.security.PrivateKey; | 42 | import java.security.PrivateKey; |
39 | import java.security.PublicKey; | 43 | import java.security.PublicKey; |
40 | import java.security.cert.X509Certificate; | 44 | import java.security.cert.X509Certificate; |
45 | +import java.util.List; | ||
41 | 46 | ||
42 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mNetworkConfig.getCoapConfig; | 47 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mNetworkConfig.getCoapConfig; |
43 | 48 | ||
@@ -79,12 +84,14 @@ public class LwM2MTransportBootstrapService { | @@ -79,12 +84,14 @@ public class LwM2MTransportBootstrapService { | ||
79 | builder.setCoapConfig(getCoapConfig(bootstrapConfig.getPort(), bootstrapConfig.getSecurePort(), serverConfig)); | 84 | builder.setCoapConfig(getCoapConfig(bootstrapConfig.getPort(), bootstrapConfig.getSecurePort(), serverConfig)); |
80 | 85 | ||
81 | /* Define model provider (Create Models )*/ | 86 | /* Define model provider (Create Models )*/ |
87 | + List<ObjectModel> models = ObjectLoader.loadDefault(); | ||
88 | + builder.setModel(new StaticModel(models)); | ||
82 | 89 | ||
83 | /* Create credentials */ | 90 | /* Create credentials */ |
84 | this.setServerWithCredentials(builder); | 91 | this.setServerWithCredentials(builder); |
85 | 92 | ||
86 | -// /** Set securityStore with new ConfigStore */ | ||
87 | -// builder.setConfigStore(lwM2MInMemoryBootstrapConfigStore); | 93 | + /* Set securityStore with new ConfigStore */ |
94 | + builder.setConfigStore(new LwM2MInMemoryBootstrapConfigurationAdapter(lwM2MInMemoryBootstrapConfigStore)); | ||
88 | 95 | ||
89 | /* SecurityStore */ | 96 | /* SecurityStore */ |
90 | builder.setSecurityStore(lwM2MBootstrapSecurityStore); | 97 | builder.setSecurityStore(lwM2MBootstrapSecurityStore); |
@@ -74,15 +74,19 @@ public class LwM2MBootstrapConfig implements Serializable { | @@ -74,15 +74,19 @@ public class LwM2MBootstrapConfig implements Serializable { | ||
74 | configBs.servers.put(0, server0); | 74 | configBs.servers.put(0, server0); |
75 | /* Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Bootstrap instance = 0 */ | 75 | /* Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Bootstrap instance = 0 */ |
76 | this.bootstrapServer.setBootstrapServerIs(true); | 76 | this.bootstrapServer.setBootstrapServerIs(true); |
77 | - configBs.security.put(0, setServerSecurity(this.bootstrapServer.getHost(), this.bootstrapServer.getPort(), this.bootstrapServer.isBootstrapServerIs(), this.bootstrapServer.getSecurityMode(), this.bootstrapServer.getClientPublicKeyOrId(), this.bootstrapServer.getServerPublicKey(), this.bootstrapServer.getClientSecretKey(), this.bootstrapServer.getServerId())); | 77 | + configBs.security.put(0, setServerSecurity(this.lwm2mServer.getHost(), this.lwm2mServer.getPort(), this.lwm2mServer.getSecurityHost(), this.lwm2mServer.getSecurityPort(), this.bootstrapServer.isBootstrapServerIs(), this.bootstrapServer.getSecurityMode(), this.bootstrapServer.getClientPublicKeyOrId(), this.bootstrapServer.getServerPublicKey(), this.bootstrapServer.getClientSecretKey(), this.bootstrapServer.getServerId())); |
78 | /* Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Server instance = 1 */ | 78 | /* Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Server instance = 1 */ |
79 | - configBs.security.put(1, setServerSecurity(this.lwm2mServer.getHost(), this.lwm2mServer.getPort(), this.lwm2mServer.isBootstrapServerIs(), this.lwm2mServer.getSecurityMode(), this.lwm2mServer.getClientPublicKeyOrId(), this.lwm2mServer.getServerPublicKey(), this.lwm2mServer.getClientSecretKey(), this.lwm2mServer.getServerId())); | 79 | + configBs.security.put(1, setServerSecurity(this.lwm2mServer.getHost(), this.lwm2mServer.getPort(), this.lwm2mServer.getSecurityHost(), this.lwm2mServer.getSecurityPort(), this.lwm2mServer.isBootstrapServerIs(), this.lwm2mServer.getSecurityMode(), this.lwm2mServer.getClientPublicKeyOrId(), this.lwm2mServer.getServerPublicKey(), this.lwm2mServer.getClientSecretKey(), this.lwm2mServer.getServerId())); |
80 | return configBs; | 80 | return configBs; |
81 | } | 81 | } |
82 | 82 | ||
83 | - private BootstrapConfig.ServerSecurity setServerSecurity(String host, Integer port, boolean bootstrapServer, SecurityMode securityMode, String clientPublicKey, String serverPublicKey, String secretKey, int serverId) { | 83 | + private BootstrapConfig.ServerSecurity setServerSecurity(String host, Integer port, String securityHost, Integer securityPort, boolean bootstrapServer, SecurityMode securityMode, String clientPublicKey, String serverPublicKey, String secretKey, int serverId) { |
84 | BootstrapConfig.ServerSecurity serverSecurity = new BootstrapConfig.ServerSecurity(); | 84 | BootstrapConfig.ServerSecurity serverSecurity = new BootstrapConfig.ServerSecurity(); |
85 | - serverSecurity.uri = "coaps://" + host + ":" + Integer.toString(port); | 85 | + if (securityMode.equals(SecurityMode.NO_SEC)) { |
86 | + serverSecurity.uri = "coap://" + host + ":" + Integer.toString(port); | ||
87 | + } else { | ||
88 | + serverSecurity.uri = "coaps://" + securityHost + ":" + Integer.toString(securityPort); | ||
89 | + } | ||
86 | serverSecurity.bootstrapServer = bootstrapServer; | 90 | serverSecurity.bootstrapServer = bootstrapServer; |
87 | serverSecurity.securityMode = securityMode; | 91 | serverSecurity.securityMode = securityMode; |
88 | serverSecurity.publicKeyOrId = setPublicKeyOrId(clientPublicKey, securityMode); | 92 | serverSecurity.publicKeyOrId = setPublicKeyOrId(clientPublicKey, securityMode); |
1 | +/** | ||
2 | + * Copyright © 2016-2021 The Thingsboard Authors | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.thingsboard.server.transport.lwm2m.bootstrap.secure; | ||
17 | + | ||
18 | +import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore; | ||
19 | +import org.eclipse.leshan.server.bootstrap.BootstrapConfigurationStoreAdapter; | ||
20 | + | ||
21 | +public class LwM2MInMemoryBootstrapConfigurationAdapter extends BootstrapConfigurationStoreAdapter { | ||
22 | + | ||
23 | + public LwM2MInMemoryBootstrapConfigurationAdapter(BootstrapConfigStore store) { | ||
24 | + super(store); | ||
25 | + } | ||
26 | + | ||
27 | +} |
@@ -31,24 +31,31 @@ public class LwM2MServerBootstrap { | @@ -31,24 +31,31 @@ public class LwM2MServerBootstrap { | ||
31 | 31 | ||
32 | String host = "0.0.0.0"; | 32 | String host = "0.0.0.0"; |
33 | Integer port = 0; | 33 | Integer port = 0; |
34 | + String securityHost = "0.0.0.0"; | ||
35 | + Integer securityPort = 0; | ||
34 | 36 | ||
35 | SecurityMode securityMode = SecurityMode.NO_SEC; | 37 | SecurityMode securityMode = SecurityMode.NO_SEC; |
36 | 38 | ||
37 | Integer serverId = 123; | 39 | Integer serverId = 123; |
38 | boolean bootstrapServerIs = false; | 40 | boolean bootstrapServerIs = false; |
39 | 41 | ||
40 | - public LwM2MServerBootstrap(){}; | 42 | + public LwM2MServerBootstrap() { |
43 | + } | ||
44 | + | ||
45 | + ; | ||
41 | 46 | ||
42 | public LwM2MServerBootstrap(LwM2MServerBootstrap bootstrapFromCredential, LwM2MServerBootstrap profileServerBootstrap) { | 47 | public LwM2MServerBootstrap(LwM2MServerBootstrap bootstrapFromCredential, LwM2MServerBootstrap profileServerBootstrap) { |
43 | - this.clientPublicKeyOrId = bootstrapFromCredential.getClientPublicKeyOrId(); | ||
44 | - this.clientSecretKey = bootstrapFromCredential.getClientSecretKey(); | ||
45 | - this.serverPublicKey = profileServerBootstrap.getServerPublicKey(); | ||
46 | - this.clientHoldOffTime = profileServerBootstrap.getClientHoldOffTime(); | ||
47 | - this.bootstrapServerAccountTimeout = profileServerBootstrap.getBootstrapServerAccountTimeout(); | ||
48 | - this.host = (profileServerBootstrap.getHost().equals("0.0.0.0")) ? "localhost" : profileServerBootstrap.getHost(); | ||
49 | - this.port = profileServerBootstrap.getPort(); | ||
50 | - this.securityMode = profileServerBootstrap.getSecurityMode(); | ||
51 | - this.serverId = profileServerBootstrap.getServerId(); | ||
52 | - this.bootstrapServerIs = profileServerBootstrap.bootstrapServerIs; | 48 | + this.clientPublicKeyOrId = bootstrapFromCredential.getClientPublicKeyOrId(); |
49 | + this.clientSecretKey = bootstrapFromCredential.getClientSecretKey(); | ||
50 | + this.serverPublicKey = profileServerBootstrap.getServerPublicKey(); | ||
51 | + this.clientHoldOffTime = profileServerBootstrap.getClientHoldOffTime(); | ||
52 | + this.bootstrapServerAccountTimeout = profileServerBootstrap.getBootstrapServerAccountTimeout(); | ||
53 | + this.host = (profileServerBootstrap.getHost().equals("0.0.0.0")) ? "localhost" : profileServerBootstrap.getHost(); | ||
54 | + this.port = profileServerBootstrap.getPort(); | ||
55 | + this.securityHost = (profileServerBootstrap.getSecurityHost().equals("0.0.0.0")) ? "localhost" : profileServerBootstrap.getSecurityHost(); | ||
56 | + this.securityPort = profileServerBootstrap.getSecurityPort(); | ||
57 | + this.securityMode = profileServerBootstrap.getSecurityMode(); | ||
58 | + this.serverId = profileServerBootstrap.getServerId(); | ||
59 | + this.bootstrapServerIs = profileServerBootstrap.bootstrapServerIs; | ||
53 | } | 60 | } |
54 | } | 61 | } |