From 8df1bdabf90fa2a2b60dcb8e3cee37b8360c4d13 Mon Sep 17 00:00:00 2001 From: timtay-microsoft Date: Tue, 17 Oct 2023 09:52:06 -0700 Subject: [PATCH 1/2] fix(iot-dev): fix bug where client options was overridden If a user provides a client options instance, but without an SSLContext, we should still keep all the settings they provided while adding the SSLContext generated by the SDK --- .../azure/sdk/iot/device/ModuleClient.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java b/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java index 06f090e39f..6cc5acd95f 100644 --- a/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java +++ b/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java @@ -224,9 +224,31 @@ public static ModuleClient createFromEnvironment(UnixDomainSocketChannel unixDom sslContext = new IotHubSSLContext().getSSLContext(); } - if (clientOptions == null || clientOptions.getSslContext() == null) + if (clientOptions != null && clientOptions.getSslContext() == null) { - // only override the SSLContext if the user didn't set it + // Clone the existing client options, but with the new SSLContext + clientOptions = ClientOptions.builder() + .sasTokenExpiryTime(clientOptions.getSasTokenExpiryTime()) + .logRoutineDisconnectsAsErrors(clientOptions.isLoggingRoutineDisconnectsAsErrors()) + .receiveInterval(clientOptions.getReceiveInterval()) + .amqpAuthenticationSessionTimeout(clientOptions.getAmqpAuthenticationSessionTimeout()) + .amqpDeviceSessionTimeout(clientOptions.getAmqpDeviceSessionTimeout()) + .httpsConnectTimeout(clientOptions.getHttpsConnectTimeout()) + .httpsReadTimeout(clientOptions.getHttpsReadTimeout()) + .modelId(clientOptions.getModelId()) + .proxySettings(clientOptions.getProxySettings()) + .keepAliveInterval(clientOptions.getKeepAliveInterval()) + .threadNamePrefix(clientOptions.getThreadNamePrefix()) + .threadNameSuffix(clientOptions.getThreadNameSuffix()) + .useIdentifiableThreadNames(clientOptions.isUsingIdentifiableThreadNames()) + .sendInterval(clientOptions.getSendInterval()) + .messagesSentPerSendInterval(clientOptions.getMessagesSentPerSendInterval()) + .sslContext(sslContext) + .build(); + } + else if (clientOptions == null) + { + // only override the client options completely if the user didn't provide any clientOptions = ClientOptions.builder().sslContext(sslContext).build(); } else From caa6d0d91342c424e53428e45bfaa185ceb61473 Mon Sep 17 00:00:00 2001 From: timtay-microsoft Date: Tue, 17 Oct 2023 14:39:39 -0700 Subject: [PATCH 2/2] fixup --- .../azure/sdk/iot/device/ClientOptions.java | 2 +- .../azure/sdk/iot/device/ModuleClient.java | 18 +----------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ClientOptions.java b/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ClientOptions.java index d6273464ef..22b0fbe0a5 100644 --- a/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ClientOptions.java +++ b/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ClientOptions.java @@ -13,7 +13,7 @@ /** * Options that allow configuration of the device client instance during initialization. */ -@Builder +@Builder(toBuilder = true) public final class ClientOptions { private static final int DEFAULT_HTTPS_CONNECT_TIMEOUT_MILLISECONDS = 0; diff --git a/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java b/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java index 6cc5acd95f..d68e64522e 100644 --- a/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java +++ b/iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/ModuleClient.java @@ -227,23 +227,7 @@ public static ModuleClient createFromEnvironment(UnixDomainSocketChannel unixDom if (clientOptions != null && clientOptions.getSslContext() == null) { // Clone the existing client options, but with the new SSLContext - clientOptions = ClientOptions.builder() - .sasTokenExpiryTime(clientOptions.getSasTokenExpiryTime()) - .logRoutineDisconnectsAsErrors(clientOptions.isLoggingRoutineDisconnectsAsErrors()) - .receiveInterval(clientOptions.getReceiveInterval()) - .amqpAuthenticationSessionTimeout(clientOptions.getAmqpAuthenticationSessionTimeout()) - .amqpDeviceSessionTimeout(clientOptions.getAmqpDeviceSessionTimeout()) - .httpsConnectTimeout(clientOptions.getHttpsConnectTimeout()) - .httpsReadTimeout(clientOptions.getHttpsReadTimeout()) - .modelId(clientOptions.getModelId()) - .proxySettings(clientOptions.getProxySettings()) - .keepAliveInterval(clientOptions.getKeepAliveInterval()) - .threadNamePrefix(clientOptions.getThreadNamePrefix()) - .threadNameSuffix(clientOptions.getThreadNameSuffix()) - .useIdentifiableThreadNames(clientOptions.isUsingIdentifiableThreadNames()) - .sendInterval(clientOptions.getSendInterval()) - .messagesSentPerSendInterval(clientOptions.getMessagesSentPerSendInterval()) - .sslContext(sslContext) + clientOptions = clientOptions.toBuilder().sslContext(sslContext) .build(); } else if (clientOptions == null)