@@ -73,6 +73,10 @@ internal abstract class PolicyAssignmentVisitor : ResourceManagerVisitor
73
73
private const string PROPERTY_PATH = "path" ;
74
74
private const string PROPERTY_CONVERT = "convert" ;
75
75
private const string PROPERTY_NONCOMPLIANCEMESSAGES = "NonComplianceMessages" ;
76
+ private const string PROPERTY_HASVALUE = "hasValue" ;
77
+ private const string PROPERTY_EMPTY = "empty" ;
78
+ private const string PROPERTY_LENGTH = "length" ;
79
+
76
80
private const string EFFECT_DISABLED = "Disabled" ;
77
81
private const string EFFECT_AUDITIFNOTEXISTS = "AuditIfNotExists" ;
78
82
private const string EFFECT_DEPLOYIFNOTEXISTS = "DeployIfNotExists" ;
@@ -90,6 +94,7 @@ internal abstract class PolicyAssignmentVisitor : ResourceManagerVisitor
90
94
private const char SLASH = '/' ;
91
95
private const char GROUP_OPEN = '(' ;
92
96
private const char GROUP_CLOSE = ')' ;
97
+
93
98
private const string TYPE_SECURITYASSESSMENTS = "Microsoft.Security/assessments" ;
94
99
private const string TYPE_GUESTCONFIGURATIONASSIGNMENTS = "Microsoft.GuestConfiguration/guestConfigurationAssignments" ;
95
100
private const string TYPE_BACKUPPROTECTEDITEMS = "Microsoft.RecoveryServices/backupprotecteditems" ;
@@ -1020,7 +1025,7 @@ private static void VisitCondition(PolicyAssignmentContext context, PolicyDefini
1020
1025
private static void VisitCountExpression ( PolicyAssignmentContext context , PolicyDefinition policyDefinition , JObject parent , JObject count )
1021
1026
{
1022
1027
// Remove from parent
1023
- parent . Remove ( PROPERTY_COUNT ) ;
1028
+ parent . RemoveProperty ( PROPERTY_COUNT ) ;
1024
1029
1025
1030
if ( count . TryGetProperty ( PROPERTY_FIELD , out var field ) )
1026
1031
{
@@ -1203,7 +1208,7 @@ private static JObject VisitField(PolicyAssignmentContext context, PolicyDefinit
1203
1208
field = TemplateVisitor . ExpandString ( context , field ) ;
1204
1209
if ( string . Equals ( field , PROPERTY_TYPE , StringComparison . OrdinalIgnoreCase ) )
1205
1210
{
1206
- condition . Remove ( PROPERTY_FIELD ) ;
1211
+ condition . RemoveProperty ( PROPERTY_FIELD ) ;
1207
1212
condition . Add ( PROPERTY_TYPE , DOT ) ;
1208
1213
AddTypes ( context , policyDefinition , condition ) ;
1209
1214
}
@@ -1257,32 +1262,32 @@ private static JObject VisitValueExpression(PolicyAssignmentContext context, JOb
1257
1262
tokens . ConsumeGroup ( ) &&
1258
1263
tokens . ConsumePropertyName ( PROPERTY_APIVERSION ) )
1259
1264
{
1260
- condition . Remove ( PROPERTY_VALUE ) ;
1265
+ condition . RemoveProperty ( PROPERTY_VALUE ) ;
1261
1266
condition . Add ( PROPERTY_FIELD , PROPERTY_APIVERSION ) ;
1262
1267
if ( condition . TryGetProperty ( PROPERTY_LESS , out var value ) )
1263
1268
{
1264
- condition . Remove ( PROPERTY_LESS ) ;
1269
+ condition . RemoveProperty ( PROPERTY_LESS ) ;
1265
1270
condition . Add ( PROPERTY_APIVERSION , string . Concat ( LESS_OPERATOR , value ) ) ;
1266
1271
}
1267
1272
else if ( condition . TryGetProperty ( PROPERTY_LESSOREQUALS , out value ) )
1268
1273
{
1269
- condition . Remove ( PROPERTY_LESSOREQUALS ) ;
1274
+ condition . RemoveProperty ( PROPERTY_LESSOREQUALS ) ;
1270
1275
condition . Add ( PROPERTY_APIVERSION , string . Concat ( LESSOREQUAL_OPERATOR , value ) ) ;
1271
1276
1272
1277
}
1273
1278
else if ( condition . TryGetProperty ( PROPERTY_GREATER , out value ) )
1274
1279
{
1275
- condition . Remove ( PROPERTY_GREATER ) ;
1280
+ condition . RemoveProperty ( PROPERTY_GREATER ) ;
1276
1281
condition . Add ( PROPERTY_APIVERSION , string . Concat ( GREATER_OPERATOR , value ) ) ;
1277
1282
}
1278
1283
else if ( condition . TryGetProperty ( PROPERTY_GREATEROREQUALS , out value ) )
1279
1284
{
1280
- condition . Remove ( PROPERTY_GREATEROREQUALS ) ;
1285
+ condition . RemoveProperty ( PROPERTY_GREATEROREQUALS ) ;
1281
1286
condition . Add ( PROPERTY_APIVERSION , string . Concat ( GREATEROREQUAL_OPERATOR , value ) ) ;
1282
1287
}
1283
1288
else if ( condition . TryGetProperty ( PROPERTY_EQUALS , out value ) )
1284
1289
{
1285
- condition . Remove ( PROPERTY_EQUALS ) ;
1290
+ condition . RemoveProperty ( PROPERTY_EQUALS ) ;
1286
1291
condition . Add ( PROPERTY_APIVERSION , value ) ;
1287
1292
}
1288
1293
}
@@ -1313,19 +1318,19 @@ private static JObject VisitFieldTokens(PolicyAssignmentContext context, JObject
1313
1318
// Handle [field('type')]
1314
1319
if ( string . Equals ( field , PROPERTY_TYPE , StringComparison . OrdinalIgnoreCase ) )
1315
1320
{
1316
- condition . Remove ( PROPERTY_VALUE ) ;
1321
+ condition . RemoveProperty ( PROPERTY_VALUE ) ;
1317
1322
condition . Add ( PROPERTY_TYPE , DOT ) ;
1318
1323
}
1319
1324
else
1320
1325
{
1321
- condition . Remove ( PROPERTY_VALUE ) ;
1326
+ condition . RemoveProperty ( PROPERTY_VALUE ) ;
1322
1327
1323
1328
field = context . TryPolicyAliasPath ( field , out var aliasPath ) ? TrimFieldName ( context , aliasPath ) : field ;
1324
1329
condition . Add ( PROPERTY_FIELD , field ) ;
1325
1330
}
1326
1331
}
1327
1332
1328
- else if ( tokens . ConsumeFunction ( "if" ) &&
1333
+ else if ( tokens . ConsumeFunction ( PROPERTY_IF ) &&
1329
1334
tokens . TryTokenType ( ExpressionTokenType . GroupStart , out _ ) )
1330
1335
{
1331
1336
var orginal = condition ;
@@ -1369,17 +1374,126 @@ private static JObject VisitFieldTokens(PolicyAssignmentContext context, JObject
1369
1374
tokens . TryTokenType ( ExpressionTokenType . GroupEnd , out _ ) ;
1370
1375
}
1371
1376
1372
- else if ( tokens . ConsumeFunction ( "empty" ) &&
1377
+ else if ( tokens . ConsumeFunction ( PROPERTY_EMPTY ) &&
1373
1378
tokens . TryTokenType ( ExpressionTokenType . GroupStart , out _ ) )
1374
1379
{
1375
1380
if ( condition . TryBoolProperty ( PROPERTY_EQUALS , out var emptyEquals ) )
1376
1381
{
1377
- condition . Remove ( PROPERTY_EQUALS ) ;
1378
- condition . Add ( "hasValue" , ! emptyEquals . Value ) ;
1382
+ condition . RemoveProperty ( PROPERTY_EQUALS ) ;
1383
+ condition . Add ( PROPERTY_HASVALUE , ! emptyEquals . Value ) ;
1384
+ }
1385
+ VisitFieldTokens ( context , condition , tokens ) ;
1386
+
1387
+ tokens . TryTokenType ( ExpressionTokenType . GroupEnd , out _ ) ;
1388
+ }
1389
+
1390
+ else if ( tokens . ConsumeFunction ( PROPERTY_LESS ) &&
1391
+ tokens . TryTokenType ( ExpressionTokenType . GroupStart , out _ ) )
1392
+ {
1393
+ VisitFieldTokens ( context , condition , tokens ) ;
1394
+
1395
+ if ( tokens . ConsumeInteger ( out var comparisonInt ) && comparisonInt . HasValue )
1396
+ {
1397
+ if ( condition . TryBoolProperty ( PROPERTY_EQUALS , out var comparison ) )
1398
+ {
1399
+ condition . RemoveProperty ( PROPERTY_EQUALS ) ;
1400
+ condition . Add ( comparison . Value ? PROPERTY_LESS : PROPERTY_GREATEROREQUALS , comparisonInt . Value ) ;
1401
+ }
1402
+ else if ( condition . TryBoolProperty ( PROPERTY_NOTEQUALS , out comparison ) )
1403
+ {
1404
+ condition . RemoveProperty ( PROPERTY_NOTEQUALS ) ;
1405
+ condition . Add ( comparison . Value ? PROPERTY_GREATEROREQUALS : PROPERTY_LESS , comparisonInt . Value ) ;
1406
+ }
1407
+ }
1408
+
1409
+ tokens . TryTokenType ( ExpressionTokenType . GroupEnd , out _ ) ;
1410
+ }
1411
+
1412
+ else if ( tokens . ConsumeFunction ( PROPERTY_LESSOREQUALS ) &&
1413
+ tokens . TryTokenType ( ExpressionTokenType . GroupStart , out _ ) )
1414
+ {
1415
+ VisitFieldTokens ( context , condition , tokens ) ;
1416
+
1417
+ if ( tokens . ConsumeInteger ( out var comparisonInt ) && comparisonInt . HasValue )
1418
+ {
1419
+ if ( condition . TryBoolProperty ( PROPERTY_EQUALS , out var comparison ) )
1420
+ {
1421
+ condition . RemoveProperty ( PROPERTY_EQUALS ) ;
1422
+ condition . Add ( comparison . Value ? PROPERTY_LESSOREQUALS : PROPERTY_GREATER , comparisonInt . Value ) ;
1423
+ }
1424
+ else if ( condition . TryBoolProperty ( PROPERTY_NOTEQUALS , out comparison ) )
1425
+ {
1426
+ condition . RemoveProperty ( PROPERTY_NOTEQUALS ) ;
1427
+ condition . Add ( comparison . Value ? PROPERTY_GREATER : PROPERTY_LESSOREQUALS , comparisonInt . Value ) ;
1428
+ }
1429
+ }
1430
+
1431
+ tokens . TryTokenType ( ExpressionTokenType . GroupEnd , out _ ) ;
1432
+ }
1433
+
1434
+ else if ( tokens . ConsumeFunction ( PROPERTY_GREATER ) &&
1435
+ tokens . TryTokenType ( ExpressionTokenType . GroupStart , out _ ) )
1436
+ {
1437
+ VisitFieldTokens ( context , condition , tokens ) ;
1438
+
1439
+ if ( tokens . ConsumeInteger ( out var comparisonInt ) && comparisonInt . HasValue )
1440
+ {
1441
+ if ( condition . TryBoolProperty ( PROPERTY_EQUALS , out var comparison ) )
1442
+ {
1443
+ condition . RemoveProperty ( PROPERTY_EQUALS ) ;
1444
+ condition . Add ( comparison . Value ? PROPERTY_GREATER : PROPERTY_LESSOREQUALS , comparisonInt . Value ) ;
1445
+ }
1446
+ else if ( condition . TryBoolProperty ( PROPERTY_NOTEQUALS , out comparison ) )
1447
+ {
1448
+ condition . RemoveProperty ( PROPERTY_NOTEQUALS ) ;
1449
+ condition . Add ( comparison . Value ? PROPERTY_LESSOREQUALS : PROPERTY_GREATER , comparisonInt . Value ) ;
1450
+ }
1451
+ }
1452
+
1453
+ tokens . TryTokenType ( ExpressionTokenType . GroupEnd , out _ ) ;
1454
+ }
1455
+
1456
+ else if ( tokens . ConsumeFunction ( PROPERTY_GREATEROREQUALS ) &&
1457
+ tokens . TryTokenType ( ExpressionTokenType . GroupStart , out _ ) )
1458
+ {
1459
+ VisitFieldTokens ( context , condition , tokens ) ;
1460
+
1461
+ if ( tokens . ConsumeInteger ( out var comparisonInt ) && comparisonInt . HasValue )
1462
+ {
1463
+ if ( condition . TryBoolProperty ( PROPERTY_EQUALS , out var comparison ) )
1464
+ {
1465
+ condition . RemoveProperty ( PROPERTY_EQUALS ) ;
1466
+ condition . Add ( comparison . Value ? PROPERTY_GREATEROREQUALS : PROPERTY_LESS , comparisonInt . Value ) ;
1467
+ }
1468
+ else if ( condition . TryBoolProperty ( PROPERTY_NOTEQUALS , out comparison ) )
1469
+ {
1470
+ condition . RemoveProperty ( PROPERTY_NOTEQUALS ) ;
1471
+ condition . Add ( comparison . Value ? PROPERTY_LESS : PROPERTY_GREATEROREQUALS , comparisonInt . Value ) ;
1472
+ }
1379
1473
}
1474
+
1475
+ tokens . TryTokenType ( ExpressionTokenType . GroupEnd , out _ ) ;
1476
+ }
1477
+
1478
+ else if ( tokens . ConsumeFunction ( PROPERTY_LENGTH ) &&
1479
+ tokens . TryTokenType ( ExpressionTokenType . GroupStart , out _ ) )
1480
+ {
1380
1481
VisitFieldTokens ( context , condition , tokens ) ;
1482
+
1483
+ if ( condition . TryIntegerProperty ( PROPERTY_EQUALS , out var comparison ) )
1484
+ {
1485
+ condition . RemoveProperty ( PROPERTY_EQUALS ) ;
1486
+ condition . Add ( PROPERTY_COUNT , comparison . Value ) ;
1487
+ }
1488
+ else if ( condition . TryIntegerProperty ( PROPERTY_NOTEQUALS , out comparison ) )
1489
+ {
1490
+ condition . RemoveProperty ( PROPERTY_NOTEQUALS ) ;
1491
+ condition . Add ( PROPERTY_NOTCOUNT , comparison . Value ) ;
1492
+ }
1493
+
1381
1494
tokens . TryTokenType ( ExpressionTokenType . GroupEnd , out _ ) ;
1382
1495
}
1496
+
1383
1497
return condition ;
1384
1498
}
1385
1499
@@ -1748,7 +1862,7 @@ private static void TrimPolicyRule(JObject policyRule)
1748
1862
effectBlock . TryObjectProperty ( PROPERTY_DETAILS , out var details ) &&
1749
1863
details . TryObjectProperty ( PROPERTY_DEPLOYMENT , out _ ) )
1750
1864
{
1751
- details . Remove ( PROPERTY_DEPLOYMENT ) ;
1865
+ details . RemoveProperty ( PROPERTY_DEPLOYMENT ) ;
1752
1866
policyRule [ PROPERTY_THEN ] [ PROPERTY_DETAILS ] = details ;
1753
1867
}
1754
1868
}
0 commit comments