Skip to content

Commit 1498869

Browse files
Merge pull request #382 from borisf94/borisf/skip_powershell_rules
Allow to skip the powershell rules
2 parents f777442 + 4d703d2 commit 1498869

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Analyzer.Core.UnitTests/TemplateAnalyzerTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ public void FilterRules_ValidConfiguration_NoExceptionThrown()
226226
TemplateAnalyzer.Create(false).FilterRules(new ConfigurationDefinition());
227227
}
228228

229+
[TestMethod]
230+
public void AnalyzeTemplate_NoPowershellRulesRunning_ReturnsLessEvaluations()
231+
{
232+
string[] resourceProperties = {
233+
GenerateResource(
234+
@"{ ""azureActiveDirectory"": { ""tenantId"": ""tenantIdValue"" } }",
235+
"Microsoft.ServiceFabric/clusters", "resource1")
236+
};
237+
string template = GenerateTemplate(resourceProperties);
238+
239+
var noPowershellEvaluations = TemplateAnalyzer.Create(includeNonSecurityRules: true, includePowerShellRules: false).AnalyzeTemplate(template, "aFilePath");
240+
var allRules = templateAnalyzerAllRules.AnalyzeTemplate(template, "aFilePath");
241+
242+
Assert.IsTrue(noPowershellEvaluations.Count() < allRules.Count());
243+
}
244+
229245
[TestMethod]
230246
public void CustomRulesFileIsProvided_NoExceptionThrown()
231247
{

src/Analyzer.Core/TemplateAnalyzer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using System;
@@ -52,8 +52,9 @@ private TemplateAnalyzer(JsonRuleEngine jsonRuleEngine, PowerShellRuleEngine pow
5252
/// <param name="includeNonSecurityRules">Whether or not to run also non-security rules against the template.</param>
5353
/// <param name="logger">A logger to report errors and debug information</param>
5454
/// <param name="customJsonRulesPath">An optional custom rules json file path.</param>
55+
/// <param name="includePowerShellRules">Whether or not to run also powershell rules against the template.</param>
5556
/// <returns>A new <see cref="TemplateAnalyzer"/> instance.</returns>
56-
public static TemplateAnalyzer Create(bool includeNonSecurityRules, ILogger logger = null, FileInfo customJsonRulesPath = null)
57+
public static TemplateAnalyzer Create(bool includeNonSecurityRules, ILogger logger = null, FileInfo customJsonRulesPath = null, bool includePowerShellRules = true)
5758
{
5859
string rules;
5960
try
@@ -72,7 +73,7 @@ public static TemplateAnalyzer Create(bool includeNonSecurityRules, ILogger logg
7273
? new BicepSourceLocationResolver(templateContext)
7374
: new JsonSourceLocationResolver(templateContext),
7475
logger),
75-
new PowerShellRuleEngine(includeNonSecurityRules, logger),
76+
includePowerShellRules ? new PowerShellRuleEngine(includeNonSecurityRules, logger) : null,
7677
logger);
7778
}
7879

@@ -158,7 +159,11 @@ private IEnumerable<IEvaluation> AnalyzeAllIncludedTemplates(string populatedTem
158159
try
159160
{
160161
IEnumerable<IEvaluation> evaluations = this.jsonRuleEngine.AnalyzeTemplate(templateContext);
161-
evaluations = evaluations.Concat(this.powerShellRuleEngine.AnalyzeTemplate(templateContext));
162+
163+
if (this.powerShellRuleEngine is not null)
164+
{
165+
evaluations = evaluations.Concat(this.powerShellRuleEngine.AnalyzeTemplate(templateContext));
166+
}
162167

163168
// Recursively handle nested templates
164169
var jsonTemplate = JObject.Parse(populatedTemplate);
@@ -187,7 +192,7 @@ private IEnumerable<IEvaluation> AnalyzeAllIncludedTemplates(string populatedTem
187192
// Variables, parameters and functions inherited from parent template
188193
string functionsKey = populatedNestedTemplate.InsensitiveToken("functions")?.Parent.Path ?? "functions";
189194
string variablesKey = populatedNestedTemplate.InsensitiveToken("variables")?.Parent.Path ?? "variables";
190-
string parametersKey = populatedNestedTemplate.InsensitiveToken("parameters")?.Parent.Path ?? "parameters" ;
195+
string parametersKey = populatedNestedTemplate.InsensitiveToken("parameters")?.Parent.Path ?? "parameters";
191196

192197
populatedNestedTemplate[functionsKey] = jsonTemplate.InsensitiveToken("functions");
193198
populatedNestedTemplate[variablesKey] = jsonTemplate.InsensitiveToken("variables");

0 commit comments

Comments
 (0)