From b3a369208d10a8352cfc2df98c1228a779d8e88e Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 10 Oct 2019 10:20:55 -0500 Subject: [PATCH 1/3] Add cloudId builder to the HLRC Elastic cloud has a concept of a cloud Id. This Id is a base64 encoded url, split up into a few parts. This commit allows the user to pass in a cloud id now, which is translated to a HttpHost that is defined by the encoded parts therein. --- .../org/elasticsearch/client/RestClient.java | 27 +++++++++++++++ .../client/RestClientBuilderTests.java | 33 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java index a31732d742731..217ad6cc29b11 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java @@ -56,6 +56,7 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Base64; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -119,6 +120,32 @@ public class RestClient implements Closeable { setNodes(nodes); } + /** + * Returns a new {@link RestClientBuilder} to help with {@link RestClient} creation. + * Creates a new builder instance and sets the nodes that the client will send requests to. + * + * @param cloudId a valid elastic cloud cloudId that will route to a cluster + */ + public static RestClientBuilder builder(String cloudId) { + // there is an optional first portion of the cloudId that is a human readable string, but it is not used. + if (cloudId.contains(":")) { + if (cloudId.indexOf(":") == cloudId.length() - 1) { + throw new IllegalStateException("cloudId " + cloudId + " is invalid"); + } + cloudId = cloudId.substring(cloudId.indexOf(":") + 1); + } + + String decoded = new String(Base64.getDecoder().decode(cloudId)); + // once decoded the parts are separated by a $ character + String[] decodedParts = decoded.split("\\$"); + if (decodedParts.length != 3) { + throw new IllegalStateException("cloudId " + cloudId + " did not contain the correct number of parts"); + } + + String url = decodedParts[1] + "." + decodedParts[0]; + return builder(new HttpHost(url, 443, "https")); + } + /** * Returns a new {@link RestClientBuilder} to help with {@link RestClient} creation. * Creates a new builder instance and sets the hosts that the client will send requests to. diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java index 1e16d94076a05..5dae857c08fb8 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java @@ -26,8 +26,10 @@ import org.apache.http.message.BasicHeader; import java.io.IOException; +import java.util.Base64; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -159,6 +161,37 @@ public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder reques } } + public void testBuildCloudId() { + String host = "us-east-1.aws.found.io"; + String esId = "elasticsearch"; + String kibanaId = "kibana"; + String toEncode = host + "$" + esId + "$" + kibanaId; + String encodedId = Base64.getEncoder().encodeToString(toEncode.getBytes()); + assertNotNull(RestClient.builder(encodedId)); + assertNotNull(RestClient.builder("humanReadable:" + encodedId)); + + String badId = Base64.getEncoder().encodeToString("foo$bar".getBytes()); + try { + RestClient.builder(badId); + fail("should hae failed"); + } catch (IllegalStateException e) { + assertEquals("cloudId " + badId + " did not contain the correct number of parts", e.getMessage()); + } + + try { + RestClient.builder(badId + ":"); + fail("should hae failed"); + } catch (IllegalStateException e) { + assertEquals("cloudId " + badId + ":" + " is invalid", e.getMessage()); + } + + RestClient client = RestClient.builder(encodedId).build(); + assertThat(client.getNodes().size(), equalTo(1)); + assertThat(client.getNodes().get(0).getHost().getHostName(), equalTo(esId + "." + host)); + assertThat(client.getNodes().get(0).getHost().getPort(), equalTo(443)); + assertThat(client.getNodes().get(0).getHost().getSchemeName(), equalTo("https")); + } + public void testSetPathPrefixNull() { try { RestClient.builder(new HttpHost("localhost", 9200)).setPathPrefix(null); From 081591cc3bcb1b3bd7dfec284c39ac49f694fd89 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Mon, 14 Oct 2019 10:30:13 -0500 Subject: [PATCH 2/3] review suggestions --- .../main/java/org/elasticsearch/client/RestClient.java | 8 +++++--- .../org/elasticsearch/client/RestClientBuilderTests.java | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java index 217ad6cc29b11..8a47bb46eee9d 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java @@ -124,13 +124,15 @@ public class RestClient implements Closeable { * Returns a new {@link RestClientBuilder} to help with {@link RestClient} creation. * Creates a new builder instance and sets the nodes that the client will send requests to. * - * @param cloudId a valid elastic cloud cloudId that will route to a cluster + * @param cloudId a valid elastic cloud cloudId that will route to a cluster. The cloudId is located in + * the user console https://cloud.elastic.co and will resemble a string like the following + * optionalHumanReadableName:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRlbGFzdGljc2VhcmNoJGtpYmFuYQ== */ public static RestClientBuilder builder(String cloudId) { // there is an optional first portion of the cloudId that is a human readable string, but it is not used. if (cloudId.contains(":")) { if (cloudId.indexOf(":") == cloudId.length() - 1) { - throw new IllegalStateException("cloudId " + cloudId + " is invalid"); + throw new IllegalStateException("cloudId " + cloudId + " must begin with a human readable identifier followed by a colon"); } cloudId = cloudId.substring(cloudId.indexOf(":") + 1); } @@ -139,7 +141,7 @@ public static RestClientBuilder builder(String cloudId) { // once decoded the parts are separated by a $ character String[] decodedParts = decoded.split("\\$"); if (decodedParts.length != 3) { - throw new IllegalStateException("cloudId " + cloudId + " did not contain the correct number of parts"); + throw new IllegalStateException("cloudId " + cloudId + " did not decode to a cluster identifier correctly"); } String url = decodedParts[1] + "." + decodedParts[0]; diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java index 5dae857c08fb8..d42e93405ef78 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java @@ -173,16 +173,16 @@ public void testBuildCloudId() { String badId = Base64.getEncoder().encodeToString("foo$bar".getBytes()); try { RestClient.builder(badId); - fail("should hae failed"); + fail("should have failed"); } catch (IllegalStateException e) { - assertEquals("cloudId " + badId + " did not contain the correct number of parts", e.getMessage()); + assertEquals("cloudId " + badId + " did not decode to a cluster identifier correctly", e.getMessage()); } try { RestClient.builder(badId + ":"); - fail("should hae failed"); + fail("should have failed"); } catch (IllegalStateException e) { - assertEquals("cloudId " + badId + ":" + " is invalid", e.getMessage()); + assertEquals("cloudId " + badId + ":" + " must begin with a human readable identifier followed by a colon", e.getMessage()); } RestClient client = RestClient.builder(encodedId).build(); From c0759255ca1e7f3fe7d7d25f9fd6dd5e3ad95f0a Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Mon, 14 Oct 2019 11:12:50 -0500 Subject: [PATCH 3/3] Fix tests --- .../java/org/elasticsearch/client/RestClient.java | 3 ++- .../elasticsearch/client/RestClientBuilderTests.java | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java index 8a47bb46eee9d..b791c9578a599 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java @@ -74,6 +74,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Collections.singletonList; /** @@ -137,7 +138,7 @@ public static RestClientBuilder builder(String cloudId) { cloudId = cloudId.substring(cloudId.indexOf(":") + 1); } - String decoded = new String(Base64.getDecoder().decode(cloudId)); + String decoded = new String(Base64.getDecoder().decode(cloudId), UTF_8); // once decoded the parts are separated by a $ character String[] decodedParts = decoded.split("\\$"); if (decodedParts.length != 3) { diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java index d42e93405ef78..748dbfe284271 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java @@ -161,28 +161,28 @@ public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder reques } } - public void testBuildCloudId() { + public void testBuildCloudId() throws IOException { String host = "us-east-1.aws.found.io"; String esId = "elasticsearch"; String kibanaId = "kibana"; String toEncode = host + "$" + esId + "$" + kibanaId; - String encodedId = Base64.getEncoder().encodeToString(toEncode.getBytes()); + String encodedId = Base64.getEncoder().encodeToString(toEncode.getBytes(UTF8)); assertNotNull(RestClient.builder(encodedId)); assertNotNull(RestClient.builder("humanReadable:" + encodedId)); - String badId = Base64.getEncoder().encodeToString("foo$bar".getBytes()); + String badId = Base64.getEncoder().encodeToString("foo$bar".getBytes(UTF8)); try { RestClient.builder(badId); fail("should have failed"); } catch (IllegalStateException e) { - assertEquals("cloudId " + badId + " did not decode to a cluster identifier correctly", e.getMessage()); + assertEquals("cloudId " + badId + " did not decode to a cluster identifier correctly", e.getMessage()); } try { RestClient.builder(badId + ":"); fail("should have failed"); } catch (IllegalStateException e) { - assertEquals("cloudId " + badId + ":" + " must begin with a human readable identifier followed by a colon", e.getMessage()); + assertEquals("cloudId " + badId + ":" + " must begin with a human readable identifier followed by a colon", e.getMessage()); } RestClient client = RestClient.builder(encodedId).build(); @@ -190,6 +190,7 @@ public void testBuildCloudId() { assertThat(client.getNodes().get(0).getHost().getHostName(), equalTo(esId + "." + host)); assertThat(client.getNodes().get(0).getHost().getPort(), equalTo(443)); assertThat(client.getNodes().get(0).getHost().getSchemeName(), equalTo("https")); + client.close(); } public void testSetPathPrefixNull() {