-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Create NamedRoute to map extension routes to a shortened name #6870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
owaiskazi19
merged 59 commits into
opensearch-project:main
from
cwperks:move-ext-rest-request
May 18, 2023
Merged
Changes from 57 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
a688fd0
WIP on rest layer authz
cwperks 88955ff
Create PermissibleRoute
cwperks a7655e5
Update extension handshake
cwperks de8b8b8
Add connectToNodeAsExtension in TransportService
cwperks ad68321
Merge branch 'extension-handshake' into move-ext-rest-request
cwperks 0003dcc
Add to CHANGELOG
cwperks 4d87934
Merge branch 'main' into extension-handshake
cwperks 6c41e96
Merge branch 'extension-handshake' into move-ext-rest-request
cwperks f1d9c71
Add to CHANGELOG
cwperks 64e8667
Update RouteHandler
cwperks 98b1af0
Update java docstrings
cwperks a7f83b5
Merge branch 'extension-handshake' into move-ext-rest-request
cwperks e1c754b
Merge branch 'main' into extension-handshake
cwperks 4268341
Merge branch 'extension-handshake' into move-ext-rest-request
cwperks 1ef93fc
Merge branch 'main' into extension-handshake
cwperks e041cf6
Merge branch 'extension-handshake' into move-ext-rest-request
cwperks 3b4b91b
Run spotlessApply
cwperks d90d065
Fix merge conflicts
cwperks 38ca9da
Merge branch 'main' into extension-handshake
cwperks 8913f9f
Merge branch 'extension-handshake' into move-ext-rest-request
cwperks c202da9
Merge branch 'main' into move-ext-rest-request
cwperks dace95e
Rename to ProtectedRoute
cwperks bff0ea2
Merge branch 'main' into move-ext-rest-request
cwperks 13a00a0
Merge branch 'main' into move-ext-rest-request
cwperks 29284a1
Merge branch 'main' into move-ext-rest-request
cwperks 634e0b8
Merge branch 'main' into move-ext-rest-request
cwperks b2c60d9
Create method to get extension settings from extensions.yml
cwperks 7d38fee
Add ExtensionsManager.lookupExtensionSettings
cwperks f188c34
Small change to name
cwperks 140440d
Add to CHANGELOG
cwperks 48264c2
Move extensionSettingsMap.put
cwperks 00f3ece
Re-run CI
cwperks 5fc95e4
Merge branch 'lookup-extension-settings' into move-ext-rest-request
cwperks 955a1de
Merge branch 'main' into move-ext-rest-request
cwperks b305e9a
Address review feedback
cwperks 9f53b37
Add test for ProtectedRoute
cwperks 91b2753
spotlessApply
cwperks 097e8d2
Merge branch 'main' into move-ext-rest-request
cwperks 8155630
Merge branch 'main' into move-ext-rest-request
cwperks 5e1239a
Add RouteHandlerTests
cwperks 271b6f6
Switch to NamedRoute and add validation for action naming
cwperks 8e087b3
Merge branch 'main' into move-ext-rest-request
cwperks 9e453ec
Avoid magic numbers
cwperks 1fbde26
Remove @Test annotation
cwperks c3f3a0e
Address code review feedback
cwperks 6cb42fe
Update error message
cwperks eceda9c
Check for REST Action name uniqueness across all registered actions
cwperks 6095f8d
minimize code in the test
cwperks 52c3f1e
Merge branch 'check-rest-action-name-in-registry' into move-ext-rest-…
cwperks 300b6a9
Update changelog
cwperks 2f35272
Add DynamicRouteRegistry
cwperks cb1244f
Address code review feedback
cwperks 5a9c363
Add mock DynamicRouteRegistry.class
cwperks 9ca3c90
Add RouteRegistry to DynamicActionModule
cwperks c7d7c15
Pass around dynamicActionRegistry instead of ActionModule
cwperks fd53943
Only pass dynamic action registry
cwperks ccbe4b5
Add DynamicActionRegistryTests for tests of dynamic registry
cwperks 0ad12ee
Move CHANGELOG entry
cwperks 98a9d44
Merge branch 'main' into move-ext-rest-request
cwperks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
|
owaiskazi19 marked this conversation as resolved.
|
||
| 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; | ||
| } | ||
| } | ||
|
cwperks marked this conversation as resolved.
|
||
This file contains hidden or 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; | ||
| } | ||
|
owaiskazi19 marked this conversation as resolved.
|
||
| return routeName.matches(VALID_ACTION_NAME_PATTERN); | ||
| } | ||
|
owaiskazi19 marked this conversation as resolved.
|
||
|
|
||
| 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.