-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10063 from Rhyaldir/add_dtls_cid_support
Support DTLS Connection ID with configuration
- Loading branch information
Showing
14 changed files
with
721 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...in/java/org/thingsboard/server/transport/lwm2m/server/store/util/LwM2MIdentitySerDes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright © 2016-2024 The Thingsboard Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.thingsboard.server.transport.lwm2m.server.store.util; | ||
|
||
import com.eclipsesource.json.Json; | ||
import com.eclipsesource.json.JsonObject; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.eclipse.leshan.core.request.Identity; | ||
import org.eclipse.leshan.core.util.Hex; | ||
|
||
import java.security.PublicKey; | ||
|
||
public class LwM2MIdentitySerDes { | ||
|
||
private static final String KEY_ADDRESS = "address"; | ||
private static final String KEY_PORT = "port"; | ||
private static final String KEY_ID = "id"; | ||
private static final String KEY_CN = "cn"; | ||
private static final String KEY_RPK = "rpk"; | ||
protected static final String KEY_LWM2MIDENTITY_TYPE = "type"; | ||
protected static final String LWM2MIDENTITY_TYPE_UNSECURE = "unsecure"; | ||
protected static final String LWM2MIDENTITY_TYPE_PSK = "psk"; | ||
protected static final String LWM2MIDENTITY_TYPE_X509 = "x509"; | ||
protected static final String LWM2MIDENTITY_TYPE_RPK = "rpk"; | ||
|
||
public static JsonObject serialize(Identity identity) { | ||
JsonObject o = Json.object(); | ||
|
||
if (identity.isPSK()) { | ||
o.set(KEY_LWM2MIDENTITY_TYPE, LWM2MIDENTITY_TYPE_PSK); | ||
o.set(KEY_ID, identity.getPskIdentity()); | ||
} else if (identity.isRPK()) { | ||
o.set(KEY_LWM2MIDENTITY_TYPE, LWM2MIDENTITY_TYPE_RPK); | ||
PublicKey publicKey = identity.getRawPublicKey(); | ||
o.set(KEY_RPK, Hex.encodeHexString(publicKey.getEncoded())); | ||
} else if (identity.isX509()) { | ||
o.set(KEY_LWM2MIDENTITY_TYPE, LWM2MIDENTITY_TYPE_X509); | ||
o.set(KEY_CN, identity.getX509CommonName()); | ||
} else { | ||
o.set(KEY_LWM2MIDENTITY_TYPE, LWM2MIDENTITY_TYPE_UNSECURE); | ||
o.set(KEY_ADDRESS, identity.getPeerAddress().getHostString()); | ||
o.set(KEY_PORT, identity.getPeerAddress().getPort()); | ||
} | ||
return o; | ||
} | ||
|
||
public static Identity deserialize(JsonObject peer) { | ||
throw new NotImplementedException(); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
.../org/thingsboard/server/transport/lwm2m/bootstrap/LwM2MTransportBootstrapServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* Copyright © 2016-2024 The Thingsboard Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.thingsboard.server.transport.lwm2m.bootstrap; | ||
|
||
import org.eclipse.californium.core.network.CoapEndpoint; | ||
import org.eclipse.californium.scandium.config.DtlsConnectorConfig; | ||
import org.eclipse.leshan.server.californium.LeshanServer; | ||
import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServer; | ||
import org.eclipse.leshan.server.californium.registration.CaliforniumRegistrationStore; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.thingsboard.server.cache.ota.OtaPackageDataCache; | ||
import org.thingsboard.server.common.transport.TransportService; | ||
import org.thingsboard.server.transport.lwm2m.bootstrap.secure.TbLwM2MDtlsBootstrapCertificateVerifier; | ||
import org.thingsboard.server.transport.lwm2m.bootstrap.store.LwM2MBootstrapSecurityStore; | ||
import org.thingsboard.server.transport.lwm2m.bootstrap.store.LwM2MInMemoryBootstrapConfigStore; | ||
import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportBootstrapConfig; | ||
import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; | ||
import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MAuthorizer; | ||
import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MDtlsCertificateVerifier; | ||
import org.thingsboard.server.transport.lwm2m.server.store.TbSecurityStore; | ||
import org.thingsboard.server.transport.lwm2m.server.uplink.LwM2mUplinkMsgHandler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class LwM2MTransportBootstrapServiceTest { | ||
|
||
@Mock | ||
private LwM2MTransportServerConfig serverConfig; | ||
@Mock | ||
private LwM2MTransportBootstrapConfig bootstrapConfig; | ||
@Mock | ||
private LwM2MBootstrapSecurityStore lwM2MBootstrapSecurityStore; | ||
@Mock | ||
private LwM2MInMemoryBootstrapConfigStore lwM2MInMemoryBootstrapConfigStore; | ||
@Mock | ||
private TransportService transportService; | ||
@Mock | ||
private TbLwM2MDtlsBootstrapCertificateVerifier certificateVerifier; | ||
|
||
|
||
@Test | ||
public void getLHServer_creates_ConnectionIdGenerator_when_connection_id_length_not_null(){ | ||
final Integer CONNECTION_ID_LENGTH = 6; | ||
when(serverConfig.getDtlsConnectionIdLength()).thenReturn(CONNECTION_ID_LENGTH); | ||
var lwM2MBootstrapService = createLwM2MBootstrapService(); | ||
|
||
var server = lwM2MBootstrapService.getLhBootstrapServer(); | ||
var securedEndpoint = (CoapEndpoint) ReflectionTestUtils.getField(server, "securedEndpoint"); | ||
assertThat(securedEndpoint).isNotNull(); | ||
|
||
var config = (DtlsConnectorConfig) ReflectionTestUtils.getField(securedEndpoint.getConnector(), "config"); | ||
assertThat(config).isNotNull(); | ||
assertThat(config.getConnectionIdGenerator()).isNotNull(); | ||
assertThat((Integer) ReflectionTestUtils.getField(config.getConnectionIdGenerator(), "connectionIdLength")) | ||
.isEqualTo(CONNECTION_ID_LENGTH); | ||
} | ||
|
||
@Test | ||
public void getLHServer_creates_no_ConnectionIdGenerator_when_connection_id_length_is_null(){ | ||
when(serverConfig.getDtlsConnectionIdLength()).thenReturn(null); | ||
var lwM2MBootstrapService = createLwM2MBootstrapService(); | ||
|
||
var server = lwM2MBootstrapService.getLhBootstrapServer(); | ||
var securedEndpoint = (CoapEndpoint) ReflectionTestUtils.getField(server, "securedEndpoint"); | ||
assertThat(securedEndpoint).isNotNull(); | ||
|
||
var config = (DtlsConnectorConfig) ReflectionTestUtils.getField(securedEndpoint.getConnector(), "config"); | ||
assertThat(config).isNotNull(); | ||
assertThat(config.getConnectionIdGenerator()).isNull(); | ||
} | ||
|
||
private LwM2MTransportBootstrapService createLwM2MBootstrapService() { | ||
setDefaultConfigVariables(); | ||
return new LwM2MTransportBootstrapService(serverConfig, bootstrapConfig, lwM2MBootstrapSecurityStore, | ||
lwM2MInMemoryBootstrapConfigStore, transportService, certificateVerifier); | ||
} | ||
|
||
private void setDefaultConfigVariables(){ | ||
when(bootstrapConfig.getPort()).thenReturn(5683); | ||
when(bootstrapConfig.getSecurePort()).thenReturn(5684); | ||
when(serverConfig.isRecommendedCiphers()).thenReturn(false); | ||
when(serverConfig.getDtlsRetransmissionTimeout()).thenReturn(9000); | ||
} | ||
|
||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...st/java/org/thingsboard/server/transport/lwm2m/config/LwM2MTransportServerConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright © 2016-2024 The Thingsboard Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.thingsboard.server.transport.lwm2m.config; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.boot.test.context.SpringBootContextLoader; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.thingsboard.server.common.transport.config.ssl.SslCredentialsConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@EnableConfigurationProperties(value = LwM2MTransportServerConfig.class) | ||
@ContextConfiguration(classes = {LwM2MTransportServerConfig.class}, loader = SpringBootContextLoader.class) | ||
@TestPropertySource(properties = { | ||
"transport.sessions.report_timeout=10", | ||
"transport.lwm2m.security.recommended_ciphers=true", | ||
"transport.lwm2m.security.recommended_supported_groups=true", | ||
"transport.lwm2m.downlink_pool_size=10", | ||
"transport.lwm2m.uplink_pool_size=10", | ||
"transport.lwm2m.ota_pool_size=10", | ||
"transport.lwm2m.clean_period_in_sec=2", | ||
"transport.lwm2m.dtls.connection_id_length=" | ||
|
||
}) | ||
class LwM2MTransportServerConfigTest { | ||
|
||
@MockBean(name = "lwm2mServerCredentials") | ||
private SslCredentialsConfig credentialsConfig; | ||
|
||
@MockBean(name = "lwm2mTrustCredentials") | ||
private SslCredentialsConfig trustCredentialsConfig; | ||
|
||
@Autowired | ||
private LwM2MTransportServerConfig serverConfig; | ||
|
||
@Test | ||
void getDtlsConnectionIdLength_return_null_is_property_is_empty() { | ||
// note: transport.lwm2m.dtls.connect_id_length is set in TestPropertySource | ||
assertThat(serverConfig.getDtlsConnectionIdLength()).isNull(); | ||
} | ||
} |
Oops, something went wrong.