Skip to content

Commit

Permalink
Adding Policy Field Count Expression Support (#1480)
Browse files Browse the repository at this point in the history
* Adding intial changes

* Added more changes

* Order properties

* Minor fixes

* Add parameter to assignment

* Add conversion for in & exists operators

* Split from last collection alias substring

* More cleanup

* Convert notEquals to notCount

* Undo formatting

* Minor fixes

* More fixes

* Added refactoring

* Fix tests

* Updated changelog

* Update docs/CHANGELOG-v1.md

Co-authored-by: Bernie White <[email protected]>

Co-authored-by: Bernie White <[email protected]>
  • Loading branch information
ArmaanMcleod and BernieWhite authored Jun 13, 2022
1 parent 927d3f7 commit e642216
Show file tree
Hide file tree
Showing 16 changed files with 1,275 additions and 40 deletions.
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

0 comments on commit e642216

Please sign in to comment.