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.
Signed-off-by: Peter Nied <[email protected]>
- Loading branch information
Showing
6 changed files
with
203 additions
and
36 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
server/src/main/java/org/opensearch/action/ResourceRequest.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,41 @@ | ||
package org.opensearch.action; | ||
|
||
import static org.opensearch.action.ValidateActions.addValidationError; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
import org.opensearch.common.ValidationException; | ||
import org.opensearch.common.annotation.ExperimentalApi; | ||
|
||
/** | ||
* TODO: This validation should be associated with REST requests to ensure the parameter is from the URL | ||
* | ||
* Context: If all of the resource information is in the URL, caching which can include responses or authorization are trivial | ||
*/ | ||
@ExperimentalApi | ||
public interface ResourceRequest { | ||
|
||
static final Pattern ALLOWED_KEY_PATTERN = Pattern.compile("[a-zA-Z_-]+"); | ||
static final Pattern ALLOWED_VALUE_PATTERN = Pattern.compile("[a-zA-Z_-]+"); | ||
|
||
/** | ||
* Extracts resource key value pairs from the request parameters | ||
* Note; these resource must be part of the | ||
*/ | ||
Map<String, String> getResourceIds(); | ||
|
||
public static ActionRequestValidationException validResourceIds(final ResourceRequest resourceRequest, final ActionRequestValidationException validationException) { | ||
resourceRequest.getResourceIds().entrySet().forEach(entry -> { | ||
if (!ALLOWED_KEY_PATTERN.matcher(entry.getKey()).matches()) { | ||
addValidationError("Unsupported resource key is not supported; key: " + entry.getKey() + " value: " + entry.getValue() + " allowed pattern " + ALLOWED_VALUE_PATTERN.pattern(), validationException); | ||
} | ||
|
||
if (!ALLOWED_VALUE_PATTERN.matcher(entry.getKey()).matches()) { | ||
addValidationError("Unsupported resource value is not supported; key: " + entry.getKey() + " value: " + entry.getValue() + " allowed pattern " + ALLOWED_VALUE_PATTERN.pattern(), validationException); | ||
} | ||
}); | ||
|
||
return validationException; | ||
} | ||
} |
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
113 changes: 113 additions & 0 deletions
113
server/src/main/java/org/opensearch/action/search/ViewSearchRequest.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,113 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.action.search; | ||
|
||
import static org.opensearch.action.ValidateActions.addValidationError; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.regex.Pattern; | ||
|
||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.action.ResourceRequest; | ||
import org.opensearch.cluster.metadata.View; | ||
import org.opensearch.common.ValidationException; | ||
import org.opensearch.common.annotation.ExperimentalApi; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.rest.action.admin.indices.RestViewAction; | ||
|
||
/** Wraps the functionality of search requests and tailors for what is available when searching through views | ||
*/ | ||
@ExperimentalApi | ||
public class ViewSearchRequest extends SearchRequest implements ResourceRequest { | ||
|
||
public final View view; | ||
|
||
public ViewSearchRequest(final View view) { | ||
super(); | ||
this.view = view; | ||
} | ||
|
||
public ViewSearchRequest(final StreamInput in) throws IOException { | ||
super(in); | ||
view = new View(in); | ||
} | ||
|
||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
final Function<String, String> unsupported = (String x) -> x + " is not supported when searching views"; | ||
ActionRequestValidationException validationException = super.validate(); | ||
|
||
if (scroll() != null) { | ||
validationException = addValidationError(unsupported.apply("Scroll"), validationException); | ||
} | ||
|
||
// TODO: Filter out anything additional search features that are not supported | ||
|
||
validationException = ResourceRequest.validResourceIds(this, validationException); | ||
|
||
return validationException; | ||
} | ||
|
||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
view.writeTo(out); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
// TODO: Maybe this isn't standard practice | ||
return this.hashCode() == o.hashCode(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(view, super.hashCode()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString().replace("SearchRequest{", "ViewSearchRequest{view=" + view + ","); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getResourceIds() { | ||
return Map.of(RestViewAction.VIEW_ID, view.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
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