-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Reject bulk requests with invalid actions #5302
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,33 @@ public final class BulkRequestParser { | |
private static final ParseField IF_PRIMARY_TERM = new ParseField("if_primary_term"); | ||
private static final ParseField REQUIRE_ALIAS = new ParseField(DocWriteRequest.REQUIRE_ALIAS); | ||
|
||
private enum Action { | ||
adnapibar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CREATE, | ||
DELETE, | ||
INDEX, | ||
UPDATE; | ||
|
||
private static final Map<String, Action> VALID_ACTIONS = Map.of( | ||
"create", | ||
CREATE, | ||
"delete", | ||
DELETE, | ||
"index", | ||
INDEX, | ||
"update", | ||
UPDATE | ||
); | ||
|
||
static Action of(String name, int line) { | ||
if (name != null && VALID_ACTIONS.containsKey(name)) { | ||
return VALID_ACTIONS.get(name); | ||
} | ||
throw new IllegalArgumentException( | ||
"Unknown action line [" + line + "], expected one of [create, delete, index, update]" + " but found [" + name + "]" | ||
adnapibar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} | ||
|
||
private static int findNextMarker(byte marker, int from, BytesReference data) { | ||
final int res = data.indexOf(marker, from); | ||
if (res != -1) { | ||
|
@@ -176,7 +203,7 @@ public void parse( | |
+ "]" | ||
); | ||
} | ||
String action = parser.currentName(); | ||
Action action = Action.of(parser.currentName(), line); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could maybe refactor this as Feel free to think of a better name than "consume" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, your new error message says that. I was suggesting to make it say that for the case on line 195 where a non-field name is encountered as well.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On line 195, the check is the type of token encountered, so I think we can keep the error message as is. |
||
|
||
String index = defaultIndex; | ||
String id = null; | ||
|
@@ -272,7 +299,7 @@ public void parse( | |
); | ||
} | ||
|
||
if ("delete".equals(action)) { | ||
if (action == Action.DELETE) { | ||
deleteRequestConsumer.accept( | ||
new DeleteRequest(index).id(id) | ||
.routing(routing) | ||
|
@@ -290,7 +317,7 @@ public void parse( | |
|
||
// we use internalAdd so we don't fork here, this allows us not to copy over the big byte array to small chunks | ||
// of index request. | ||
if ("index".equals(action)) { | ||
if (action == Action.INDEX) { | ||
if (opType == null) { | ||
indexRequestConsumer.accept( | ||
new IndexRequest(index).id(id) | ||
|
@@ -317,7 +344,7 @@ public void parse( | |
.setRequireAlias(requireAlias) | ||
); | ||
} | ||
} else if ("create".equals(action)) { | ||
} else if (action == Action.CREATE) { | ||
indexRequestConsumer.accept( | ||
new IndexRequest(index).id(id) | ||
.routing(routing) | ||
|
@@ -330,7 +357,7 @@ public void parse( | |
.source(sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType), xContentType) | ||
.setRequireAlias(requireAlias) | ||
); | ||
} else if ("update".equals(action)) { | ||
} else if (action == Action.UPDATE) { | ||
if (version != Versions.MATCH_ANY || versionType != VersionType.INTERNAL) { | ||
throw new IllegalArgumentException( | ||
"Update requests do not support versioning. " + "Please use `if_seq_no` and `if_primary_term` instead" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be included in the CHANGELOG as per the guidelines, removing it.