Skip to content

Commit

Permalink
fix(iot-device): Fix issue where we accidentally disposed the user su…
Browse files Browse the repository at this point in the history
…pplied certificates
  • Loading branch information
abhipsaMisra committed Feb 6, 2021
1 parent 3cab90e commit 3eaf88e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
48 changes: 45 additions & 3 deletions e2e/test/iothub/messaging/MessageReceiveE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,52 @@ public async Task DeviceDoesNotReceivePendingMessageUsingCallback_Mqtt()
[LoggedTestMethod]
public async Task DeviceDoesNotReceivePendingMessageUsingCallback_MqttWs()
{
var settings = new ITransportSettings[] { new MqttTransportSettings(Client.TransportType.Mqtt_Tcp_Only) { CleanSession = true } };
var settings = new ITransportSettings[] { new MqttTransportSettings(Client.TransportType.Mqtt_WebSocket_Only) { CleanSession = true } };
await DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceType.Sasl, settings).ConfigureAwait(false);
}

[LoggedTestMethod]
public async Task DeviceDoesNotReceivePendingMessageUsingCallback_Amqp()
{
var settings = new ITransportSettings[] { new AmqpTransportSettings(Client.TransportType.Amqp_Tcp_Only) };
await DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceType.Sasl, settings).ConfigureAwait(false);
}

[LoggedTestMethod]
public async Task DeviceDoesNotReceivePendingMessageUsingCallback_AmqpWs()
{
var settings = new ITransportSettings[] { new AmqpTransportSettings(Client.TransportType.Amqp_WebSocket_Only) };
await DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceType.Sasl, settings).ConfigureAwait(false);
}

[LoggedTestMethod]
public async Task X509_DeviceDoesNotReceivePendingMessageUsingCallback_Mqtt()
{
var settings = new ITransportSettings[] { new MqttTransportSettings(Client.TransportType.Mqtt_Tcp_Only) { CleanSession = true } };
await DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceType.X509, settings).ConfigureAwait(false);
}

[LoggedTestMethod]
public async Task X509_DeviceDoesNotReceivePendingMessageUsingCallback_MqttWs()
{
var settings = new ITransportSettings[] { new MqttTransportSettings(Client.TransportType.Mqtt_WebSocket_Only) { CleanSession = true } };
await DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceType.X509, settings).ConfigureAwait(false);
}

[LoggedTestMethod]
public async Task X509_DeviceDoesNotReceivePendingMessageUsingCallback_Amqp()
{
var settings = new ITransportSettings[] { new AmqpTransportSettings(Client.TransportType.Amqp_Tcp_Only) };
await DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceType.X509, settings).ConfigureAwait(false);
}

[LoggedTestMethod]
public async Task X509_DeviceDoesNotReceivePendingMessageUsingCallback_AmqpWs()
{
var settings = new ITransportSettings[] { new AmqpTransportSettings(Client.TransportType.Amqp_WebSocket_Only) };
await DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceType.X509, settings).ConfigureAwait(false);
}

public static (Message message, string payload, string p1Value) ComposeC2dTestMessage(MsTestLogger logger)
{
string payload = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -875,11 +917,11 @@ private async Task DoNotReceiveMessagesSentBeforeSubscriptionAsync(TestDeviceTyp
testDeviceCallbackHandler.Dispose();
testDeviceCallbackHandler = null;

// Create a device client instance over MQTT, with CleanSession flag set to true.
// This will ensure that messages sent before the device had subscribed to c2d topic are not delivered.
deviceClient = testDevice.CreateDeviceClient(settings);
testDeviceCallbackHandler = new TestDeviceCallbackHandler(deviceClient, testDevice, Logger);

// Open the device client - for MQTT, this will connect the device with CleanSession flag set to true.
// This will ensure that messages sent before the device had subscribed to c2d topic are not delivered.
await deviceClient.OpenAsync().ConfigureAwait(false);

// Send the message from service.
Expand Down
13 changes: 12 additions & 1 deletion iothub/device/src/DeviceAuthenticationWithX509Certificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Azure.Devices.Client
/// <summary>
/// Authentication method that uses a X.509 certificate
/// </summary>
public sealed class DeviceAuthenticationWithX509Certificate : IAuthenticationMethod
public sealed class DeviceAuthenticationWithX509Certificate : IAuthenticationMethod, IDisposable
{
private string _deviceId;

Expand Down Expand Up @@ -80,5 +80,16 @@ private void SetDeviceId(string deviceId)

_deviceId = deviceId;
}

/// <summary>
/// Dispose the X509 certificate associated with this authentication method.
/// </summary>
public void Dispose()
{
#if !NET451
Certificate?.Dispose();
Certificate = null;
#endif
}
}
}
4 changes: 0 additions & 4 deletions iothub/device/src/InternalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,10 +1980,6 @@ public void Dispose()
_fileUploadHttpTransportHandler?.Dispose();
_deviceReceiveMessageSemaphore?.Dispose();
_twinDesiredPropertySemaphore?.Dispose();
#if !NET451
Certificate?.Dispose();
Certificate = null;
#endif
IotHubConnectionString?.TokenRefresher?.Dispose();
}

Expand Down

0 comments on commit 3eaf88e

Please sign in to comment.