Add cloudId builder to the HLRC (#47868)

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.
This commit is contained in:
Michael Basnight 2019-10-14 12:46:16 -05:00
parent 170266765b
commit f6f5efe141
2 changed files with 64 additions and 0 deletions

View File

@ -56,6 +56,7 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -73,6 +74,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
/** /**
@ -119,6 +121,34 @@ public class RestClient implements Closeable {
setNodes(nodes); 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. 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 + " must begin with a human readable identifier followed by a colon");
}
cloudId = cloudId.substring(cloudId.indexOf(":") + 1);
}
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) {
throw new IllegalStateException("cloudId " + cloudId + " did not decode to a cluster identifier correctly");
}
String url = decodedParts[1] + "." + decodedParts[0];
return builder(new HttpHost(url, 443, "https"));
}
/** /**
* Returns a new {@link RestClientBuilder} to help with {@link RestClient} creation. * 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. * Creates a new builder instance and sets the hosts that the client will send requests to.

View File

@ -26,8 +26,10 @@ import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import java.io.IOException; import java.io.IOException;
import java.util.Base64;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -159,6 +161,38 @@ public class RestClientBuilderTests extends RestClientTestCase {
} }
} }
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(UTF8));
assertNotNull(RestClient.builder(encodedId));
assertNotNull(RestClient.builder("humanReadable:" + encodedId));
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());
}
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());
}
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"));
client.close();
}
public void testSetPathPrefixNull() { public void testSetPathPrefixNull() {
try { try {
RestClient.builder(new HttpHost("localhost", 9200)).setPathPrefix(null); RestClient.builder(new HttpHost("localhost", 9200)).setPathPrefix(null);