From d0de9db15bfb6fda5672a1966818246e8767f981 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Fri, 21 Jun 2019 17:05:29 +0200 Subject: [PATCH] Fix unwanted warning when creating server without secure CoAP endpoint. --- .../LeshanBootstrapServerBuilder.java | 17 +++++++++++++---- .../californium/LeshanServerBuilder.java | 19 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java index a7f69a50a9..9f6753cb80 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java @@ -397,18 +397,19 @@ public BootstrapHandler create(BootstrapConfigStore store, LwM2mBootstrapRequest // handle dtlsConfig DtlsConnectorConfig dtlsConfig = null; - if (!noSecuredEndpoint) { + if (!noSecuredEndpoint && shouldTryToCreateSecureEndpoint()) { if (dtlsConfigBuilder == null) { dtlsConfigBuilder = new DtlsConnectorConfig.Builder(); } + // Set default DTLS setting for Leshan unless user change it. DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig(); // Handle PSK Store - if (incompleteConfig.getPskStore() == null && securityStore != null) { - dtlsConfigBuilder.setPskStore(new LwM2mBootstrapPskStore(securityStore)); - } else { + if (incompleteConfig.getPskStore() != null) { LOG.warn( "PskStore should be automatically set by Leshan. Using a custom implementation is not advised."); + } else if (securityStore != null) { + dtlsConfigBuilder.setPskStore(new LwM2mBootstrapPskStore(securityStore)); } // Handle secure address @@ -538,6 +539,14 @@ public BootstrapHandler create(BootstrapConfigStore store, LwM2mBootstrapRequest bootstrapHandlerFactory, model, coapConfig); } + /** + * @return true if we should try to create a secure endpoint on {@link #build()} + */ + protected boolean shouldTryToCreateSecureEndpoint() { + return dtlsConfigBuilder != null || certificateChain != null || privateKey != null || publicKey != null + || securityStore != null || trustedCertificates != null; + } + /** * Create the LeshanBootstrapServer. *

diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServerBuilder.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServerBuilder.java index 813ef492b4..b1771d4c66 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServerBuilder.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServerBuilder.java @@ -379,18 +379,19 @@ public LeshanServer build() { // handle dtlsConfig DtlsConnectorConfig dtlsConfig = null; - if (!noSecuredEndpoint) { + if (!noSecuredEndpoint && shouldTryToCreateSecureEndpoint()) { if (dtlsConfigBuilder == null) { dtlsConfigBuilder = new DtlsConnectorConfig.Builder(); } - // set default DTLS setting for Leshan unless user change it. + // Set default DTLS setting for Leshan unless user change it. DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig(); + // Handle PSK Store - if (incompleteConfig.getPskStore() == null && securityStore != null) { - dtlsConfigBuilder.setPskStore(new LwM2mPskStore(this.securityStore, registrationStore)); - } else { + if (incompleteConfig.getPskStore() != null) { LOG.warn( "PskStore should be automatically set by Leshan. Using a custom implementation is not advised."); + } else if (securityStore != null) { + dtlsConfigBuilder.setPskStore(new LwM2mPskStore(this.securityStore, registrationStore)); } // Handle secure address @@ -516,4 +517,12 @@ public LeshanServer build() { return new LeshanServer(unsecuredEndpoint, securedEndpoint, registrationStore, securityStore, authorizer, modelProvider, encoder, decoder, coapConfig, noQueueMode, awakeTimeProvider, registrationIdProvider); } + + /** + * @return true if we should try to create a secure endpoint on {@link #build()} + */ + protected boolean shouldTryToCreateSecureEndpoint() { + return dtlsConfigBuilder != null || certificateChain != null || privateKey != null || publicKey != null + || securityStore != null || trustedCertificates != null; + } }