From 969392e1996820298852f5cbaea2fc306527a0f3 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Tue, 6 Oct 2026 13:44:03 +0300 Subject: [PATCH 1/2] fix: reload CoAP DTLS certificates in CoAP integration services CertificateReloadManager was limited to transport services, while the shared CoAP server also runs in tb-integration, tb-integration-executor and tb-core (coap.server.enabled). There the DTLS reload callback was registered but never triggered, so rotated certificates were not picked up by CoAP integrations. - Remove the service-type condition from CertificateReloadManager: it already discovers SslCredentialsConfig beans at runtime, and now skips the scheduler when there is nothing to watch - Import CertificateReloadManager into the remote CoAP integration application - Add transport.ssl.certificate.reload settings to tb-coap-integration.yml and tb-integration-executor.yml - Replace unused flat DTLS keys in tb-coap-integration.yml with the coap.dtls.credentials block read by TbCoapDtlsSettings --- .../service/CertificateReloadManager.java | 9 ++- ...CertificateReloadManagerDiscoveryTest.java | 74 +++++++++++++++++++ ...ThingsboardCoapIntegrationApplication.java | 3 + .../main/resources/tb-coap-integration.yml | 47 +++++++++--- .../resources/tb-integration-executor.yml | 13 +++- .../python/license-headers/curations.json | 2 +- 6 files changed, 133 insertions(+), 15 deletions(-) create mode 100644 common/transport/transport-api/src/test/java/org/thingsboard/server/common/transport/service/CertificateReloadManagerDiscoveryTest.java diff --git a/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/service/CertificateReloadManager.java b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/service/CertificateReloadManager.java index 53ed70ec37..e33b0bd0b2 100644 --- a/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/service/CertificateReloadManager.java +++ b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/service/CertificateReloadManager.java @@ -1,5 +1,6 @@ // SPDX-FileCopyrightText: Copyright The ThingsBoard Authors -// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 package org.thingsboard.server.common.transport.service; import lombok.extern.slf4j.Slf4j; @@ -12,7 +13,6 @@ import org.springframework.stereotype.Component; import org.thingsboard.common.util.ThingsBoardThreadFactory; import org.thingsboard.server.common.transport.config.ssl.SslCredentials; import org.thingsboard.server.common.transport.config.ssl.SslCredentialsConfig; -import org.thingsboard.server.queue.util.TbTransportComponent; import java.io.InputStream; import java.nio.file.Files; @@ -30,7 +30,6 @@ import java.util.concurrent.TimeUnit; @Slf4j @Component -@TbTransportComponent public class CertificateReloadManager implements SmartInitializingSingleton, DisposableBean { private static final int MAX_CONSECUTIVE_FAILURES = 10; @@ -143,6 +142,10 @@ public class CertificateReloadManager implements SmartInitializingSingleton, Dis log.info("Initializing Certificate Reload Manager..."); discoverAndRegisterSslCredentials(); + if (watchers.isEmpty()) { + log.debug("No certificate files to watch. Skipping scheduler initialization..."); + return; + } scheduler = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("certificate-reload-manager")); scheduler.scheduleWithFixedDelay(this::checkCertificates, checkIntervalInSeconds, checkIntervalInSeconds, TimeUnit.SECONDS); diff --git a/common/transport/transport-api/src/test/java/org/thingsboard/server/common/transport/service/CertificateReloadManagerDiscoveryTest.java b/common/transport/transport-api/src/test/java/org/thingsboard/server/common/transport/service/CertificateReloadManagerDiscoveryTest.java new file mode 100644 index 0000000000..59bfd816fa --- /dev/null +++ b/common/transport/transport-api/src/test/java/org/thingsboard/server/common/transport/service/CertificateReloadManagerDiscoveryTest.java @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +package org.thingsboard.server.common.transport.service; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.test.util.ReflectionTestUtils; +import org.thingsboard.server.common.transport.config.ssl.SslCredentials; +import org.thingsboard.server.common.transport.config.ssl.SslCredentialsConfig; + +import java.nio.file.Path; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CertificateReloadManagerDiscoveryTest { + + @TempDir + Path tempDir; + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withUserConfiguration(CertificateReloadManager.class); + + @Test + public void givenNoSslCredentialsConfigs_whenContextStarts_thenManagerCreatedWithoutScheduler() { + contextRunner.run(context -> { + assertThat(context).hasSingleBean(CertificateReloadManager.class); + assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNull(); + }); + } + + @Test + public void givenDisabledSslCredentialsConfig_whenContextStarts_thenSchedulerNotStarted() { + SslCredentialsConfig config = mockSslCredentialsConfig(false, List.of(tempDir.resolve("server.pem"))); + contextRunner + .withBean(SslCredentialsConfig.class, () -> config) + .run(context -> assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNull()); + } + + @Test + public void givenSslCredentialsConfigWithCertificateFile_whenContextStarts_thenSchedulerStarted() { + SslCredentialsConfig config = mockSslCredentialsConfig(true, List.of(tempDir.resolve("server.pem"))); + contextRunner + .withBean(SslCredentialsConfig.class, () -> config) + .run(context -> assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNotNull()); + } + + @Test + public void givenReloadDisabled_whenContextStarts_thenSchedulerNotStarted() { + SslCredentialsConfig config = mockSslCredentialsConfig(true, List.of(tempDir.resolve("server.pem"))); + contextRunner + .withPropertyValues("transport.ssl.certificate.reload.enabled=false") + .withBean(SslCredentialsConfig.class, () -> config) + .run(context -> assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNull()); + } + + private static SslCredentialsConfig mockSslCredentialsConfig(boolean enabled, List certificateFilePaths) { + SslCredentials credentials = mock(SslCredentials.class); + when(credentials.getCertificateFilePaths()).thenReturn(certificateFilePaths); + SslCredentialsConfig config = mock(SslCredentialsConfig.class); + when(config.isEnabled()).thenReturn(enabled); + when(config.getName()).thenReturn("Test SSL Credentials"); + when(config.getCredentials()).thenReturn(credentials); + return config; + } + + private static Object getScheduler(CertificateReloadManager manager) { + return ReflectionTestUtils.getField(manager, "scheduler"); + } + +} diff --git a/integration/coap/src/main/java/org/thingsboard/integration/ThingsboardCoapIntegrationApplication.java b/integration/coap/src/main/java/org/thingsboard/integration/ThingsboardCoapIntegrationApplication.java index 46f419d853..691675e579 100644 --- a/integration/coap/src/main/java/org/thingsboard/integration/ThingsboardCoapIntegrationApplication.java +++ b/integration/coap/src/main/java/org/thingsboard/integration/ThingsboardCoapIntegrationApplication.java @@ -5,13 +5,16 @@ package org.thingsboard.integration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; import org.springframework.scheduling.annotation.EnableAsync; +import org.thingsboard.server.common.transport.service.CertificateReloadManager; import java.util.Arrays; @SpringBootApplication @EnableAsync @ComponentScan({"org.thingsboard.integration", "org.thingsboard.script.api", "org.thingsboard.server.coapserver"}) +@Import(CertificateReloadManager.class) public class ThingsboardCoapIntegrationApplication { private static final String SPRING_CONFIG_NAME_KEY = "--spring.config.name"; diff --git a/integration/coap/src/main/resources/tb-coap-integration.yml b/integration/coap/src/main/resources/tb-coap-integration.yml index a7fde74529..267c91dba0 100644 --- a/integration/coap/src/main/resources/tb-coap-integration.yml +++ b/integration/coap/src/main/resources/tb-coap-integration.yml @@ -3,6 +3,8 @@ # SPDX-License-Identifier: BUSL-1.1 # +spring.main.allow-circular-references: "true" # Spring Boot configuration property that controls whether circular dependencies between beans are allowed. + server: # Server bind address address: "${HTTP_BIND_ADDRESS:0.0.0.0}" @@ -134,14 +136,41 @@ coap: # } MaxFragmentLength; # TLS already requires clients and servers to support fragmentation of handshake messages. max_fragment_length: "${COAP_DTLS_MAX_FRAGMENT_LENGTH:1024}" - # Path to the key store that holds the certificate - key_store: "${COAP_DTLS_KEY_STORE:coapserver.jks}" - # Password used to access the key store - key_store_password: "${COAP_DTLS_KEY_STORE_PASSWORD:server_ks_password}" - # Password used to access the key - key_password: "${COAP_DTLS_KEY_PASSWORD:server_key_password}" - # Key alias - key_alias: "${COAP_DTLS_KEY_ALIAS:serveralias}" + # Server DTLS credentials + credentials: + # Server credentials type (PEM - pem certificate file; KEYSTORE - java keystore) + type: "${COAP_DTLS_CREDENTIALS_TYPE:PEM}" + # PEM server credentials + pem: + # Path to the server certificate file (holds server certificate or certificate chain, may include server private key) + cert_file: "${COAP_DTLS_PEM_CERT:coapserver.pem}" + # Path to the server certificate private key file. Optional by default. Required if the private key is not present in server certificate file; + key_file: "${COAP_DTLS_PEM_KEY:coapserver_key.pem}" + # Server certificate private key password (optional) + key_password: "${COAP_DTLS_PEM_KEY_PASSWORD:server_key_password}" + # Keystore server credentials + keystore: + # Type of the key store (JKS or PKCS12) + type: "${COAP_DTLS_KEY_STORE_TYPE:JKS}" + # Path to the key store that holds the SSL certificate + store_file: "${COAP_DTLS_KEY_STORE:coapserver.jks}" + # Password used to access the key store + store_password: "${COAP_DTLS_KEY_STORE_PASSWORD:server_ks_password}" + # Key alias + key_alias: "${COAP_DTLS_KEY_ALIAS:serveralias}" + # Password used to access the key + key_password: "${COAP_DTLS_KEY_PASSWORD:server_key_password}" + +transport: + ssl: + # SSL/TLS settings + certificate: + # X.509 certificate configuration to auto-detect and reload certificate used by the CoAP server (DTLS) in real-time + reload: + # Enable/disable automatic SSL certificates reload + enabled: "${TB_TRANSPORT_SSL_CERTIFICATE_RELOAD_ENABLED:true}" + # Check interval in seconds for certificates reload + check_interval_seconds: "${TB_TRANSPORT_SSL_CERTIFICATE_RELOAD_CHECK_INTERVAL_SECONDS:60}" service: - type: "${TB_SERVICE_TYPE:tb-integration}" \ No newline at end of file + type: "${TB_SERVICE_TYPE:tb-integration}" diff --git a/integration/executor/src/main/resources/tb-integration-executor.yml b/integration/executor/src/main/resources/tb-integration-executor.yml index de77beb11c..7e2f6d107c 100644 --- a/integration/executor/src/main/resources/tb-integration-executor.yml +++ b/integration/executor/src/main/resources/tb-integration-executor.yml @@ -429,14 +429,23 @@ js: # Interval of logging for remote JS executor stats print_interval_ms: "${TB_JS_REMOTE_STATS_PRINT_INTERVAL_MS:10000}" -# JSON converter parameters -# Controls JSON telemetry/attribute parsing behaviour, including type casting and maximum string value length. +# JSON converter and SSL certificate reload parameters +# Controls JSON telemetry/attribute parsing behaviour, including type casting and maximum string value length, and real-time reload of CoAP DTLS certificates. transport: json: # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:true}" # Maximum allowed string value length when processing Telemetry/Attributes JSON (0 value disables string value length check) max_string_value_length: "${JSON_MAX_STRING_VALUE_LENGTH:0}" + ssl: + # SSL/TLS settings + certificate: + # X.509 certificate configuration to auto-detect and reload certificate used by the CoAP server (DTLS) in real-time + reload: + # Enable/disable automatic SSL certificates reload + enabled: "${TB_TRANSPORT_SSL_CERTIFICATE_RELOAD_ENABLED:true}" + # Check interval in seconds for certificates reload + check_interval_seconds: "${TB_TRANSPORT_SSL_CERTIFICATE_RELOAD_CHECK_INTERVAL_SECONDS:60}" # Cache parameters # Configures the cache backend type (Caffeine or Redis) and per-cache TTL and size limits. diff --git a/tools/src/main/python/license-headers/curations.json b/tools/src/main/python/license-headers/curations.json index 75b0eb6a2c..91d0e42065 100644 --- a/tools/src/main/python/license-headers/curations.json +++ b/tools/src/main/python/license-headers/curations.json @@ -797,7 +797,7 @@ "path": "integration/coap/src/main/java/org/thingsboard/integration/ThingsboardCoapIntegrationApplication.java", "header": "busl", "reason": "Launch, logging or runtime scaffolding for a PE service. Maintainer explicitly resolved this PE module scaffolding as PE-only; matching CE startup/logging conventions do not override that decision. Reviewed similarity candidate transport/http/src/main/java/org/thingsboard/server/http/ThingsboardHttpTransportApplication.java at 0d3b41e002f958887a317ca8366fdc4efa549058 (blob 98be09069a487befab217c9b7c036fc6132fc97b).", - "contentHash": "sha256:691ed1e7c96845e81ccfdcf522c836794f4045180c74d50be1613122517397c4" + "contentHash": "sha256:d603f73f715f82368dcfd54325b95c653684077d49afeafb69b9117b7196dfbc" }, { "path": "integration/coap/src/main/resources/logback.xml", From 1def2c6506998b17ab1e577e793efcce4e6cc0d2 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Thu, 15 Oct 2026 11:21:36 +0300 Subject: [PATCH 2/2] Move allow-circular-references into the spring block of the CoAP integration config --- integration/coap/src/main/resources/tb-coap-integration.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration/coap/src/main/resources/tb-coap-integration.yml b/integration/coap/src/main/resources/tb-coap-integration.yml index 267c91dba0..560f20b9f7 100644 --- a/integration/coap/src/main/resources/tb-coap-integration.yml +++ b/integration/coap/src/main/resources/tb-coap-integration.yml @@ -3,8 +3,6 @@ # SPDX-License-Identifier: BUSL-1.1 # -spring.main.allow-circular-references: "true" # Spring Boot configuration property that controls whether circular dependencies between beans are allowed. - server: # Server bind address address: "${HTTP_BIND_ADDRESS:0.0.0.0}" @@ -29,6 +27,8 @@ spring: exclude: - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration - org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration + main: + allow-circular-references: "true" # Spring Boot configuration property that controls whether circular dependencies between beans are allowed. storage: # Location of the folder to store data files