Skip to content

Commit d24b425

Browse files
authored
Allow filtering of operator requests in ApiFilteringActionFilter (#130941)
1 parent e57a0d0 commit d24b425

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ public abstract class ApiFilteringActionFilter<Res extends ActionResponse> imple
2222
private final ThreadContext threadContext;
2323
private final String actionName;
2424
private final Class<Res> responseClass;
25+
private final boolean filterOperatorRequests;
2526

2627
protected ApiFilteringActionFilter(ThreadContext threadContext, String actionName, Class<Res> responseClass) {
28+
this(threadContext, actionName, responseClass, false);
29+
}
30+
31+
protected ApiFilteringActionFilter(
32+
ThreadContext threadContext,
33+
String actionName,
34+
Class<Res> responseClass,
35+
boolean filterOperatorRequests
36+
) {
2737
assert threadContext != null : "threadContext cannot be null";
2838
assert actionName != null : "actionName cannot be null";
2939
assert responseClass != null : "responseClass cannot be null";
3040
this.threadContext = threadContext;
3141
this.actionName = actionName;
3242
this.responseClass = responseClass;
43+
this.filterOperatorRequests = filterOperatorRequests;
3344
}
3445

3546
@Override
@@ -46,7 +57,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
4657
ActionFilterChain<Request, Response> chain
4758
) {
4859
final ActionListener<Response> responseFilteringListener;
49-
if (isOperator(threadContext) == false && actionName.equals(action)) {
60+
if ((filterOperatorRequests || isOperator(threadContext) == false) && actionName.equals(action)) {
5061
responseFilteringListener = listener.map(this::filter);
5162
} else {
5263
responseFilteringListener = listener;

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilterTests.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void testApply() {
3030
boolean isOperator = randomBoolean();
3131
final ThreadContext threadContext = getTestThreadContext(isOperator);
3232
String action = "test.action";
33-
ApiFilteringActionFilter<TestResponse> filter = new TestFilter(threadContext);
33+
ApiFilteringActionFilter<TestResponse> filter = new TestFilter(threadContext, false);
3434
Task task = null;
3535
TestRequest request = new TestRequest();
3636
AtomicBoolean listenerCalled = new AtomicBoolean(false);
@@ -59,6 +59,37 @@ public void onFailure(Exception e) {
5959
assertThat(responseModified.get(), equalTo(isOperator == false));
6060
}
6161

62+
public void testApplyAsOperator() {
63+
final ThreadContext threadContext = getTestThreadContext(true);
64+
ApiFilteringActionFilter<TestResponse> filter = new TestFilter(threadContext, true);
65+
Task task = null;
66+
TestRequest request = new TestRequest();
67+
AtomicBoolean listenerCalled = new AtomicBoolean(false);
68+
AtomicBoolean responseModified = new AtomicBoolean(false);
69+
ActionListener<TestResponse> listener = new ActionListener<>() {
70+
@Override
71+
public void onResponse(TestResponse testResponse) {
72+
listenerCalled.set(true);
73+
responseModified.set(testResponse.modified);
74+
}
75+
76+
@Override
77+
public void onFailure(Exception e) {
78+
fail(Strings.format("Unexpected exception: %s", e.getMessage()));
79+
}
80+
};
81+
ActionFilterChain<TestRequest, TestResponse> chain = (task1, action1, request1, listener1) -> {
82+
listener1.onResponse(new TestResponse());
83+
};
84+
filter.apply(task, "wrong.action", request, listener, chain);
85+
assertThat(listenerCalled.get(), equalTo(true));
86+
assertThat(responseModified.get(), equalTo(false));
87+
filter.apply(task, "test.action", request, listener, chain);
88+
assertThat(listenerCalled.get(), equalTo(true));
89+
// The response should always be modified
90+
assertThat(responseModified.get(), equalTo(true));
91+
}
92+
6293
public void testApplyWithException() {
6394
/*
6495
* 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) {
94125

95126
private static class TestFilter extends ApiFilteringActionFilter<TestResponse> {
96127

97-
TestFilter(ThreadContext threadContext) {
98-
super(threadContext, "test.action", TestResponse.class);
128+
TestFilter(ThreadContext threadContext, boolean filterOperatorRequests) {
129+
super(threadContext, "test.action", TestResponse.class, filterOperatorRequests);
99130
}
100131

101132
@Override

0 commit comments

Comments
 (0)