Skip to content

Commit

Permalink
Drop compile limit on runtime fields scripts (#61297)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nik9000 authored Aug 19, 2020
1 parent 40e3aec commit 51a56b3
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Map;

public abstract class BooleanScriptFieldScript extends AbstractScriptFieldScript {
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("boolean_script_field", Factory.class);
public static final ScriptContext<Factory> CONTEXT = newContext("boolean_script_field", Factory.class);

static List<Whitelist> whitelist() {
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "boolean_whitelist.txt"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Map;

public abstract class DoubleScriptFieldScript extends AbstractScriptFieldScript {
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("double_script_field", Factory.class);
public static final ScriptContext<Factory> CONTEXT = newContext("double_script_field", Factory.class);

static List<Whitelist> whitelist() {
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "double_whitelist.txt"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* </ul>
*/
public abstract class IpScriptFieldScript extends AbstractScriptFieldScript {
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("ip_script_field", Factory.class);
public static final ScriptContext<Factory> CONTEXT = newContext("ip_script_field", Factory.class);

static List<Whitelist> whitelist() {
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "ip_whitelist.txt"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Map;

public abstract class LongScriptFieldScript extends AbstractLongScriptFieldScript {
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("long_script_field", Factory.class);
public static final ScriptContext<Factory> CONTEXT = newContext("long_script_field", Factory.class);

static List<Whitelist> whitelist() {
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "long_whitelist.txt"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Map;

public abstract class StringScriptFieldScript extends AbstractScriptFieldScript {
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("string_script_field", Factory.class);
public static final ScriptContext<Factory> CONTEXT = newContext("string_script_field", Factory.class);

static List<Whitelist> whitelist() {
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "string_whitelist.txt"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BooleanScriptFieldScript.Factory> {
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<BooleanScriptFieldScript.Factory> context() {
return BooleanScriptFieldScript.CONTEXT;
}

@Override
protected BooleanScriptFieldScript.Factory dummyScript() {
return DUMMY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateScriptFieldScript.Factory> {
public static final DateScriptFieldScript.Factory DUMMY = (params, lookup, formatter) -> ctx -> new DateScriptFieldScript(
params,
lookup,
Expand All @@ -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<DateScriptFieldScript.Factory> context() {
return DateScriptFieldScript.CONTEXT;
}

@Override
protected DateScriptFieldScript.Factory dummyScript() {
return DUMMY;
}
}
Original file line number Diff line number Diff line change
@@ -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<DoubleScriptFieldScript.Factory> {
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<DoubleScriptFieldScript.Factory> context() {
return DoubleScriptFieldScript.CONTEXT;
}

@Override
protected DoubleScriptFieldScript.Factory dummyScript() {
return DUMMY;
}
}
Original file line number Diff line number Diff line change
@@ -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<IpScriptFieldScript.Factory> {
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<IpScriptFieldScript.Factory> context() {
return IpScriptFieldScript.CONTEXT;
}

@Override
protected IpScriptFieldScript.Factory dummyScript() {
return DUMMY;
}
}
Original file line number Diff line number Diff line change
@@ -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<LongScriptFieldScript.Factory> {
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<LongScriptFieldScript.Factory> context() {
return LongScriptFieldScript.CONTEXT;
}

@Override
protected LongScriptFieldScript.Factory dummyScript() {
return DUMMY;
}
}
Original file line number Diff line number Diff line change
@@ -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<T> extends ESTestCase {
protected abstract ScriptContext<T> 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());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<StringScriptFieldScript.Factory> {
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<StringScriptFieldScript.Factory> context() {
return StringScriptFieldScript.CONTEXT;
}

@Override
protected StringScriptFieldScript.Factory dummyScript() {
return DUMMY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -320,59 +325,22 @@ public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<
@Override
protected Object buildScriptFactory(ScriptContext<?> 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 + "]");
}
Expand Down

0 comments on commit 51a56b3

Please sign in to comment.