@@ -2,7 +2,9 @@ namespace SampleModule
2
2
{
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . IO ;
5
6
using System . Runtime . Loader ;
7
+ using System . Security . Cryptography . X509Certificates ;
6
8
using System . Text ;
7
9
using System . Threading ;
8
10
using System . Threading . Tasks ;
@@ -14,8 +16,12 @@ namespace SampleModule
14
16
class Program
15
17
{
16
18
static int counter ;
19
+
17
20
static void Main ( string [ ] args )
18
21
{
22
+ // Install CA certificate
23
+ InstallCert ( ) ;
24
+
19
25
// Initialize Edge Module
20
26
InitEdgeModule ( ) . Wait ( ) ;
21
27
@@ -25,6 +31,7 @@ static void Main(string[] args)
25
31
Console . CancelKeyPress += ( sender , cpe ) => cts . Cancel ( ) ;
26
32
WhenCancelled ( cts . Token ) . Wait ( ) ;
27
33
}
34
+
28
35
/// <summary>
29
36
/// Handles cleanup operations when app is cancelled or unloads
30
37
/// </summary>
@@ -35,27 +42,46 @@ public static Task WhenCancelled(CancellationToken cancellationToken)
35
42
return tcs . Task ;
36
43
}
37
44
45
+ /// <summary>
46
+ /// Add certificate in local cert store for use by client for secure connection to IoT Edge runtime
47
+ /// </summary>
48
+ static void InstallCert ( )
49
+ {
50
+ string certPath = Environment . GetEnvironmentVariable ( "EdgeModuleCACertificateFile" ) ;
51
+ if ( string . IsNullOrWhiteSpace ( certPath ) )
52
+ {
53
+ // We cannot proceed further without a proper cert file
54
+ Console . WriteLine ( "Missing path to certificate collection file." ) ;
55
+ throw new InvalidOperationException ( "Missing path to certificate file." ) ;
56
+ } else if ( ! File . Exists ( certPath ) )
57
+ {
58
+ // We cannot proceed further without a proper cert file
59
+ Console . WriteLine ( "Missing certificate collection file." ) ;
60
+ throw new InvalidOperationException ( "Missing certificate file." ) ;
61
+ }
62
+ X509Store store = new X509Store ( StoreName . Root , StoreLocation . CurrentUser ) ;
63
+ store . Open ( OpenFlags . ReadWrite ) ;
64
+ store . Add ( new X509Certificate2 ( X509Certificate2 . CreateFromCertFile ( certPath ) ) ) ;
65
+ Console . WriteLine ( "Added Cert: " + certPath ) ;
66
+ store . Close ( ) ;
67
+ }
68
+
69
+
38
70
/// <summary>
39
71
/// Initializes the Azure IoT Client for the Edge Module
40
72
/// </summary>
41
73
static async Task InitEdgeModule ( )
42
74
{
43
75
try
44
76
{
45
- // Open a connection to the Edge runtime using MQTT transport and
77
+ // Open a connection to the Edge runtime using MQTT transport and
46
78
// the connection string provided as an environment variable
47
- ITransportSettings [ ] settings =
48
- {
49
- new MqttTransportSettings ( TransportType . Mqtt_Tcp_Only )
50
- { RemoteCertificateValidationCallback = ( sender , certificate , chain , sslPolicyErrors ) => true }
51
- } ;
52
-
53
- DeviceClient IoTHubModuleClient = DeviceClient . CreateFromConnectionString ( Environment . GetEnvironmentVariable ( "EdgeHubConnectionString" ) , settings ) ;
54
- await IoTHubModuleClient . OpenAsync ( ) ;
79
+ DeviceClient ioTHubModuleClient = DeviceClient . CreateFromConnectionString ( Environment . GetEnvironmentVariable ( "EdgeHubConnectionString" ) , TransportType . Mqtt_Tcp_Only ) ;
80
+ await ioTHubModuleClient . OpenAsync ( ) ;
55
81
Console . WriteLine ( "IoT Hub module client initialized." ) ;
56
82
57
83
// Register callback to be called when a message is received by the module
58
- await IoTHubModuleClient . SetInputMessageHandlerAsync ( "input1" , PipeMessage , IoTHubModuleClient ) ;
84
+ await ioTHubModuleClient . SetInputMessageHandlerAsync ( "input1" , PipeMessage , ioTHubModuleClient ) ;
59
85
60
86
}
61
87
catch ( AggregateException ex )
0 commit comments