Azure / azure-iot-sdk-node

A Node.js SDK for connecting devices to Microsoft Azure IoT services
https://docs.microsoft.com/en-us/azure/iot-hub/
Other
261 stars 227 forks source link

IoTHub Client SAS token expiration [Technical Question] #945

Closed Zachery2008 closed 3 years ago

Zachery2008 commented 3 years ago

We have a question about the IoTHub service client which is created by connectionSrtring. By default, the client's SAS token would expire in one hour. Does this mean we need to re-create the client every hour? or the client will automatically renew the token itself?

The real issue is: sometimes the client is losing connection of listening file upload notification silently, but we never successfully grabbed "disconnection" event. It's just gone. The solution we came up is that we establish the client connection periodically(1hour). To do this, do we need to re-create a new client or use the previous client to open the connection again with consideration of its SAS token already expired?

Right now, we are re-create the client everytime.

let client = new DevieMessageService({
  client: Client.fromConnectionString(connStr),
})
// Start Client
eventHubClient.start()

setInterval(async () => {
  // Renew SAS Token for every hour
  try {
    await client .stop()
    console.log(`Device message listener client is closed`)
  } catch (err) {
    console.log(`Error occured during closing client: ${err}`)
  }

  client = new DevieMessageService({
    client: Client.fromConnectionString(connStr),
  })

  client.start()
}, RECEIVER_RECONNECT_TIME)
anthonyvercolano commented 3 years ago

There are two questions only somewhat related.

When connecting via a connection string you should NOT have to re-create the client every hour. This is taken care of by the service client itself.

Now as to the question of what's going on with your file upload notification.

You can get logs by setting DEBUG="azure,rhea"

These will generate log files. Do not just publish the logs here. It will contain private data.

You should be able to examine the logs. Look particularly for strings of the form "Error on the file notification link". Are you seeing anything that looks like errors handling going on?

Is this always reproducible?

Does it happen after just 1 hour?

Zachery2008 commented 3 years ago

@anthonyvercolano Thank you for your response.

Quote: "When connecting via a connection string you should NOT have to re-create the client every hour. This is taken care of by the service client itself."

Does this suggest that the client will renew its SAS token at the client level even though it had only 1 hour valid SAS token when it was created? or the client SAS token will expire but each https request has a new SAS token at http request level so we don't have to worry?

Quote: "You can get logs by setting DEBUG="azure,rhea"

We actually never enabled this Azure logging. I will enable it as soon as possible.

Quote: "Is this always reproducible? Does it happen after just 1 hour?"

We actually opened an issue a month ago about losing connection of iothub file upload notification. And followed your suggestion to listening to "disconnect" event and reconnect. We're good about several days. However, there was an incident that we lost all connection, and we didn't know this during the weekend. and we didn't grab "disconnect" event. It's really hard to purposely reproduce.

Another question, say like a 'disconnect' happened a few days after the client was created. Could you still use the same client to open a new connection?

anthonyvercolano commented 3 years ago

The not worry about it case would be if you created the client with an invocation of .fromConnectionString

You would have to provide a new SharedAccessSignature if you created the client with an invocation of .fromSharedAccessSignature. You would invoke .updateSharedAccessSignature on the client you created with .fromSharedAccessSignature.

anthonyvercolano commented 3 years ago

Well in the case of a disconnect on the service side, is it simpler for you do simply do a close to clean up then after the callback from the close to do an open? Or you should be able to create a whole new client again at that point.

anthonyvercolano commented 3 years ago

Closing due to in-activity.