Skip to content

Commit

Permalink
Revert "WIP: Remove Code QL errors (#201)" (#204)
Browse files Browse the repository at this point in the history
This reverts commit e35dd7f.
  • Loading branch information
kbeaugrand authored Feb 5, 2022
1 parent e35dd7f commit b3f73e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
34 changes: 12 additions & 22 deletions src/AzureIoTHub.Portal/Server/Controllers/ConfigsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace AzureIoTHub.Portal.Server.Controllers
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Server.Helpers;
Expand All @@ -18,10 +17,18 @@ namespace AzureIoTHub.Portal.Server.Controllers
[Route("api/[controller]")]
public class ConfigsController : ControllerBase
{
private readonly ILogger<ConfigsController> logger;

private readonly RegistryManager registryManager;
private readonly ConfigsServices configService;

public ConfigsController(ConfigsServices configService)
public ConfigsController(
ILogger<ConfigsController> logger,
ConfigsServices configService,
RegistryManager registryManager)
{
this.logger = logger;
this.registryManager = registryManager;
this.configService = configService;
}

Expand All @@ -35,13 +42,13 @@ public async Task<IEnumerable<ConfigListItem>> Get()
// Retrieve every Configurations, regardless of the parameter given... Why?
// TODO : Check & fix this
// List<Configuration> configList = await this.registryManager.GetConfigurationsAsync(0) as List<Configuration>;
var configList = await this.configService.GetAllConfigs();
List<Configuration> configList = await this.configService.GetAllConfigs() as List<Configuration>;
var results = new List<ConfigListItem>();

// Azure Configurations may have different types: "Configuration", "Deployment" or "LayeredDeployment"
foreach (Configuration config in configList)
{
var moduleList = new List<GatewayModule>();
List<GatewayModule> moduleList = new ();

// 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)
Expand Down Expand Up @@ -74,7 +81,7 @@ public async Task<IEnumerable<ConfigListItem>> Get()
public async Task<ConfigListItem> Get(string configurationID)
{
var config = await this.configService.GetConfigItem(configurationID);
var moduleList = new List<GatewayModule>();
List<GatewayModule> moduleList = new ();
if (config.Content.ModulesContent != null)
{
// Details of every modules are stored within the EdgeAgent module data
Expand All @@ -85,22 +92,11 @@ public async Task<ConfigListItem> 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);
Expand All @@ -113,12 +109,6 @@ public async Task<ConfigListItem> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<IActionResult> Get()
GatewayListItem gateway = new ()
{
DeviceId = deviceTwin.DeviceId,
Status = deviceTwin.Status?.ToString(),
Status = deviceTwin.Status.Value.ToString(),
Type = DeviceHelper.RetrieveTagValue(devicesWithoutProperties.ElementAt(index), "purpose"),
NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin)
};
Expand Down Expand Up @@ -106,10 +106,10 @@ public async Task<IActionResult> Get(string deviceId)
Gateway gateway = new ()
{
DeviceId = deviceTwin.DeviceId,
Status = deviceTwin.Status?.ToString(),
Status = deviceTwin.Status.Value.ToString(),
EndPoint = this.configuration["IoTDPS:ServiceEndpoint"],
Scope = deviceTwin.DeviceScope,
Connection_state = deviceTwin.ConnectionState?.ToString(),
Connection_state = deviceTwin.ConnectionState.Value.ToString(),
// we retrieve the symmetric Key
// SymmetricKey = DeviceHelper.RetrieveSymmetricKey(deviceTwin.DeviceId, this.devicesService.GetDpsAttestionMechanism().Result),
// We retrieve the values of tags
Expand Down
8 changes: 1 addition & 7 deletions src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ public static class DeviceHelper
public static string RetrieveSymmetricKey(string deviceId, AttestationMechanism attestationMechanism)
{
// then we get the symmetricKey
var symmetricKey = attestationMechanism.GetAttestation() as SymmetricKeyAttestation;

if (symmetricKey == null)
{
throw new InvalidOperationException($"Cannot get symmetric key for {attestationMechanism.Type} attestation mechanism type.");
}

SymmetricKeyAttestation symmetricKey = attestationMechanism.GetAttestation() as SymmetricKeyAttestation;
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(symmetricKey.PrimaryKey));

return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(deviceId)));
Expand Down

0 comments on commit b3f73e3

Please sign in to comment.