From 3f37cb28b3e5caa1049bf2fae9f0bc708d1a4643 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Fri, 18 Sep 2026 11:16:29 +0300 Subject: [PATCH] lwm2m: fix bootstrap-short-server-id = null - comments 9 --- .../service/lwm2m/LwM2MServiceImpl.java | 11 ++++--- .../src/main/resources/thingsboard.yml | 2 +- .../LwM2MServiceImplInvalidConfigTest.java | 32 +++++++++++-------- .../config/LwM2MTransportServerConfig.java | 2 +- .../src/main/resources/tb-lwm2m-transport.yml | 2 +- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/lwm2m/LwM2MServiceImpl.java b/application/src/main/java/org/thingsboard/server/service/lwm2m/LwM2MServiceImpl.java index 9071babb45..ae777fbff7 100644 --- a/application/src/main/java/org/thingsboard/server/service/lwm2m/LwM2MServiceImpl.java +++ b/application/src/main/java/org/thingsboard/server/service/lwm2m/LwM2MServiceImpl.java @@ -6,6 +6,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.device.credentials.lwm2m.Lwm2mServerIdentifier; import org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.LwM2MServerSecurityConfigDefault; import org.thingsboard.server.common.transport.config.ssl.SslCredentials; import org.thingsboard.server.queue.util.TbCoreComponent; @@ -50,11 +51,11 @@ public class LwM2MServiceImpl implements LwM2MService { bsServ.setShortServerId(null); } else { Integer configId = bsServerConfig.getId(); - if (configId == null || configId <= 0 || configId >= 65535) { - throw new IllegalArgumentException(String.format( - "Invalid LwM2M Server ShortServerId [%s] in configuration (transport.lwm2m.server.id). Must be in range [%d - %d]!", - configId, PRIMARY_LWM2M_SERVER.getId(), LWM2M_SERVER_MAX.getId() - )); + if (Lwm2mServerIdentifier.isNotLwm2mServer(configId)) { + log.warn("Invalid LwM2M Server ShortServerId [{}] in configuration (transport.lwm2m.server.id). " + + "Must be in range [{} - {}]. Using [{}] instead.", + configId, PRIMARY_LWM2M_SERVER.getId(), LWM2M_SERVER_MAX.getId(), PRIMARY_LWM2M_SERVER.getId()); + configId = PRIMARY_LWM2M_SERVER.getId(); } bsServ.setShortServerId(configId); } diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 85ad4b1806..beb29333ba 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -1291,7 +1291,7 @@ transport: #
  • A value that are > 4: MultiNodeConnectionIdGenerator is used
  • connection_id_length: "${LWM2M_DTLS_CONNECTION_ID_LENGTH:8}" server: - # LwM2M Server ID + # LwM2M Server Short Server ID [1 - 65534]: 0 and 65535 are reserved. An invalid or missing value is ignored and 1 is used instead. id: "${LWM2M_SERVER_ID:123}" # LwM2M server bind address. Bind to all interfaces by default bind_address: "${LWM2M_BIND_ADDRESS:0.0.0.0}" diff --git a/application/src/test/java/org/thingsboard/server/controller/LwM2MServiceImplInvalidConfigTest.java b/application/src/test/java/org/thingsboard/server/controller/LwM2MServiceImplInvalidConfigTest.java index 7273e2c558..6beeccb1af 100644 --- a/application/src/test/java/org/thingsboard/server/controller/LwM2MServiceImplInvalidConfigTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/LwM2MServiceImplInvalidConfigTest.java @@ -15,7 +15,6 @@ import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.BDDMockito.given; import static org.thingsboard.server.common.data.device.credentials.lwm2m.Lwm2mServerIdentifier.NOT_USED_IDENTIFYING_LWM2M_SERVER_MAX; import static org.thingsboard.server.common.data.device.credentials.lwm2m.Lwm2mServerIdentifier.PRIMARY_LWM2M_SERVER; @@ -64,32 +63,37 @@ public class LwM2MServiceImplInvalidConfigTest { } @Test - void testGetServerSecurityInfo_DmServer_Less_PRIMARY_LWM2M_SERVER_ThrowsIllegalArgumentException() { + void testGetServerSecurityInfo_Server_Less_PRIMARY_LWM2M_SERVER_DefaultsToOneAndLogsWarn() { Integer shortServerId = PRIMARY_LWM2M_SERVER.getId() - 1; given(serverConfig.getId()).willReturn(shortServerId); - assertThatThrownBy(() -> lwM2MService.getServerSecurityInfo(false)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Invalid LwM2M Server ShortServerId [" + shortServerId + "] in configuration (transport.lwm2m.server.id). Must be in range [1 - 65534]!"); + LwM2MServerSecurityConfigDefault result = lwM2MService.getServerSecurityInfo(false); + + assertThat(result).isNotNull(); + assertThat(result.isBootstrapServerIs()).isFalse(); + assertThat(result.getShortServerId()).isEqualTo(PRIMARY_LWM2M_SERVER.getId()); } @Test - void testGetServerSecurityInfo_DmServer_NOT_USED_IDENTIFYING_LWM2M_SERVER_MAX_ThrowsIllegalArgumentException() { + void testGetServerSecurityInfo_Server_NOT_USED_IDENTIFYING_LWM2M_SERVER_MAX_DefaultsToOneAndLogsWarn() { Integer shortServerId = NOT_USED_IDENTIFYING_LWM2M_SERVER_MAX.getId(); given(serverConfig.getId()).willReturn(shortServerId); - assertThatThrownBy(() -> lwM2MService.getServerSecurityInfo(false)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Invalid LwM2M Server ShortServerId [" + shortServerId + "] in configuration (transport.lwm2m.server.id). Must be in range [1 - 65534]!"); + LwM2MServerSecurityConfigDefault result = lwM2MService.getServerSecurityInfo(false); + + assertThat(result).isNotNull(); + assertThat(result.isBootstrapServerIs()).isFalse(); + assertThat(result.getShortServerId()).isEqualTo(PRIMARY_LWM2M_SERVER.getId()); } @Test - void testGetServerSecurityInfo_DmServerWithNullId_ThrowsIllegalArgumentException() { + void testGetServerSecurityInfo_ServerWithNullId_DefaultsToOneAndLogsWarn() { Integer shortServerId = null; given(serverConfig.getId()).willReturn(shortServerId); - assertThatThrownBy(() -> lwM2MService.getServerSecurityInfo(false)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Invalid LwM2M Server ShortServerId [" + shortServerId + "] in configuration (transport.lwm2m.server.id). Must be in range [1 - 65534]!"); - } + LwM2MServerSecurityConfigDefault result = lwM2MService.getServerSecurityInfo(false); + + assertThat(result).isNotNull(); + assertThat(result.isBootstrapServerIs()).isFalse(); + assertThat(result.getShortServerId()).isEqualTo(PRIMARY_LWM2M_SERVER.getId()); } } \ No newline at end of file diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/config/LwM2MTransportServerConfig.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/config/LwM2MTransportServerConfig.java index 15f5a03555..6f2a510783 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/config/LwM2MTransportServerConfig.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/config/LwM2MTransportServerConfig.java @@ -81,7 +81,7 @@ public class LwM2MTransportServerConfig implements LwM2MSecureServerConfig { private int cleanPeriodInSec; @Getter - @Value("${transport.lwm2m.server.id:}") + @Value("${transport.lwm2m.server.id:123}") private Integer id; @Getter diff --git a/transport/lwm2m/src/main/resources/tb-lwm2m-transport.yml b/transport/lwm2m/src/main/resources/tb-lwm2m-transport.yml index b9c22ad5f2..c7cf5100db 100644 --- a/transport/lwm2m/src/main/resources/tb-lwm2m-transport.yml +++ b/transport/lwm2m/src/main/resources/tb-lwm2m-transport.yml @@ -174,7 +174,7 @@ transport: #
  • A value that are > 4: MultiNodeConnectionIdGenerator is used
  • connection_id_length: "${LWM2M_DTLS_CONNECTION_ID_LENGTH:8}" server: - # LwM2M Server ID + # LwM2M Server Short Server ID [1 - 65534]: 0 and 65535 are reserved. An invalid or missing value is ignored and 1 is used instead. id: "${LWM2M_SERVER_ID:123}" # LwM2M server bind address. Bind to all interfaces by default bind_address: "${LWM2M_BIND_ADDRESS:0.0.0.0}"