diff --git a/policy/src/main/java/dev/cel/policy/CelPolicyYamlParser.java b/policy/src/main/java/dev/cel/policy/CelPolicyYamlParser.java index 09702e77c..8db1d725c 100644 --- a/policy/src/main/java/dev/cel/policy/CelPolicyYamlParser.java +++ b/policy/src/main/java/dev/cel/policy/CelPolicyYamlParser.java @@ -298,7 +298,7 @@ public CelPolicy.Rule parseRule( } hasMatch = true; ruleBuilder - .addMatches(parseMatches(ctx, policyBuilder, value, false)) + .addMatches(parseMatches(ctx, policyBuilder, value)) .setSemantic(EvaluationSemantic.FIRST_MATCH); break; case "aggregate": @@ -307,7 +307,7 @@ public CelPolicy.Rule parseRule( } hasAggregate = true; ruleBuilder - .addMatches(parseMatches(ctx, policyBuilder, value, true)) + .addMatches(parseMatches(ctx, policyBuilder, value)) .setSemantic(EvaluationSemantic.AGGREGATE); break; @@ -320,10 +320,7 @@ public CelPolicy.Rule parseRule( } private ImmutableSet parseMatches( - PolicyParserContext ctx, - CelPolicy.Builder policyBuilder, - Node node, - boolean isAggregate) { + PolicyParserContext ctx, CelPolicy.Builder policyBuilder, Node node) { long valueId = ctx.collectMetadata(node); ImmutableSet.Builder matchesBuilder = ImmutableSet.builder(); if (!assertYamlType(ctx, valueId, node, YamlNodeType.LIST)) { @@ -332,7 +329,7 @@ private ImmutableSet parseMatches( SequenceNode matchListNode = (SequenceNode) node; for (Node elementNode : matchListNode.getValue()) { - matchesBuilder.add(parseMatchInternal(ctx, policyBuilder, elementNode, isAggregate)); + matchesBuilder.add(parseMatch(ctx, policyBuilder, elementNode)); } return matchesBuilder.build(); @@ -341,14 +338,6 @@ private ImmutableSet parseMatches( @Override public CelPolicy.Match parseMatch( PolicyParserContext ctx, CelPolicy.Builder policyBuilder, Node node) { - return parseMatchInternal(ctx, policyBuilder, node, false); - } - - private CelPolicy.Match parseMatchInternal( - PolicyParserContext ctx, - CelPolicy.Builder policyBuilder, - Node node, - boolean isAggregate) { long nodeId = ctx.collectMetadata(node); if (!assertYamlType(ctx, nodeId, node, YamlNodeType.MAP)) { return ERROR_MATCH; @@ -369,20 +358,6 @@ private CelPolicy.Match parseMatchInternal( matchBuilder.setCondition(ctx.newSourceString(value)); break; case "output": - if (isAggregate) { - ctx.reportError(tagId, "Rule aggregate requires 'emit' tag instead of 'output'"); - } - matchBuilder - .result() - .filter(result -> result.kind().equals(Match.Result.Kind.RULE)) - .ifPresent( - result -> ctx.reportError(tagId, "Only the rule or the output may be set")); - matchBuilder.setResult(Match.Result.ofOutput(ctx.newSourceString(value))); - break; - case "emit": - if (!isAggregate) { - ctx.reportError(tagId, "Rule match requires 'output' tag instead of 'emit'"); - } matchBuilder .result() .filter(result -> result.kind().equals(Match.Result.Kind.RULE)) diff --git a/policy/src/test/java/dev/cel/policy/CelPolicyCompilerImplTest.java b/policy/src/test/java/dev/cel/policy/CelPolicyCompilerImplTest.java index 2e3274912..5f697e0b9 100644 --- a/policy/src/test/java/dev/cel/policy/CelPolicyCompilerImplTest.java +++ b/policy/src/test/java/dev/cel/policy/CelPolicyCompilerImplTest.java @@ -146,9 +146,9 @@ public void evalYamlPolicy_aggregate() throws Exception { + "rule:\n" + " aggregate:\n" + " - condition: 'true'\n" - + " emit: '\"PII\"'\n" + + " output: '\"PII\"'\n" + " - condition: 'true'\n" - + " emit: '\"CONFIDENTIAL\"'\n"; + + " output: '\"CONFIDENTIAL\"'\n"; Cel cel = newCel(); CelPolicy policy = POLICY_PARSER.parse(policySource); @@ -166,11 +166,11 @@ public void evaluateYamlPolicy_aggregate_cseApplied() throws Exception { + "rule:\n" + " aggregate:\n" + " - condition: \"size(resource.payload) > 5\"\n" - + " emit: '\"CSE1\"'\n" + + " output: '\"CSE1\"'\n" + " - condition: \"size(resource.payload) > 5\"\n" - + " emit: '\"CSE2\"'\n" + + " output: '\"CSE2\"'\n" + " - condition: 'true'\n" - + " emit: '\"ALWAYS\"'\n"; + + " output: '\"ALWAYS\"'\n"; Cel cel = newCel() .toCelBuilder() @@ -213,7 +213,7 @@ public void compileYamlPolicy_aggregate_macrosPreserved() throws Exception { + " - condition: \"true\"\n" + " output: \"payload.filter(x, x > 10).exists(y, y % 2 == 0)\"\n" + " - condition: \"true\"\n" - + " emit: \"payload.all(x, x > 0)\"\n"; + + " output: \"payload.all(x, x > 0)\"\n"; Cel cel = newCel() .toCelBuilder() @@ -243,7 +243,7 @@ public void compileYamlPolicy_nestedAggregate_throws() throws Exception { + " rule:\n" + " aggregate:\n" + " - condition: 'true'\n" - + " emit: \"'foo'\"\n"; + + " output: \"'foo'\"\n"; CelPolicy policy = POLICY_PARSER.parse(policySource); CelPolicyValidationException e = @@ -269,7 +269,7 @@ public void compileYamlPolicy_nestedAggregate_withInterveningMatch_throws() thro + " rule:\n" + " aggregate:\n" + " - condition: 'true'\n" - + " emit: \"'foo'\"\n"; + + " output: \"'foo'\"\n"; CelPolicy policy = POLICY_PARSER.parse(policySource); CelPolicyValidationException e = @@ -292,7 +292,7 @@ public void compileYamlPolicy_aggregateUnderMatch_success() throws Exception { + " rule:\n" + " aggregate:\n" + " - condition: 'true'\n" - + " emit: \"'foo'\"\n"; + + " output: \"'foo'\"\n"; CelPolicy policy = POLICY_PARSER.parse(policySource); CelAbstractSyntaxTree ast = diff --git a/policy/src/test/java/dev/cel/policy/CelPolicyYamlParserTest.java b/policy/src/test/java/dev/cel/policy/CelPolicyYamlParserTest.java index dfb483981..a881afb03 100644 --- a/policy/src/test/java/dev/cel/policy/CelPolicyYamlParserTest.java +++ b/policy/src/test/java/dev/cel/policy/CelPolicyYamlParserTest.java @@ -334,7 +334,7 @@ private enum PolicyParseErrorTestCase { + " match:\n" + " - output: 'true'\n" + " aggregate:\n" - + " - emit: 'true'\n", + + " - output: 'true'\n", "ERROR: :5:3: Only one of 'match' or 'aggregate' may be set in a rule\n" + " | aggregate:\n" + " | ..^"), @@ -342,22 +342,12 @@ private enum PolicyParseErrorTestCase { "name: test\n" + "rule:\n" + " aggregate:\n" - + " - emit: 'true'\n" + + " - output: 'true'\n" + " match:\n" + " - output: 'true'\n", "ERROR: :5:3: Only one of 'match' or 'aggregate' may be set in a rule\n" + " | match:\n" + " | ..^"), - AGGREGATE_RULE_USES_OUTPUT( - "name: test\n" + "rule:\n" + " aggregate:\n" + " - output: 'true'\n", - "ERROR: :4:7: Rule aggregate requires 'emit' tag instead of 'output'\n" - + " | - output: 'true'\n" - + " | ......^"), - MATCH_RULE_USES_EMIT( - "name: test\n" + "rule:\n" + " match:\n" + " - emit: 'true'\n", - "ERROR: :4:7: Rule match requires 'output' tag instead of 'emit'\n" - + " | - emit: 'true'\n" - + " | ......^"), ILLEGAL_YAML_TYPE_ON_RULE_VALUE( "rule: illegal", "ERROR: :1:7: Got yaml node type tag:yaml.org,2002:str, wanted type(s)"