Script: no compile rate limit for ingest templates (#69841)

* Script: no compile rate limit for ingest templates

Remove the compilation rate limit for ingest templates.

Creates a new context, `ingest_template`, with an unlimited
compilation rate limit.

The `template` context is used in many places so it cannot be
exempted from rate limits.

Fixes: #64595
This commit is contained in:
Stuart Tettemer 2021-03-04 07:58:34 -06:00 committed by GitHub
parent e432934e82
commit 9370b1c006
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 2 deletions

View file

@ -75,7 +75,7 @@ public final class MustacheScriptEngine implements ScriptEngine {
@Override
public Set<ScriptContext<?>> getSupportedContexts() {
return Set.of(TemplateScript.CONTEXT);
return Set.of(TemplateScript.CONTEXT, TemplateScript.INGEST_CONTEXT);
}
private CustomMustacheFactory createMustacheFactory(Map<String, String> options) {

View file

@ -69,7 +69,7 @@ public interface ValueSource {
// modified if templating is not available
if (scriptService.isLangSupported(DEFAULT_TEMPLATE_LANG) && ((String) value).contains("{{")) {
Script script = new Script(ScriptType.INLINE, DEFAULT_TEMPLATE_LANG, (String) value, scriptOptions, Map.of());
return new TemplatedValue(scriptService.compile(script, TemplateScript.CONTEXT));
return new TemplatedValue(scriptService.compile(script, TemplateScript.INGEST_CONTEXT));
} else {
return new ObjectValue(value);
}

View file

@ -53,6 +53,7 @@ public class ScriptModule {
SimilarityScript.CONTEXT,
SimilarityWeightScript.CONTEXT,
TemplateScript.CONTEXT,
TemplateScript.INGEST_CONTEXT,
MovingFunctionScript.CONTEXT,
ScriptedMetricAggContexts.InitScript.CONTEXT,
ScriptedMetricAggContexts.MapScript.CONTEXT,

View file

@ -8,6 +8,8 @@
package org.elasticsearch.script;
import org.elasticsearch.common.unit.TimeValue;
import java.util.Map;
/**
@ -35,4 +37,10 @@ public abstract class TemplateScript {
}
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("template", Factory.class);
// Remove compilation rate limit for ingest. Ingest pipelines may use many mustache templates, triggering compilation
// rate limiting. MustacheScriptEngine explicitly checks for TemplateScript. Rather than complicating the implementation there by
// creating a new Script class (as would be customary), this context is used to avoid the default rate limit.
public static final ScriptContext<Factory> INGEST_CONTEXT = new ScriptContext<>("ingest_template", Factory.class,
200, TimeValue.timeValueMillis(0), ScriptCache.UNLIMITED_COMPILATION_RATE.asTuple());
}