forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 3
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 <cwperx@amazon.com> * Create PermissibleRoute Signed-off-by: Craig Perkins <cwperx@amazon.com> * Update extension handshake Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add connectToNodeAsExtension in TransportService Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add to CHANGELOG Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add to CHANGELOG Signed-off-by: Craig Perkins <cwperx@amazon.com> * Update RouteHandler Signed-off-by: Craig Perkins <cwperx@amazon.com> * Update java docstrings Signed-off-by: Craig Perkins <cwperx@amazon.com> * Run spotlessApply Signed-off-by: Craig Perkins <cwperx@amazon.com> * Fix merge conflicts Signed-off-by: Craig Perkins <cwperx@amazon.com> * Rename to ProtectedRoute Signed-off-by: Craig Perkins <cwperx@amazon.com> * Create method to get extension settings from extensions.yml Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add ExtensionsManager.lookupExtensionSettings Signed-off-by: Craig Perkins <cwperx@amazon.com> * Small change to name Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add to CHANGELOG Signed-off-by: Craig Perkins <cwperx@amazon.com> * Move extensionSettingsMap.put Signed-off-by: Craig Perkins <cwperx@amazon.com> * Re-run CI Signed-off-by: Craig Perkins <cwperx@amazon.com> * Address review feedback Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add test for ProtectedRoute Signed-off-by: Craig Perkins <cwperx@amazon.com> * spotlessApply Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add RouteHandlerTests Signed-off-by: Craig Perkins <cwperx@amazon.com> * Switch to NamedRoute and add validation for action naming Signed-off-by: Craig Perkins <cwperx@amazon.com> * Avoid magic numbers Signed-off-by: Craig Perkins <cwperx@amazon.com> * Remove @test annotation Signed-off-by: Craig Perkins <cwperx@amazon.com> * Address code review feedback Signed-off-by: Craig Perkins <cwperx@amazon.com> * Update error message Signed-off-by: Craig Perkins <cwperx@amazon.com> * Check for REST Action name uniqueness across all registered actions Signed-off-by: Craig Perkins <cwperx@amazon.com> * minimize code in the test Signed-off-by: Craig Perkins <cwperx@amazon.com> * Update changelog Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add DynamicRouteRegistry Signed-off-by: Craig Perkins <cwperx@amazon.com> * Address code review feedback Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add mock DynamicRouteRegistry.class Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add RouteRegistry to DynamicActionModule Signed-off-by: Craig Perkins <cwperx@amazon.com> * Pass around dynamicActionRegistry instead of ActionModule Signed-off-by: Craig Perkins <cwperx@amazon.com> * Only pass dynamic action registry Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add DynamicActionRegistryTests for tests of dynamic registry Signed-off-by: Craig Perkins <cwperx@amazon.com> * Move CHANGELOG entry Signed-off-by: Craig Perkins <cwperx@amazon.com> --------- Signed-off-by: Craig Perkins <cwperx@amazon.com>
- Loading branch information
1 parent
4f7af91
commit 52b8ebf
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.