Introduce NamedXContentRegistry (elastic/elasticsearch#4399)

This "super registry" will eventually replace things like
`IndiciesQueriesRegistry` but for now it is just another thing
to plumb across requests.

Original commit: elastic/x-pack-elasticsearch@da26a42b36
This commit is contained in:
Nik Everett 2016-12-20 11:05:42 -05:00 committed by GitHub
parent 6f7a065605
commit b2dc1bdca3
40 changed files with 248 additions and 190 deletions

View File

@ -10,6 +10,7 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -491,7 +492,8 @@ public class License implements ToXContent {
}
public static License fromSource(byte[] bytes) throws IOException {
final XContentParser parser = XContentFactory.xContent(bytes).createParser(bytes);
// EMPTY is safe here because we don't call namedObject
final XContentParser parser = XContentFactory.xContent(bytes).createParser(NamedXContentRegistry.EMPTY, bytes);
License license = null;
if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
if (parser.nextToken() == XContentParser.Token.FIELD_NAME) {

View File

@ -6,6 +6,7 @@
package org.elasticsearch.license;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -55,7 +56,9 @@ class TrialLicense {
byte[] content = new byte[contentLen];
byteBuffer.get(content);
final License expectedLicense;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(decrypt(content))) {
// EMPTY is safe here because we don't call namedObject
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY,
decrypt(content))) {
parser.nextToken();
expectedLicense = License.builder().fromLicenseSpec(License.fromXContent(parser),
license.signature()).version(-version).build();

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
@ -24,6 +23,7 @@ import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.env.Environment;
import org.elasticsearch.http.HttpServerTransport;
import org.elasticsearch.index.IndexModule;
@ -221,7 +221,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I
@Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
SearchRequestParsers searchRequestParsers) {
SearchRequestParsers searchRequestParsers, NamedXContentRegistry xContentRegistry) {
List<Object> components = new ArrayList<>();
components.add(sslService);
@ -256,7 +256,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I
components.addAll(notificationComponents);
components.addAll(watcher.createComponents(getClock(), scriptService, internalClient, searchRequestParsers, licenseState,
httpClient, httpTemplateParser, threadPool, clusterService, security.getCryptoService(), components));
httpClient, httpTemplateParser, threadPool, clusterService, security.getCryptoService(), xContentRegistry, components));
// just create the reloader as it will pull all of the loaded ssl configurations and start watching them
@ -466,8 +466,10 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I
public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays,
CircuitBreakerService circuitBreakerService,
NamedWriteableRegistry namedWriteableRegistry,
NamedXContentRegistry xContentRegistry,
NetworkService networkService) {
return security.getHttpTransports(settings, threadPool, bigArrays, circuitBreakerService, namedWriteableRegistry, networkService);
return security.getHttpTransports(settings, threadPool, bigArrays, circuitBreakerService, namedWriteableRegistry, xContentRegistry,
networkService);
}
@Override

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.common.xcontent;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
@ -38,7 +39,8 @@ public class XContentUtils {
public static Tuple<XContentType, Object> convertToObject(BytesReference bytes) throws ElasticsearchParseException {
try {
XContentParser parser = XContentHelper.createParser(bytes);
// EMPTY is safe here because we never call namedObject
XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, bytes);
return Tuple.tuple(parser.contentType(), readValue(parser, parser.nextToken()));
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e);

View File

@ -18,7 +18,7 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.rest.RestStatus;
@ -54,21 +54,19 @@ public class BackwardsCompatibilityAliasesResource extends HttpResource {
XContentBuilder request;
try {
Response response = client.performRequest("GET", "/.marvel-es-1-*", Collections.singletonMap("filter_path", "*.aliases"));
try (XContentParser parser = JsonXContent.jsonXContent.createParser(response.getEntity().getContent())) {
Map<String, Object> indices = parser.map();
request = JsonXContent.contentBuilder();
request.startObject().startArray("actions");
for (Map.Entry<String, Object> e : indices.entrySet()) {
String index = e.getKey();
// we add a suffix so that it will not collide with today's monitoring index following an upgrade
String alias = ".monitoring-es-2-" + index.substring(".marvel-es-1-".length()) + "-alias";
if (false == aliasesForIndex(e.getValue()).contains(alias)) {
needNewAliases = true;
addAlias(request, index, alias);
}
Map<String, Object> indices = XContentHelper.convertToMap(JsonXContent.jsonXContent, response.getEntity().getContent(), false);
request = JsonXContent.contentBuilder();
request.startObject().startArray("actions");
for (Map.Entry<String, Object> e : indices.entrySet()) {
String index = e.getKey();
// we add a suffix so that it will not collide with today's monitoring index following an upgrade
String alias = ".monitoring-es-2-" + index.substring(".marvel-es-1-".length()) + "-alias";
if (false == aliasesForIndex(e.getValue()).contains(alias)) {
needNewAliases = true;
addAlias(request, index, alias);
}
request.endArray().endObject();
}
request.endArray().endObject();
} catch (ResponseException e) {
int statusCode = e.getResponse().getStatusLine().getStatusCode();
@ -97,15 +95,14 @@ public class BackwardsCompatibilityAliasesResource extends HttpResource {
BytesRef bytes = request.bytes().toBytesRef();
HttpEntity body = new ByteArrayEntity(bytes.bytes, bytes.offset, bytes.length, ContentType.APPLICATION_JSON);
Response response = client.performRequest("POST", "/_aliases", parameters(), body);
try (XContentParser parser = JsonXContent.jsonXContent.createParser(response.getEntity().getContent())) {
Map<String, Object> aliasesResponse = parser.map();
Boolean acked = (Boolean) aliasesResponse.get("acknowledged");
if (acked == null) {
logger.error("Unexpected response format from _aliases action {}", aliasesResponse);
return false;
}
return acked;
Map<String, Object> aliasesResponse = XContentHelper.convertToMap(JsonXContent.jsonXContent, response.getEntity().getContent(),
false);
Boolean acked = (Boolean) aliasesResponse.get("acknowledged");
if (acked == null) {
logger.error("Unexpected response format from _aliases action {}", aliasesResponse);
return false;
}
return acked;
} catch (IOException | RuntimeException e) {
logger.error("failed to create aliases for 2.x monitoring indexes", e);
return false;

View File

@ -9,6 +9,7 @@ import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
@ -49,7 +50,8 @@ class HttpExportBulkResponseListener implements ResponseListener {
*/
@Override
public void onSuccess(final Response response) {
try (final XContentParser parser = xContent.createParser(response.getEntity().getContent())) {
// EMPTY is safe here because we never call namedObject
try (final XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, response.getEntity().getContent())) {
// avoid parsing the entire payload if we don't need too
XContentParser.Token token = parser.nextToken();

View File

@ -12,8 +12,8 @@ import org.elasticsearch.Version;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import java.io.IOException;
import java.util.Collections;
@ -82,24 +82,19 @@ public class VersionHttpResource extends HttpResource {
* @throws IOException if any parsing issue occurs.
*/
private boolean validateVersion(final Response response) throws IOException {
boolean supported = false;
Map<String, Object> map = XContentHelper.convertToMap(JsonXContent.jsonXContent, response.getEntity().getContent(), false);
// the response should be filtered to just '{"version":{"number":"xyz"}}', so this is cheap and guaranteed
@SuppressWarnings("unchecked")
final String versionNumber = (String) ((Map<String, Object>) map.get("version")).get("number");
final Version version = Version.fromString(versionNumber);
try (final XContentParser parser = XContentType.JSON.xContent().createParser(response.getEntity().getContent())) {
// the response should be filtered to just '{"version":{"number":"xyz"}}', so this is cheap and guaranteed
@SuppressWarnings("unchecked")
final String versionNumber = (String)((Map<String, Object>)parser.map().get("version")).get("number");
final Version version = Version.fromString(versionNumber);
if (version.onOrAfter(minimumVersion)) {
logger.debug("version [{}] >= [{}] and supported for [{}]", version, minimumVersion, resourceOwnerName);
supported = true;
} else {
logger.error("version [{}] < [{}] and NOT supported for [{}]", version, minimumVersion, resourceOwnerName);
}
if (version.onOrAfter(minimumVersion)) {
logger.debug("version [{}] >= [{}] and supported for [{}]", version, minimumVersion, resourceOwnerName);
return true;
} else {
logger.error("version [{}] < [{}] and NOT supported for [{}]", version, minimumVersion, resourceOwnerName);
return false;
}
return supported;
}
}

View File

@ -14,7 +14,6 @@ import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestActions;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.monitoring.action.MonitoringBulkRequestBuilder;

View File

@ -17,6 +17,7 @@ import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
@ -195,7 +196,8 @@ public class ReportingAttachmentParser implements EmailAttachmentParser<Reportin
* Extract the id from JSON payload, so we know which ID to poll for
*/
private String extractIdFromJson(String watchId, String attachmentId, BytesReference body) throws IOException {
try (XContentParser parser = JsonXContent.jsonXContent.createParser(body)) {
// EMPTY is safe here becaus we never call namedObject
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, body)) {
KibanaReportingPayload payload = new KibanaReportingPayload();
PAYLOAD_PARSER.parse(parser, payload, STRICT_PARSING);
String path = payload.getPath();

View File

@ -10,6 +10,7 @@ import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -17,7 +18,6 @@ import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.common.http.HttpRequest;
import org.elasticsearch.xpack.common.http.HttpResponse;
import org.elasticsearch.xpack.watcher.actions.jira.JiraAction;
import org.elasticsearch.xpack.watcher.support.xcontent.WatcherParams;
import java.io.IOException;
import java.util.ArrayList;
@ -147,8 +147,8 @@ public class JiraIssue implements ToXContent {
if (response.hasContent()) {
final List<String> errors = new ArrayList<>();
try (XContentParser parser = JsonXContent.jsonXContent.createParser(response.body())) {
// EMPTY is safe here because we never call namedObject
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, response.body())) {
XContentParser.Token token = parser.currentToken();
if (token == null) {
token = parser.nextToken();

View File

@ -9,6 +9,7 @@ import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -102,7 +103,8 @@ public class SentEvent implements ToXContent {
// lets first try to parse the error response in the body
// based on https://developer.pagerduty.com/documentation/rest/errors
try {
XContentParser parser = JsonXContent.jsonXContent.createParser(response.body());
// EMPTY is safe here because we never call namedObject
XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, response.body());
parser.nextToken();
String message = null;

View File

@ -28,6 +28,7 @@ import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.env.Environment;
import org.elasticsearch.http.HttpServerTransport;
import org.elasticsearch.index.IndexModule;
@ -714,15 +715,13 @@ public class Security implements ActionPlugin, IngestPlugin, NetworkPlugin {
@Override
public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays,
CircuitBreakerService circuitBreakerService,
NamedWriteableRegistry namedWriteableRegistry,
NetworkService networkService) {
CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry,
NamedXContentRegistry xContentRegistry, NetworkService networkService) {
if (enabled == false) { // don't register anything if we are not enabled
return Collections.emptyMap();
}
return Collections.singletonMap(Security.NAME4, () -> new SecurityNetty4HttpServerTransport(settings, networkService, bigArrays,
ipFilter.get(), sslService,
threadPool));
ipFilter.get(), sslService, threadPool, xContentRegistry));
}
@Override

View File

@ -9,6 +9,7 @@ import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
@ -21,13 +22,14 @@ import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.gateway.GatewayService;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.xpack.template.TemplateUtils;
@ -101,9 +103,8 @@ public class SecurityTemplateService extends AbstractComponent implements Cluste
, SECURITY_INDEX_TEMPLATE_VERSION_PATTERN);
Map<String, Object> typeMappingMap;
try {
XContentParser xParser = XContentFactory.xContent(template).createParser(template);
typeMappingMap = xParser.map();
} catch (IOException e) {
typeMappingMap = XContentHelper.convertToMap(JsonXContent.jsonXContent, template, false);
} catch (ElasticsearchParseException e) {
updateMappingPending.set(false);
logger.error("failed to parse the security index template", e);
throw new ElasticsearchException("failed to parse the security index template", e);
@ -218,9 +219,9 @@ public class SecurityTemplateService extends AbstractComponent implements Cluste
// we have to parse the source here which is annoying
for (Object typeMapping : mappings.values().toArray()) {
CompressedXContent typeMappingXContent = (CompressedXContent) typeMapping;
try (XContentParser xParser = XContentFactory.xContent(typeMappingXContent.toString())
.createParser(typeMappingXContent.toString())) {
Map<String, Object> typeMappingMap = xParser.map();
try {
Map<String, Object> typeMappingMap = XContentHelper.convertToMap(new BytesArray(typeMappingXContent.uncompressed()), false)
.v2();
// should always contain one entry with key = typename
assert (typeMappingMap.size() == 1);
String key = typeMappingMap.keySet().iterator().next();
@ -230,7 +231,7 @@ public class SecurityTemplateService extends AbstractComponent implements Cluste
if (containsCorrectVersion(mappingMap, predicate) == false) {
return false;
}
} catch (IOException e) {
} catch (ElasticsearchParseException e) {
logger.error("Cannot parse the template for security index.", e);
throw new IllegalStateException("Cannot parse the template for security index.", e);
}

View File

@ -12,6 +12,7 @@ import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.security.authc.support.Hasher;
@ -58,7 +59,8 @@ public class ChangePasswordRequestBuilder
}
public ChangePasswordRequestBuilder source(BytesReference source) throws IOException {
try (XContentParser parser = XContentHelper.createParser(source)) {
// EMPTY is ok here because we never call namedObject
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, source)) {
XContentUtils.verifyObject(parser);
XContentParser.Token token;
String currentFieldName = null;

View File

@ -14,6 +14,7 @@ import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
@ -92,7 +93,8 @@ public class PutUserRequestBuilder extends ActionRequestBuilder<PutUserRequest,
public PutUserRequestBuilder source(String username, BytesReference source) throws IOException {
username(username);
try (XContentParser parser = XContentHelper.createParser(source)) {
// EMPTY is ok here because we never call namedObject
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, source)) {
XContentUtils.verifyObject(parser);
XContentParser.Token token;
String currentFieldName = null;

View File

@ -5,11 +5,12 @@
*/
package org.elasticsearch.xpack.security.authc.esnative;
import com.google.common.base.Charsets;
import javax.net.ssl.HttpsURLConnection;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import com.google.common.base.Charsets;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -30,6 +31,7 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -60,6 +62,8 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.net.ssl.HttpsURLConnection;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.security.Security.setting;
@ -194,7 +198,8 @@ public class ESNativeRealmMigrateTool extends MultiCommand {
Set<String> getUsersThatExist(Terminal terminal, Settings settings, Environment env, OptionSet options) throws Exception {
Set<String> existingUsers = new HashSet<>();
String allUsersJson = postURL(settings, env, "GET", this.url.value(options) + "/_xpack/security/user/", options, null);
try (XContentParser parser = JsonXContent.jsonXContent.createParser(allUsersJson)) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, allUsersJson)) {
XContentParser.Token token = parser.nextToken();
String userName;
if (token == XContentParser.Token.START_OBJECT) {
@ -275,7 +280,8 @@ public class ESNativeRealmMigrateTool extends MultiCommand {
Set<String> getRolesThatExist(Terminal terminal, Settings settings, Environment env, OptionSet options) throws Exception {
Set<String> existingRoles = new HashSet<>();
String allRolesJson = postURL(settings, env, "GET", this.url.value(options) + "/_xpack/security/role/", options, null);
try (XContentParser parser = JsonXContent.jsonXContent.createParser(allRolesJson)) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, allRolesJson)) {
XContentParser.Token token = parser.nextToken();
String roleName;
if (token == XContentParser.Token.START_OBJECT) {

View File

@ -16,6 +16,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
@ -162,7 +163,8 @@ public class RoleDescriptor implements ToXContent {
public static RoleDescriptor parse(String name, BytesReference source, boolean allow2xFormat) throws IOException {
assert name != null;
try (XContentParser parser = XContentHelper.createParser(source)) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, source)) {
return parse(name, parser, allow2xFormat);
}
}

View File

@ -37,6 +37,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.logging.LoggerMessageFormat;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.IndexSettings;
@ -156,7 +157,8 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper {
for (BytesReference bytesReference : permissions.getQueries()) {
QueryShardContext queryShardContext = queryShardContextProvider.apply(shardId);
bytesReference = evaluateTemplate(bytesReference);
try (XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference)) {
try (XContentParser parser = XContentFactory.xContent(bytesReference)
.createParser(queryShardContext.getXContentRegistry(), bytesReference)) {
QueryBuilder queryBuilder = queryShardContext.newParseContext(parser).parseInnerQueryBuilder();
verifyRoleQuery(queryBuilder);
failIfQueryUsesClient(scriptService, queryBuilder, queryShardContext);
@ -286,7 +288,8 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper {
}
BytesReference evaluateTemplate(BytesReference querySource) throws IOException {
try (XContentParser parser = XContentFactory.xContent(querySource).createParser(querySource)) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = XContentFactory.xContent(querySource).createParser(NamedXContentRegistry.EMPTY, querySource)) {
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.START_OBJECT) {
throw new ElasticsearchParseException("Unexpected token [" + token + "]");
@ -399,8 +402,8 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper {
throw new IllegalStateException("role queries are not allowed to execute additional requests");
}
};
QueryRewriteContext copy = new QueryRewriteContext(original.getIndexSettings(), original.getMapperService(), scriptService, null,
client, original.getIndexReader(), original::nowInMillis);
QueryRewriteContext copy = new QueryRewriteContext(original.getIndexSettings(), original.getMapperService(), scriptService,
original.getXContentRegistry(), null, client, original.getIndexReader(), original::nowInMillis);
queryBuilder.rewrite(copy);
}
}

View File

@ -13,6 +13,7 @@ import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
import org.elasticsearch.env.Environment;
@ -222,7 +223,8 @@ public class FileRolesStore extends AbstractLifecycleComponent {
boolean resolvePermissions, Settings settings) {
String roleName = null;
try {
XContentParser parser = YamlXContent.yamlXContent.createParser(segment);
// EMPTY is safe here because we never use namedObject
XContentParser parser = YamlXContent.yamlXContent.createParser(NamedXContentRegistry.EMPTY, segment);
XContentParser.Token token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
token = parser.nextToken();

View File

@ -7,7 +7,6 @@ package org.elasticsearch.xpack.security.transport;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;

View File

@ -14,6 +14,7 @@ import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.http.netty4.Netty4HttpServerTransport;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.netty4.Netty4Utils;
@ -34,8 +35,8 @@ public class SecurityNetty4HttpServerTransport extends Netty4HttpServerTransport
private final boolean ssl;
public SecurityNetty4HttpServerTransport(Settings settings, NetworkService networkService, BigArrays bigArrays, IPFilter ipFilter,
SSLService sslService, ThreadPool threadPool) {
super(settings, networkService, bigArrays, threadPool);
SSLService sslService, ThreadPool threadPool, NamedXContentRegistry xContentRegistry) {
super(settings, networkService, bigArrays, threadPool, xContentRegistry);
this.ipFilter = ipFilter;
this.ssl = HTTP_SSL_ENABLED.get(settings);
this.sslService = sslService;

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.ssl;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
@ -25,6 +26,7 @@ import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
@ -32,20 +34,19 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.node.internal.InternalSettingsPreparer;
import org.elasticsearch.xpack.XPackPlugin;
import javax.security.auth.x500.X500Principal;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.security.cert.Certificate;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
@ -61,6 +62,8 @@ import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.security.auth.x500.X500Principal;
/**
* CLI tool to make generation of certificates or certificate requests easier for users
*/
@ -243,7 +246,8 @@ public class CertificateTool extends EnvironmentAwareCommand {
*/
static Collection<CertificateInformation> parseFile(Path file) throws Exception {
try (Reader reader = Files.newBufferedReader(file)) {
XContentParser xContentParser = XContentType.YAML.xContent().createParser(reader);
// EMPTY is safe here because we never use namedObject
XContentParser xContentParser = XContentType.YAML.xContent().createParser(NamedXContentRegistry.EMPTY, reader);
return PARSER.parse(xContentParser, new ArrayList<>(), new CertInfoParseContext());
}
}

View File

@ -22,6 +22,7 @@ import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.ScriptPlugin;
@ -207,7 +208,7 @@ public class Watcher implements ActionPlugin, ScriptPlugin {
SearchRequestParsers searchRequestParsers, XPackLicenseState licenseState,
HttpClient httpClient, HttpRequestTemplate.Parser httpTemplateParser,
ThreadPool threadPool, ClusterService clusterService, CryptoService cryptoService,
Collection<Object> components) {
NamedXContentRegistry xContentRegistry, Collection<Object> components) {
if (enabled == false) {
return Collections.emptyList();
}
@ -223,7 +224,7 @@ public class Watcher implements ActionPlugin, ScriptPlugin {
final Map<String, TransformFactory> transformFactories = new HashMap<>();
transformFactories.put(ScriptTransform.TYPE, new ScriptTransformFactory(settings, scriptService));
transformFactories.put(SearchTransform.TYPE, new SearchTransformFactory(settings, internalClient, searchRequestParsers,
scriptService));
xContentRegistry, scriptService));
final TransformRegistry transformRegistry = new TransformRegistry(settings, Collections.unmodifiableMap(transformFactories));
final Map<String, ActionFactory> actionFactoryMap = new HashMap<>();
@ -245,7 +246,8 @@ public class Watcher implements ActionPlugin, ScriptPlugin {
final ActionRegistry registry = new ActionRegistry(actionFactoryMap, conditionRegistry, transformRegistry, clock, licenseState);
final Map<String, InputFactory> inputFactories = new HashMap<>();
inputFactories.put(SearchInput.TYPE, new SearchInputFactory(settings, internalClient, searchRequestParsers, scriptService));
inputFactories.put(SearchInput.TYPE,
new SearchInputFactory(settings, internalClient, searchRequestParsers, xContentRegistry, scriptService));
inputFactories.put(SimpleInput.TYPE, new SimpleInputFactory(settings));
inputFactories.put(HttpInput.TYPE, new HttpInputFactory(settings, httpClient, templateEngine, httpTemplateParser));
inputFactories.put(NoneInput.TYPE, new NoneInputFactory(settings));
@ -278,7 +280,7 @@ public class Watcher implements ActionPlugin, ScriptPlugin {
final TriggeredWatchStore triggeredWatchStore = new TriggeredWatchStore(settings, watcherClientProxy, triggeredWatchParser);
final WatcherSearchTemplateService watcherSearchTemplateService =
new WatcherSearchTemplateService(settings, scriptService, searchRequestParsers);
new WatcherSearchTemplateService(settings, scriptService, searchRequestParsers, xContentRegistry);
final WatchExecutor watchExecutor = getWatchExecutor(threadPool);
final Watch.Parser watchParser = new Watch.Parser(settings, triggerService, registry, inputRegistry, cryptoService, clock);

View File

@ -11,6 +11,7 @@ import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
@ -58,7 +59,8 @@ public class TriggeredWatch implements ToXContent {
}
public TriggeredWatch parse(String id, long version, BytesReference source) {
try (XContentParser parser = XContentHelper.createParser(source)) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, source)) {
return parse(id, version, parser);
} catch (IOException e) {
throw new ElasticsearchException("unable to parse watch record", e);

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.watcher.input.http;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
@ -77,7 +78,8 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
}
if (contentType != null) {
try (XContentParser parser = contentType.xContent().createParser(response.body())) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = contentType.xContent().createParser(NamedXContentRegistry.EMPTY, response.body())) {
if (input.getExtractKeys() != null) {
payloadMap.putAll(XContentFilterKeysUtils.filterMapOrdered(input.getExtractKeys(), parser));
} else {

View File

@ -11,6 +11,7 @@ import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
@ -80,7 +81,8 @@ public class ExecutableSearchInput extends ExecutableInput<SearchInput, SearchIn
final Payload payload;
if (input.getExtractKeys() != null) {
XContentBuilder builder = jsonBuilder().startObject().value(response).endObject();
XContentParser parser = XContentHelper.createParser(builder.bytes());
// EMPTY is safe here because we never use namedObject
XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, builder.bytes());
Map<String, Object> filteredKeys = XContentFilterKeysUtils.filterMapOrdered(input.getExtractKeys(), parser);
payload = new Payload.Simple(filteredKeys);
} else {

View File

@ -9,9 +9,9 @@ import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptSettings;
import org.elasticsearch.search.SearchRequestParsers;
import org.elasticsearch.xpack.security.InternalClient;
import org.elasticsearch.xpack.watcher.input.InputFactory;
@ -22,28 +22,25 @@ import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateServi
import java.io.IOException;
public class SearchInputFactory extends InputFactory<SearchInput, SearchInput.Result, ExecutableSearchInput> {
private final Settings settings;
private final WatcherClientProxy client;
private final TimeValue defaultTimeout;
private final SearchRequestParsers searchRequestParsers;
private final ParseFieldMatcher parseFieldMatcher;
private final WatcherSearchTemplateService searchTemplateService;
public SearchInputFactory(Settings settings, InternalClient client,
SearchRequestParsers searchRequestParsers, ScriptService scriptService) {
this(settings, new WatcherClientProxy(settings, client), searchRequestParsers, scriptService);
public SearchInputFactory(Settings settings, InternalClient client, SearchRequestParsers searchRequestParsers,
NamedXContentRegistry xContentRegistry, ScriptService scriptService) {
this(settings, new WatcherClientProxy(settings, client), searchRequestParsers, xContentRegistry, scriptService);
}
public SearchInputFactory(Settings settings, WatcherClientProxy client,
SearchRequestParsers searchRequestParsers, ScriptService scriptService) {
public SearchInputFactory(Settings settings, WatcherClientProxy client, SearchRequestParsers searchRequestParsers,
NamedXContentRegistry xContentRegistry, ScriptService scriptService) {
super(Loggers.getLogger(ExecutableSimpleInput.class, settings));
this.settings = settings;
this.parseFieldMatcher = new ParseFieldMatcher(settings);
this.client = client;
this.searchRequestParsers = searchRequestParsers;
this.defaultTimeout = settings.getAsTime("xpack.watcher.input.search.default_timeout", null);
this.searchTemplateService = new WatcherSearchTemplateService(settings, scriptService, searchRequestParsers);
this.searchTemplateService = new WatcherSearchTemplateService(settings, scriptService, searchRequestParsers, xContentRegistry);
}
@Override

View File

@ -84,54 +84,56 @@ public class RestExecuteWatchAction extends WatcherRestHandler {
builder.setRecordExecution(request.paramAsBoolean(RECORD_EXECUTION.getPreferredName(), builder.request().isRecordExecution()));
builder.setIgnoreCondition(request.paramAsBoolean(IGNORE_CONDITION.getPreferredName(), builder.request().isIgnoreCondition()));
XContentParser parser = XContentHelper.createParser(request.content());
parser.nextToken();
try (XContentParser parser = request.contentParser()) {
parser.nextToken();
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_BOOLEAN) {
if (ParseFieldMatcher.STRICT.match(currentFieldName, IGNORE_CONDITION)) {
builder.setIgnoreCondition(parser.booleanValue());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, RECORD_EXECUTION)) {
builder.setRecordExecution(parser.booleanValue());
} else {
throw new ElasticsearchParseException("could not parse watch execution request. unexpected boolean field [{}]",
currentFieldName);
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ALTERNATIVE_INPUT)) {
builder.setAlternativeInput(parser.map());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.TRIGGER_DATA)) {
builder.setTriggerData(parser.map());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.WATCH)) {
XContentBuilder watcherSource = XContentBuilder.builder(parser.contentType().xContent());
XContentHelper.copyCurrentStructure(watcherSource.generator(), parser);
builder.setWatchSource(watcherSource.bytes());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ACTION_MODES)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
try {
ActionExecutionMode mode = ActionExecutionMode.resolve(parser.textOrNull());
builder.setActionMode(currentFieldName, mode);
} catch (IllegalArgumentException iae) {
throw new ElasticsearchParseException("could not parse watch execution request", iae);
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_BOOLEAN) {
if (ParseFieldMatcher.STRICT.match(currentFieldName, IGNORE_CONDITION)) {
builder.setIgnoreCondition(parser.booleanValue());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, RECORD_EXECUTION)) {
builder.setRecordExecution(parser.booleanValue());
} else {
throw new ElasticsearchParseException("could not parse watch execution request. unexpected boolean field [{}]",
currentFieldName);
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ALTERNATIVE_INPUT)) {
builder.setAlternativeInput(parser.map());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.TRIGGER_DATA)) {
builder.setTriggerData(parser.map());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.WATCH)) {
XContentBuilder watcherSource = XContentBuilder.builder(parser.contentType().xContent());
XContentHelper.copyCurrentStructure(watcherSource.generator(), parser);
builder.setWatchSource(watcherSource.bytes());
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ACTION_MODES)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
try {
ActionExecutionMode mode = ActionExecutionMode.resolve(parser.textOrNull());
builder.setActionMode(currentFieldName, mode);
} catch (IllegalArgumentException iae) {
throw new ElasticsearchParseException("could not parse watch execution request", iae);
}
} else {
throw new ElasticsearchParseException(
"could not parse watch execution request. unexpected array field [{}]",
currentFieldName);
}
} else {
throw new ElasticsearchParseException("could not parse watch execution request. unexpected array field [{}]",
currentFieldName);
}
} else {
throw new ElasticsearchParseException("could not parse watch execution request. unexpected object field [{}]",
currentFieldName);
}
} else {
throw new ElasticsearchParseException("could not parse watch execution request. unexpected object field [{}]",
currentFieldName);
throw new ElasticsearchParseException("could not parse watch execution request. unexpected token [{}]", token);
}
} else {
throw new ElasticsearchParseException("could not parse watch execution request. unexpected token [{}]", token);
}
}

View File

@ -10,6 +10,7 @@ import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
@ -35,12 +36,15 @@ public class WatcherSearchTemplateService extends AbstractComponent {
private final ScriptService scriptService;
private final ParseFieldMatcher parseFieldMatcher;
private final SearchRequestParsers searchRequestParsers;
private final NamedXContentRegistry xContentRegistry;
public WatcherSearchTemplateService(Settings settings, ScriptService scriptService, SearchRequestParsers searchRequestParsers) {
public WatcherSearchTemplateService(Settings settings, ScriptService scriptService, SearchRequestParsers searchRequestParsers,
NamedXContentRegistry xContentRegistry) {
super(settings);
this.scriptService = scriptService;
this.searchRequestParsers = searchRequestParsers;
this.parseFieldMatcher = new ParseFieldMatcher(settings);
this.xContentRegistry = xContentRegistry;
}
public BytesReference renderTemplate(Script source,
@ -69,7 +73,7 @@ public class WatcherSearchTemplateService extends AbstractComponent {
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
BytesReference source = request.getSearchSource();
if (source != null && source.length() > 0) {
try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
try (XContentParser parser = XContentFactory.xContent(source).createParser(xContentRegistry, source)) {
sourceBuilder.parseXContent(new QueryParseContext(searchRequestParsers.queryParsers, parser, parseFieldMatcher),
searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers);
searchRequest.source(sourceBuilder);

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.watcher.support.xcontent;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentLocation;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
@ -264,6 +265,16 @@ public class WatcherXContentParser implements XContentParser {
return parser.isClosed();
}
@Override
public <T> T namedObject(Class<T> categoryClass, String name, Object context) throws IOException {
return parser.namedObject(categoryClass, name, context);
}
@Override
public NamedXContentRegistry getXContentRegistry() {
return parser.getXContentRegistry();
}
@Override
public void close() throws ElasticsearchException {
parser.close();

View File

@ -10,6 +10,7 @@ import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
@ -101,15 +102,16 @@ public class XContentSource implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
try (XContentParser parser = parser()) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = parser(NamedXContentRegistry.EMPTY)) {
parser.nextToken();
XContentHelper.copyCurrentStructure(builder.generator(), parser);
return builder;
}
}
public XContentParser parser() throws IOException {
return contentType.xContent().createParser(bytes);
public XContentParser parser(NamedXContentRegistry xContentRegistry) throws IOException {
return contentType.xContent().createParser(xContentRegistry, bytes);
}
public static XContentSource readFrom(StreamInput in) throws IOException {
@ -123,7 +125,8 @@ public class XContentSource implements ToXContent {
private Object data() {
if (data == null) {
try (XContentParser parser = parser()) {
// EMPTY is safe here because we never use namedObject
try (XContentParser parser = parser(NamedXContentRegistry.EMPTY)) {
data = XContentUtils.readValue(parser, parser.nextToken());
} catch (IOException ex) {
throw new ElasticsearchException("failed to read value", ex);

View File

@ -5,45 +5,41 @@
*/
package org.elasticsearch.xpack.watcher.transform.search;
import java.io.IOException;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptSettings;
import org.elasticsearch.search.SearchRequestParsers;
import org.elasticsearch.xpack.security.InternalClient;
import org.elasticsearch.xpack.watcher.support.init.proxy.WatcherClientProxy;
import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateService;
import org.elasticsearch.xpack.watcher.transform.TransformFactory;
public class SearchTransformFactory extends TransformFactory<SearchTransform, SearchTransform.Result, ExecutableSearchTransform> {
import java.io.IOException;
private final Settings settings;
public class SearchTransformFactory extends TransformFactory<SearchTransform, SearchTransform.Result, ExecutableSearchTransform> {
protected final WatcherClientProxy client;
private final TimeValue defaultTimeout;
private final SearchRequestParsers searchRequestParsers;
private final ParseFieldMatcher parseFieldMatcher;
private final WatcherSearchTemplateService searchTemplateService;
public SearchTransformFactory(Settings settings, InternalClient client,
SearchRequestParsers searchRequestParsers, ScriptService scriptService) {
this(settings, new WatcherClientProxy(settings, client), searchRequestParsers, scriptService);
public SearchTransformFactory(Settings settings, InternalClient client, SearchRequestParsers searchRequestParsers,
NamedXContentRegistry xContentRegistry, ScriptService scriptService) {
this(settings, new WatcherClientProxy(settings, client), searchRequestParsers, xContentRegistry, scriptService);
}
public SearchTransformFactory(Settings settings, WatcherClientProxy client,
SearchRequestParsers searchRequestParsers, ScriptService scriptService) {
public SearchTransformFactory(Settings settings, WatcherClientProxy client, SearchRequestParsers searchRequestParsers,
NamedXContentRegistry xContentRegistry, ScriptService scriptService) {
super(Loggers.getLogger(ExecutableSearchTransform.class, settings));
this.settings = settings;
this.client = client;
this.parseFieldMatcher = new ParseFieldMatcher(settings);
this.searchRequestParsers = searchRequestParsers;
this.defaultTimeout = settings.getAsTime("xpack.watcher.transform.search.default_timeout", null);
this.searchTemplateService = new WatcherSearchTemplateService(settings, scriptService, searchRequestParsers);
this.searchTemplateService = new WatcherSearchTemplateService(settings, scriptService, searchRequestParsers, xContentRegistry);
}
@Override

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -257,7 +258,9 @@ public class Watch implements TriggerEngine.Job, ToXContent {
}
XContentParser parser = null;
try {
parser = new WatcherXContentParser(createParser(source), new HaltedClock(now), withSecrets ? cryptoService : null);
// EMPTY is safe here because we never use namedObject
parser = new WatcherXContentParser(createParser(NamedXContentRegistry.EMPTY, source), new HaltedClock(now),
withSecrets ? cryptoService : null);
parser.nextToken();
return parse(id, includeStatus, parser);
} catch (IOException ioe) {

View File

@ -9,10 +9,12 @@ import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.mock.orig.Mockito;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
@ -49,7 +51,7 @@ public class HttpExportBulkResponseListenerTests extends ESTestCase {
when(response.getEntity()).thenReturn(entity);
when(entity.getContent()).thenReturn(stream);
when(xContent.createParser(stream)).thenReturn(parser);
when(xContent.createParser(Mockito.any(NamedXContentRegistry.class), Mockito.eq(stream))).thenReturn(parser);
// {, "took", 4, "errors", false
when(parser.nextToken()).thenReturn(Token.START_OBJECT,
@ -105,7 +107,7 @@ public class HttpExportBulkResponseListenerTests extends ESTestCase {
when(response.getEntity()).thenReturn(entity);
when(entity.getContent()).thenReturn(stream);
when(xContent.createParser(stream)).thenReturn(parser);
when(xContent.createParser(Mockito.any(NamedXContentRegistry.class), Mockito.eq(stream))).thenReturn(parser);
// {, "took", 4, "errors", false nextToken, currentName
when(parser.nextToken()).thenReturn(Token.START_OBJECT, // 1

View File

@ -75,7 +75,7 @@ public class SecurityIndexSearcherWrapperIntegrationTests extends ESTestCase {
when(client.settings()).thenReturn(Settings.EMPTY);
final long nowInMillis = randomPositiveLong();
QueryShardContext realQueryShardContext = new QueryShardContext(shardId.id(), indexSettings, null, null, mapperService, null,
null, indicesQueriesRegistry, client, null, () -> nowInMillis);
null, xContentRegistry(), indicesQueriesRegistry, client, null, () -> nowInMillis);
QueryShardContext queryShardContext = spy(realQueryShardContext);
QueryParseContext queryParseContext = mock(QueryParseContext.class);
IndexSettings settings = IndexSettingsModule.newIndexSettings("_index", Settings.EMPTY);
@ -172,7 +172,7 @@ public class SecurityIndexSearcherWrapperIntegrationTests extends ESTestCase {
*/
private static XContentParser anyParser() {
any(XContentParser.class);
return new JsonXContentParser(null);
return new JsonXContentParser(null, null);
}
}

View File

@ -130,7 +130,7 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, namedAnalyzer, namedAnalyzer, namedAnalyzer,
Collections.emptyMap());
SimilarityService similarityService = new SimilarityService(indexSettings, Collections.emptyMap());
mapperService = new MapperService(indexSettings, indexAnalyzers, similarityService,
mapperService = new MapperService(indexSettings, indexAnalyzers, xContentRegistry(), similarityService,
new IndicesModule(emptyList()).getMapperRegistry(), () -> null);
ShardId shardId = new ShardId(index, 0);
@ -815,7 +815,8 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
Client client = mock(Client.class);
when(client.settings()).thenReturn(Settings.EMPTY);
final long nowInMillis = randomPositiveLong();
QueryRewriteContext context = new QueryRewriteContext(null, mapperService, scriptService, null, client, null, () -> nowInMillis);
QueryRewriteContext context = new QueryRewriteContext(null, mapperService, scriptService, xContentRegistry(), null, client, null,
() -> nowInMillis);
QueryBuilder queryBuilder1 = new TermsQueryBuilder("field", "val1", "val2");
SecurityIndexSearcherWrapper.failIfQueryUsesClient(scriptService, queryBuilder1, context);

View File

@ -57,7 +57,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
.put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build();
sslService = new SSLService(settings, env);
SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
Netty4HttpMockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.configureServerChannelHandler();
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -73,7 +73,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
.put("xpack.security.http.ssl.client_authentication", value).build();
sslService = new SSLService(settings, env);
SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
Netty4HttpMockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.configureServerChannelHandler();
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -89,7 +89,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
.put("xpack.security.http.ssl.client_authentication", value).build();
sslService = new SSLService(settings, env);
SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
Netty4HttpMockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.configureServerChannelHandler();
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -105,7 +105,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
.put("xpack.security.http.ssl.client_authentication", value).build();
sslService = new SSLService(settings, env);
SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
Netty4HttpMockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.configureServerChannelHandler();
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -119,7 +119,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
.put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build();
sslService = new SSLService(settings, env);
SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
Netty4HttpMockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.configureServerChannelHandler();
EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -132,7 +132,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
.build();
sslService = new SSLService(settings, new Environment(settings));
transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
Netty4HttpMockUtil.setOpenChannelsHandlerToMock(transport);
handler = transport.configureServerChannelHandler();
ch = new EmbeddedChannel(handler);
@ -180,7 +180,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
env = new Environment(settings);
sslService = new SSLService(settings, env);
SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, transport::configureServerChannelHandler);
assertThat(e.getMessage(), containsString("key must be provided"));
}
@ -195,7 +195,7 @@ public class SecurityNetty4HttpServerTransportTests extends ESTestCase {
env = new Environment(settings);
sslService = new SSLService(settings, env);
SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, mock(NetworkService.class),
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class));
mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry());
assertNotNull(transport.configureServerChannelHandler());
}
}

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.watcher.test.integration;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
@ -147,7 +148,7 @@ public class SearchInputTests extends ESIntegTestCase {
IndicesQueriesRegistry indicesQueryRegistry = internalCluster().getInstance(IndicesQueriesRegistry.class);
SearchRequestParsers searchParsers = new SearchRequestParsers(indicesQueryRegistry, null, null, null);
SearchInputFactory factory = new SearchInputFactory(Settings.EMPTY, WatcherClientProxy.of(client()),
searchParsers, scriptService());
searchParsers, xContentRegistry(), scriptService());
SearchInput searchInput = factory.parseInput("_id", parser);
assertEquals(SearchInput.TYPE, searchInput.type());
@ -158,7 +159,8 @@ public class SearchInputTests extends ESIntegTestCase {
String master = internalCluster().getMasterName();
return new WatcherSearchTemplateService(internalCluster().clusterService(master).getSettings(),
internalCluster().getInstance(ScriptService.class, master),
internalCluster().getInstance(SearchRequestParsers.class, master)
internalCluster().getInstance(SearchRequestParsers.class, master),
internalCluster().getInstance(NamedXContentRegistry.class, master)
);
}

View File

@ -214,7 +214,7 @@ public class SearchTransformTests extends ESIntegTestCase {
SearchRequestParsers searchRequestParsers = internalCluster().getInstance(SearchRequestParsers.class);
SearchTransformFactory transformFactory = new SearchTransformFactory(Settings.EMPTY, WatcherClientProxy.of(client()),
searchRequestParsers, scriptService());
searchRequestParsers, xContentRegistry(), scriptService());
ExecutableSearchTransform executable = transformFactory.parseExecutable("_id", parser);
assertThat(executable, notNullValue());
@ -284,7 +284,8 @@ public class SearchTransformTests extends ESIntegTestCase {
String master = internalCluster().getMasterName();
return new WatcherSearchTemplateService(internalCluster().clusterService(master).getSettings(),
internalCluster().getInstance(ScriptService.class, master),
internalCluster().getInstance(SearchRequestParsers.class, master)
internalCluster().getInstance(SearchRequestParsers.class, master),
xContentRegistry()
);
}

View File

@ -292,7 +292,8 @@ public class WatchTests extends ESTestCase {
QueryParser<ScriptQueryBuilder> queryParser2 = ScriptQueryBuilder::fromXContent;
queryRegistry.register(queryParser2, ScriptQueryBuilder.NAME);
SearchRequestParsers searchParsers = new SearchRequestParsers(queryRegistry, null, null, null);
WatcherSearchTemplateService searchTemplateService = new WatcherSearchTemplateService(settings, scriptService, searchParsers);
WatcherSearchTemplateService searchTemplateService = new WatcherSearchTemplateService(settings, scriptService, searchParsers,
xContentRegistry());
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
@ -417,7 +418,7 @@ public class WatchTests extends ESTestCase {
QueryParser<ScriptQueryBuilder> queryParser2 = ScriptQueryBuilder::fromXContent;
queryRegistry.register(queryParser2, ScriptQueryBuilder.NAME);
SearchRequestParsers searchParsers = new SearchRequestParsers(queryRegistry, null, null, null);
parsers.put(SearchInput.TYPE, new SearchInputFactory(settings, client, searchParsers, scriptService));
parsers.put(SearchInput.TYPE, new SearchInputFactory(settings, client, searchParsers, xContentRegistry(), scriptService));
return new InputRegistry(Settings.EMPTY, parsers);
default:
parsers.put(SimpleInput.TYPE, new SimpleInputFactory(settings));
@ -470,7 +471,7 @@ public class WatchTests extends ESTestCase {
SearchRequestParsers searchParsers = new SearchRequestParsers(queryRegistry, null, null, null);
Map<String, TransformFactory> factories = new HashMap<>();
factories.put(ScriptTransform.TYPE, new ScriptTransformFactory(settings, scriptService));
factories.put(SearchTransform.TYPE, new SearchTransformFactory(settings, client, searchParsers, scriptService));
factories.put(SearchTransform.TYPE, new SearchTransformFactory(settings, client, searchParsers, xContentRegistry(), scriptService));
return new TransformRegistry(Settings.EMPTY, unmodifiableMap(factories));
}