From b627eb14a07359eaeeaee3c59632ff7704a5279f Mon Sep 17 00:00:00 2001 From: "Beaugrand, Kevin" Date: Sat, 5 Feb 2022 13:07:30 +0100 Subject: [PATCH 1/3] Fix Dereferenced variable may be null --- src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs index 1ecd0cb38..200b2f71b 100644 --- a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs +++ b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs @@ -23,7 +23,13 @@ public static class DeviceHelper public static string RetrieveSymmetricKey(string deviceId, AttestationMechanism attestationMechanism) { // then we get the symmetricKey - SymmetricKeyAttestation symmetricKey = attestationMechanism.GetAttestation() as SymmetricKeyAttestation; + var symmetricKey = attestationMechanism.GetAttestation() as SymmetricKeyAttestation; + + if (symmetricKey == null) + { + throw new InvalidOperationException($"Cannot get symmetric key for {attestationMechanism.Type} attestation mechanism type."); + } + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(symmetricKey.PrimaryKey)); return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(deviceId))); From 336c425117c6b5ba211dd2dae94028837f476890 Mon Sep 17 00:00:00 2001 From: "Beaugrand, Kevin" Date: Sat, 5 Feb 2022 13:21:42 +0100 Subject: [PATCH 2/3] Fix warnings in GatewaysController --- .../Server/Controllers/GatewaysController.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs b/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs index 2b9552614..afcfeda2a 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs @@ -71,7 +71,7 @@ public async Task Get() GatewayListItem gateway = new () { DeviceId = deviceTwin.DeviceId, - Status = deviceTwin.Status.Value.ToString(), + Status = deviceTwin.Status?.ToString(), Type = DeviceHelper.RetrieveTagValue(devicesWithoutProperties.ElementAt(index), "purpose"), NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin) }; @@ -106,10 +106,10 @@ public async Task Get(string deviceId) Gateway gateway = new () { DeviceId = deviceTwin.DeviceId, - Status = deviceTwin.Status.Value.ToString(), + Status = deviceTwin.Status?.ToString(), EndPoint = this.configuration["IoTDPS:ServiceEndpoint"], Scope = deviceTwin.DeviceScope, - Connection_state = deviceTwin.ConnectionState.Value.ToString(), + Connection_state = deviceTwin.ConnectionState?.ToString(), // we retrieve the symmetric Key // SymmetricKey = DeviceHelper.RetrieveSymmetricKey(deviceTwin.DeviceId, this.devicesService.GetDpsAttestionMechanism().Result), // We retrieve the values of tags From 5d1aa819f0ab8907934ddec357edcf9ee925dee7 Mon Sep 17 00:00:00 2001 From: "Beaugrand, Kevin" Date: Sat, 5 Feb 2022 13:29:14 +0100 Subject: [PATCH 3/3] Fix analysis warnings in ConfigsController --- .../Server/Controllers/ConfigsController.cs | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/AzureIoTHub.Portal/Server/Controllers/ConfigsController.cs b/src/AzureIoTHub.Portal/Server/Controllers/ConfigsController.cs index dd3ba149c..68b1a1c9b 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/ConfigsController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/ConfigsController.cs @@ -3,6 +3,7 @@ namespace AzureIoTHub.Portal.Server.Controllers { + using System; using System.Collections.Generic; using System.Threading.Tasks; using AzureIoTHub.Portal.Server.Helpers; @@ -17,18 +18,10 @@ namespace AzureIoTHub.Portal.Server.Controllers [Route("api/[controller]")] public class ConfigsController : ControllerBase { - private readonly ILogger logger; - - private readonly RegistryManager registryManager; private readonly ConfigsServices configService; - public ConfigsController( - ILogger logger, - ConfigsServices configService, - RegistryManager registryManager) + public ConfigsController(ConfigsServices configService) { - this.logger = logger; - this.registryManager = registryManager; this.configService = configService; } @@ -42,13 +35,13 @@ public async Task> Get() // Retrieve every Configurations, regardless of the parameter given... Why? // TODO : Check & fix this // List configList = await this.registryManager.GetConfigurationsAsync(0) as List; - List configList = await this.configService.GetAllConfigs() as List; + var configList = await this.configService.GetAllConfigs(); var results = new List(); // Azure Configurations may have different types: "Configuration", "Deployment" or "LayeredDeployment" foreach (Configuration config in configList) { - List moduleList = new (); + var moduleList = new List(); // Only deployments have modules. If it doesn't, it's a configuration and we don't want to keep it. if (config.Content.ModulesContent != null) @@ -81,7 +74,7 @@ public async Task> Get() public async Task Get(string configurationID) { var config = await this.configService.GetConfigItem(configurationID); - List moduleList = new (); + var moduleList = new List(); if (config.Content.ModulesContent != null) { // Details of every modules are stored within the EdgeAgent module data @@ -92,11 +85,22 @@ public async Task Get(string configurationID) // Converts the object to a JObject to access its properties more easily JObject modObject = config.Content.ModulesContent["$edgeAgent"]["properties.desired"] as JObject; + if (modObject == null) + { + throw new InvalidOperationException("Could not parse properties.desired."); + } + // Adds regular modules to the list of modules if (modObject.ContainsKey("modules")) { // Converts it to a JObject to be able to iterate through it JObject modules = modObject["modules"] as JObject; + + if (modules == null) + { + throw new InvalidOperationException("Could not parse modules."); + } + foreach (var m in modules) { GatewayModule newModule = ConfigHelper.CreateGatewayModule(config, m); @@ -109,6 +113,12 @@ public async Task Get(string configurationID) { // Converts it to a JObject to be able to iterate through it JObject systemModules = modObject["systemModules"] as JObject; + + if (systemModules == null) + { + throw new InvalidOperationException("Could not parse systemModules."); + } + foreach (var sm in systemModules) { GatewayModule newModule = ConfigHelper.CreateGatewayModule(config, sm);