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

Avoid null reference exceptions reported in prod #637

Merged
merged 2 commits into from
Feb 19, 2025
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
79 changes: 41 additions & 38 deletions src/Storage/Authorization/AuthorizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
private const string ResourceId = "r";

/// <inheritdoc/>>
public async Task<List<MessageBoxInstance>> AuthorizeMesseageBoxInstances(List<Instance> instances, bool includeInstantiate)

Check warning on line 49 in src/Storage/Authorization/AuthorizationService.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Refactor this method to reduce its Cognitive Complexity from 30 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if (instances.Count <= 0)
{
Expand All @@ -70,53 +70,56 @@
XacmlJsonRequestRoot xacmlJsonRequest = CreateMultiDecisionRequest(user, instances, actionTypes);

XacmlJsonResponse response = await _pdp.GetDecisionForRequest(xacmlJsonRequest);
foreach (XacmlJsonResult result in response.Response.Where(result => DecisionHelper.ValidateDecisionResult(result, user)))
if (response != null && response.Response?.Count > 0)
{
string instanceId = string.Empty;
string actiontype = string.Empty;

foreach (var attributes in result.Category.Select(c => c.Attribute))
foreach (XacmlJsonResult result in response.Response.Where(result => DecisionHelper.ValidateDecisionResult(result, user)))
{
foreach (var attribute in attributes)
{
if (attribute.AttributeId.Equals(XacmlResourceActionId))
{
actiontype = attribute.Value;
}
string instanceId = string.Empty;
string actiontype = string.Empty;

if (attribute.AttributeId.Equals(AltinnXacmlUrns.InstanceId))
foreach (var attributes in result.Category.Select(c => c.Attribute))
{
foreach (var attribute in attributes)
{
instanceId = attribute.Value;
if (attribute.AttributeId.Equals(XacmlResourceActionId))
{
actiontype = attribute.Value;
}

if (attribute.AttributeId.Equals(AltinnXacmlUrns.InstanceId))
{
instanceId = attribute.Value;
}
}
}
}

Instance authorizedInstance = instances.First(i => i.Id == instanceId);
Instance authorizedInstance = instances.First(i => i.Id == instanceId);

MessageBoxInstance authorizedMessageBoxInstance =
authorizedInstanceList.Find(i => i.Id.Equals(authorizedInstance.Id.Split("/")[1]));
if (authorizedMessageBoxInstance is null)
{
authorizedMessageBoxInstance = InstanceHelper.ConvertToMessageBoxInstance(authorizedInstance);
authorizedInstanceList.Add(authorizedMessageBoxInstance);
}
MessageBoxInstance authorizedMessageBoxInstance =
authorizedInstanceList.Find(i => i.Id.Equals(authorizedInstance.Id.Split("/")[1]));
if (authorizedMessageBoxInstance is null)
{
authorizedMessageBoxInstance = InstanceHelper.ConvertToMessageBoxInstance(authorizedInstance);
authorizedInstanceList.Add(authorizedMessageBoxInstance);
}

switch (actiontype)
{
case "write":
authorizedMessageBoxInstance.AuthorizedForWrite = true;
break;
case "delete":
authorizedMessageBoxInstance.AllowDelete = true;
break;
case "instantiate":
authorizedMessageBoxInstance.AllowNewCopy = true;
break;
case "sign":
authorizedMessageBoxInstance.AuthorizedForSign = true;
break;
case "read":
break;
switch (actiontype)
{
case "write":
authorizedMessageBoxInstance.AuthorizedForWrite = true;
break;
case "delete":
authorizedMessageBoxInstance.AllowDelete = true;
break;
case "instantiate":
authorizedMessageBoxInstance.AllowNewCopy = true;
break;
case "sign":
authorizedMessageBoxInstance.AuthorizedForSign = true;
break;
case "read":
break;
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/Storage/Repository/PgInstanceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
{
while (await reader.ReadAsync())
{
Instance i = await reader.GetFieldValueAsync<Instance>("instance");

Check warning on line 136 in src/Storage/Repository/PgInstanceRepository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Define a constant instead of using this literal 'instance' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
if ((i.CompleteConfirmations != null && i.CompleteConfirmations.Exists(c => c.StakeholderId.ToLower().Equals(i.Org) && c.ConfirmedOn <= DateTime.UtcNow.AddDays(-7)))
|| !i.Status.IsArchived)
{
Expand Down Expand Up @@ -173,7 +173,7 @@

if (currentInstanceAllowsDelete)
{
elements.Add(await reader.GetFieldValueAsync<DataElement>("element"));

Check warning on line 176 in src/Storage/Repository/PgInstanceRepository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Define a constant instead of using this literal 'element' 5 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
}
}

Expand Down Expand Up @@ -222,6 +222,11 @@

private static string ArrayVariableFromText(string[] arr)
{
if (arr.Length == 0)
{
return "NULL";
}

StringBuilder value = new("'{");
foreach (string param in arr)
{
Expand All @@ -233,6 +238,11 @@

private static string ArrayVariableFromJsonText(string[] arr)
{
if (arr.Length == 0)
{
return "NULL";
}

StringBuilder value = new("ARRAY [");
foreach (string param in arr)
{
Expand All @@ -244,6 +254,11 @@

private static string ArrayVariableFromInteger(int?[] arr)
{
if (arr.Length == 0)
{
return "NULL";
}

StringBuilder value = new("'{");
foreach (int? param in arr)
{
Expand Down Expand Up @@ -452,7 +467,7 @@
{
// This dictionary should be sorted alphabetically by key to match the sorted parameter list to the db function
{ "_appId", NpgsqlDbType.Text },
{ "_appIds", NpgsqlDbType.Text | NpgsqlDbType.Array },

Check warning on line 470 in src/Storage/Repository/PgInstanceRepository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Remove this bitwise operation; the enum 'NpgsqlDbType' is not marked with 'Flags' attribute. (https://rules.sonarsource.com/csharp/RSPEC-3265)
{ "_archiveReference", NpgsqlDbType.Text },
{ "_continue_idx", NpgsqlDbType.Bigint },
{ "_created_eq", NpgsqlDbType.TimestampTz },
Expand All @@ -465,10 +480,10 @@
{ "_dueBefore_gte", NpgsqlDbType.Text },
{ "_dueBefore_lt", NpgsqlDbType.Text },
{ "_dueBefore_lte", NpgsqlDbType.Text },
{ "_excludeConfirmedBy", NpgsqlDbType.Jsonb | NpgsqlDbType.Array },

Check warning on line 483 in src/Storage/Repository/PgInstanceRepository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Remove this bitwise operation; the enum 'NpgsqlDbType' is not marked with 'Flags' attribute. (https://rules.sonarsource.com/csharp/RSPEC-3265)
{ "_includeElements", NpgsqlDbType.Boolean },
{ "_instanceOwner_partyId", NpgsqlDbType.Integer },
{ "_instanceOwner_partyIds", NpgsqlDbType.Integer | NpgsqlDbType.Array },

Check warning on line 486 in src/Storage/Repository/PgInstanceRepository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Remove this bitwise operation; the enum 'NpgsqlDbType' is not marked with 'Flags' attribute. (https://rules.sonarsource.com/csharp/RSPEC-3265)
{ "_lastChanged_eq", NpgsqlDbType.TimestampTz },
{ "_lastChanged_gt", NpgsqlDbType.TimestampTz },
{ "_lastChanged_gte", NpgsqlDbType.TimestampTz },
Expand Down
Loading