Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleClient.setOptions overwrites the previous options #1214

Open
hiroyha1 opened this issue Mar 5, 2024 · 0 comments
Open

ModuleClient.setOptions overwrites the previous options #1214

hiroyha1 opened this issue Mar 5, 2024 · 0 comments
Labels

Comments

@hiroyha1
Copy link

hiroyha1 commented Mar 5, 2024

Context

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);
@hiroyha1 hiroyha1 added the bug label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant