From dfd0c5c7fa98a1ae63761c7e3fa65e7f0e794ce7 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Thu, 17 Sep 2026 14:28:19 +0300 Subject: [PATCH] lwm2m: fix bootstrap-short-server-id = null - comments 4 --- .../service/lwm2m/LwM2MServiceImpl.java | 26 +++++++++++++------ .../DeviceProfileControllerTest.java | 24 +++++++++++++++++ .../bootstrap/LwM2MServerSecurityConfig.java | 2 +- .../DeviceProfileDataValidatorTest.java | 4 +-- 4 files changed, 45 insertions(+), 11 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 6f8a39ce73..33d0487fdd 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 @@ -28,23 +28,33 @@ public class LwM2MServiceImpl implements LwM2MService { public LwM2MServerSecurityConfigDefault getServerSecurityInfo(boolean bootstrapServer) { LwM2MSecureServerConfig bsServerConfig = bootstrapServer ? bootstrapConfig.orElse(null) : serverConfig; if (bsServerConfig!= null) { - LwM2MServerSecurityConfigDefault result = getServerSecurityConfig(bsServerConfig); - result.setBootstrapServerIs(bootstrapServer); - if (bootstrapServer) { - result.setShortServerId(null); - } - return result; + return getServerSecurityConfig(bsServerConfig, bootstrapServer); } else { return null; } } - private LwM2MServerSecurityConfigDefault getServerSecurityConfig(LwM2MSecureServerConfig bsServerConfig) { + private LwM2MServerSecurityConfigDefault getServerSecurityConfig(LwM2MSecureServerConfig bsServerConfig, boolean bootstrapServer) { LwM2MServerSecurityConfigDefault bsServ = new LwM2MServerSecurityConfigDefault(); - bsServ.setShortServerId(bsServerConfig.getId()); bsServ.setHost(bsServerConfig.getHost()); bsServ.setPort(bsServerConfig.getPort()); + bsServ.setBootstrapServerIs(bootstrapServer); + if (bootstrapServer) { + if (bsServerConfig.getId() != null) { + log.warn("Ignoring Short Server ID [{}] on the Bootstrap Server entry: cleared to null for backward compatibility (ThingsBoard <= 4.2).", + bsServerConfig.getId()); + } + bsServ.setShortServerId(null); + } else { + Integer configId = bsServerConfig.getId(); + if (configId == null || configId <= 0 || configId >= 65535) { + log.warn("Invalid Short Server ID [{}] for LwM2M Server entry: defaulting to [1] for backward compatibility (ThingsBoard <= 4.2).", + configId); + configId = 1; + } + bsServ.setShortServerId(configId); + } bsServ.setSecurityHost(bsServerConfig.getSecureHost()); bsServ.setSecurityPort(bsServerConfig.getSecurePort()); byte[] publicKeyBase64 = getPublicKey(bsServerConfig); diff --git a/application/src/test/java/org/thingsboard/server/controller/DeviceProfileControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/DeviceProfileControllerTest.java index fa4d65b1a4..3d226d903a 100644 --- a/application/src/test/java/org/thingsboard/server/controller/DeviceProfileControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/DeviceProfileControllerTest.java @@ -34,6 +34,7 @@ import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.device.profile.JsonTransportPayloadConfiguration; import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration; +import org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.LwM2MServerSecurityConfigDefault; import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -1167,6 +1168,29 @@ public class DeviceProfileControllerTest extends AbstractControllerTest { assertThat(deviceProfile.getVersion()).isEqualTo(3); } + @Test + public void testGetLwm2mBootstrapSecurityInfo_BootstrapServer_Returns_ShortServerId_Null() throws Exception { + LwM2MServerSecurityConfigDefault result = doGet( + "/api/lwm2m/deviceProfile/bootstrap/true", + LwM2MServerSecurityConfigDefault.class + ); + Assert.assertNotNull(result); + Assert.assertTrue(result.isBootstrapServerIs()); + Assert.assertNull(result.getShortServerId()); + } + + @Test + public void testGetLwm2mBootstrapSecurityInfo_BootstrapServer_Returns_ShortServerId_1() throws Exception { + LwM2MServerSecurityConfigDefault result = doGet( + "/api/lwm2m/deviceProfile/bootstrap/false", + LwM2MServerSecurityConfigDefault.class + ); + Assert.assertNotNull(result); + Assert.assertFalse(result.isBootstrapServerIs()); + Assert.assertNotNull(result.getShortServerId()); + Assert.assertEquals(Integer.valueOf(123), result.getShortServerId()); + } + private DeviceProfile saveDeviceProfile(String name) { DeviceProfile deviceProfile = createDeviceProfile(name); return saveDeviceProfile(deviceProfile); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/device/profile/lwm2m/bootstrap/LwM2MServerSecurityConfig.java b/common/data/src/main/java/org/thingsboard/server/common/data/device/profile/lwm2m/bootstrap/LwM2MServerSecurityConfig.java index 7b98bae597..e2bdb9a6fd 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/device/profile/lwm2m/bootstrap/LwM2MServerSecurityConfig.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/device/profile/lwm2m/bootstrap/LwM2MServerSecurityConfig.java @@ -13,7 +13,7 @@ public class LwM2MServerSecurityConfig implements Serializable { @Schema(description = "Server short Id. Used as link to associate server Object Instance. This identifier uniquely identifies each LwM2M Server configured for the LwM2M Client. " + "This Resource MUST be set when the Bootstrap-Server Resource has a value of 'false' and MUST be null when Bootstrap-Server is 'true'. " + - "The values ID:0 and ID:65535 values MUST NOT be used for identifying the LwM2M Server.", example = "1", accessMode = Schema.AccessMode.READ_ONLY) + "The values 0 and 65535 MUST NOT be used for identifying the LwM2M Server.", example = "1", accessMode = Schema.AccessMode.READ_ONLY) protected Integer shortServerId; /** Security -> ObjectId = 0 'LWM2M Security' */ @Schema(description = "Is Bootstrap Server or Lwm2m Server. " + diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java index 3a4fa13281..2231b52d2e 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java @@ -220,7 +220,7 @@ class DeviceProfileDataValidatorTest { (Lwm2mDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration(); return transportConfig.getBootstrap().stream() - .filter(credential -> ((NoSecLwM2MBootstrapServerCredential)credential).isBootstrapServerIs()) + .filter(credential -> ((AbstractLwM2MBootstrapServerCredential)credential).isBootstrapServerIs()) .findFirst() .orElseThrow(() -> new AssertionError("Bootstrap server not found in profile")); } @@ -231,7 +231,7 @@ class DeviceProfileDataValidatorTest { validator.validateDataImpl(tenantId, deviceProfile); verify(validator).validateString("Device profile name", deviceProfile.getName()); LwM2MBootstrapServerCredential bsCredential = getBootstrapCredential(deviceProfile); - assertThat(((NoSecLwM2MBootstrapServerCredential)bsCredential).getShortServerId()); + assertThat(((AbstractLwM2MBootstrapServerCredential) bsCredential).getShortServerId()).isNull(); } }