Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudId builder to the HLRC #47868

Merged
merged 4 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions client/rest/src/main/java/org/elasticsearch/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding an example of what a cloudId looks like to the javadoc would help for people who don't know what that is.

*
* @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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new IllegalStateException("cloudId " + cloudId + " is invalid");
throw new IllegalStateException("cloudId " + cloudId + " is invalid, it cannot end with a ':'");

}
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error will be really confusing for users who have no idea that cloudIds contain multiple parts, or that they are even encoded at all. Maybe we can change it to something like:

"clouldId ... is invalid and does not decode to a cluster identifier correctly"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well the cloudID is masked for the user. It would be a copy/paste issue since these are generated and displayed in the UI for a user. But i can change it for sure.

}

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down