public void testAllOpsDisabled() throws Exception { ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder(); Settings settings = Settings.builder() .put("script.engine." + MockScriptEngine.NAME + ".file.aggs", false) .put("script.engine." + MockScriptEngine.NAME + ".file.search", false) .put("script.engine." + MockScriptEngine.NAME + ".file.mapping", false) .put("script.engine." + MockScriptEngine.NAME + ".file.update", false) .put("script.engine." + MockScriptEngine.NAME + ".file.ingest", false) .build(); ScriptService scriptService = makeScriptService(settings); Script script = new Script("script1", ScriptService.ScriptType.FILE, MockScriptEngine.NAME, null); for (ScriptContext context : ScriptContext.Standard.values()) { try { 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")); } } }
@Test public void testFineGrainedSettingsDontAffectNativeScripts() throws IOException { Settings.Builder builder = Settings.settingsBuilder(); if (randomBoolean()) { ScriptType scriptType = randomFrom(ScriptType.values()); builder.put(ScriptModes.SCRIPT_SETTINGS_PREFIX + scriptType, randomFrom(ScriptMode.values())); } else { String scriptContext = randomFrom(ScriptContext.Standard.values()).getKey(); builder.put( ScriptModes.SCRIPT_SETTINGS_PREFIX + scriptContext, randomFrom(ScriptMode.values())); } Settings settings = builder.put("path.home", createTempDir()).build(); Environment environment = new Environment(settings); ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, null); Map<String, NativeScriptFactory> nativeScriptFactoryMap = new HashMap<>(); nativeScriptFactoryMap.put("my", new MyNativeScriptFactory()); Set<ScriptEngineService> scriptEngineServices = ImmutableSet.<ScriptEngineService>of( new NativeScriptEngineService(settings, nativeScriptFactoryMap)); ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Lists.<ScriptContext.Plugin>newArrayList()); ScriptService scriptService = new ScriptService( settings, environment, scriptEngineServices, resourceWatcherService, scriptContextRegistry); for (ScriptContext scriptContext : scriptContextRegistry.scriptContexts()) { assertThat( scriptService.compile( new Script("my", ScriptType.INLINE, NativeScriptEngineService.NAME, null), scriptContext), notNullValue()); } }