Azure / Azure/azure-iot-sdk-node
ModuleClient.setOptions overwrites the previous options
- Dominant language
- JavaScript
- Stars
- 268
- Forks
- 228
- PR merge metrics
- No merged PRs in 30d
Description
# Context
- **OS and version used:** Ubuntu 22.04.4 LTS (WSL2)
- **Node.js version:** v18.19.1
- **npm version:** 10.2.4
- **list of installed packages:**
- azure-iot-device-mqtt@1.16.3
- azure-iot-device@1.18.3
## Description of the issue
The `ca` option is set in [_fromEnvironmentEdge](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/device/core/src/module_client.ts#L449) function.
```js
const methodClient = new MethodClient(authenticationProvider);
methodClient.setOptions({ ca });
```
However, [`MqttBase.setOptions`](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/common/transport/mqtt/src/mqtt_base.ts#L382) does not merge `_options`, so calling `setOptions` method in the callback of `ModuleClient.fromEnvironment` will overwrite the `ca` option, resulting in a certificate error.
```js
setOptions(options: any): void {
this._options = options;
}
```
## Code sample exhibiting the issue
```js
'use strict';
var Transport = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').ModuleClient;
Client.fromEnvironment(Transport, function (err, client) {
if (err) {
console.error(err.toString());
process.exit(-1);
} else {
let options = {};
client.setOptions(options);
client.open(function (err) {
if (err) {
console.error(err.toString());
process.exit(-1);
}
console.log('IoT Hub module client initialized');
});
}
});
```
## Console log of the issue
```
$ nodejs app.js
UnauthorizedError: mqtt.js returned Failure on first connection (Not authorized): self-signed certificate error
```
## Workaround
Get the current options from `client._methodClient._options` (there is not `getOptions` method!) and merge them by yourself.
```js
let options = client._methodClient._options;
options.tokenRenewal = {
tokenValidTimeInSeconds: 3600,
tokenRenewalMarginInSeconds: 900
};
client.setOptions(options);
```
Contributor guide
Assessment
This issue has not been assessed yet.