Skip to content

Commit

Permalink
Fixes for simple field cases Azure#1323
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Jan 29, 2023
1 parent cfd65d2 commit f91d24d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ What's changed since pre-release v1.24.0-B0013:
[#2004](https://github.com/Azure/PSRule.Rules.Azure/issues/2004)
- Fixed `apiVersion` comparison of `requestContext` by @BernieWhite.
[#1654](https://github.com/Azure/PSRule.Rules.Azure/issues/1654)
- Fixed simple cases for field type expressions by @BernieWhite.
[#1323](https://github.com/Azure/PSRule.Rules.Azure/issues/1323)

## v1.24.0-B0013 (pre-release)

Expand Down
17 changes: 13 additions & 4 deletions src/PSRule.Rules.Azure/Data/Policy/PolicyAssignmentVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,14 +922,23 @@ private static void VisitValueExpression(PolicyAssignmentContext context, JObjec
}
}

// Handle "[field('type')]
// Handle [field('string')]
else if (tokens.ConsumeFunction(PROPERTY_FIELD) &&
tokens.TryTokenType(ExpressionTokenType.GroupStart, out _) &&
tokens.ConsumeString(PROPERTY_TYPE) &&
tokens.ConsumeString(out var field) &&
tokens.TryTokenType(ExpressionTokenType.GroupEnd, out _))
{
condition.Remove(PROPERTY_VALUE);
condition.Add(PROPERTY_TYPE, DOT);
// Handle [field('type')]
if (string.Equals(field, PROPERTY_TYPE, StringComparison.OrdinalIgnoreCase))
{
condition.Remove(PROPERTY_VALUE);
condition.Add(PROPERTY_TYPE, DOT);
}
else
{
condition.Remove(PROPERTY_VALUE);
condition.Add(PROPERTY_FIELD, field);
}
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/PSRule.Rules.Azure/Data/Template/TokenStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,27 @@ internal static bool ConsumeString(this TokenStream stream, string s)
{
if (stream == null ||
stream.Count == 0 ||
stream.Current.Type != ExpressionTokenType.Property ||
stream.Current.Type != ExpressionTokenType.String ||
!string.Equals(stream.Current.Content, s, StringComparison.OrdinalIgnoreCase))
return false;

stream.Pop();
return true;
}

internal static bool ConsumeString(this TokenStream stream, out string s)
{
s = null;
if (stream == null ||
stream.Count == 0 ||
stream.Current.Type != ExpressionTokenType.String)
return false;

s = stream.Current.Content;
stream.Pop();
return true;
}

internal static bool ConsumeGroup(this TokenStream stream)
{
if (stream == null ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void GetPolicyDefinition()

var definitions = context.GetDefinitions();
Assert.NotNull(definitions);
Assert.Equal(105, definitions.Length);
Assert.Equal(107, definitions.Length);

// Check category and version
var actual = definitions.FirstOrDefault(definition => definition.DefinitionId == "/providers/Microsoft.Authorization/policyDefinitions/34c877ad-507e-4c82-993e-3452a6e0ad3c");
Expand Down Expand Up @@ -74,7 +74,7 @@ public void GetPolicyDefinitionWithIgnore()

var definitions = context.GetDefinitions();
Assert.NotNull(definitions);
Assert.Equal(104, definitions.Length);
Assert.Equal(106, definitions.Length);

// Check category and version
var actual = definitions.FirstOrDefault(definition => definition.DefinitionId == "/providers/Microsoft.Authorization/policyDefinitions/34c877ad-507e-4c82-993e-3452a6e0ad3c");
Expand Down

0 comments on commit f91d24d

Please sign in to comment.