Browse Source

lwm2m: fix bootstrap-short-server-id = null - comments 4

pull/16133/head
nickAS21 2 months ago
parent
commit
dfd0c5c7fa
  1. 26
      application/src/main/java/org/thingsboard/server/service/lwm2m/LwM2MServiceImpl.java
  2. 24
      application/src/test/java/org/thingsboard/server/controller/DeviceProfileControllerTest.java
  3. 2
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/lwm2m/bootstrap/LwM2MServerSecurityConfig.java
  4. 4
      dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java

26
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);

24
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);

2
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. " +

4
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();
}
}

Loading…
Cancel
Save