From 1d97cee3151f74b94689e6c21a6f5c4eab643a07 Mon Sep 17 00:00:00 2001 From: James Baiera Date: Thu, 5 Dec 2019 13:53:42 -0500 Subject: [PATCH] Support es7 node http publish_address format (#49279) (#49839) Add parsing support to node http publish_address format cname/ip:port. --- .../sniff/ElasticsearchNodesSniffer.java | 18 +++++++++-- .../ElasticsearchNodesSnifferParseTests.java | 22 ++++++++++++++ .../es6_nodes_publication_address_format.json | 30 +++++++++++++++++++ .../es7_nodes_publication_address_format.json | 30 +++++++++++++++++++ 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 client/sniffer/src/test/resources/es6_nodes_publication_address_format.json create mode 100644 client/sniffer/src/test/resources/es7_nodes_publication_address_format.json diff --git a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java index 5c947f5625b..e7f055bfe01 100644 --- a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java +++ b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java @@ -164,9 +164,21 @@ public final class ElasticsearchNodesSniffer implements NodesSniffer { if ("http".equals(fieldName)) { while (parser.nextToken() != JsonToken.END_OBJECT) { if (parser.getCurrentToken() == JsonToken.VALUE_STRING && "publish_address".equals(parser.getCurrentName())) { - URI publishAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString()); - publishedHost = new HttpHost(publishAddressAsURI.getHost(), publishAddressAsURI.getPort(), - publishAddressAsURI.getScheme()); + String address = parser.getValueAsString(); + String host; + URI publishAddressAsURI; + + // ES7 cname/ip:port format + if(address.contains("/")) { + String[] cnameAndURI = address.split("/", 2); + publishAddressAsURI = URI.create(scheme + "://" + cnameAndURI[1]); + host = cnameAndURI[0]; + } + else { + publishAddressAsURI = URI.create(scheme + "://" + address); + host = publishAddressAsURI.getHost(); + } + publishedHost = new HttpHost(host, publishAddressAsURI.getPort(), publishAddressAsURI.getScheme()); } else if (parser.currentToken() == JsonToken.START_ARRAY && "bound_address".equals(parser.getCurrentName())) { while (parser.nextToken() != JsonToken.END_ARRAY) { URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString()); diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java index edc7330c130..6a017e32728 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java @@ -107,6 +107,28 @@ public class ElasticsearchNodesSnifferParseTests extends RestClientTestCase { node(9207, "c2", "6.0.0", false, false, true)); } + public void testParsingPublishAddressWithPreES7Format() throws IOException { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es6_nodes_publication_address_format.json"); + + HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON); + List nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory()); + + assertEquals("127.0.0.1", nodes.get(0).getHost().getHostName()); + assertEquals(9200, nodes.get(0).getHost().getPort()); + assertEquals("http", nodes.get(0).getHost().getSchemeName()); + } + + public void testParsingPublishAddressWithES7Format() throws IOException { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es7_nodes_publication_address_format.json"); + + HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON); + List nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory()); + + assertEquals("elastic.test", nodes.get(0).getHost().getHostName()); + assertEquals(9200, nodes.get(0).getHost().getPort()); + assertEquals("http", nodes.get(0).getHost().getSchemeName()); + } + private Node node(int port, String name, String version, boolean master, boolean data, boolean ingest) { HttpHost host = new HttpHost("127.0.0.1", port); Set boundHosts = new HashSet<>(2); diff --git a/client/sniffer/src/test/resources/es6_nodes_publication_address_format.json b/client/sniffer/src/test/resources/es6_nodes_publication_address_format.json new file mode 100644 index 00000000000..7ded043b811 --- /dev/null +++ b/client/sniffer/src/test/resources/es6_nodes_publication_address_format.json @@ -0,0 +1,30 @@ +{ + "_nodes": { + "total": 8, + "successful": 8, + "failed": 0 + }, + "cluster_name": "elasticsearch", + "nodes": { + "ikXK_skVTfWkhONhldnbkw": { + "name": "m1", + "transport_address": "127.0.0.1:9300", + "host": "127.0.0.1", + "ip": "127.0.0.1", + "version": "6.0.0", + "build_hash": "8f0685b", + "roles": [ + "master", + "ingest" + ], + "attributes": { }, + "http": { + "bound_address": [ + "127.0.0.1:9200" + ], + "publish_address": "127.0.0.1:9200", + "max_content_length_in_bytes": 104857600 + } + } + } +} diff --git a/client/sniffer/src/test/resources/es7_nodes_publication_address_format.json b/client/sniffer/src/test/resources/es7_nodes_publication_address_format.json new file mode 100644 index 00000000000..295bf3cbd23 --- /dev/null +++ b/client/sniffer/src/test/resources/es7_nodes_publication_address_format.json @@ -0,0 +1,30 @@ +{ + "_nodes": { + "total": 8, + "successful": 8, + "failed": 0 + }, + "cluster_name": "elasticsearch", + "nodes": { + "ikXK_skVTfWkhONhldnbkw": { + "name": "m1", + "transport_address": "127.0.0.1:9300", + "host": "127.0.0.1", + "ip": "127.0.0.1", + "version": "6.0.0", + "build_hash": "8f0685b", + "roles": [ + "master", + "ingest" + ], + "attributes": { }, + "http": { + "bound_address": [ + "elastic.test:9200" + ], + "publish_address": "elastic.test/127.0.0.1:9200", + "max_content_length_in_bytes": 104857600 + } + } + } +}