From 51a56b3ca83db40b4c2ebd84b95ccaf07af09387 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 19 Aug 2020 09:06:19 -0400 Subject: [PATCH] Drop compile limit on runtime fields scripts (#61297) This opts all of the script contexts used by `runtime_script` our of the compilation rate limiting because it'd be lame to prevent a mapping update because we can't compile the script. --- .../BooleanScriptFieldScript.java | 2 +- .../DoubleScriptFieldScript.java | 2 +- .../runtimefields/IpScriptFieldScript.java | 2 +- .../runtimefields/LongScriptFieldScript.java | 2 +- .../StringScriptFieldScript.java | 2 +- .../BooleanScriptFieldScriptTests.java | 32 +++++++++++ .../DateScriptFieldScriptTests.java | 24 ++++---- .../DoubleScriptFieldScriptTests.java | 32 +++++++++++ .../IpScriptFieldScriptTests.java | 28 ++++++++++ .../LongScriptFieldScriptTests.java | 28 ++++++++++ .../ScriptFieldScriptTestCase.java | 30 ++++++++++ .../StringScriptFieldScriptTests.java | 32 +++++++++++ .../mapper/RuntimeScriptFieldMapperTests.java | 56 ++++--------------- 13 files changed, 209 insertions(+), 63 deletions(-) create mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScriptTests.java create mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java create mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScriptTests.java create mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java create mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java create mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java diff --git a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScript.java b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScript.java index d05c1bedff58f..cc597cb8127d5 100644 --- a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScript.java +++ b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScript.java @@ -19,7 +19,7 @@ import java.util.Map; public abstract class BooleanScriptFieldScript extends AbstractScriptFieldScript { - public static final ScriptContext CONTEXT = new ScriptContext<>("boolean_script_field", Factory.class); + public static final ScriptContext CONTEXT = newContext("boolean_script_field", Factory.class); static List whitelist() { return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "boolean_whitelist.txt")); diff --git a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScript.java b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScript.java index 6824c21f4586f..cb3d4b48805d7 100644 --- a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScript.java +++ b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScript.java @@ -19,7 +19,7 @@ import java.util.Map; public abstract class DoubleScriptFieldScript extends AbstractScriptFieldScript { - public static final ScriptContext CONTEXT = new ScriptContext<>("double_script_field", Factory.class); + public static final ScriptContext CONTEXT = newContext("double_script_field", Factory.class); static List whitelist() { return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "double_whitelist.txt")); diff --git a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScript.java b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScript.java index 32ac41e66853d..5739e89a0c701 100644 --- a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScript.java +++ b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScript.java @@ -40,7 +40,7 @@ * */ public abstract class IpScriptFieldScript extends AbstractScriptFieldScript { - public static final ScriptContext CONTEXT = new ScriptContext<>("ip_script_field", Factory.class); + public static final ScriptContext CONTEXT = newContext("ip_script_field", Factory.class); static List whitelist() { return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "ip_whitelist.txt")); diff --git a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScript.java b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScript.java index 7f48f7d9d57df..a30b191eb2c1d 100644 --- a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScript.java +++ b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScript.java @@ -18,7 +18,7 @@ import java.util.Map; public abstract class LongScriptFieldScript extends AbstractLongScriptFieldScript { - public static final ScriptContext CONTEXT = new ScriptContext<>("long_script_field", Factory.class); + public static final ScriptContext CONTEXT = newContext("long_script_field", Factory.class); static List whitelist() { return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "long_whitelist.txt")); diff --git a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScript.java b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScript.java index 09321de9a225d..8bb94783a8afc 100644 --- a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScript.java +++ b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScript.java @@ -19,7 +19,7 @@ import java.util.Map; public abstract class StringScriptFieldScript extends AbstractScriptFieldScript { - public static final ScriptContext CONTEXT = new ScriptContext<>("string_script_field", Factory.class); + public static final ScriptContext CONTEXT = newContext("string_script_field", Factory.class); static List whitelist() { return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "string_whitelist.txt")); diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScriptTests.java new file mode 100644 index 0000000000000..5aff2c601236d --- /dev/null +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScriptTests.java @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.runtimefields; + +import org.elasticsearch.script.ScriptContext; + +public class BooleanScriptFieldScriptTests extends ScriptFieldScriptTestCase { + public static final BooleanScriptFieldScript.Factory DUMMY = (params, lookup) -> ctx -> new BooleanScriptFieldScript( + params, + lookup, + ctx + ) { + @Override + public void execute() { + new BooleanScriptFieldScript.Value(this).value(false); + } + }; + + @Override + protected ScriptContext context() { + return BooleanScriptFieldScript.CONTEXT; + } + + @Override + protected BooleanScriptFieldScript.Factory dummyScript() { + return DUMMY; + } +} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DateScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DateScriptFieldScriptTests.java index bf8e0b1685621..7aed194cee180 100644 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DateScriptFieldScriptTests.java +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DateScriptFieldScriptTests.java @@ -6,15 +6,9 @@ package org.elasticsearch.xpack.runtimefields; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptService; -import org.elasticsearch.script.ScriptType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.script.ScriptContext; -import java.io.IOException; -import java.util.Map; - -public class DateScriptFieldScriptTests extends ESTestCase { +public class DateScriptFieldScriptTests extends ScriptFieldScriptTestCase { public static final DateScriptFieldScript.Factory DUMMY = (params, lookup, formatter) -> ctx -> new DateScriptFieldScript( params, lookup, @@ -27,11 +21,13 @@ public void execute() { } }; - public void testRateLimitingDisabled() throws IOException { - try (ScriptService scriptService = TestScriptEngine.scriptService(DateScriptFieldScript.CONTEXT, DUMMY)) { - for (int i = 0; i < 1000; i++) { - scriptService.compile(new Script(ScriptType.INLINE, "test", "test_" + i, Map.of()), DateScriptFieldScript.CONTEXT); - } - } + @Override + protected ScriptContext context() { + return DateScriptFieldScript.CONTEXT; + } + + @Override + protected DateScriptFieldScript.Factory dummyScript() { + return DUMMY; } } diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java new file mode 100644 index 0000000000000..274fb26cdc37a --- /dev/null +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.runtimefields; + +import org.elasticsearch.script.ScriptContext; + +public class DoubleScriptFieldScriptTests extends ScriptFieldScriptTestCase { + public static final DoubleScriptFieldScript.Factory DUMMY = (params, lookup) -> ctx -> new DoubleScriptFieldScript( + params, + lookup, + ctx + ) { + @Override + public void execute() { + new DoubleScriptFieldScript.Value(this).value(1.0); + } + }; + + @Override + protected ScriptContext context() { + return DoubleScriptFieldScript.CONTEXT; + } + + @Override + protected DoubleScriptFieldScript.Factory dummyScript() { + return DUMMY; + } +} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScriptTests.java new file mode 100644 index 0000000000000..6a860c4b7ff39 --- /dev/null +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScriptTests.java @@ -0,0 +1,28 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.runtimefields; + +import org.elasticsearch.script.ScriptContext; + +public class IpScriptFieldScriptTests extends ScriptFieldScriptTestCase { + public static final IpScriptFieldScript.Factory DUMMY = (params, lookup) -> ctx -> new IpScriptFieldScript(params, lookup, ctx) { + @Override + public void execute() { + new IpScriptFieldScript.StringValue(this).stringValue("192.168.0.1"); + } + }; + + @Override + protected ScriptContext context() { + return IpScriptFieldScript.CONTEXT; + } + + @Override + protected IpScriptFieldScript.Factory dummyScript() { + return DUMMY; + } +} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java new file mode 100644 index 0000000000000..0dc3b7d4d7e07 --- /dev/null +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java @@ -0,0 +1,28 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.runtimefields; + +import org.elasticsearch.script.ScriptContext; + +public class LongScriptFieldScriptTests extends ScriptFieldScriptTestCase { + public static final LongScriptFieldScript.Factory DUMMY = (params, lookup) -> ctx -> new LongScriptFieldScript(params, lookup, ctx) { + @Override + public void execute() { + new LongScriptFieldScript.Value(this).value(1); + } + }; + + @Override + protected ScriptContext context() { + return LongScriptFieldScript.CONTEXT; + } + + @Override + protected LongScriptFieldScript.Factory dummyScript() { + return DUMMY; + } +} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java new file mode 100644 index 0000000000000..c2a0455806aa0 --- /dev/null +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.runtimefields; + +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptContext; +import org.elasticsearch.script.ScriptService; +import org.elasticsearch.script.ScriptType; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; +import java.util.Map; + +public abstract class ScriptFieldScriptTestCase extends ESTestCase { + protected abstract ScriptContext context(); + + protected abstract T dummyScript(); + + public final void testRateLimitingDisabled() throws IOException { + try (ScriptService scriptService = TestScriptEngine.scriptService(context(), dummyScript())) { + for (int i = 0; i < 1000; i++) { + scriptService.compile(new Script(ScriptType.INLINE, "test", "test_" + i, Map.of()), context()); + } + } + } +} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java new file mode 100644 index 0000000000000..81576a2038aa2 --- /dev/null +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.runtimefields; + +import org.elasticsearch.script.ScriptContext; + +public class StringScriptFieldScriptTests extends ScriptFieldScriptTestCase { + public static final StringScriptFieldScript.Factory DUMMY = (params, lookup) -> ctx -> new StringScriptFieldScript( + params, + lookup, + ctx + ) { + @Override + public void execute() { + new StringScriptFieldScript.Value(this).value("foo"); + } + }; + + @Override + protected ScriptContext context() { + return StringScriptFieldScript.CONTEXT; + } + + @Override + protected StringScriptFieldScript.Factory dummyScript() { + return DUMMY; + } +} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/RuntimeScriptFieldMapperTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/RuntimeScriptFieldMapperTests.java index 65b66774e2af9..0348712a5ef97 100644 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/RuntimeScriptFieldMapperTests.java +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/RuntimeScriptFieldMapperTests.java @@ -24,13 +24,18 @@ import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; import org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript; +import org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScriptTests; import org.elasticsearch.xpack.runtimefields.DateScriptFieldScript; import org.elasticsearch.xpack.runtimefields.DateScriptFieldScriptTests; import org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript; +import org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScriptTests; import org.elasticsearch.xpack.runtimefields.IpScriptFieldScript; +import org.elasticsearch.xpack.runtimefields.IpScriptFieldScriptTests; import org.elasticsearch.xpack.runtimefields.LongScriptFieldScript; +import org.elasticsearch.xpack.runtimefields.LongScriptFieldScriptTests; import org.elasticsearch.xpack.runtimefields.RuntimeFields; import org.elasticsearch.xpack.runtimefields.StringScriptFieldScript; +import org.elasticsearch.xpack.runtimefields.StringScriptFieldScriptTests; import org.elasticsearch.xpack.runtimefields.TestScriptEngine; import java.io.IOException; @@ -320,59 +325,22 @@ public ScriptEngine getScriptEngine(Settings settings, Collection context) { if (context == BooleanScriptFieldScript.CONTEXT) { - return (BooleanScriptFieldScript.Factory) (params, lookup) -> ctx -> new BooleanScriptFieldScript( - params, - lookup, - ctx - ) { - @Override - public void execute() { - new BooleanScriptFieldScript.Value(this).value(true); - } - }; + return BooleanScriptFieldScriptTests.DUMMY; } if (context == DateScriptFieldScript.CONTEXT) { return DateScriptFieldScriptTests.DUMMY; } if (context == DoubleScriptFieldScript.CONTEXT) { - return (DoubleScriptFieldScript.Factory) (params, lookup) -> ctx -> new DoubleScriptFieldScript( - params, - lookup, - ctx - ) { - @Override - public void execute() { - new DoubleScriptFieldScript.Value(this).value(1.0); - } - }; + return DoubleScriptFieldScriptTests.DUMMY; } if (context == IpScriptFieldScript.CONTEXT) { - return (IpScriptFieldScript.Factory) (params, lookup) -> ctx -> new IpScriptFieldScript(params, lookup, ctx) { - @Override - public void execute() { - new IpScriptFieldScript.StringValue(this).stringValue("192.168.0.1"); - } - }; - } - if (context == StringScriptFieldScript.CONTEXT) { - return (StringScriptFieldScript.Factory) (params, lookup) -> ctx -> new StringScriptFieldScript( - params, - lookup, - ctx - ) { - @Override - public void execute() { - new StringScriptFieldScript.Value(this).value("test"); - } - }; + return IpScriptFieldScriptTests.DUMMY; } if (context == LongScriptFieldScript.CONTEXT) { - return (LongScriptFieldScript.Factory) (params, lookup) -> ctx -> new LongScriptFieldScript(params, lookup, ctx) { - @Override - public void execute() { - new LongScriptFieldScript.Value(this).value(1); - } - }; + return LongScriptFieldScriptTests.DUMMY; + } + if (context == StringScriptFieldScript.CONTEXT) { + return StringScriptFieldScriptTests.DUMMY; } throw new IllegalArgumentException("No test script for [" + context + "]"); }