Browse Source

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

pull/16133/head
nickAS21 2 months ago
parent
commit
3f37cb28b3
  1. 11
      application/src/main/java/org/thingsboard/server/service/lwm2m/LwM2MServiceImpl.java
  2. 2
      application/src/main/resources/thingsboard.yml
  3. 32
      application/src/test/java/org/thingsboard/server/controller/LwM2MServiceImplInvalidConfigTest.java
  4. 2
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/config/LwM2MTransportServerConfig.java
  5. 2
      transport/lwm2m/src/main/resources/tb-lwm2m-transport.yml

11
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);
}

2
application/src/main/resources/thingsboard.yml

@ -1291,7 +1291,7 @@ transport:
# <li> A value that are > 4: MultiNodeConnectionIdGenerator is used </li> </ul>
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}"

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

2
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

2
transport/lwm2m/src/main/resources/tb-lwm2m-transport.yml

@ -174,7 +174,7 @@ transport:
# <li> A value that are > 4: MultiNodeConnectionIdGenerator is used </li> </ul>
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}"

Loading…
Cancel
Save