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

Adding Policy Field Count Expression Support #1480

Merged
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
16 changes: 16 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"[yaml]": {
"editor.tabSize": 2
},
"[json]": {
"editor.formatOnSave": true,
"editor.tabSize": 2
},
"[markdown]": {
"editor.tabSize": 2
},
Expand All @@ -32,25 +36,37 @@
"agentpool",
"APIM",
"apiserver",
"APIVERSION",
"APPGW",
"Architected",
"AUTOMATIONACCOUNT",
"autoscaler",
"cmdlet",
"cmdlets",
"Concat",
"DEFAULTVALUE",
"DISPLAYNAME",
"endregion",
"failover",
"GREATEROREQUAL",
"GREATEROREQUALS",
"Hashtable",
"kube",
"kubelet",
"kubenet",
"Kubernetes",
"LESSOREQUAL",
"LESSOREQUALS",
"lifecycle",
"Newtonsoft",
"nics",
"NOTCOUNT",
"NOTEQUALS",
"NOTIN",
"NSGs",
"OWASP",
"POLICYDEFINITIONID",
"POLICYRULE",
"psarm",
"PUBLICIP",
"pwsh",
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ What's changed since v1.16.1:
- Deployment:
- Check for secure values in outputs by @BernieWhite.
[#297](https://github.com/Azure/PSRule.Rules.Azure/issues/297)
- New features:
- Added more field count expression support for Azure Policy JSON rules by @ArmaanMcleod.
[#181](https://github.com/Azure/PSRule.Rules.Azure/issues/181)

## v1.16.1

Expand Down
13 changes: 12 additions & 1 deletion src/PSRule.Rules.Azure/Common/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Linq;

namespace PSRule.Rules.Azure
Expand All @@ -16,7 +17,17 @@ internal static string ToCamelCase(this string str)

internal static int CountCharacterOccurrences(this string str, char chr)
{
return str.Count(c => c == chr);
return !string.IsNullOrEmpty(str)
? str.Count(c => c == chr)
: 0;
}

internal static string[] SplitByLastSubstring(this string str, string substring)
{
var lastSubstringIndex = str.LastIndexOf(substring, StringComparison.OrdinalIgnoreCase);
var firstPart = str.Substring(0, lastSubstringIndex);
var secondPart = str.Substring(lastSubstringIndex + substring.Length);
return new string[] { firstPart, secondPart };
}

internal static bool IsExpressionString(this string str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ internal bool ResolvePolicyAliasPath(string aliasName, out string aliasPath)
// Handle aliases like Microsoft.Compute/imageId with only one slash
if (slashOccurrences == 1)
{
if (_DefaultRuleType != null && _Providers.TryResourceType(_DefaultRuleType, out var type2))
return type2.Aliases != null &&
type2.Aliases.TryGetValue(aliasName, out aliasPath);

return false;
return _DefaultRuleType != null
&& _Providers.TryResourceType(_DefaultRuleType, out var type2)
&& type2.Aliases != null
&& type2.Aliases.TryGetValue(aliasName, out aliasPath);
}

// Any aliases with two slashes or more will be resolved here
Expand Down
14 changes: 5 additions & 9 deletions src/PSRule.Rules.Azure/Data/Policy/PolicyAssignmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ internal PolicyDefinition[] ProcessAssignment(string assignmentFile, out PolicyA
{
var assignmentArray = ReadFileArray(rootedAssignmentFile);

foreach (JObject assignment in assignmentArray)
visitor.Visit(assignmentContext, assignment);
foreach (var assignment in assignmentArray)
visitor.Visit(assignmentContext, assignment.ToObject<JObject>());
}
catch (Exception inner)
{
Expand All @@ -60,13 +60,9 @@ internal PolicyDefinition[] ProcessAssignment(string assignmentFile, out PolicyA

private static JArray ReadFileArray(string path)
{
using (var stream = new StreamReader(path))
{
using (var reader = new CamelCasePropertyNameJsonTextReader(stream))
{
return JArray.Load(reader);
}
}
using var stream = new StreamReader(path);
using var reader = new CamelCasePropertyNameJsonTextReader(stream);
return JArray.Load(reader);
}

private sealed class CamelCasePropertyNameJsonTextReader : JsonTextReader
Expand Down
Loading