forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create NamedRoute to map extension routes to a shortened name (opense…
…arch-project#6870) * WIP on rest layer authz Signed-off-by: Craig Perkins <[email protected]> * Create PermissibleRoute Signed-off-by: Craig Perkins <[email protected]> * Update extension handshake Signed-off-by: Craig Perkins <[email protected]> * Add connectToNodeAsExtension in TransportService Signed-off-by: Craig Perkins <[email protected]> * Add to CHANGELOG Signed-off-by: Craig Perkins <[email protected]> * Add to CHANGELOG Signed-off-by: Craig Perkins <[email protected]> * Update RouteHandler Signed-off-by: Craig Perkins <[email protected]> * Update java docstrings Signed-off-by: Craig Perkins <[email protected]> * Run spotlessApply Signed-off-by: Craig Perkins <[email protected]> * Fix merge conflicts Signed-off-by: Craig Perkins <[email protected]> * Rename to ProtectedRoute Signed-off-by: Craig Perkins <[email protected]> * Create method to get extension settings from extensions.yml Signed-off-by: Craig Perkins <[email protected]> * Add ExtensionsManager.lookupExtensionSettings Signed-off-by: Craig Perkins <[email protected]> * Small change to name Signed-off-by: Craig Perkins <[email protected]> * Add to CHANGELOG Signed-off-by: Craig Perkins <[email protected]> * Move extensionSettingsMap.put Signed-off-by: Craig Perkins <[email protected]> * Re-run CI Signed-off-by: Craig Perkins <[email protected]> * Address review feedback Signed-off-by: Craig Perkins <[email protected]> * Add test for ProtectedRoute Signed-off-by: Craig Perkins <[email protected]> * spotlessApply Signed-off-by: Craig Perkins <[email protected]> * Add RouteHandlerTests Signed-off-by: Craig Perkins <[email protected]> * Switch to NamedRoute and add validation for action naming Signed-off-by: Craig Perkins <[email protected]> * Avoid magic numbers Signed-off-by: Craig Perkins <[email protected]> * Remove @test annotation Signed-off-by: Craig Perkins <[email protected]> * Address code review feedback Signed-off-by: Craig Perkins <[email protected]> * Update error message Signed-off-by: Craig Perkins <[email protected]> * Check for REST Action name uniqueness across all registered actions Signed-off-by: Craig Perkins <[email protected]> * minimize code in the test Signed-off-by: Craig Perkins <[email protected]> * Update changelog Signed-off-by: Craig Perkins <[email protected]> * Add DynamicRouteRegistry Signed-off-by: Craig Perkins <[email protected]> * Address code review feedback Signed-off-by: Craig Perkins <[email protected]> * Add mock DynamicRouteRegistry.class Signed-off-by: Craig Perkins <[email protected]> * Add RouteRegistry to DynamicActionModule Signed-off-by: Craig Perkins <[email protected]> * Pass around dynamicActionRegistry instead of ActionModule Signed-off-by: Craig Perkins <[email protected]> * Only pass dynamic action registry Signed-off-by: Craig Perkins <[email protected]> * Add DynamicActionRegistryTests for tests of dynamic registry Signed-off-by: Craig Perkins <[email protected]> * Move CHANGELOG entry Signed-off-by: Craig Perkins <[email protected]> --------- Signed-off-by: Craig Perkins <[email protected]> Signed-off-by: Bharathwaj G <[email protected]>
- Loading branch information
1 parent
d919e48
commit 8915128
Showing
15 changed files
with
720 additions
and
100 deletions.
There are no files selected for viewing
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
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
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
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
69 changes: 69 additions & 0 deletions
69
server/src/main/java/org/opensearch/extensions/rest/RouteHandler.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,69 @@ | ||
/* | ||
* 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.extensions.rest; | ||
|
||
import java.util.function.Function; | ||
|
||
import org.opensearch.rest.RestHandler.Route; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.RestRequest.Method; | ||
|
||
/** | ||
* A subclass of {@link Route} that includes a handler method for that route. | ||
*/ | ||
public class RouteHandler extends Route { | ||
|
||
private final String name; | ||
|
||
private final Function<RestRequest, ExtensionRestResponse> responseHandler; | ||
|
||
/** | ||
* Handle the method and path with the specified handler. | ||
* | ||
* @param method The {@link Method} to handle. | ||
* @param path The path to handle. | ||
* @param handler The method which handles the method and path. | ||
*/ | ||
public RouteHandler(Method method, String path, Function<RestRequest, ExtensionRestResponse> handler) { | ||
super(method, path); | ||
this.responseHandler = handler; | ||
this.name = null; | ||
} | ||
|
||
/** | ||
* Handle the method and path with the specified handler. | ||
* | ||
* @param name The name of the handler. | ||
* @param method The {@link Method} to handle. | ||
* @param path The path to handle. | ||
* @param handler The method which handles the method and path. | ||
*/ | ||
public RouteHandler(String name, Method method, String path, Function<RestRequest, ExtensionRestResponse> handler) { | ||
super(method, path); | ||
this.responseHandler = handler; | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* Executes the handler for this route. | ||
* | ||
* @param request The request to handle | ||
* @return the {@link ExtensionRestResponse} result from the handler for this route. | ||
*/ | ||
public ExtensionRestResponse handleRequest(RestRequest request) { | ||
return responseHandler.apply(request); | ||
} | ||
|
||
/** | ||
* The name of the RouteHandler. Must be unique across route handlers. | ||
*/ | ||
public String name() { | ||
return this.name; | ||
} | ||
} |
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,55 @@ | ||
/* | ||
* 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.rest; | ||
|
||
import org.opensearch.OpenSearchException; | ||
|
||
/** | ||
* A named Route | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class NamedRoute extends RestHandler.Route { | ||
private static final String VALID_ACTION_NAME_PATTERN = "^[a-zA-Z0-9:/*_]*$"; | ||
static final int MAX_LENGTH_OF_ACTION_NAME = 250; | ||
|
||
private final String name; | ||
|
||
public boolean isValidRouteName(String routeName) { | ||
if (routeName == null || routeName.isBlank() || routeName.length() > MAX_LENGTH_OF_ACTION_NAME) { | ||
return false; | ||
} | ||
return routeName.matches(VALID_ACTION_NAME_PATTERN); | ||
} | ||
|
||
public NamedRoute(RestRequest.Method method, String path, String name) { | ||
super(method, path); | ||
if (!isValidRouteName(name)) { | ||
throw new OpenSearchException( | ||
"Invalid route name specified. The route name may include the following characters" | ||
+ " 'a-z', 'A-Z', '0-9', ':', '/', '*', '_' and be less than " | ||
+ MAX_LENGTH_OF_ACTION_NAME | ||
+ " characters" | ||
); | ||
} | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* The name of the Route. Must be unique across Route. | ||
*/ | ||
public String name() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "NamedRoute [method=" + method + ", path=" + path + ", name=" + name + "]"; | ||
} | ||
} |
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
Oops, something went wrong.