Skip to content

Commit

Permalink
Remove transport client from xpack (#42202)
Browse files Browse the repository at this point in the history
This commit removes support for the transport client from xpack.
  • Loading branch information
rjernst authored May 23, 2019
1 parent 274b634 commit 4520e88
Show file tree
Hide file tree
Showing 67 changed files with 699 additions and 1,913 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ allprojects {
"org.elasticsearch.plugin:percolator-client:${version}": ':modules:percolator',
"org.elasticsearch.plugin:rank-eval-client:${version}": ':modules:rank-eval',
// for security example plugins
"org.elasticsearch.plugin:x-pack-core:${version}": ':x-pack:plugin:core',
"org.elasticsearch.client:x-pack-transport:${version}": ':x-pack:transport-client'
"org.elasticsearch.plugin:x-pack-core:${version}": ':x-pack:plugin:core'
]

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ class RestIntegTestTask extends DefaultTask {
runner.ext.nonInputProperties = nonInputProperties

if (System.getProperty("tests.rest.cluster") == null) {
if (System.getProperty("tests.cluster") != null) {
throw new IllegalArgumentException("tests.rest.cluster and tests.cluster must both be null or non-null")
if (System.getProperty("tests.cluster") != null || System.getProperty("tests.clustername") != null) {
throw new IllegalArgumentException("tests.rest.cluster, tests.cluster, and tests.clustername must all be null or non-null")
}
if (usesTestclusters == true) {
ElasticsearchCluster cluster = project.testClusters."${name}"
nonInputProperties.systemProperty('tests.rest.cluster', "${-> cluster.allHttpSocketURI.join(",") }")
nonInputProperties.systemProperty('tests.cluster', "${-> cluster.transportPortURI }")
nonInputProperties.systemProperty('tests.clustername', "${-> cluster.getName() }")
} else {
// we pass all nodes to the rest cluster to allow the clients to round-robin between them
// this is more realistic than just talking to a single node
Expand All @@ -130,6 +131,7 @@ class RestIntegTestTask extends DefaultTask {
// that sets up the test cluster and passes this transport uri instead of http uri. Until then, we pass
// both as separate sysprops
nonInputProperties.systemProperty('tests.cluster', "${-> nodes[0].transportUri()}")
nonInputProperties.systemProperty('tests.clustername', "${-> nodes[0].clusterName}")

// dump errors and warnings from cluster log on failure
TaskExecutionAdapter logDumpListener = new TaskExecutionAdapter() {
Expand All @@ -150,12 +152,13 @@ class RestIntegTestTask extends DefaultTask {
}
}
} else {
if (System.getProperty("tests.cluster") == null) {
throw new IllegalArgumentException("tests.rest.cluster and tests.cluster must both be null or non-null")
if (System.getProperty("tests.cluster") == null || System.getProperty("tests.clustername") == null) {
throw new IllegalArgumentException("tests.rest.cluster, tests.cluster, and tests.clustername must all be null or non-null")
}
// an external cluster was specified and all responsibility for cluster configuration is taken by the user
runner.systemProperty('tests.rest.cluster', System.getProperty("tests.rest.cluster"))
runner.systemProperty('test.cluster', System.getProperty("tests.cluster"))
runner.systemProperty('test.clustername', System.getProperty("tests.clustername"))
}

// copy the rest spec/tests into the test resources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ esplugin {

dependencies {
compileOnly "org.elasticsearch.plugin:x-pack-core:${versions.elasticsearch}"
testCompile "org.elasticsearch.client:x-pack-transport:${versions.elasticsearch}"
testCompile "org.elasticsearch.client:elasticsearch-rest-high-level-client:${versions.elasticsearch}"
}

integTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.security.PutUserRequest;
import org.elasticsearch.client.security.RefreshPolicy;
import org.elasticsearch.client.security.user.User;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.xpack.core.XPackClientPlugin;
import org.elasticsearch.xpack.core.security.authc.support.Hasher;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
import org.elasticsearch.xpack.core.security.client.SecurityClient;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -50,80 +49,81 @@
* an external cluster with the custom authorization plugin installed to validate the functionality
* when running as a plugin
*/
public class CustomAuthorizationEngineIT extends ESIntegTestCase {
public class CustomAuthorizationEngineIT extends ESRestTestCase {

@Override
protected Settings externalClusterClientSettings() {
protected Settings restClientSettings() {
final String token = "Basic " +
Base64.getEncoder().encodeToString(("test_user:x-pack-test-password").getBytes(StandardCharsets.UTF_8));
return Settings.builder()
.put(ThreadContext.PREFIX + ".Authorization", token)
.put(NetworkModule.TRANSPORT_TYPE_KEY, "security4")
.build();
}

@Override
protected Collection<Class<? extends Plugin>> transportClientPlugins() {
return Collections.singleton(XPackClientPlugin.class);
}

public void testClusterAction() throws IOException {
SecurityClient securityClient = new SecurityClient(client());
securityClient.preparePutUser("custom_user", "x-pack-test-password".toCharArray(), Hasher.BCRYPT, "custom_superuser").get();
RestHighLevelClient restClient = new TestRestHighLevelClient();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);

{
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
basicAuthHeaderValue("custom_user", new SecureString("x-pack-test-password".toCharArray())));
Request request = new Request("GET", "_cluster/health");
request.setOptions(options);
Response response = getRestClient().performRequest(request);
Response response = client().performRequest(request);
assertThat(response.getStatusLine().getStatusCode(), is(200));
}

{
securityClient.preparePutUser("custom_user2", "x-pack-test-password".toCharArray(), Hasher.BCRYPT, "not_superuser").get();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user2", List.of("not_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
basicAuthHeaderValue("custom_user2", new SecureString("x-pack-test-password".toCharArray())));
Request request = new Request("GET", "_cluster/health");
request.setOptions(options);
ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403));
}
}

public void testIndexAction() throws IOException {
SecurityClient securityClient = new SecurityClient(client());
securityClient.preparePutUser("custom_user", "x-pack-test-password".toCharArray(), Hasher.BCRYPT, "custom_superuser").get();
RestHighLevelClient restClient = new TestRestHighLevelClient();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);

{
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
basicAuthHeaderValue("custom_user", new SecureString("x-pack-test-password".toCharArray())));
Request request = new Request("PUT", "/index");
request.setOptions(options);
Response response = getRestClient().performRequest(request);
Response response = client().performRequest(request);
assertThat(response.getStatusLine().getStatusCode(), is(200));
}

{
securityClient.preparePutUser("custom_user2", "x-pack-test-password".toCharArray(), Hasher.BCRYPT, "not_superuser").get();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user2", List.of("not_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
basicAuthHeaderValue("custom_user2", new SecureString("x-pack-test-password".toCharArray())));
Request request = new Request("PUT", "/index");
request.setOptions(options);
ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403));
}
}

public void testRunAs() throws IOException {
SecurityClient securityClient = new SecurityClient(client());
securityClient.preparePutUser("custom_user", "x-pack-test-password".toCharArray(), Hasher.BCRYPT, "custom_superuser").get();
securityClient.preparePutUser("custom_user2", "x-pack-test-password".toCharArray(), Hasher.BCRYPT, "custom_superuser").get();
securityClient.preparePutUser("custom_user3", "x-pack-test-password".toCharArray(), Hasher.BCRYPT, "not_superuser").get();
RestHighLevelClient restClient = new TestRestHighLevelClient();
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user2", List.of("custom_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
restClient.security().putUser(PutUserRequest.withPassword(new User("custom_user3", List.of("not_superuser")),
"x-pack-test-password".toCharArray(), true, RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);

{
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
Expand All @@ -132,7 +132,7 @@ public void testRunAs() throws IOException {
options.addHeader("es-security-runas-user", "custom_user2");
Request request = new Request("GET", "/_security/_authenticate");
request.setOptions(options);
Response response = getRestClient().performRequest(request);
Response response = client().performRequest(request);
assertThat(response.getStatusLine().getStatusCode(), is(200));
String responseStr = EntityUtils.toString(response.getEntity());
assertThat(responseStr, containsString("custom_user2"));
Expand All @@ -145,7 +145,7 @@ public void testRunAs() throws IOException {
options.addHeader("es-security-runas-user", "custom_user3");
Request request = new Request("PUT", "/index");
request.setOptions(options);
ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403));
}

Expand All @@ -156,8 +156,14 @@ public void testRunAs() throws IOException {
options.addHeader("es-security-runas-user", "custom_user2");
Request request = new Request("PUT", "/index");
request.setOptions(options);
ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403));
}
}

private class TestRestHighLevelClient extends RestHighLevelClient {
TestRestHighLevelClient() {
super(client(), restClient -> {}, Collections.emptyList());
}
}
}
2 changes: 2 additions & 0 deletions server/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public class Node implements Closeable {
private final Collection<LifecycleComponent> pluginLifecycleComponents;
private final LocalNodeFactory localNodeFactory;
private final NodeService nodeService;
final NamedWriteableRegistry namedWriteableRegistry;

public Node(Environment environment) {
this(environment, Collections.emptyList(), true);
Expand Down Expand Up @@ -589,6 +590,7 @@ protected Node(
this.pluginLifecycleComponents = Collections.unmodifiableList(pluginLifecycleComponents);
client.initialize(injector.getInstance(new Key<Map<Action, TransportAction>>() {}),
() -> clusterService.localNode().getId(), transportService.getRemoteClusterService());
this.namedWriteableRegistry = namedWriteableRegistry;

logger.debug("initializing HTTP handlers ...");
actionModule.initRestHandlers(() -> clusterService.state().nodes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.cluster.MockInternalClusterInfoService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -179,4 +180,8 @@ protected HttpServerTransport newHttpTransport(NetworkModule networkModule) {
protected void configureNodeAndClusterIdStateListener(ClusterService clusterService) {
//do not configure this in tests as this is causing SetOnce to throw exceptions when jvm is used for multiple tests
}

public NamedWriteableRegistry getNamedWriteableRegistry() {
return namedWriteableRegistry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ public abstract class ESIntegTestCase extends ESTestCase {
*/
public static final String TESTS_CLUSTER = "tests.cluster";

/**
* Key used to eventually switch to using an external cluster and provide the cluster name
*/
public static final String TESTS_CLUSTER_NAME = "tests.clustername";

/**
* Key used to retrieve the index random seed from the index settings on a running node.
* The value of this seed can be used to initialize a random context for a specific index.
Expand Down Expand Up @@ -1829,7 +1834,7 @@ protected Settings transportClientSettings() {
return Settings.EMPTY;
}

private ExternalTestCluster buildExternalCluster(String clusterAddresses) throws IOException {
private ExternalTestCluster buildExternalCluster(String clusterAddresses, String clusterName) throws IOException {
String[] stringAddresses = clusterAddresses.split(",");
TransportAddress[] transportAddresses = new TransportAddress[stringAddresses.length];
int i = 0;
Expand All @@ -1838,7 +1843,8 @@ private ExternalTestCluster buildExternalCluster(String clusterAddresses) throws
InetAddress inetAddress = InetAddress.getByName(url.getHost());
transportAddresses[i++] = new TransportAddress(new InetSocketAddress(inetAddress, url.getPort()));
}
return new ExternalTestCluster(createTempDir(), externalClusterClientSettings(), transportClientPlugins(), transportAddresses);
return new ExternalTestCluster(createTempDir(), externalClusterClientSettings(), nodePlugins(), getClientWrapper(), clusterName,
transportAddresses);
}

protected Settings externalClusterClientSettings() {
Expand All @@ -1855,7 +1861,11 @@ protected TestCluster buildTestCluster(Scope scope, long seed) throws IOExceptio
if (scope == Scope.TEST) {
throw new IllegalArgumentException("Cannot run TEST scope test with " + TESTS_CLUSTER);
}
return buildExternalCluster(clusterAddresses);
String clusterName = System.getProperty(TESTS_CLUSTER_NAME);
if (Strings.isNullOrEmpty(clusterName)) {
throw new IllegalArgumentException("External test cluster name must be provided");
}
return buildExternalCluster(clusterAddresses, clusterName);
}

final String nodePrefix;
Expand Down
Loading

0 comments on commit 4520e88

Please sign in to comment.