diff --git a/watcher/src/main/java/org/elasticsearch/watcher/shield/ShieldIntegration.java b/watcher/src/main/java/org/elasticsearch/watcher/shield/ShieldIntegration.java index e0d084b9f87..eec5b59046f 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/shield/ShieldIntegration.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/shield/ShieldIntegration.java @@ -6,6 +6,7 @@ package org.elasticsearch.watcher.shield; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.common.HasContext; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.settings.Settings; @@ -51,6 +52,13 @@ public class ShieldIntegration { } } + // TODO this is a hack that needs to go away with proper fixes in core + public void putUserInContext(HasContext context) { + if (userHolder != null) { + context.putInContext("_shield_user", ((WatcherUserHolder) userHolder).user); + } + } + static boolean installed() { try { ShieldIntegration.class.getClassLoader().loadClass("org.elasticsearch.shield.ShieldPlugin"); diff --git a/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java b/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java index edafbfbb6b7..e74b40a41e1 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/support/init/proxy/ScriptServiceProxy.java @@ -5,8 +5,10 @@ */ package org.elasticsearch.watcher.support.init.proxy; +import org.elasticsearch.common.ContextAndHeaderHolder; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.script.*; +import org.elasticsearch.watcher.shield.ShieldIntegration; import org.elasticsearch.watcher.support.Script; import org.elasticsearch.watcher.support.init.InitializingService; @@ -19,6 +21,7 @@ import java.util.Map; public class ScriptServiceProxy implements InitializingService.Initializable { private ScriptService service; + private ContextAndHeaderHolder contextAndHeaderHolder = new ContextAndHeaderHolder(); /** * Creates a proxy to the given script service (can be used for testing) @@ -32,6 +35,10 @@ public class ScriptServiceProxy implements InitializingService.Initializable { @Override public void init(Injector injector) { this.service = injector.getInstance(ScriptService.class); + ShieldIntegration shieldIntegration = injector.getInstance(ShieldIntegration.class); + if (shieldIntegration != null) { + shieldIntegration.putUserInContext(contextAndHeaderHolder); + } } public CompiledScript compile(Script script) { @@ -39,7 +46,7 @@ public class ScriptServiceProxy implements InitializingService.Initializable { } public CompiledScript compile(org.elasticsearch.script.Script script) { - return service.compile(script, WatcherScriptContext.CTX); + return service.compile(script, WatcherScriptContext.CTX, contextAndHeaderHolder); } public ExecutableScript executable(CompiledScript compiledScript, Map vars) { @@ -48,7 +55,7 @@ public class ScriptServiceProxy implements InitializingService.Initializable { public ExecutableScript executable(org.elasticsearch.script.Script script) { - return service.executable(script, WatcherScriptContext.CTX); + return service.executable(script, WatcherScriptContext.CTX, contextAndHeaderHolder); } public static final ScriptContext.Plugin INSTANCE = new ScriptContext.Plugin("elasticsearch-watcher", "watch");