mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-18 19:05:06 +00:00
Make MetaData parsing less lenient.
Today this simply ignores everything that is not recognized.
This commit is contained in:
parent
132df10342
commit
6ce7a972bc
@ -859,10 +859,16 @@ public class IndexMetaData implements Diffable<IndexMetaData>, FromXContentBuild
|
|||||||
if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token
|
if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
}
|
}
|
||||||
|
if (parser.currentToken() != XContentParser.Token.FIELD_NAME) {
|
||||||
|
throw new IllegalArgumentException("expected field name but got a " + parser.currentToken());
|
||||||
|
}
|
||||||
Builder builder = new Builder(parser.currentName());
|
Builder builder = new Builder(parser.currentName());
|
||||||
|
|
||||||
String currentFieldName = null;
|
String currentFieldName = null;
|
||||||
XContentParser.Token token = parser.nextToken();
|
XContentParser.Token token = parser.nextToken();
|
||||||
|
if (token != XContentParser.Token.START_OBJECT) {
|
||||||
|
throw new IllegalArgumentException("expected object but got a " + token);
|
||||||
|
}
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
currentFieldName = parser.currentName();
|
currentFieldName = parser.currentName();
|
||||||
@ -877,6 +883,8 @@ public class IndexMetaData implements Diffable<IndexMetaData>, FromXContentBuild
|
|||||||
String mappingType = currentFieldName;
|
String mappingType = currentFieldName;
|
||||||
Map<String, Object> mappingSource = MapBuilder.<String, Object>newMapBuilder().put(mappingType, parser.mapOrdered()).map();
|
Map<String, Object> mappingSource = MapBuilder.<String, Object>newMapBuilder().put(mappingType, parser.mapOrdered()).map();
|
||||||
builder.putMapping(new MappingMetaData(mappingType, mappingSource));
|
builder.putMapping(new MappingMetaData(mappingType, mappingSource));
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected token: " + token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ("aliases".equals(currentFieldName)) {
|
} else if ("aliases".equals(currentFieldName)) {
|
||||||
@ -896,6 +904,8 @@ public class IndexMetaData implements Diffable<IndexMetaData>, FromXContentBuild
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
builder.putActiveAllocationIds(Integer.valueOf(shardId), allocationIds);
|
builder.putActiveAllocationIds(Integer.valueOf(shardId), allocationIds);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected token: " + token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ("warmers".equals(currentFieldName)) {
|
} else if ("warmers".equals(currentFieldName)) {
|
||||||
@ -904,6 +914,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, FromXContentBuild
|
|||||||
// ignore: warmers have been removed in 3.0 and are
|
// ignore: warmers have been removed in 3.0 and are
|
||||||
// simply ignored when upgrading from 2.x
|
// simply ignored when upgrading from 2.x
|
||||||
assert Version.CURRENT.major <= 3;
|
assert Version.CURRENT.major <= 3;
|
||||||
|
parser.skipChildren();
|
||||||
} else {
|
} else {
|
||||||
// check if its a custom index metadata
|
// check if its a custom index metadata
|
||||||
Custom proto = lookupPrototype(currentFieldName);
|
Custom proto = lookupPrototype(currentFieldName);
|
||||||
@ -928,13 +939,19 @@ public class IndexMetaData implements Diffable<IndexMetaData>, FromXContentBuild
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected field for an array " + currentFieldName);
|
||||||
}
|
}
|
||||||
} else if (token.isValue()) {
|
} else if (token.isValue()) {
|
||||||
if ("state".equals(currentFieldName)) {
|
if ("state".equals(currentFieldName)) {
|
||||||
builder.state(State.fromString(parser.text()));
|
builder.state(State.fromString(parser.text()));
|
||||||
} else if ("version".equals(currentFieldName)) {
|
} else if ("version".equals(currentFieldName)) {
|
||||||
builder.version(parser.longValue());
|
builder.version(parser.longValue());
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected field [" + currentFieldName + "]");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected token " + token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return builder.build();
|
return builder.build();
|
||||||
|
@ -1074,14 +1074,20 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, Fr
|
|||||||
if (token == XContentParser.Token.START_OBJECT) {
|
if (token == XContentParser.Token.START_OBJECT) {
|
||||||
// move to the field name (meta-data)
|
// move to the field name (meta-data)
|
||||||
token = parser.nextToken();
|
token = parser.nextToken();
|
||||||
|
if (token != XContentParser.Token.FIELD_NAME) {
|
||||||
|
throw new IllegalArgumentException("Expected a field name but got " + token);
|
||||||
|
}
|
||||||
// move to the next object
|
// move to the next object
|
||||||
token = parser.nextToken();
|
token = parser.nextToken();
|
||||||
}
|
}
|
||||||
currentFieldName = parser.currentName();
|
currentFieldName = parser.currentName();
|
||||||
if (token == null) {
|
|
||||||
// no data...
|
|
||||||
return builder.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!"meta-data".equals(parser.currentName())) {
|
||||||
|
throw new IllegalArgumentException("Expected [meta-data] as a field name but got " + currentFieldName);
|
||||||
|
}
|
||||||
|
if (token != XContentParser.Token.START_OBJECT) {
|
||||||
|
throw new IllegalArgumentException("Expected a START_OBJECT but got " + token);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
@ -1114,7 +1120,11 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, Fr
|
|||||||
builder.version = parser.longValue();
|
builder.version = parser.longValue();
|
||||||
} else if ("cluster_uuid".equals(currentFieldName) || "uuid".equals(currentFieldName)) {
|
} else if ("cluster_uuid".equals(currentFieldName) || "uuid".equals(currentFieldName)) {
|
||||||
builder.clusterUUID = parser.text();
|
builder.clusterUUID = parser.text();
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected field [" + currentFieldName + "]");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected token " + token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return builder.build();
|
return builder.build();
|
||||||
|
@ -20,9 +20,15 @@
|
|||||||
package org.elasticsearch.cluster.metadata;
|
package org.elasticsearch.cluster.metadata;
|
||||||
|
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
|
import org.elasticsearch.common.bytes.BytesReference;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
@ -110,4 +116,36 @@ public class MetaDataTests extends ESTestCase {
|
|||||||
assertThat(ex.getMessage(), is("index/alias [alias2] provided with routing value [1,2] that resolved to several routing values, rejecting operation"));
|
assertThat(ex.getMessage(), is("index/alias [alias2] provided with routing value [1,2] that resolved to several routing values, rejecting operation"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testUnknownFieldClusterMetaData() throws IOException {
|
||||||
|
BytesReference metadata = JsonXContent.contentBuilder()
|
||||||
|
.startObject()
|
||||||
|
.startObject("meta-data")
|
||||||
|
.field("random", "value")
|
||||||
|
.endObject()
|
||||||
|
.endObject().bytes();
|
||||||
|
XContentParser parser = JsonXContent.jsonXContent.createParser(metadata);
|
||||||
|
try {
|
||||||
|
MetaData.Builder.fromXContent(parser);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertEquals("Unexpected field [random]", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testUnknownFieldIndexMetaData() throws IOException {
|
||||||
|
BytesReference metadata = JsonXContent.contentBuilder()
|
||||||
|
.startObject()
|
||||||
|
.startObject("index_name")
|
||||||
|
.field("random", "value")
|
||||||
|
.endObject()
|
||||||
|
.endObject().bytes();
|
||||||
|
XContentParser parser = JsonXContent.jsonXContent.createParser(metadata);
|
||||||
|
try {
|
||||||
|
IndexMetaData.Builder.fromXContent(parser);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertEquals("Unexpected field [random]", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user