Skip to content

Commit e7c246b

Browse files
l46kokcopybara-github
authored andcommitted
Make iteration limit configurable for rule composer
PiperOrigin-RevId: 651551461
1 parent 8321498 commit e7c246b

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

policy/src/main/java/dev/cel/policy/CelPolicyCompilerBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@
2020
/** Interface for building an instance of {@link CelPolicyCompiler} */
2121
public interface CelPolicyCompilerBuilder {
2222

23+
/** Sets the prefix for the policy variables. Default is `variables.`. */
2324
@CanIgnoreReturnValue
2425
CelPolicyCompilerBuilder setVariablesPrefix(String prefix);
2526

27+
/**
28+
* Limit the number of iteration while composing rules into a single AST. An exception is thrown
29+
* if the iteration count exceeds the set value.
30+
*/
31+
@CanIgnoreReturnValue
32+
CelPolicyCompilerBuilder setIterationLimit(int iterationLimit);
33+
2634
@CheckReturnValue
2735
CelPolicyCompiler build();
2836
}

policy/src/main/java/dev/cel/policy/CelPolicyCompilerImpl.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444
/** Package-private implementation for policy compiler. */
4545
final class CelPolicyCompilerImpl implements CelPolicyCompiler {
4646
private static final String DEFAULT_VARIABLE_PREFIX = "variables.";
47+
private static final int DEFAULT_ITERATION_LIMIT = 1000;
4748
private final Cel cel;
4849
private final String variablesPrefix;
50+
private final int iterationLimit;
4951

5052
@Override
5153
public CelAbstractSyntaxTree compile(CelPolicy policy) throws CelPolicyValidationException {
@@ -59,7 +61,10 @@ public CelAbstractSyntaxTree compile(CelPolicy policy) throws CelPolicyValidatio
5961
CelOptimizerFactory.standardCelOptimizerBuilder(compiledRule.cel())
6062
.addAstOptimizers(
6163
RuleComposer.newInstance(
62-
compiledRule, compilerContext.newVariableDeclarations, variablesPrefix))
64+
compiledRule,
65+
compilerContext.newVariableDeclarations,
66+
variablesPrefix,
67+
iterationLimit))
6368
.build();
6469

6570
CelAbstractSyntaxTree ast;
@@ -194,6 +199,7 @@ private CompilerContext(CelPolicySource celPolicySource) {
194199
static final class Builder implements CelPolicyCompilerBuilder {
195200
private final Cel cel;
196201
private String variablesPrefix;
202+
private int iterationLimit;
197203

198204
private Builder(Cel cel) {
199205
this.cel = cel;
@@ -206,18 +212,28 @@ public Builder setVariablesPrefix(String prefix) {
206212
return this;
207213
}
208214

215+
@Override
216+
@CanIgnoreReturnValue
217+
public Builder setIterationLimit(int iterationLimit) {
218+
this.iterationLimit = iterationLimit;
219+
return this;
220+
}
221+
209222
@Override
210223
public CelPolicyCompiler build() {
211-
return new CelPolicyCompilerImpl(cel, this.variablesPrefix);
224+
return new CelPolicyCompilerImpl(cel, this.variablesPrefix, this.iterationLimit);
212225
}
213226
}
214227

215228
static Builder newBuilder(Cel cel) {
216-
return new Builder(cel).setVariablesPrefix(DEFAULT_VARIABLE_PREFIX);
229+
return new Builder(cel)
230+
.setVariablesPrefix(DEFAULT_VARIABLE_PREFIX)
231+
.setIterationLimit(DEFAULT_ITERATION_LIMIT);
217232
}
218233

219-
private CelPolicyCompilerImpl(Cel cel, String variablesPrefix) {
234+
private CelPolicyCompilerImpl(Cel cel, String variablesPrefix, int iterationLimit) {
220235
this.cel = checkNotNull(cel);
221236
this.variablesPrefix = checkNotNull(variablesPrefix);
237+
this.iterationLimit = iterationLimit;
222238
}
223239
}

policy/src/main/java/dev/cel/policy/RuleComposer.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
/** Package-private class for composing various rules into a single expression using optimizer. */
3636
final class RuleComposer implements CelAstOptimizer {
37-
private static final int AST_MUTATOR_ITERATION_LIMIT = 1000;
3837
private final CelCompiledRule compiledRule;
3938
private final ImmutableList<CelVarDecl> newVarDecls;
4039
private final String variablePrefix;
@@ -121,15 +120,21 @@ private RuleOptimizationResult optimizeRule(CelCompiledRule compiledRule) {
121120
}
122121

123122
static RuleComposer newInstance(
124-
CelCompiledRule compiledRule, List<CelVarDecl> newVarDecls, String variablePrefix) {
125-
return new RuleComposer(compiledRule, newVarDecls, variablePrefix);
123+
CelCompiledRule compiledRule,
124+
List<CelVarDecl> newVarDecls,
125+
String variablePrefix,
126+
int iterationLimit) {
127+
return new RuleComposer(compiledRule, newVarDecls, variablePrefix, iterationLimit);
126128
}
127129

128130
private RuleComposer(
129-
CelCompiledRule compiledRule, List<CelVarDecl> newVarDecls, String variablePrefix) {
131+
CelCompiledRule compiledRule,
132+
List<CelVarDecl> newVarDecls,
133+
String variablePrefix,
134+
int iterationLimit) {
130135
this.compiledRule = checkNotNull(compiledRule);
131136
this.newVarDecls = ImmutableList.copyOf(checkNotNull(newVarDecls));
132137
this.variablePrefix = variablePrefix;
133-
this.astMutator = AstMutator.newInstance(AST_MUTATOR_ITERATION_LIMIT);
138+
this.astMutator = AstMutator.newInstance(iterationLimit);
134139
}
135140
}

0 commit comments

Comments
 (0)