Skip to content

ModuleClient.setOptions overwrites the previous options #1214

Open

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

const methodClient = new MethodClient(authenticationProvider);
methodClient.setOptions({ ca });

However, MqttBase.setOptions does not merge _options, so calling setOptions method in the callback of ModuleClient.fromEnvironment will overwrite the ca option, resulting in a certificate error.

  setOptions(options: any): void {
    this._options = options;
  }

Code sample exhibiting the issue

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

    let options = client._methodClient._options;
    options.tokenRenewal = {
      tokenValidTimeInSeconds: 3600,
      tokenRenewalMarginInSeconds: 900
    };
    client.setOptions(options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions