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

Allow to skip the powershell rules #378

Closed
wants to merge 10 commits into from
16 changes: 16 additions & 0 deletions src/Analyzer.Core.UnitTests/TemplateAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ public void FilterRules_ValidConfiguration_NoExceptionThrown()
TemplateAnalyzer.Create(false).FilterRules(new ConfigurationDefinition());
}

[TestMethod]
public void AnalyzeTemplate_NoPowershellRulesRunning_ReturnsLessEvaluations()
{
string[] resourceProperties = {
GenerateResource(
@"{ ""azureActiveDirectory"": { ""tenantId"": ""tenantIdValue"" } }",
"Microsoft.ServiceFabric/clusters", "resource1")
};
string template = GenerateTemplate(resourceProperties);

var noPowershellEvaluations = TemplateAnalyzer.Create(includeNonSecurityRules: true, includePowerShellRules: false).AnalyzeTemplate(template, "aFilePath");
var allRules = templateAnalyzerAllRules.AnalyzeTemplate(template, "aFilePath");
borisf94 marked this conversation as resolved.
Show resolved Hide resolved

Assert.IsTrue(noPowershellEvaluations.Count() < allRules.Count());
}

[TestMethod]
public void CustomRulesFileIsProvided_NoExceptionThrown()
{
Expand Down
15 changes: 10 additions & 5 deletions src/Analyzer.Core/TemplateAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -52,8 +52,9 @@ private TemplateAnalyzer(JsonRuleEngine jsonRuleEngine, PowerShellRuleEngine pow
/// <param name="includeNonSecurityRules">Whether or not to run also non-security rules against the template.</param>
/// <param name="logger">A logger to report errors and debug information</param>
/// <param name="customJsonRulesPath">An optional custom rules json file path.</param>
/// <param name="includePowerShellRules">Whether or not to run also powershell rules against the template.</param>
/// <returns>A new <see cref="TemplateAnalyzer"/> instance.</returns>
public static TemplateAnalyzer Create(bool includeNonSecurityRules, ILogger logger = null, FileInfo customJsonRulesPath = null)
public static TemplateAnalyzer Create(bool includeNonSecurityRules, ILogger logger = null, FileInfo customJsonRulesPath = null, bool includePowerShellRules = true)
{
string rules;
try
Expand All @@ -72,7 +73,7 @@ public static TemplateAnalyzer Create(bool includeNonSecurityRules, ILogger logg
? new BicepSourceLocationResolver(templateContext)
: new JsonSourceLocationResolver(templateContext),
logger),
new PowerShellRuleEngine(includeNonSecurityRules, logger),
includePowerShellRules ? new PowerShellRuleEngine(includeNonSecurityRules, logger) : null,
logger);
}
borisf94 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -158,7 +159,11 @@ private IEnumerable<IEvaluation> AnalyzeAllIncludedTemplates(string populatedTem
try
{
IEnumerable<IEvaluation> evaluations = this.jsonRuleEngine.AnalyzeTemplate(templateContext);
evaluations = evaluations.Concat(this.powerShellRuleEngine.AnalyzeTemplate(templateContext));

if (this.powerShellRuleEngine is not null)
{
evaluations = evaluations.Concat(this.powerShellRuleEngine.AnalyzeTemplate(templateContext));
}
borisf94 marked this conversation as resolved.
Show resolved Hide resolved

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

populatedNestedTemplate[functionsKey] = jsonTemplate.InsensitiveToken("functions");
populatedNestedTemplate[variablesKey] = jsonTemplate.InsensitiveToken("variables");
Expand Down
Loading