Add level parameter validation in rest layer.#94136
Add level parameter validation in rest layer.#94136DaveCTurner merged 29 commits intoelastic:mainfrom
Conversation
|
@howardhuanghua please enable the option "Allow edits and access to secrets by maintainers" on your PR. For more information, see the documentation. |
|
Pinging @elastic/es-core-infra (Team:Core/Infra) |
|
Thanks for the PR @howardhuanghua. This could really do with tests being added. All the Action classes have got associated Test classes; could you add tests for valid/invalid parameters to those classes please. |
Hi @thecoop, thanks. I am going to add test case, just to make sure should be in |
| final ClusterHealthRequest clusterHealthRequest = fromRequest(request); | ||
| return channel -> client.admin().cluster().health(clusterHealthRequest, new RestStatusToXContentListener<>(channel)); | ||
| return channel -> { | ||
| ActionListener<ClusterHealthResponse> listener = new RestStatusToXContentListener<>(channel); |
There was a problem hiding this comment.
Rather than checking here, ActionRequest has a validate method that checks if the request is valid, the checking should be done there. eg for RestClusterHealthAction, its in the ClusterHealthRequest.validate() method.
| } | ||
| } | ||
|
|
||
| public static void validateClusterResponseLevel(final String level) { |
There was a problem hiding this comment.
These methods are too specific to go into ActionResponse, ideally in some subclass of ActionRequest common to all the request classes using the level parameter.
There was a problem hiding this comment.
Actually I'm not sure there is a good place for the common code, as some Request classes have their own separate way of representing level. We may just need to keep each implementation separate for now, and not have a common method.
|
The tests should be in the corresponding classes for the request - eg |
|
OK. Thanks, I would update PR later. |
DaveCTurner
left a comment
There was a problem hiding this comment.
I reckon we should have an enum for the level parameter rather than having all these string constants around.
Also I think we should require response params to be validated while reading the request, i.e. that we should drop the filter line here:
Thanks @DaveCTurner , just want to double check that in Above |
Only if we don't validate them in |
|
Update PR based on suggestions. Please help to review again, thank you. |
DaveCTurner
left a comment
There was a problem hiding this comment.
I would prefer to keep the change you proposed to BaseRestHandler, and adjust the other REST handlers accordingly.
Ah ok I didn't see this comment until after my previous one. A follow-up PR would be fine too. Let me take another look. |
Hmm, I think remove the response filter would cause lots of case failed. So I will do it in a follow-up PR. |
DaveCTurner
left a comment
There was a problem hiding this comment.
Ok I like it, LGTM (assuming CI is happy)
server/src/main/java/org/elasticsearch/action/ClusterStatsLevel.java
Outdated
Show resolved
Hide resolved
server/src/main/java/org/elasticsearch/action/NodeStatsLevel.java
Outdated
Show resolved
Hide resolved
DaveCTurner
left a comment
There was a problem hiding this comment.
Ah sorry, LGTM except those two small nits. I'll enable CI anyway.
|
@elasticmachine ok to test |
…l.java Co-authored-by: David Turner <david.turner@elastic.co>
Co-authored-by: David Turner <david.turner@elastic.co>
|
I don't have permission to fix the changelog, but here's the patch I'd like you to apply: diff --git a/docs/changelog/94136.yaml b/docs/changelog/94136.yaml
index 94b55c7f4b4..6b63723b039 100644
--- a/docs/changelog/94136.yaml
+++ b/docs/changelog/94136.yaml
@@ -1,5 +1,5 @@
pr: 94136
-summary: Add level parameter validation in rest layer.
-area: REST API
+summary: Add level parameter validation in REST layer
+area: Infra/REST API
type: bug
issues: [93981] |
|
It seems all the tests passed finally. ^^ |
DaveCTurner
left a comment
There was a problem hiding this comment.
Nice work, I like it. I left a few tiny suggestions.
server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterHealthAction.java
Outdated
Show resolved
Hide resolved
...src/test/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponsesTests.java
Outdated
Show resolved
Hide resolved
...src/test/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponsesTests.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/searchablesnapshots/rest/RestSearchableSnapshotsStatsAction.java
Outdated
Show resolved
Hide resolved
| PARSER.declareString(constructorArg(), new ParseField(STATUS)); | ||
| // Can be absent if LEVEL == 'indices' or 'cluster' | ||
| PARSER.declareNamedObjects(optionalConstructorArg(), SHARD_PARSER, new ParseField(SHARDS)); | ||
| PARSER.declareNamedObjects(optionalConstructorArg(), SHARD_PARSER, new ParseField(ClusterStatsLevel.SHARDS.getLevel())); |
There was a problem hiding this comment.
Hmm it is true that it's the same literal string, but that's something of a coincidence. I don't think we should link the field names in the response to the query parameters in the request like this.
| PARSER.declareString(constructorArg(), new ParseField(STATUS)); | ||
| // Can be absent if LEVEL == 'cluster' | ||
| PARSER.declareNamedObjects(optionalConstructorArg(), INDEX_PARSER, new ParseField(INDICES)); | ||
| PARSER.declareNamedObjects(optionalConstructorArg(), INDEX_PARSER, new ParseField(ClusterStatsLevel.INDICES.getLevel())); |
There was a problem hiding this comment.
Likewise here - it is true that it's the same literal string, but that's something of a coincidence. I don't think we should link the field names in the response to the query parameters in the request like this.
server/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java
Outdated
Show resolved
Hide resolved
Thanks for the patient help. |
|
I have added a independent PR to remove base rest handler parameter filter logic. |
This PR is going to fix #93981.
Validate
?level=up-front in rest layer, also keeps validation in transport layer.