Added plumbing for compile time script parameters.

Closes #15464
This commit is contained in:
Jack Conradson 2015-12-16 12:36:13 -08:00
parent a1312a5350
commit 4523eaec88
53 changed files with 280 additions and 197 deletions

View File

@ -33,6 +33,8 @@ import org.elasticsearch.script.ScriptService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import java.util.Collections;
public class TransportRenderSearchTemplateAction extends HandledTransportAction<RenderSearchTemplateRequest, RenderSearchTemplateResponse> {
private final ScriptService scriptService;
@ -55,7 +57,7 @@ public class TransportRenderSearchTemplateAction extends HandledTransportAction<
@Override
protected void doRun() throws Exception {
ExecutableScript executable = scriptService.executable(request.template(), ScriptContext.Standard.SEARCH, request);
ExecutableScript executable = scriptService.executable(request.template(), ScriptContext.Standard.SEARCH, request, Collections.emptyMap());
BytesReference processedTemplate = (BytesReference) executable.run();
RenderSearchTemplateResponse response = new RenderSearchTemplateResponse();
response.source(processedTemplate);

View File

@ -50,6 +50,7 @@ import org.elasticsearch.search.fetch.source.FetchSourceContext;
import org.elasticsearch.search.lookup.SourceLookup;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -245,7 +246,7 @@ public class UpdateHelper extends AbstractComponent {
private Map<String, Object> executeScript(UpdateRequest request, Map<String, Object> ctx) {
try {
if (scriptService != null) {
ExecutableScript script = scriptService.executable(request.script, ScriptContext.Standard.UPDATE, request);
ExecutableScript script = scriptService.executable(request.script, ScriptContext.Standard.UPDATE, request, Collections.emptyMap());
script.setNextVar("ctx", ctx);
script.run();
// we need to unwrap the ctx...

View File

@ -63,6 +63,7 @@ import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -364,7 +365,7 @@ public class QueryShardContext {
* Executes the given template, and returns the response.
*/
public BytesReference executeQueryTemplate(Template template, SearchContext searchContext) {
ExecutableScript executable = getScriptService().executable(template, ScriptContext.Standard.SEARCH, searchContext);
ExecutableScript executable = getScriptService().executable(template, ScriptContext.Standard.SEARCH, searchContext, Collections.emptyMap());
return (BytesReference) executable.run();
}

View File

@ -33,6 +33,7 @@ import org.elasticsearch.script.Script.ScriptField;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.Collections;
import java.util.Objects;
public class ScriptQueryBuilder extends AbstractQueryBuilder<ScriptQueryBuilder> {
@ -80,7 +81,7 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder<ScriptQueryBuilder>
public ScriptQuery(Script script, ScriptService scriptService, SearchLookup searchLookup) {
this.script = script;
this.searchScript = scriptService.search(searchLookup, script, ScriptContext.Standard.SEARCH);
this.searchScript = scriptService.search(searchLookup, script, ScriptContext.Standard.SEARCH, Collections.emptyMap());
}
@Override
@ -161,4 +162,4 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder<ScriptQueryBuilder>
protected boolean doEquals(ScriptQueryBuilder other) {
return Objects.equals(script, other.script);
}
}
}

View File

@ -33,6 +33,7 @@ import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.SearchScript;
import java.io.IOException;
import java.util.Collections;
import java.util.Objects;
/**
@ -89,10 +90,10 @@ public class ScriptScoreFunctionBuilder extends ScoreFunctionBuilder<ScriptScore
@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
try {
SearchScript searchScript = context.getScriptService().search(context.lookup(), script, ScriptContext.Standard.SEARCH);
SearchScript searchScript = context.getScriptService().search(context.lookup(), script, ScriptContext.Standard.SEARCH, Collections.emptyMap());
return new ScriptScoreFunction(script, searchScript);
} catch (Exception e) {
throw new QueryShardException(context, "script_score: the script could not be loaded", e);
}
}
}
}

View File

@ -62,7 +62,7 @@ public class NativeScriptEngineService extends AbstractComponent implements Scri
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
NativeScriptFactory scriptFactory = scripts.get(script);
if (scriptFactory != null) {
return scriptFactory;

View File

@ -36,7 +36,7 @@ public interface ScriptEngineService extends Closeable {
boolean sandboxed();
Object compile(String script);
Object compile(String script, Map<String, String> params);
ExecutableScript executable(CompiledScript compiledScript, @Nullable Map<String, Object> vars);

View File

@ -67,6 +67,7 @@ import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@ -96,9 +97,9 @@ public class ScriptService extends AbstractComponent implements Closeable {
private final Map<String, ScriptEngineService> scriptEnginesByLang;
private final Map<String, ScriptEngineService> scriptEnginesByExt;
private final ConcurrentMap<String, CompiledScript> staticCache = ConcurrentCollections.newConcurrentMap();
private final ConcurrentMap<CacheKey, CompiledScript> staticCache = ConcurrentCollections.newConcurrentMap();
private final Cache<String, CompiledScript> cache;
private final Cache<CacheKey, CompiledScript> cache;
private final Path scriptsDirectory;
private final ScriptModes scriptModes;
@ -153,7 +154,7 @@ public class ScriptService extends AbstractComponent implements Closeable {
this.defaultLang = settings.get(DEFAULT_SCRIPTING_LANGUAGE_SETTING, DEFAULT_LANG);
CacheBuilder<String, CompiledScript> cacheBuilder = CacheBuilder.builder();
CacheBuilder<CacheKey, CompiledScript> cacheBuilder = CacheBuilder.builder();
if (cacheMaxSize >= 0) {
cacheBuilder.setMaximumWeight(cacheMaxSize);
}
@ -224,7 +225,7 @@ public class ScriptService extends AbstractComponent implements Closeable {
/**
* Checks if a script can be executed and compiles it if needed, or returns the previously compiled and cached script.
*/
public CompiledScript compile(Script script, ScriptContext scriptContext, HasContextAndHeaders headersContext) {
public CompiledScript compile(Script script, ScriptContext scriptContext, HasContextAndHeaders headersContext, Map<String, String> params) {
if (script == null) {
throw new IllegalArgumentException("The parameter script (Script) must not be null.");
}
@ -252,14 +253,14 @@ public class ScriptService extends AbstractComponent implements Closeable {
" operation [" + scriptContext.getKey() + "] and lang [" + lang + "] are not supported");
}
return compileInternal(script, headersContext);
return compileInternal(script, headersContext, params);
}
/**
* Compiles a script straight-away, or returns the previously compiled and cached script,
* without checking if it can be executed based on settings.
*/
public CompiledScript compileInternal(Script script, HasContextAndHeaders context) {
public CompiledScript compileInternal(Script script, HasContextAndHeaders context, Map<String, String> params) {
if (script == null) {
throw new IllegalArgumentException("The parameter script (Script) must not be null.");
}
@ -277,7 +278,7 @@ public class ScriptService extends AbstractComponent implements Closeable {
ScriptEngineService scriptEngineService = getScriptEngineServiceForLang(lang);
if (type == ScriptType.FILE) {
String cacheKey = getCacheKey(scriptEngineService, name, null);
CacheKey cacheKey = new CacheKey(scriptEngineService, name, null, params);
//On disk scripts will be loaded into the staticCache by the listener
CompiledScript compiledScript = staticCache.get(cacheKey);
@ -299,14 +300,14 @@ public class ScriptService extends AbstractComponent implements Closeable {
code = getScriptFromIndex(indexedScript.lang, indexedScript.id, context);
}
String cacheKey = getCacheKey(scriptEngineService, type == ScriptType.INLINE ? null : name, code);
CacheKey cacheKey = new CacheKey(scriptEngineService, type == ScriptType.INLINE ? null : name, code, params);
CompiledScript compiledScript = cache.get(cacheKey);
if (compiledScript == null) {
//Either an un-cached inline script or indexed script
//If the script type is inline the name will be the same as the code for identification in exceptions
try {
compiledScript = new CompiledScript(type, name, lang, scriptEngineService.compile(code));
compiledScript = new CompiledScript(type, name, lang, scriptEngineService.compile(code, params));
} catch (Exception exception) {
throw new ScriptException("Failed to compile " + type + " script [" + name + "] using lang [" + lang + "]", exception);
}
@ -364,7 +365,7 @@ public class ScriptService extends AbstractComponent implements Closeable {
//we don't know yet what the script will be used for, but if all of the operations for this lang with
//indexed scripts are disabled, it makes no sense to even compile it.
if (isAnyScriptContextEnabled(scriptLang, scriptEngineService, ScriptType.INDEXED)) {
Object compiled = scriptEngineService.compile(template.getScript());
Object compiled = scriptEngineService.compile(template.getScript(), Collections.emptyMap());
if (compiled == null) {
throw new IllegalArgumentException("Unable to parse [" + template.getScript() +
"] lang [" + scriptLang + "] (ScriptService.compile returned null)");
@ -436,8 +437,8 @@ public class ScriptService extends AbstractComponent implements Closeable {
/**
* Compiles (or retrieves from cache) and executes the provided script
*/
public ExecutableScript executable(Script script, ScriptContext scriptContext, HasContextAndHeaders headersContext) {
return executable(compile(script, scriptContext, headersContext), script.getParams());
public ExecutableScript executable(Script script, ScriptContext scriptContext, HasContextAndHeaders headersContext, Map<String, String> params) {
return executable(compile(script, scriptContext, headersContext, params), script.getParams());
}
/**
@ -450,8 +451,8 @@ public class ScriptService extends AbstractComponent implements Closeable {
/**
* Compiles (or retrieves from cache) and executes the provided search script
*/
public SearchScript search(SearchLookup lookup, Script script, ScriptContext scriptContext) {
CompiledScript compiledScript = compile(script, scriptContext, SearchContext.current());
public SearchScript search(SearchLookup lookup, Script script, ScriptContext scriptContext, Map<String, String> params) {
CompiledScript compiledScript = compile(script, scriptContext, SearchContext.current(), params);
return getScriptEngineServiceForLang(compiledScript.lang()).search(compiledScript, lookup, script.getParams());
}
@ -491,9 +492,9 @@ public class ScriptService extends AbstractComponent implements Closeable {
* {@code ScriptEngineService}'s {@code scriptRemoved} method when the
* script has been removed from the cache
*/
private class ScriptCacheRemovalListener implements RemovalListener<String, CompiledScript> {
private class ScriptCacheRemovalListener implements RemovalListener<CacheKey, CompiledScript> {
@Override
public void onRemoval(RemovalNotification<String, CompiledScript> notification) {
public void onRemoval(RemovalNotification<CacheKey, CompiledScript> notification) {
scriptMetrics.onCacheEviction();
for (ScriptEngineService service : scriptEngines) {
try {
@ -539,8 +540,8 @@ public class ScriptService extends AbstractComponent implements Closeable {
logger.info("compiling script file [{}]", file.toAbsolutePath());
try(InputStreamReader reader = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8)) {
String script = Streams.copyToString(reader);
String cacheKey = getCacheKey(engineService, scriptNameExt.v1(), null);
staticCache.put(cacheKey, new CompiledScript(ScriptType.FILE, scriptNameExt.v1(), engineService.types()[0], engineService.compile(script)));
CacheKey cacheKey = new CacheKey(engineService, scriptNameExt.v1(), null, Collections.emptyMap());
staticCache.put(cacheKey, new CompiledScript(ScriptType.FILE, scriptNameExt.v1(), engineService.types()[0], engineService.compile(script, Collections.emptyMap())));
scriptMetrics.onCompilation();
}
} else {
@ -565,7 +566,7 @@ public class ScriptService extends AbstractComponent implements Closeable {
ScriptEngineService engineService = getScriptEngineServiceForFileExt(scriptNameExt.v2());
assert engineService != null;
logger.info("removing script file [{}]", file.toAbsolutePath());
staticCache.remove(getCacheKey(engineService, scriptNameExt.v1(), null));
staticCache.remove(new CacheKey(engineService, scriptNameExt.v1(), null, Collections.emptyMap()));
}
}
@ -625,11 +626,44 @@ public class ScriptService extends AbstractComponent implements Closeable {
}
}
private static String getCacheKey(ScriptEngineService scriptEngineService, String name, String code) {
String lang = scriptEngineService.types()[0];
return lang + ":" + (name != null ? ":" + name : "") + (code != null ? ":" + code : "");
private static final class CacheKey {
final String lang;
final String name;
final String code;
final Map<String, String> params;
private CacheKey(final ScriptEngineService service, final String name, final String code, final Map<String, String> params) {
this.lang = service.types()[0];
this.name = name;
this.code = code;
this.params = params;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey = (CacheKey)o;
if (!lang.equals(cacheKey.lang)) return false;
if (name != null ? !name.equals(cacheKey.name) : cacheKey.name != null) return false;
if (code != null ? !code.equals(cacheKey.code) : cacheKey.code != null) return false;
return params.equals(cacheKey.params);
}
@Override
public int hashCode() {
int result = lang.hashCode();
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (code != null ? code.hashCode() : 0);
result = 31 * result + params.hashCode();
return result;
}
}
private static class IndexedScript {
private final String lang;
private final String id;

View File

@ -91,6 +91,7 @@ import org.elasticsearch.search.warmer.IndexWarmersMetaData;
import org.elasticsearch.threadpool.ThreadPool;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@ -560,7 +561,7 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> imp
context.scrollContext().scroll = request.scroll();
}
if (request.template() != null) {
ExecutableScript executable = this.scriptService.executable(request.template(), ScriptContext.Standard.SEARCH, context);
ExecutableScript executable = this.scriptService.executable(request.template(), ScriptContext.Standard.SEARCH, context, Collections.emptyMap());
BytesReference run = (BytesReference) executable.run();
try (XContentParser parser = XContentFactory.xContent(run).createParser(run)) {
QueryParseContext queryParseContext = new QueryParseContext(indicesService.getIndicesQueryRegistry());
@ -835,7 +836,7 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> imp
}
if (source.scriptFields() != null) {
for (org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField field : source.scriptFields()) {
SearchScript searchScript = context.scriptService().search(context.lookup(), field.script(), ScriptContext.Standard.SEARCH);
SearchScript searchScript = context.scriptService().search(context.lookup(), field.script(), ScriptContext.Standard.SEARCH, Collections.emptyMap());
context.scriptFields().add(new ScriptField(field.fieldName(), searchScript, field.ignoreFailure()));
}
}

View File

@ -37,6 +37,7 @@ import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -82,7 +83,7 @@ public class ScriptHeuristic extends SignificanceHeuristic {
@Override
public void initialize(InternalAggregation.ReduceContext context) {
searchScript = context.scriptService().executable(script, ScriptContext.Standard.AGGS, context);
searchScript = context.scriptService().executable(script, ScriptContext.Standard.AGGS, context, Collections.emptyMap());
searchScript.setNextVar("_subset_freq", subsetDfHolder);
searchScript.setNextVar("_subset_size", subsetSizeHolder);
searchScript.setNextVar("_superset_freq", supersetDfHolder);
@ -170,7 +171,7 @@ public class ScriptHeuristic extends SignificanceHeuristic {
}
ExecutableScript searchScript;
try {
searchScript = scriptService.executable(script, ScriptContext.Standard.AGGS, context);
searchScript = scriptService.executable(script, ScriptContext.Standard.AGGS, context, Collections.emptyMap());
} catch (Exception e) {
throw new ElasticsearchParseException("failed to parse [{}] significance heuristic. the script [{}] could not be loaded", e, script, heuristicName);
}

View File

@ -33,6 +33,7 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -91,7 +92,7 @@ public class InternalScriptedMetric extends InternalMetricsAggregation implement
vars.putAll(firstAggregation.reduceScript.getParams());
}
CompiledScript compiledScript = reduceContext.scriptService().compile(firstAggregation.reduceScript,
ScriptContext.Standard.AGGS, reduceContext);
ScriptContext.Standard.AGGS, reduceContext, Collections.emptyMap());
ExecutableScript script = reduceContext.scriptService().executable(compiledScript, vars);
aggregation = script.run();
} else {

View File

@ -39,6 +39,7 @@ import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -58,11 +59,11 @@ public class ScriptedMetricAggregator extends MetricsAggregator {
this.params = params;
ScriptService scriptService = context.searchContext().scriptService();
if (initScript != null) {
scriptService.executable(initScript, ScriptContext.Standard.AGGS, context.searchContext()).run();
scriptService.executable(initScript, ScriptContext.Standard.AGGS, context.searchContext(), Collections.emptyMap()).run();
}
this.mapScript = scriptService.search(context.searchContext().lookup(), mapScript, ScriptContext.Standard.AGGS);
this.mapScript = scriptService.search(context.searchContext().lookup(), mapScript, ScriptContext.Standard.AGGS, Collections.emptyMap());
if (combineScript != null) {
this.combineScript = scriptService.executable(combineScript, ScriptContext.Standard.AGGS, context.searchContext());
this.combineScript = scriptService.executable(combineScript, ScriptContext.Standard.AGGS, context.searchContext(), Collections.emptyMap());
} else {
this.combineScript = null;
}

View File

@ -90,7 +90,7 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator {
InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
List<? extends Bucket> buckets = originalAgg.getBuckets();
CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS, reduceContext);
CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS, reduceContext, Collections.emptyMap());
List newBuckets = new ArrayList<>();
for (Bucket bucket : buckets) {
Map<String, Object> vars = new HashMap<>();

View File

@ -38,6 +38,7 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -88,7 +89,7 @@ public class BucketSelectorPipelineAggregator extends PipelineAggregator {
InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
List<? extends Bucket> buckets = originalAgg.getBuckets();
CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS, reduceContext);
CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS, reduceContext, Collections.emptyMap());
List newBuckets = new ArrayList<>();
for (Bucket bucket : buckets) {
Map<String, Object> vars = new HashMap<>();

View File

@ -43,6 +43,7 @@ import org.elasticsearch.search.internal.SearchContext;
import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -227,7 +228,7 @@ public class ValuesSourceParser<VS extends ValuesSource> {
}
private SearchScript createScript() {
return input.script == null ? null : context.scriptService().search(context.lookup(), input.script, ScriptContext.Standard.AGGS);
return input.script == null ? null : context.scriptService().search(context.lookup(), input.script, ScriptContext.Standard.AGGS, Collections.emptyMap());
}
private static ValueFormat resolveFormat(@Nullable String format, @Nullable ValueType valueType) {

View File

@ -30,6 +30,7 @@ import org.elasticsearch.search.SearchParseElement;
import org.elasticsearch.search.SearchParseException;
import org.elasticsearch.search.internal.SearchContext;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -97,9 +98,9 @@ public class ScriptFieldsParseElement implements SearchParseElement {
throw new SearchParseException(context, "must specify a script in script fields", parser.getTokenLocation());
}
SearchScript searchScript = context.scriptService().search(context.lookup(), script, ScriptContext.Standard.SEARCH);
SearchScript searchScript = context.scriptService().search(context.lookup(), script, ScriptContext.Standard.SEARCH, Collections.emptyMap());
context.scriptFields().add(new ScriptFieldsContext.ScriptField(fieldName, searchScript, ignoreException));
}
}
}
}
}

View File

@ -50,6 +50,7 @@ import org.elasticsearch.search.SearchParseException;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -130,7 +131,7 @@ public class ScriptSortParser implements SortParser {
if (type == null) {
throw new SearchParseException(context, "_script sorting requires setting the type of the script", parser.getTokenLocation());
}
final SearchScript searchScript = context.scriptService().search(context.lookup(), script, ScriptContext.Standard.SEARCH);
final SearchScript searchScript = context.scriptService().search(context.lookup(), script, ScriptContext.Standard.SEARCH, Collections.emptyMap());
if (STRING_SORT_TYPE.equals(type) && (sortMode == MultiValueMode.SUM || sortMode == MultiValueMode.AVG)) {
throw new SearchParseException(context, "type [string] doesn't support mode [" + sortMode + "]", parser.getTokenLocation());

View File

@ -39,6 +39,7 @@ import org.elasticsearch.search.suggest.SuggestionSearchContext;
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionContext.DirectCandidateGenerator;
import java.io.IOException;
import java.util.Collections;
public final class PhraseSuggestParser implements SuggestContextParser {
@ -143,7 +144,7 @@ public final class PhraseSuggestParser implements SuggestContextParser {
}
Template template = Template.parse(parser, parseFieldMatcher);
CompiledScript compiledScript = suggester.scriptService().compile(template, ScriptContext.Standard.SEARCH,
headersContext);
headersContext, Collections.emptyMap());
suggestion.setCollateQueryScript(compiledScript);
} else if ("params".equals(fieldName)) {
suggestion.setCollateScriptParams(parser.map());

View File

@ -54,7 +54,7 @@ public class FileScriptTests extends ESTestCase {
.put("script.engine." + MockScriptEngine.NAME + ".file.aggs", false).build();
ScriptService scriptService = makeScriptService(settings);
Script script = new Script("script1", ScriptService.ScriptType.FILE, MockScriptEngine.NAME, null);
assertNotNull(scriptService.compile(script, ScriptContext.Standard.SEARCH, contextAndHeaders));
assertNotNull(scriptService.compile(script, ScriptContext.Standard.SEARCH, contextAndHeaders, Collections.emptyMap()));
}
public void testAllOpsDisabled() throws Exception {
@ -68,7 +68,7 @@ public class FileScriptTests extends ESTestCase {
Script script = new Script("script1", ScriptService.ScriptType.FILE, MockScriptEngine.NAME, null);
for (ScriptContext context : ScriptContext.Standard.values()) {
try {
scriptService.compile(script, context, contextAndHeaders);
scriptService.compile(script, context, contextAndHeaders, Collections.emptyMap());
fail(context.getKey() + " script should have been rejected");
} catch(Exception e) {
assertTrue(e.getMessage(), e.getMessage().contains("scripts of type [file], operation [" + context.getKey() + "] and lang [" + MockScriptEngine.NAME + "] are disabled"));

View File

@ -36,6 +36,7 @@ import org.elasticsearch.watcher.ResourceWatcherService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@ -62,7 +63,7 @@ public class NativeScriptTests extends ESTestCase {
ScriptService scriptService = injector.getInstance(ScriptService.class);
ExecutableScript executable = scriptService.executable(new Script("my", ScriptType.INLINE, NativeScriptEngineService.NAME, null),
ScriptContext.Standard.SEARCH, contextAndHeaders);
ScriptContext.Standard.SEARCH, contextAndHeaders, Collections.emptyMap());
assertThat(executable.run().toString(), equalTo("test"));
terminate(injector.getInstance(ThreadPool.class));
}
@ -88,7 +89,7 @@ public class NativeScriptTests extends ESTestCase {
for (ScriptContext scriptContext : scriptContextRegistry.scriptContexts()) {
assertThat(scriptService.compile(new Script("my", ScriptType.INLINE, NativeScriptEngineService.NAME, null), scriptContext,
contextAndHeaders), notNullValue());
contextAndHeaders, Collections.emptyMap()), notNullValue());
}
}
@ -110,4 +111,4 @@ public class NativeScriptTests extends ESTestCase {
return "test";
}
}
}
}

View File

@ -58,7 +58,7 @@ public class ScriptContextTests extends ESTestCase {
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
try {
Script script = new Script("1", scriptType, MockScriptEngine.NAME, null);
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"), contextAndHeaders);
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"), contextAndHeaders, Collections.emptyMap());
fail("script compilation should have been rejected");
} catch (ScriptException e) {
assertThat(e.getMessage(), containsString("scripts of type [" + scriptType + "], operation [" + PLUGIN_NAME + "_custom_globally_disabled_op] and lang [" + MockScriptEngine.NAME + "] are disabled"));
@ -71,16 +71,16 @@ public class ScriptContextTests extends ESTestCase {
ScriptService scriptService = makeScriptService();
Script script = new Script("1", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, null);
try {
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"), contextAndHeaders);
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"), contextAndHeaders, Collections.emptyMap());
fail("script compilation should have been rejected");
} catch (ScriptException e) {
assertTrue(e.getMessage(), e.getMessage().contains("scripts of type [inline], operation [" + PLUGIN_NAME + "_custom_exp_disabled_op] and lang [" + MockScriptEngine.NAME + "] are disabled"));
}
// still works for other script contexts
assertNotNull(scriptService.compile(script, ScriptContext.Standard.AGGS, contextAndHeaders));
assertNotNull(scriptService.compile(script, ScriptContext.Standard.SEARCH, contextAndHeaders));
assertNotNull(scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"), contextAndHeaders));
assertNotNull(scriptService.compile(script, ScriptContext.Standard.AGGS, contextAndHeaders, Collections.emptyMap()));
assertNotNull(scriptService.compile(script, ScriptContext.Standard.SEARCH, contextAndHeaders, Collections.emptyMap()));
assertNotNull(scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"), contextAndHeaders, Collections.emptyMap()));
}
public void testUnknownPluginScriptContext() throws Exception {
@ -89,7 +89,7 @@ public class ScriptContextTests extends ESTestCase {
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
try {
Script script = new Script("1", scriptType, MockScriptEngine.NAME, null);
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "unknown"), contextAndHeaders);
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "unknown"), contextAndHeaders, Collections.emptyMap());
fail("script compilation should have been rejected");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("script context [" + PLUGIN_NAME + "_unknown] not supported"));
@ -109,7 +109,7 @@ public class ScriptContextTests extends ESTestCase {
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
try {
Script script = new Script("1", scriptType, MockScriptEngine.NAME, null);
scriptService.compile(script, context, contextAndHeaders);
scriptService.compile(script, context, contextAndHeaders, Collections.emptyMap());
fail("script compilation should have been rejected");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("script context [test] not supported"));

View File

@ -252,7 +252,7 @@ public class ScriptModesTests extends ESTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return null;
}

View File

@ -132,7 +132,7 @@ public class ScriptServiceTests extends ESTestCase {
logger.info("--> verify that file with extension was correctly processed");
CompiledScript compiledScript = scriptService.compile(new Script("test_script", ScriptType.FILE, "test", null),
ScriptContext.Standard.SEARCH, contextAndHeaders);
ScriptContext.Standard.SEARCH, contextAndHeaders, Collections.emptyMap());
assertThat(compiledScript.compiled(), equalTo((Object) "compiled_test_file"));
logger.info("--> delete both files");
@ -143,7 +143,7 @@ public class ScriptServiceTests extends ESTestCase {
logger.info("--> verify that file with extension was correctly removed");
try {
scriptService.compile(new Script("test_script", ScriptType.FILE, "test", null), ScriptContext.Standard.SEARCH,
contextAndHeaders);
contextAndHeaders, Collections.emptyMap());
fail("the script test_script should no longer exist");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("Unable to find on disk file script [test_script] using lang [test]"));
@ -154,9 +154,9 @@ public class ScriptServiceTests extends ESTestCase {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
buildScriptService(Settings.EMPTY);
CompiledScript compiledScript1 = scriptService.compile(new Script("1+1", ScriptType.INLINE, "test", null),
randomFrom(scriptContexts), contextAndHeaders);
randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
CompiledScript compiledScript2 = scriptService.compile(new Script("1+1", ScriptType.INLINE, "test", null),
randomFrom(scriptContexts), contextAndHeaders);
randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertThat(compiledScript1.compiled(), sameInstance(compiledScript2.compiled()));
}
@ -164,9 +164,9 @@ public class ScriptServiceTests extends ESTestCase {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
buildScriptService(Settings.EMPTY);
CompiledScript compiledScript1 = scriptService.compile(new Script("script", ScriptType.INLINE, "test", null),
randomFrom(scriptContexts), contextAndHeaders);
randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
CompiledScript compiledScript2 = scriptService.compile(new Script("script", ScriptType.INLINE, "test2", null),
randomFrom(scriptContexts), contextAndHeaders);
randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertThat(compiledScript1.compiled(), sameInstance(compiledScript2.compiled()));
}
@ -175,9 +175,9 @@ public class ScriptServiceTests extends ESTestCase {
buildScriptService(Settings.EMPTY);
createFileScripts("test");
CompiledScript compiledScript1 = scriptService.compile(new Script("file_script", ScriptType.FILE, "test", null),
randomFrom(scriptContexts), contextAndHeaders);
randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
CompiledScript compiledScript2 = scriptService.compile(new Script("file_script", ScriptType.FILE, "test2", null),
randomFrom(scriptContexts), contextAndHeaders);
randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertThat(compiledScript1.compiled(), sameInstance(compiledScript2.compiled()));
}
@ -338,7 +338,7 @@ public class ScriptServiceTests extends ESTestCase {
for (String type : scriptEngineService.types()) {
try {
scriptService.compile(new Script("test", randomFrom(ScriptType.values()), type, null), new ScriptContext.Plugin(
pluginName, unknownContext), contextAndHeaders);
pluginName, unknownContext), contextAndHeaders, Collections.emptyMap());
fail("script compilation should have been rejected");
} catch(IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("script context [" + pluginName + "_" + unknownContext + "] not supported"));
@ -349,20 +349,20 @@ public class ScriptServiceTests extends ESTestCase {
public void testCompileCountedInCompilationStats() throws IOException {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
buildScriptService(Settings.EMPTY);
scriptService.compile(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.compile(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertEquals(1L, scriptService.stats().getCompilations());
}
public void testExecutableCountedInCompilationStats() throws IOException {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
buildScriptService(Settings.EMPTY);
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertEquals(1L, scriptService.stats().getCompilations());
}
public void testSearchCountedInCompilationStats() throws IOException {
buildScriptService(Settings.EMPTY);
scriptService.search(null, new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts));
scriptService.search(null, new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), Collections.emptyMap());
assertEquals(1L, scriptService.stats().getCompilations());
}
@ -372,7 +372,7 @@ public class ScriptServiceTests extends ESTestCase {
int numberOfCompilations = randomIntBetween(1, 1024);
for (int i = 0; i < numberOfCompilations; i++) {
scriptService
.compile(new Script(i + " + " + i, ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
.compile(new Script(i + " + " + i, ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
}
assertEquals(numberOfCompilations, scriptService.stats().getCompilations());
}
@ -382,8 +382,8 @@ public class ScriptServiceTests extends ESTestCase {
Settings.Builder builder = Settings.builder();
builder.put(ScriptService.SCRIPT_CACHE_SIZE_SETTING, 1);
buildScriptService(builder.build());
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertEquals(1L, scriptService.stats().getCompilations());
}
@ -391,14 +391,14 @@ public class ScriptServiceTests extends ESTestCase {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
buildScriptService(Settings.EMPTY);
createFileScripts("test");
scriptService.compile(new Script("file_script", ScriptType.FILE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.compile(new Script("file_script", ScriptType.FILE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertEquals(1L, scriptService.stats().getCompilations());
}
public void testIndexedScriptCountedInCompilationStats() throws IOException {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
buildScriptService(Settings.EMPTY);
scriptService.compile(new Script("script", ScriptType.INDEXED, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.compile(new Script("script", ScriptType.INDEXED, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertEquals(1L, scriptService.stats().getCompilations());
}
@ -407,8 +407,8 @@ public class ScriptServiceTests extends ESTestCase {
Settings.Builder builder = Settings.builder();
builder.put(ScriptService.SCRIPT_CACHE_SIZE_SETTING, 1);
buildScriptService(builder.build());
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.executable(new Script("2+2", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders);
scriptService.executable(new Script("1+1", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
scriptService.executable(new Script("2+2", ScriptType.INLINE, "test", null), randomFrom(scriptContexts), contextAndHeaders, Collections.emptyMap());
assertEquals(2L, scriptService.stats().getCompilations());
assertEquals(1L, scriptService.stats().getCacheEvictions());
}
@ -424,7 +424,7 @@ public class ScriptServiceTests extends ESTestCase {
private void assertCompileRejected(String lang, String script, ScriptType scriptType, ScriptContext scriptContext,
HasContextAndHeaders contextAndHeaders) {
try {
scriptService.compile(new Script(script, scriptType, lang, null), scriptContext, contextAndHeaders);
scriptService.compile(new Script(script, scriptType, lang, null), scriptContext, contextAndHeaders, Collections.emptyMap());
fail("compile should have been rejected for lang [" + lang + "], script_type [" + scriptType + "], scripted_op [" + scriptContext + "]");
} catch(ScriptException e) {
//all good
@ -433,7 +433,7 @@ public class ScriptServiceTests extends ESTestCase {
private void assertCompileAccepted(String lang, String script, ScriptType scriptType, ScriptContext scriptContext,
HasContextAndHeaders contextAndHeaders) {
assertThat(scriptService.compile(new Script(script, scriptType, lang, null), scriptContext, contextAndHeaders), notNullValue());
assertThat(scriptService.compile(new Script(script, scriptType, lang, null), scriptContext, contextAndHeaders, Collections.emptyMap()), notNullValue());
}
public static class TestEngineService implements ScriptEngineService {
@ -454,7 +454,7 @@ public class ScriptServiceTests extends ESTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return "compiled_" + script;
}

View File

@ -1429,7 +1429,7 @@ public class DateHistogramIT extends ESIntegTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}
@ -1555,7 +1555,7 @@ public class DateHistogramIT extends ESIntegTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}

View File

@ -364,7 +364,7 @@ public class AvgIT extends AbstractNumericTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}
@ -500,7 +500,7 @@ public class AvgIT extends AbstractNumericTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}
@ -585,4 +585,4 @@ public class AvgIT extends AbstractNumericTestCase {
public void scriptRemoved(CompiledScript script) {
}
}
}
}

View File

@ -359,7 +359,7 @@ public class SumIT extends AbstractNumericTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}
@ -497,7 +497,7 @@ public class SumIT extends AbstractNumericTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}
@ -583,4 +583,4 @@ public class SumIT extends AbstractNumericTestCase {
public void scriptRemoved(CompiledScript script) {
}
}
}
}

View File

@ -244,7 +244,7 @@ public class ValueCountIT extends ESIntegTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}
@ -330,4 +330,4 @@ public class ValueCountIT extends ESIntegTestCase {
public void scriptRemoved(CompiledScript script) {
}
}
}
}

View File

@ -120,7 +120,7 @@ public class UpdateIT extends ESIntegTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return new Object(); // unused
}
@ -218,7 +218,7 @@ public class UpdateIT extends ESIntegTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}
@ -309,7 +309,7 @@ public class UpdateIT extends ESIntegTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return new Object(); // unused
}
@ -400,7 +400,7 @@ public class UpdateIT extends ESIntegTestCase {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return new Object(); // unused
}

View File

@ -95,7 +95,7 @@ public class ExpressionScriptEngineService extends AbstractComponent implements
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
// classloader created here
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {

View File

@ -33,23 +33,23 @@ public class ExpressionTests extends ESSingleNodeTestCase {
public void testNeedsScores() {
IndexService index = createIndex("test", Settings.EMPTY, "type", "d", "type=double");
ExpressionScriptEngineService service = new ExpressionScriptEngineService(Settings.EMPTY);
SearchLookup lookup = new SearchLookup(index.mapperService(), index.fieldData(), null);
Object compiled = service.compile("1.2");
Object compiled = service.compile("1.2", Collections.emptyMap());
SearchScript ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "expression", compiled), lookup, Collections.<String, Object>emptyMap());
assertFalse(ss.needsScores());
compiled = service.compile("doc['d'].value");
compiled = service.compile("doc['d'].value", Collections.emptyMap());
ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "expression", compiled), lookup, Collections.<String, Object>emptyMap());
assertFalse(ss.needsScores());
compiled = service.compile("1/_score");
compiled = service.compile("1/_score", Collections.emptyMap());
ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "expression", compiled), lookup, Collections.<String, Object>emptyMap());
assertTrue(ss.needsScores());
compiled = service.compile("doc['d'].value * _score");
compiled = service.compile("doc['d'].value * _score", Collections.emptyMap());
ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "expression", compiled), lookup, Collections.<String, Object>emptyMap());
assertTrue(ss.needsScores());
}

View File

@ -165,7 +165,7 @@ public class GroovyScriptEngineService extends AbstractComponent implements Scri
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
try {
// we reuse classloader, so do a security check just in case.
SecurityManager sm = System.getSecurityManager();

View File

@ -99,7 +99,7 @@ public class GroovySecurityTests extends ESTestCase {
// filtered directly by our classloader
assertFailure("getClass().getClassLoader().loadClass(\"java.lang.Runtime\").availableProcessors()", PrivilegedActionException.class);
// unfortunately, we have access to other classloaders (due to indy mechanism needing getClassLoader permission)
// but we can't do much with them directly at least.
// but we can't do much with them directly at least.
assertFailure("myobject.getClass().getClassLoader().loadClass(\"java.lang.Runtime\").availableProcessors()", SecurityException.class);
assertFailure("d = new DateTime(); d.getClass().getDeclaredMethod(\"year\").setAccessible(true)", SecurityException.class);
assertFailure("d = new DateTime(); d.\"${'get' + 'Class'}\"()." +
@ -133,9 +133,9 @@ public class GroovySecurityTests extends ESTestCase {
vars.put("myarray", Arrays.asList("foo"));
vars.put("myobject", new MyObject());
se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "test", "js", se.compile(script)), vars).run();
se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "test", "js", se.compile(script, Collections.emptyMap())), vars).run();
}
public static class MyObject {
public int getPrimitive() { return 0; }
public Object getObject() { return "value"; }

View File

@ -85,7 +85,7 @@ public class MustacheScriptEngineService extends AbstractComponent implements Sc
* @return a compiled template object for later execution.
* */
@Override
public Object compile(String template) {
public Object compile(String template, Map<String, String> params) {
/** Factory to generate Mustache objects from. */
return (new JsonEscapingMustacheFactory()).compile(new FastStringReader(template), "query-template");
}

View File

@ -28,6 +28,7 @@ import org.junit.Before;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -52,7 +53,7 @@ public class MustacheScriptEngineTests extends ESTestCase {
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}";
Map<String, Object> vars = new HashMap<>();
vars.put("boost_val", "0.3");
BytesReference o = (BytesReference) qe.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "", "mustache", qe.compile(template)), vars).run();
BytesReference o = (BytesReference) qe.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "", "mustache", qe.compile(template, Collections.emptyMap())), vars).run();
assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}",
new String(o.toBytes(), Charset.forName("UTF-8")));
@ -63,7 +64,7 @@ public class MustacheScriptEngineTests extends ESTestCase {
Map<String, Object> vars = new HashMap<>();
vars.put("boost_val", "0.3");
vars.put("body_val", "\"quick brown\"");
BytesReference o = (BytesReference) qe.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "", "mustache", qe.compile(template)), vars).run();
BytesReference o = (BytesReference) qe.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "", "mustache", qe.compile(template, Collections.emptyMap())), vars).run();
assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}",
new String(o.toBytes(), Charset.forName("UTF-8")));

View File

@ -62,7 +62,7 @@ public class JavaScriptScriptEngineService extends AbstractComponent implements
// one time initialization of rhino security manager integration
private static final CodeSource DOMAIN;
private static final int OPTIMIZATION_LEVEL = 1;
static {
try {
DOMAIN = new CodeSource(new URL("file:" + BootstrapInfo.UNTRUSTED_CODEBASE), (Certificate[]) null);
@ -110,7 +110,7 @@ public class JavaScriptScriptEngineService extends AbstractComponent implements
if (securityDomain != DOMAIN) {
throw new SecurityException("illegal securityDomain: " + securityDomain);
}
return super.createClassLoader(parent, securityDomain);
}
});
@ -157,7 +157,7 @@ public class JavaScriptScriptEngineService extends AbstractComponent implements
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
Context ctx = Context.enter();
try {
return ctx.compileString(script, generateScriptName(), 1, DOMAIN);

View File

@ -29,6 +29,7 @@ import org.junit.After;
import org.junit.Before;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -54,7 +55,7 @@ public class JavaScriptScriptEngineTests extends ESTestCase {
public void testSimpleEquation() {
Map<String, Object> vars = new HashMap<String, Object>();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testSimpleEquation", "js", se.compile("1 + 2")), vars).run();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testSimpleEquation", "js", se.compile("1 + 2", Collections.emptyMap())), vars).run();
assertThat(((Number) o).intValue(), equalTo(3));
}
@ -64,20 +65,20 @@ public class JavaScriptScriptEngineTests extends ESTestCase {
Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
Map<String, Object> obj1 = MapBuilder.<String, Object>newMapBuilder().put("prop1", "value1").put("obj2", obj2).put("l", Arrays.asList("2", "1")).map();
vars.put("obj1", obj1);
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "js", se.compile("obj1")), vars).run();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "js", se.compile("obj1", Collections.emptyMap())), vars).run();
assertThat(o, instanceOf(Map.class));
obj1 = (Map<String, Object>) o;
assertThat((String) obj1.get("prop1"), equalTo("value1"));
assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "js", se.compile("obj1.l[0]")), vars).run();
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "js", se.compile("obj1.l[0]", Collections.emptyMap())), vars).run();
assertThat(((String) o), equalTo("2"));
}
public void testJavaScriptObjectToMap() {
Map<String, Object> vars = new HashMap<String, Object>();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testJavaScriptObjectToMap", "js",
se.compile("var obj1 = {}; obj1.prop1 = 'value1'; obj1.obj2 = {}; obj1.obj2.prop2 = 'value2'; obj1")), vars).run();
se.compile("var obj1 = {}; obj1.prop1 = 'value1'; obj1.obj2 = {}; obj1.obj2.prop2 = 'value2'; obj1", Collections.emptyMap())), vars).run();
Map obj1 = (Map) o;
assertThat((String) obj1.get("prop1"), equalTo("value1"));
assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));
@ -92,7 +93,7 @@ public class JavaScriptScriptEngineTests extends ESTestCase {
vars.put("ctx", ctx);
ExecutableScript executable = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testJavaScriptObjectMapInter", "js",
se.compile("ctx.obj2 = {}; ctx.obj2.prop2 = 'value2'; ctx.obj1.prop1 = 'uvalue1'")), vars);
se.compile("ctx.obj2 = {}; ctx.obj2.prop2 = 'value2'; ctx.obj1.prop1 = 'uvalue1'", Collections.emptyMap())), vars);
executable.run();
ctx = (Map<String, Object>) executable.unwrap(vars.get("ctx"));
assertThat(ctx.containsKey("obj1"), equalTo(true));
@ -106,7 +107,7 @@ public class JavaScriptScriptEngineTests extends ESTestCase {
Map<String, Object> doc = new HashMap<String, Object>();
ctx.put("doc", doc);
Object compiled = se.compile("ctx.doc.field1 = ['value1', 'value2']");
Object compiled = se.compile("ctx.doc.field1 = ['value1', 'value2']", Collections.emptyMap());
ExecutableScript script = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testJavaScriptInnerArrayCreation", "js",
compiled), new HashMap<String, Object>());
script.setNextVar("ctx", ctx);
@ -124,21 +125,21 @@ public class JavaScriptScriptEngineTests extends ESTestCase {
vars.put("l", Arrays.asList("1", "2", "3", obj1));
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessInScript", "js",
se.compile("l.length")), vars).run();
se.compile("l.length", Collections.emptyMap())), vars).run();
assertThat(((Number) o).intValue(), equalTo(4));
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessInScript", "js",
se.compile("l[0]")), vars).run();
se.compile("l[0]", Collections.emptyMap())), vars).run();
assertThat(((String) o), equalTo("1"));
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessInScript", "js",
se.compile("l[3]")), vars).run();
se.compile("l[3]", Collections.emptyMap())), vars).run();
obj1 = (Map<String, Object>) o;
assertThat((String) obj1.get("prop1"), equalTo("value1"));
assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessInScript", "js",
se.compile("l[3].prop1")), vars).run();
se.compile("l[3].prop1", Collections.emptyMap())), vars).run();
assertThat(((String) o), equalTo("value1"));
}
@ -146,7 +147,7 @@ public class JavaScriptScriptEngineTests extends ESTestCase {
Map<String, Object> vars = new HashMap<String, Object>();
Map<String, Object> ctx = new HashMap<String, Object>();
vars.put("ctx", ctx);
Object compiledScript = se.compile("ctx.value");
Object compiledScript = se.compile("ctx.value", Collections.emptyMap());
ExecutableScript script = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testChangingVarsCrossExecution1", "js",
compiledScript), vars);
@ -161,7 +162,7 @@ public class JavaScriptScriptEngineTests extends ESTestCase {
public void testChangingVarsCrossExecution2() {
Map<String, Object> vars = new HashMap<String, Object>();
Object compiledScript = se.compile("value");
Object compiledScript = se.compile("value", Collections.emptyMap());
ExecutableScript script = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testChangingVarsCrossExecution2", "js",
compiledScript), vars);

View File

@ -25,6 +25,7 @@ import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.test.ESTestCase;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@ -40,7 +41,7 @@ import static org.hamcrest.Matchers.equalTo;
public class JavaScriptScriptMultiThreadedTests extends ESTestCase {
public void testExecutableNoRuntimeParams() throws Exception {
final JavaScriptScriptEngineService se = new JavaScriptScriptEngineService(Settings.Builder.EMPTY_SETTINGS);
final Object compiled = se.compile("x + y");
final Object compiled = se.compile("x + y", Collections.emptyMap());
final AtomicBoolean failed = new AtomicBoolean();
Thread[] threads = new Thread[50];
@ -82,7 +83,7 @@ public class JavaScriptScriptMultiThreadedTests extends ESTestCase {
public void testExecutableWithRuntimeParams() throws Exception {
final JavaScriptScriptEngineService se = new JavaScriptScriptEngineService(Settings.Builder.EMPTY_SETTINGS);
final Object compiled = se.compile("x + y");
final Object compiled = se.compile("x + y", Collections.emptyMap());
final AtomicBoolean failed = new AtomicBoolean();
Thread[] threads = new Thread[50];
@ -124,7 +125,7 @@ public class JavaScriptScriptMultiThreadedTests extends ESTestCase {
public void testExecute() throws Exception {
final JavaScriptScriptEngineService se = new JavaScriptScriptEngineService(Settings.Builder.EMPTY_SETTINGS);
final Object compiled = se.compile("x + y");
final Object compiled = se.compile("x + y", Collections.emptyMap());
final AtomicBoolean failed = new AtomicBoolean();
Thread[] threads = new Thread[50];

View File

@ -26,6 +26,7 @@ import org.elasticsearch.test.ESTestCase;
import org.mozilla.javascript.EcmaError;
import org.mozilla.javascript.WrappedException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -33,7 +34,7 @@ import java.util.Map;
* Tests for the Javascript security permissions
*/
public class JavaScriptSecurityTests extends ESTestCase {
private JavaScriptScriptEngineService se;
@Override
@ -53,14 +54,14 @@ public class JavaScriptSecurityTests extends ESTestCase {
/** runs a script */
private void doTest(String script) {
Map<String, Object> vars = new HashMap<String, Object>();
se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "test", "js", se.compile(script)), vars).run();
se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "test", "js", se.compile(script, Collections.emptyMap())), vars).run();
}
/** asserts that a script runs without exception */
private void assertSuccess(String script) {
doTest(script);
}
/** assert that a security exception is hit */
private void assertFailure(String script, Class<? extends Throwable> exceptionClass) {
try {
@ -78,13 +79,13 @@ public class JavaScriptSecurityTests extends ESTestCase {
}
}
}
/** Test some javascripts that are ok */
public void testOK() {
assertSuccess("1 + 2");
assertSuccess("Math.cos(Math.PI)");
}
/** Test some javascripts that should hit security exception */
public void testNotOK() throws Exception {
// sanity check :)

View File

@ -25,6 +25,7 @@ import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -35,7 +36,7 @@ public class SimpleBench {
public static void main(String[] args) {
JavaScriptScriptEngineService se = new JavaScriptScriptEngineService(Settings.Builder.EMPTY_SETTINGS);
Object compiled = se.compile("x + y");
Object compiled = se.compile("x + y", Collections.emptyMap());
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "testExecutableNoRuntimeParams", "js", compiled);
Map<String, Object> vars = new HashMap<String, Object>();

View File

@ -28,6 +28,7 @@ import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.LeafSearchScript;
import org.elasticsearch.script.ScriptEngineService;
import org.elasticsearch.script.ScriptException;
import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.lookup.SearchLookup;
@ -37,15 +38,16 @@ import java.security.AccessController;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.Map;
public class PlanAScriptEngineService extends AbstractComponent implements ScriptEngineService {
public static final String NAME = "plan-a";
// TODO: this should really be per-script since scripts do so many different things?
private static final CompilerSettings compilerSettings = new CompilerSettings();
public static final String NUMERIC_OVERFLOW = "plan-a.numeric_overflow";
// default settings, used unless otherwise specified
private static final CompilerSettings DEFAULT_COMPILER_SETTINGS = new CompilerSettings();
public static final String NUMERIC_OVERFLOW = "numeric_overflow";
// TODO: how should custom definitions be specified?
private Definition definition = null;
@ -53,7 +55,6 @@ public class PlanAScriptEngineService extends AbstractComponent implements Scrip
@Inject
public PlanAScriptEngineService(Settings settings) {
super(settings);
compilerSettings.setNumericOverflow(settings.getAsBoolean(NUMERIC_OVERFLOW, compilerSettings.getNumericOverflow()));
}
public void setDefinition(final Definition definition) {
@ -86,7 +87,23 @@ public class PlanAScriptEngineService extends AbstractComponent implements Scrip
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
final CompilerSettings compilerSettings;
if (params.isEmpty()) {
compilerSettings = DEFAULT_COMPILER_SETTINGS;
} else {
// custom settings
compilerSettings = new CompilerSettings();
Map<String,String> clone = new HashMap<>(params);
String value = clone.remove(NUMERIC_OVERFLOW);
if (value != null) {
// TODO: can we get a real boolean parser in here?
compilerSettings.setNumericOverflow(Boolean.parseBoolean(value));
}
if (!clone.isEmpty()) {
throw new IllegalArgumentException("Unrecognized compile-time parameter(s): " + clone);
}
}
// check we ourselves are not being called by unprivileged code
SecurityManager sm = System.getSecurityManager();
if (sm != null) {

View File

@ -19,17 +19,16 @@
package org.elasticsearch.plan.a;
import org.elasticsearch.common.settings.Settings;
import java.util.Collections;
import java.util.Map;
/** Tests floating point overflow with numeric overflow disabled */
public class FloatOverflowDisabledTests extends ScriptTestCase {
/** wire overflow to false for all tests */
@Override
protected Settings getSettings() {
Settings.Builder builder = Settings.builder();
builder.put(super.getSettings());
builder.put(PlanAScriptEngineService.NUMERIC_OVERFLOW, false);
return builder.build();
public Object exec(String script, Map<String, Object> vars) {
return exec(script, vars, Collections.singletonMap(PlanAScriptEngineService.NUMERIC_OVERFLOW, "false"));
}
public void testAssignmentAdditionOverflow() {

View File

@ -19,17 +19,16 @@
package org.elasticsearch.plan.a;
import org.elasticsearch.common.settings.Settings;
import java.util.Collections;
import java.util.Map;
/** Tests floating point overflow with numeric overflow enabled */
public class FloatOverflowEnabledTests extends ScriptTestCase {
/** wire overflow to true for all tests */
@Override
protected Settings getSettings() {
Settings.Builder builder = Settings.builder();
builder.put(super.getSettings());
builder.put(PlanAScriptEngineService.NUMERIC_OVERFLOW, true);
return builder.build();
public Object exec(String script, Map<String, Object> vars) {
return exec(script, vars, Collections.singletonMap(PlanAScriptEngineService.NUMERIC_OVERFLOW, "true"));
}
public void testAssignmentAdditionOverflow() {

View File

@ -19,17 +19,16 @@
package org.elasticsearch.plan.a;
import org.elasticsearch.common.settings.Settings;
import java.util.Collections;
import java.util.Map;
/** Tests integer overflow with numeric overflow disabled */
public class IntegerOverflowDisabledTests extends ScriptTestCase {
/** wire overflow to true for all tests */
@Override
protected Settings getSettings() {
Settings.Builder builder = Settings.builder();
builder.put(super.getSettings());
builder.put(PlanAScriptEngineService.NUMERIC_OVERFLOW, false);
return builder.build();
public Object exec(String script, Map<String, Object> vars) {
return exec(script, vars, Collections.singletonMap(PlanAScriptEngineService.NUMERIC_OVERFLOW, "false"));
}
public void testAssignmentAdditionOverflow() {

View File

@ -19,17 +19,16 @@
package org.elasticsearch.plan.a;
import org.elasticsearch.common.settings.Settings;
import java.util.Collections;
import java.util.Map;
/** Tests integer overflow with numeric overflow enabled */
public class IntegerOverflowEnabledTests extends ScriptTestCase {
/** wire overflow to true for all tests */
@Override
protected Settings getSettings() {
Settings.Builder builder = Settings.builder();
builder.put(super.getSettings());
builder.put(PlanAScriptEngineService.NUMERIC_OVERFLOW, true);
return builder.build();
public Object exec(String script, Map<String, Object> vars) {
return exec(script, vars, Collections.singletonMap(PlanAScriptEngineService.NUMERIC_OVERFLOW, "true"));
}
public void testAssignmentAdditionOverflow() {

View File

@ -24,6 +24,7 @@ import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -78,7 +79,7 @@ public class ScriptEngineTests extends ScriptTestCase {
Map<String, Object> ctx = new HashMap<>();
vars.put("ctx", ctx);
Object compiledScript = scriptEngine.compile("return ((Map<String, Object>)input.get(\"ctx\")).get(\"value\");");
Object compiledScript = scriptEngine.compile("return ((Map<String, Object>)input.get(\"ctx\")).get(\"value\");", Collections.emptyMap());
ExecutableScript script = scriptEngine.executable(new CompiledScript(ScriptService.ScriptType.INLINE,
"testChangingVarsCrossExecution1", "plan-a", compiledScript), vars);
@ -93,7 +94,7 @@ public class ScriptEngineTests extends ScriptTestCase {
public void testChangingVarsCrossExecution2() {
Map<String, Object> vars = new HashMap<>();
Object compiledScript = scriptEngine.compile("return input.get(\"value\");");
Object compiledScript = scriptEngine.compile("return input.get(\"value\");", Collections.emptyMap());
ExecutableScript script = scriptEngine.executable(new CompiledScript(ScriptService.ScriptType.INLINE,
"testChangingVarsCrossExecution2", "plan-a", compiledScript), vars);

View File

@ -25,6 +25,7 @@ import org.elasticsearch.script.ScriptService;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
import java.util.Collections;
import java.util.Map;
/**
@ -34,17 +35,10 @@ import java.util.Map;
*/
public abstract class ScriptTestCase extends ESTestCase {
protected PlanAScriptEngineService scriptEngine;
/** Override to provide different compiler settings */
protected Settings getSettings() {
Settings.Builder builder = Settings.builder();
builder.put(PlanAScriptEngineService.NUMERIC_OVERFLOW, random().nextBoolean());
return builder.build();
}
@Before
public void setup() {
scriptEngine = new PlanAScriptEngineService(getSettings());
scriptEngine = new PlanAScriptEngineService(Settings.EMPTY);
}
/** Compiles and returns the result of {@code script} */
@ -54,7 +48,12 @@ public abstract class ScriptTestCase extends ESTestCase {
/** Compiles and returns the result of {@code script} with access to {@code vars} */
public Object exec(String script, Map<String, Object> vars) {
Object object = scriptEngine.compile(script);
return exec(script, vars, Collections.singletonMap(PlanAScriptEngineService.NUMERIC_OVERFLOW, Boolean.toString(random().nextBoolean())));
}
/** Compiles and returns the result of {@code script} with access to {@code vars} and compile-time parameters */
public Object exec(String script, Map<String, Object> vars, Map<String,String> compileParams) {
Object object = scriptEngine.compile(script, compileParams);
CompiledScript compiled = new CompiledScript(ScriptService.ScriptType.INLINE, getTestName(), "plan-a", object);
return scriptEngine.executable(compiled, vars).run();
}

View File

@ -19,6 +19,8 @@
package org.elasticsearch.plan.a;
import java.util.Collections;
public class WhenThingsGoWrongTests extends ScriptTestCase {
public void testNullPointer() {
try {
@ -38,4 +40,13 @@ public class WhenThingsGoWrongTests extends ScriptTestCase {
fail("should have hit cce");
} catch (ClassCastException expected) {}
}
public void testBogusParameter() {
try {
exec("return 5;", null, Collections.singletonMap("bogusParameterKey", "bogusParameterValue"));
fail("should have hit IAE");
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().contains("Unrecognized compile-time parameter"));
}
}
}

View File

@ -60,7 +60,7 @@ import org.python.util.PythonInterpreter;
public class PythonScriptEngineService extends AbstractComponent implements ScriptEngineService {
private final PythonInterpreter interp;
@Inject
public PythonScriptEngineService(Settings settings) {
super(settings);
@ -110,7 +110,7 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
// classloader created here
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
@ -293,7 +293,7 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
if (value == null) {
return null;
} else if (value instanceof PyObject) {
// seems like this is enough, inner PyDictionary will do the conversion for us for example, so expose it directly
// seems like this is enough, inner PyDictionary will do the conversion for us for example, so expose it directly
return ((PyObject) value).__tojava__(Object.class);
}
return value;

View File

@ -29,6 +29,7 @@ import org.junit.After;
import org.junit.Before;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -53,7 +54,7 @@ public class PythonScriptEngineTests extends ESTestCase {
public void testSimpleEquation() {
Map<String, Object> vars = new HashMap<String, Object>();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testSimpleEquation", "python", se.compile("1 + 2")), vars).run();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testSimpleEquation", "python", se.compile("1 + 2", Collections.emptyMap())), vars).run();
assertThat(((Number) o).intValue(), equalTo(3));
}
@ -63,13 +64,13 @@ public class PythonScriptEngineTests extends ESTestCase {
Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
Map<String, Object> obj1 = MapBuilder.<String, Object>newMapBuilder().put("prop1", "value1").put("obj2", obj2).put("l", Arrays.asList("2", "1")).map();
vars.put("obj1", obj1);
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "python", se.compile("obj1")), vars).run();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "python", se.compile("obj1", Collections.emptyMap())), vars).run();
assertThat(o, instanceOf(Map.class));
obj1 = (Map<String, Object>) o;
assertThat((String) obj1.get("prop1"), equalTo("value1"));
assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "python", se.compile("obj1['l'][0]")), vars).run();
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testMapAccess", "python", se.compile("obj1['l'][0]", Collections.emptyMap())), vars).run();
assertThat(((String) o), equalTo("2"));
}
@ -82,7 +83,7 @@ public class PythonScriptEngineTests extends ESTestCase {
vars.put("ctx", ctx);
ExecutableScript executable = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testObjectInterMap", "python",
se.compile("ctx['obj2'] = { 'prop2' : 'value2' }; ctx['obj1']['prop1'] = 'uvalue1'")), vars);
se.compile("ctx['obj2'] = { 'prop2' : 'value2' }; ctx['obj1']['prop1'] = 'uvalue1'", Collections.emptyMap())), vars);
executable.run();
ctx = (Map<String, Object>) executable.unwrap(vars.get("ctx"));
assertThat(ctx.containsKey("obj1"), equalTo(true));
@ -100,15 +101,15 @@ public class PythonScriptEngineTests extends ESTestCase {
// Object o = se.execute(se.compile("l.length"), vars);
// assertThat(((Number) o).intValue(), equalTo(4));
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessListInScript", "python", se.compile("l[0]")), vars).run();
Object o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessListInScript", "python", se.compile("l[0]", Collections.emptyMap())), vars).run();
assertThat(((String) o), equalTo("1"));
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessListInScript", "python", se.compile("l[3]")), vars).run();
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessListInScript", "python", se.compile("l[3]", Collections.emptyMap())), vars).run();
obj1 = (Map<String, Object>) o;
assertThat((String) obj1.get("prop1"), equalTo("value1"));
assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessListInScript", "python", se.compile("l[3]['prop1']")), vars).run();
o = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testAccessListInScript", "python", se.compile("l[3]['prop1']", Collections.emptyMap())), vars).run();
assertThat(((String) o), equalTo("value1"));
}
@ -116,7 +117,7 @@ public class PythonScriptEngineTests extends ESTestCase {
Map<String, Object> vars = new HashMap<String, Object>();
Map<String, Object> ctx = new HashMap<String, Object>();
vars.put("ctx", ctx);
Object compiledScript = se.compile("ctx['value']");
Object compiledScript = se.compile("ctx['value']", Collections.emptyMap());
ExecutableScript script = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testChangingVarsCrossExecution1", "python", compiledScript), vars);
ctx.put("value", 1);
@ -131,7 +132,7 @@ public class PythonScriptEngineTests extends ESTestCase {
public void testChangingVarsCrossExecution2() {
Map<String, Object> vars = new HashMap<String, Object>();
Map<String, Object> ctx = new HashMap<String, Object>();
Object compiledScript = se.compile("value");
Object compiledScript = se.compile("value", Collections.emptyMap());
ExecutableScript script = se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "testChangingVarsCrossExecution2", "python", compiledScript), vars);
script.setNextVar("value", 1);

View File

@ -25,6 +25,7 @@ import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.test.ESTestCase;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@ -41,7 +42,7 @@ public class PythonScriptMultiThreadedTests extends ESTestCase {
public void testExecutableNoRuntimeParams() throws Exception {
final PythonScriptEngineService se = new PythonScriptEngineService(Settings.Builder.EMPTY_SETTINGS);
final Object compiled = se.compile("x + y");
final Object compiled = se.compile("x + y", Collections.emptyMap());
final CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "testExecutableNoRuntimeParams", "python", compiled);
final AtomicBoolean failed = new AtomicBoolean();
@ -127,7 +128,7 @@ public class PythonScriptMultiThreadedTests extends ESTestCase {
public void testExecute() throws Exception {
final PythonScriptEngineService se = new PythonScriptEngineService(Settings.Builder.EMPTY_SETTINGS);
final Object compiled = se.compile("x + y");
final Object compiled = se.compile("x + y", Collections.emptyMap());
final CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "testExecute", "python", compiled);
final AtomicBoolean failed = new AtomicBoolean();

View File

@ -26,6 +26,7 @@ import org.elasticsearch.test.ESTestCase;
import org.python.core.PyException;
import java.text.DecimalFormatSymbols;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@ -34,7 +35,7 @@ import java.util.Map;
* Tests for Python security permissions
*/
public class PythonSecurityTests extends ESTestCase {
private PythonScriptEngineService se;
@Override
@ -54,14 +55,14 @@ public class PythonSecurityTests extends ESTestCase {
/** runs a script */
private void doTest(String script) {
Map<String, Object> vars = new HashMap<String, Object>();
se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "test", "python", se.compile(script)), vars).run();
se.executable(new CompiledScript(ScriptService.ScriptType.INLINE, "test", "python", se.compile(script, Collections.emptyMap())), vars).run();
}
/** asserts that a script runs without exception */
private void assertSuccess(String script) {
doTest(script);
}
/** assert that a security exception is hit */
private void assertFailure(String script) {
try {
@ -76,13 +77,13 @@ public class PythonSecurityTests extends ESTestCase {
}
}
}
/** Test some py scripts that are ok */
public void testOK() {
assertSuccess("1 + 2");
assertSuccess("from java.lang import Math\nMath.cos(0)");
}
/** Test some py scripts that should hit security exception */
public void testNotOK() {
// sanity check :)
@ -93,7 +94,7 @@ public class PythonSecurityTests extends ESTestCase {
// no files
assertFailure("from java.io import File\nFile.createTempFile(\"test\", \"tmp\")");
}
/** Test again from a new thread, python has complex threadlocal configuration */
public void testNotOKFromSeparateThread() throws Exception {
Thread t = new Thread() {

View File

@ -25,6 +25,7 @@ import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -35,7 +36,7 @@ public class SimpleBench {
public static void main(String[] args) {
PythonScriptEngineService se = new PythonScriptEngineService(Settings.Builder.EMPTY_SETTINGS);
Object compiled = se.compile("x + y");
Object compiled = se.compile("x + y", Collections.emptyMap());
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "SimpleBench", "python", compiled);

View File

@ -72,7 +72,7 @@ public class MockScriptEngine implements ScriptEngineService {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}