Browse Source

Merge pull request #16258 from AndriiLandiak/fix/coap-integration-certificate-reload

Fixed CoAP DTLS certificate reload for CoAP integrations
pull/16318/head
Viacheslav Klimov 2 months ago
committed by GitHub
parent
commit
e73a5af759
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/service/CertificateReloadManager.java
  2. 74
      common/transport/transport-api/src/test/java/org/thingsboard/server/common/transport/service/CertificateReloadManagerDiscoveryTest.java
  3. 3
      integration/coap/src/main/java/org/thingsboard/integration/ThingsboardCoapIntegrationApplication.java
  4. 47
      integration/coap/src/main/resources/tb-coap-integration.yml
  5. 13
      integration/executor/src/main/resources/tb-integration-executor.yml
  6. 2
      tools/src/main/python/license-headers/curations.json

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

74
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<Path> 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");
}
}

3
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";

47
integration/coap/src/main/resources/tb-coap-integration.yml

@ -27,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
@ -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}"
type: "${TB_SERVICE_TYPE:tb-integration}"

13
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.

2
tools/src/main/python/license-headers/curations.json

@ -815,7 +815,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",

Loading…
Cancel
Save