-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] Use DelegatingRestHandler to delegate to the original …
…handler (#3547) Backport 1166c1f from #3517. Signed-off-by: Craig Perkins <cwperx@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b5622c4
commit 89910d9
Showing
4 changed files
with
199 additions
and
51 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/org/opensearch/security/filter/DelegatingRestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.security.filter; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.rest.RestChannel; | ||
import org.opensearch.rest.RestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Delegating RestHandler that delegates all implementations to original handler | ||
* | ||
* @opensearch.api | ||
*/ | ||
public class DelegatingRestHandler implements RestHandler { | ||
|
||
protected final RestHandler delegate; | ||
|
||
public DelegatingRestHandler(RestHandler delegate) { | ||
Objects.requireNonNull(delegate, "RestHandler delegate can not be null"); | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception { | ||
delegate.handleRequest(request, channel, client); | ||
} | ||
|
||
@Override | ||
public boolean canTripCircuitBreaker() { | ||
return delegate.canTripCircuitBreaker(); | ||
} | ||
|
||
@Override | ||
public boolean supportsContentStream() { | ||
return delegate.supportsContentStream(); | ||
} | ||
|
||
@Override | ||
public boolean allowsUnsafeBuffers() { | ||
return delegate.allowsUnsafeBuffers(); | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return delegate.routes(); | ||
} | ||
|
||
@Override | ||
public List<DeprecatedRoute> deprecatedRoutes() { | ||
return delegate.deprecatedRoutes(); | ||
} | ||
|
||
@Override | ||
public List<ReplacedRoute> replacedRoutes() { | ||
return delegate.replacedRoutes(); | ||
} | ||
|
||
@Override | ||
public boolean allowSystemIndexAccessByDefault() { | ||
return delegate.allowSystemIndexAccessByDefault(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return delegate.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/org/opensearch/security/filter/DelegatingRestHandlerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.security.filter; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.core.common.bytes.BytesArray; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestChannel; | ||
import org.opensearch.rest.RestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
|
||
public class DelegatingRestHandlerTests { | ||
|
||
@Test | ||
public void testDelegatingRestHandlerShouldActAsOriginal() throws Exception { | ||
RestHandler rh = new RestHandler() { | ||
@Override | ||
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception { | ||
new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); | ||
} | ||
}; | ||
RestHandler handlerSpy = spy(rh); | ||
DelegatingRestHandler drh = new DelegatingRestHandler(handlerSpy); | ||
|
||
List<Method> overridableMethods = Arrays.stream(RestHandler.class.getMethods()) | ||
.filter( | ||
m -> !(Modifier.isPrivate(m.getModifiers()) || Modifier.isStatic(m.getModifiers()) || Modifier.isFinal(m.getModifiers())) | ||
) | ||
.collect(Collectors.toList()); | ||
|
||
for (Method method : overridableMethods) { | ||
int argCount = method.getParameterCount(); | ||
Object[] args = new Object[argCount]; | ||
for (int i = 0; i < argCount; i++) { | ||
args[i] = any(); | ||
} | ||
if (args.length > 0) { | ||
method.invoke(drh, args); | ||
} else { | ||
method.invoke(drh); | ||
} | ||
method.invoke(verify(handlerSpy, times(1)), args); | ||
} | ||
verifyNoMoreInteractions(handlerSpy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters