diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilter.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilter.java index 5bbd0b487e35b..390744abf0125 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilter.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilter.java @@ -22,14 +22,25 @@ public abstract class ApiFilteringActionFilter imple private final ThreadContext threadContext; private final String actionName; private final Class responseClass; + private final boolean filterOperatorRequests; protected ApiFilteringActionFilter(ThreadContext threadContext, String actionName, Class responseClass) { + this(threadContext, actionName, responseClass, false); + } + + protected ApiFilteringActionFilter( + ThreadContext threadContext, + String actionName, + Class responseClass, + boolean filterOperatorRequests + ) { assert threadContext != null : "threadContext cannot be null"; assert actionName != null : "actionName cannot be null"; assert responseClass != null : "responseClass cannot be null"; this.threadContext = threadContext; this.actionName = actionName; this.responseClass = responseClass; + this.filterOperatorRequests = filterOperatorRequests; } @Override @@ -46,7 +57,7 @@ public void app ActionFilterChain chain ) { final ActionListener responseFilteringListener; - if (isOperator(threadContext) == false && actionName.equals(action)) { + if ((filterOperatorRequests || isOperator(threadContext) == false) && actionName.equals(action)) { responseFilteringListener = listener.map(this::filter); } else { responseFilteringListener = listener; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilterTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilterTests.java index c5365c19632b1..51d3efe652a0c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilterTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilterTests.java @@ -30,7 +30,7 @@ public void testApply() { boolean isOperator = randomBoolean(); final ThreadContext threadContext = getTestThreadContext(isOperator); String action = "test.action"; - ApiFilteringActionFilter filter = new TestFilter(threadContext); + ApiFilteringActionFilter filter = new TestFilter(threadContext, false); Task task = null; TestRequest request = new TestRequest(); AtomicBoolean listenerCalled = new AtomicBoolean(false); @@ -59,6 +59,37 @@ public void onFailure(Exception e) { assertThat(responseModified.get(), equalTo(isOperator == false)); } + public void testApplyAsOperator() { + final ThreadContext threadContext = getTestThreadContext(true); + ApiFilteringActionFilter filter = new TestFilter(threadContext, true); + Task task = null; + TestRequest request = new TestRequest(); + AtomicBoolean listenerCalled = new AtomicBoolean(false); + AtomicBoolean responseModified = new AtomicBoolean(false); + ActionListener listener = new ActionListener<>() { + @Override + public void onResponse(TestResponse testResponse) { + listenerCalled.set(true); + responseModified.set(testResponse.modified); + } + + @Override + public void onFailure(Exception e) { + fail(Strings.format("Unexpected exception: %s", e.getMessage())); + } + }; + ActionFilterChain chain = (task1, action1, request1, listener1) -> { + listener1.onResponse(new TestResponse()); + }; + filter.apply(task, "wrong.action", request, listener, chain); + assertThat(listenerCalled.get(), equalTo(true)); + assertThat(responseModified.get(), equalTo(false)); + filter.apply(task, "test.action", request, listener, chain); + assertThat(listenerCalled.get(), equalTo(true)); + // The response should always be modified + assertThat(responseModified.get(), equalTo(true)); + } + public void testApplyWithException() { /* * This test makes sure that we have correct behavior if the filter function throws an exception. In that case we expect @@ -94,8 +125,8 @@ public void onFailure(Exception e) { private static class TestFilter extends ApiFilteringActionFilter { - TestFilter(ThreadContext threadContext) { - super(threadContext, "test.action", TestResponse.class); + TestFilter(ThreadContext threadContext, boolean filterOperatorRequests) { + super(threadContext, "test.action", TestResponse.class, filterOperatorRequests); } @Override