Skip to content

Commit

Permalink
Add null guards
Browse files Browse the repository at this point in the history
  • Loading branch information
JanneMattila committed Jan 5, 2024
1 parent 19aca58 commit 63d5e56
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/AzurePolicyEvaluator/AzurePolicyEvaluator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<PublishReadyToRun>true</PublishReadyToRun>
<DebugType>embedded</DebugType>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<InvariantGlobalization>true</InvariantGlobalization>
<PackageProjectUrl>https://github.com/JanneMattila/azure-policy-evaluator</PackageProjectUrl>
<RepositoryUrl>https://github.com/JanneMattila/azure-policy-evaluator</RepositoryUrl>
Expand Down
6 changes: 6 additions & 0 deletions src/AzurePolicyEvaluator/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ internal EvaluationResult ExecuteFieldEvaluation(JsonElement fieldObject, JsonEl
propertyValue = propertyElement.GetString();
}

ArgumentNullException.ThrowIfNull(propertyValue);

result = FieldComparison(policy, propertyName, propertyValue);
}
}
Expand Down Expand Up @@ -528,10 +530,12 @@ internal object RunTemplateFunctions(string text)
{
throw new KeyNotFoundException($"Parameter {parameters} not found.");
}
ArgumentNullException.ThrowIfNull(requiredParameter.DefaultValue);
return requiredParameter.DefaultValue;

case TemplateFunctions.Concat:
var concatParameters = parameters as List<string>;
ArgumentNullException.ThrowIfNull(concatParameters);
text = string.Join(string.Empty, concatParameters);
break;
}
Expand Down Expand Up @@ -613,6 +617,8 @@ internal List<EvaluationResult> ExecuteArrayFieldEvaluation(string propertyName,
}

propertyValue = propertyElement.GetString();
ArgumentNullException.ThrowIfNull(propertyValue);

var result = FieldComparison(policy, nextName, propertyValue);
results.Add(result);
}
Expand Down
10 changes: 9 additions & 1 deletion src/AzurePolicyEvaluator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using System.CommandLine;
using System.IO;

IServiceProvider serviceProvider;
ILogger<Program> logger;
Expand Down Expand Up @@ -73,6 +74,8 @@ 1. Evaluate a single policy file against a single test file.
serviceProvider = services.BuildServiceProvider();

using var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
ArgumentNullException.ThrowIfNull(loggerFactory);

logger = loggerFactory.CreateLogger<Program>();

if (watch)
Expand Down Expand Up @@ -146,6 +149,8 @@ void PolicyFilesChanged(object sender, FileSystemEventArgs e)
{
// Azure Policy file has been changed. Look for test files in sub-folders.
var policyFolder = Directory.GetParent(policyFilename);
ArgumentNullException.ThrowIfNull(policyFolder);

testFiles = Directory.GetFiles(policyFolder.FullName, $"*.{EXTENSION}", SearchOption.AllDirectories)
.Where(f => Path.GetFileNameWithoutExtension(f) != POLICY_FILE_NAME)
.ToList();
Expand All @@ -155,9 +160,11 @@ void PolicyFilesChanged(object sender, FileSystemEventArgs e)
// Test file has been changed. Look for policy file in parent folder.
var testFile = new FileInfo(policyFilename);
testFiles.Add(testFile.FullName);
string policyFile = null;
string? policyFile = null;

var directory = testFile.Directory;
ArgumentNullException.ThrowIfNull(directory);

while (directory.FullName.Length >= rootFolder.Length)
{
logger.LogDebug($"Looking for policy file in {directory.FullName}...");
Expand All @@ -168,6 +175,7 @@ void PolicyFilesChanged(object sender, FileSystemEventArgs e)
break;
}
directory = directory.Parent;
ArgumentNullException.ThrowIfNull(directory);
}

if (policyFile == null)
Expand Down

0 comments on commit 63d5e56

Please sign in to comment.