-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
IotHubConnection.cs
executable file
·304 lines (262 loc) · 15 KB
/
IotHubConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Amqp.Framing;
using Microsoft.Azure.Amqp.Sasl;
using Microsoft.Azure.Amqp.Transport;
using Microsoft.Azure.Devices;
namespace IotHubToEventHubsSample
{
/// <summary>
/// A transient connection to the IoT Hub service, providing a set of utility-type operations that
/// span the service client and device roles.
/// </summary>
///
public static class IotHubConnection
{
/// <summary>The regular expression used to parse the Event Hub name from the IoT Hub redirection address.</summary>
private static readonly Regex EventHubNameExpression = new Regex(@":\d+\/(?<eventHubName>.*)\/\$management", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
/// <summary>
/// Requests connection string for the built-in Event Hubs messaging endpoint of the associated IoT Hub.
/// </summary>
///
/// <param name="iotHubConnectionString">The connection string for the IoT Hub instance to request the Event Hubs connection string from.</param>
/// <param name="timeout">The maximum amount of time that the request and translation should be allowed to take. If not provided, a default of 60 seconds will be assumed.</param>
///
/// <returns>A connection string which can be used to connect to the Event Hubs service and interact with the IoT Hub messaging endpoint.</returns>
///
/// <exception cref="InvalidOperationException">The Event Hubs host information was not returned by the IoT Hub service.</exception>
///
/// <seealso href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-endpoints" />
/// <seealso href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-read-builtin" />
/// <seealso href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-amqp-support#receive-telemetry-messages-service-client" />
///
public static async Task<string> RequestEventHubsConnectionStringAsync(string iotHubConnectionString,
TimeSpan? timeout = default)
{
timeout ??= TimeSpan.FromMinutes(1);
if (string.IsNullOrEmpty(iotHubConnectionString))
{
throw new ArgumentException("The IoT Hub connection string must be provided.", nameof(iotHubConnectionString));
}
// Parse the connection string into the necessary components, and ensure the information is available.
var parsedConnectionString = IotHubConnectionStringBuilder.Create(iotHubConnectionString);
var iotHubName = parsedConnectionString.HostName?.Substring(0, parsedConnectionString.HostName.IndexOf('.'));
if ((string.IsNullOrEmpty(parsedConnectionString.HostName)) || (string.IsNullOrEmpty(parsedConnectionString.SharedAccessKeyName)) || (string.IsNullOrEmpty(parsedConnectionString.SharedAccessKey)))
{
throw new ArgumentException("The IoT Hub connection string is not valid; it must contain the host, shared access key, and shared access key name.", nameof(iotHubConnectionString));
}
if (string.IsNullOrEmpty(iotHubName))
{
throw new ArgumentException("Unable to parse the IoT Hub name from the connection string.", nameof(iotHubConnectionString));
}
// Establish the IoT Hub connection via link to the necessary endpoint, which will trigger a redirect exception
// from which the Event Hubs connection string can be built.
var stopWatch = Stopwatch.StartNew();
var serviceEndpoint = new Uri($"{ AmqpConstants.SchemeAmqps }://{ parsedConnectionString.HostName }/messages/events");
var connection = default(AmqpConnection);
var link = default(AmqpLink);
var eventHubsHost = default(string);
var eventHubName = default(string);
try
{
connection = await CreateAndOpenConnectionAsync(serviceEndpoint, iotHubName, parsedConnectionString.SharedAccessKeyName, parsedConnectionString.SharedAccessKey, timeout.Value).ConfigureAwait(false);
link = await CreateRedirectLinkAsync(connection, serviceEndpoint, timeout.Value.Subtract(stopWatch.Elapsed)).ConfigureAwait(false);
await link.OpenAsync(timeout.Value.Subtract(stopWatch.Elapsed)).ConfigureAwait(false);
}
catch (AmqpException ex)
when ((ex?.Error?.Condition.Value == AmqpErrorCode.LinkRedirect.Value) && (ex?.Error?.Info != null))
{
// The Event Hubs host is returned as a first-party element of the redirect information.
ex.Error.Info.TryGetValue("hostname", out eventHubsHost);
// The Event Hub name is a variant of the IoT Hub name and must be parsed from the
// full IoT Hub address returned by the redirect.
if (ex.Error.Info.TryGetValue("address", out string iotAddress))
{
// If the address does not match the expected pattern, this will not result in an exception; the Event Hub
// name will remain null and trigger a failed validation later in the flow.
eventHubName = EventHubNameExpression.Match(iotAddress).Groups["eventHubName"].Value;
}
}
finally
{
stopWatch.Stop();
link?.Session?.SafeClose();
link?.SafeClose();
connection?.SafeClose();
}
// Attempt to assemble the Event Hubs connection string using the IoT Hub components.
if (string.IsNullOrEmpty(eventHubsHost))
{
throw new InvalidOperationException("The Event Hubs host was not returned by the IoT Hub service.");
}
if (string.IsNullOrEmpty(eventHubName))
{
throw new InvalidOperationException("The Event Hub name was not returned by the IoT Hub service.");
}
return $"Endpoint=sb://{ eventHubsHost }/;EntityPath={ eventHubName };SharedAccessKeyName={ parsedConnectionString.SharedAccessKeyName };SharedAccessKey={ parsedConnectionString.SharedAccessKey }";
}
/// <summary>
/// Performs the tasks needed to build and open a connection to the IoT Hub
/// service.
/// </summary>
///
/// <param name="serviceEndpoint">The endpoint of the IoT Hub service to connect to.</param>
/// <param name="iotHubName">The name of the IoT Hub to connect to.</param>
/// <param name="sharedAccessKeyName">The name of the shared access key being used for authentication.</param>
/// <param name="sharedAccessKey">The shared access key being used for authentication.</param>
/// <param name="timeout">The maximum amount of time that establishing the connection should be allowed to take.</param>
///
/// <returns>An <see cref="AmqpConnection" /> to the requested IoT Hub.</returns>
///
/// <seealso href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-amqp-support"/>
///
private static async Task<AmqpConnection> CreateAndOpenConnectionAsync(Uri serviceEndpoint,
string iotHubName,
string sharedAccessKeyName,
string sharedAccessKey,
TimeSpan timeout)
{
var hostName = serviceEndpoint.Host;
var userName = $"{ sharedAccessKeyName }@sas.root.{ iotHubName }";
var signature = BuildSignature($"{ hostName }{ serviceEndpoint.AbsolutePath }", sharedAccessKeyName, sharedAccessKey, TimeSpan.FromMinutes(5));
var port = 5671;
// Create the layers of settings needed to establish the connection.
var amqpVersion = new Version(1, 0, 0, 0);
var tcpSettings = new TcpTransportSettings
{
Host = hostName,
Port = port,
ReceiveBufferSize = AmqpConstants.TransportBufferSize,
SendBufferSize = AmqpConstants.TransportBufferSize
};
var transportSettings = new TlsTransportSettings(tcpSettings)
{
TargetHost = hostName,
};
var connectionSettings = new AmqpConnectionSettings
{
IdleTimeOut = (uint)TimeSpan.FromMinutes(1).TotalMilliseconds,
MaxFrameSize = AmqpConstants.DefaultMaxFrameSize,
ContainerId = Guid.NewGuid().ToString(),
HostName = hostName
};
var saslProvider = new SaslTransportProvider();
saslProvider.Versions.Add(new AmqpVersion(amqpVersion));
saslProvider.AddHandler(new SaslPlainHandler { AuthenticationIdentity = userName, Password = signature });
var amqpProvider = new AmqpTransportProvider();
amqpProvider.Versions.Add(new AmqpVersion(amqpVersion));
var amqpSettings = new AmqpSettings();
amqpSettings.TransportProviders.Add(saslProvider);
amqpSettings.TransportProviders.Add(amqpProvider);
// Create and open the connection, respecting the timeout constraint
// that was received.
var stopWatch = Stopwatch.StartNew();
var initiator = new AmqpTransportInitiator(amqpSettings, transportSettings);
var transport = await initiator.ConnectTaskAsync(timeout).ConfigureAwait(false);
try
{
var connection = new AmqpConnection(transport, amqpSettings, connectionSettings);
await connection.OpenAsync(timeout.Subtract(stopWatch.Elapsed)).ConfigureAwait(false);
return connection;
}
catch
{
transport.Abort();
throw;
}
finally
{
stopWatch.Stop();
}
}
/// <summary>
/// Creates the AMQP link used to trigger a redirection response from the
/// IoT Hub service.
/// </summary>
///
/// <param name="connection">The connection to the IoT Hub service to associate the link with.</param>
/// <param name="serviceEndpoint">The endpoint of the IoT Hub service that the connection was made to.</param>
/// <param name="timeout">The maximum amount of time that creating the link should be allowed to take.</param>
///
/// <returns>An <see cref="AmqpLink" /> to an IoT Hub resource that will trigger redirection when opened.</returns>
///
/// <remarks>
/// The link is not opened by this method; callers are required to open the link in order to trigger
/// the redirection error.
/// </remarks>
///
/// <seealso href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-amqp-support"/>
///
private static async Task<AmqpLink> CreateRedirectLinkAsync(AmqpConnection connection,
Uri serviceEndpoint,
TimeSpan timeout)
{
var linkPath = $"{ serviceEndpoint.AbsolutePath }/$management";
var session = default(AmqpSession);
try
{
var sessionSettings = new AmqpSessionSettings { Properties = new Fields() };
session = connection.CreateSession(sessionSettings);
await session.OpenAsync(timeout).ConfigureAwait(false);
var linkSettings = new AmqpLinkSettings
{
Role = true,
TotalLinkCredit = 1,
AutoSendFlow = true,
SettleType = SettleMode.SettleOnSend,
Source = new Source { Address = linkPath },
Target = new Target { Address = serviceEndpoint.AbsoluteUri }
};
var link = new ReceivingAmqpLink(linkSettings);
linkSettings.LinkName = $"{ nameof(IotHubConnection) };{ connection.Identifier }:{ session.Identifier }:{ link.Identifier }";
link.AttachTo(session);
return link;
}
catch
{
session?.Abort();
throw;
}
}
/// <summary>
/// Builds a shared access signature to use for authentication with the IoT Hub
/// service.
/// </summary>
///
/// <param name="resourceUri">The address of the resource the signature is intended to authenticate for.</param>
/// <param name="keyName">Name of the shared access key to base the signature on.</param>
/// <param name="keyValue">The value of the shared access key to base the signature on.</param>
/// <param name="validityDuration">The amount of time that the signature should be considered valid; after this time a new signature would be needed.</param>
///
/// <returns>The shared access signature, encoded and suitable for use as a credential in service communication.</returns>
///
/// <seealso href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-security#security-tokens" />
///
private static string BuildSignature(string resourceUri,
string keyName,
string keyValue,
TimeSpan validityDuration)
{
using var hmac = new HMACSHA256(Convert.FromBase64String(keyValue));
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
var expirationTime = Convert.ToInt64((DateTimeOffset.UtcNow.Add(validityDuration) - epoch).TotalSeconds);
var encodedAudience = WebUtility.UrlEncode(resourceUri);
var expiration = Convert.ToString(expirationTime, CultureInfo.InvariantCulture);
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes($"{ encodedAudience }\n{ expiration }")));
return string.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
encodedAudience,
WebUtility.UrlEncode(signature),
WebUtility.UrlEncode(expiration),
WebUtility.UrlEncode(keyName));
}
}
}