Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Remove Code QL errors #201

Merged
merged 3 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/AzureIoTHub.Portal/Server/Controllers/ConfigsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace AzureIoTHub.Portal.Server.Controllers
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Server.Helpers;
Expand All @@ -17,18 +18,10 @@ 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(
ILogger<ConfigsController> logger,
ConfigsServices configService,
RegistryManager registryManager)
public ConfigsController(ConfigsServices configService)
{
this.logger = logger;
this.registryManager = registryManager;
this.configService = configService;
}

Expand All @@ -42,13 +35,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>;
List<Configuration> configList = await this.configService.GetAllConfigs() as List<Configuration>;
var configList = await this.configService.GetAllConfigs();
var results = new List<ConfigListItem>();

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

// 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 @@ -81,7 +74,7 @@ public async Task<IEnumerable<ConfigListItem>> Get()
public async Task<ConfigListItem> Get(string configurationID)
{
var config = await this.configService.GetConfigItem(configurationID);
List<GatewayModule> moduleList = new ();
var moduleList = new List<GatewayModule>();
if (config.Content.ModulesContent != null)
{
// Details of every modules are stored within the EdgeAgent module data
Expand All @@ -92,11 +85,22 @@ 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 @@ -109,6 +113,12 @@ 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.Value.ToString(),
Status = deviceTwin.Status?.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.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
Expand Down
8 changes: 7 additions & 1 deletion src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down