Skip to content

Commit 32553ef

Browse files
authored
Remove TestXPackTransportClient (#42103)
In preparation for removing the transport client for 8.0, this commit begins removing transport client testing infrastructure. The tests removed here were all testing transport client specific behavior. For the rest, there already exist http specific test methods.
1 parent 77d0c79 commit 32553ef

File tree

9 files changed

+189
-1211
lines changed

9 files changed

+189
-1211
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/TestXPackTransportClient.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

x-pack/plugin/ml/src/test/java/org/elasticsearch/license/MachineLearningLicensingTests.java

Lines changed: 185 additions & 259 deletions
Large diffs are not rendered by default.

x-pack/plugin/security/src/test/java/org/elasticsearch/license/LicensingTests.java

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
import org.elasticsearch.client.RequestOptions;
2020
import org.elasticsearch.client.Response;
2121
import org.elasticsearch.client.ResponseException;
22-
import org.elasticsearch.client.transport.NoNodeAvailableException;
23-
import org.elasticsearch.client.transport.TransportClient;
2422
import org.elasticsearch.common.settings.SecureString;
2523
import org.elasticsearch.common.settings.Settings;
26-
import org.elasticsearch.common.util.concurrent.ThreadContext;
2724
import org.elasticsearch.discovery.DiscoveryModule;
2825
import org.elasticsearch.license.License.OperationMode;
2926
import org.elasticsearch.node.MockNode;
@@ -36,14 +33,8 @@
3633
import org.elasticsearch.test.SecuritySettingsSourceField;
3734
import org.elasticsearch.test.junit.annotations.TestLogging;
3835
import org.elasticsearch.transport.Netty4Plugin;
39-
import org.elasticsearch.transport.Transport;
40-
import org.elasticsearch.xpack.core.TestXPackTransportClient;
4136
import org.elasticsearch.xpack.core.XPackField;
42-
import org.elasticsearch.xpack.core.security.SecurityField;
43-
import org.elasticsearch.xpack.core.security.action.user.PutUserResponse;
44-
import org.elasticsearch.xpack.core.security.authc.support.Hasher;
4537
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
46-
import org.elasticsearch.xpack.core.security.client.SecurityClient;
4738
import org.elasticsearch.xpack.security.LocalStateSecurity;
4839
import org.junit.After;
4940
import org.junit.Before;
@@ -60,7 +51,6 @@
6051
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
6152
import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING;
6253
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
63-
import static org.hamcrest.Matchers.containsString;
6454
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
6555
import static org.hamcrest.Matchers.hasItem;
6656
import static org.hamcrest.Matchers.is;
@@ -156,7 +146,7 @@ public void testEnableDisableBehaviour() throws Exception {
156146
assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
157147

158148
refresh();
159-
final Client client = internalCluster().transportClient();
149+
final Client client = internalCluster().client();
160150

161151
disableLicensing();
162152

@@ -216,57 +206,6 @@ public void testRestAuthenticationByLicenseType() throws Exception {
216206
assertThat(authorizedAuthenticateResponse.getStatusLine().getStatusCode(), is(200));
217207
}
218208

219-
public void testSecurityActionsByLicenseType() throws Exception {
220-
// security actions should not work!
221-
Settings settings = internalCluster().transportClient().settings();
222-
try (TransportClient client = new TestXPackTransportClient(settings, LocalStateSecurity.class)) {
223-
client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress());
224-
new SecurityClient(client).preparePutUser("john", "password".toCharArray(), Hasher.BCRYPT).get();
225-
fail("security actions should not be enabled!");
226-
} catch (ElasticsearchSecurityException e) {
227-
assertThat(e.status(), is(RestStatus.FORBIDDEN));
228-
assertThat(e.getMessage(), containsString("non-compliant"));
229-
}
230-
231-
// enable a license that enables security
232-
License.OperationMode mode = randomFrom(License.OperationMode.GOLD, License.OperationMode.TRIAL,
233-
License.OperationMode.PLATINUM, License.OperationMode.STANDARD, OperationMode.BASIC);
234-
enableLicensing(mode);
235-
// security actions should work!
236-
try (TransportClient client = new TestXPackTransportClient(settings, LocalStateSecurity.class)) {
237-
client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress());
238-
PutUserResponse response = new SecurityClient(client).preparePutUser("john", "password".toCharArray(), Hasher.BCRYPT).get();
239-
assertNotNull(response);
240-
}
241-
}
242-
243-
public void testTransportClientAuthenticationByLicenseType() throws Exception {
244-
Settings.Builder builder = Settings.builder()
245-
.put(internalCluster().transportClient().settings());
246-
// remove user info
247-
builder.remove(SecurityField.USER_SETTING.getKey());
248-
builder.remove(ThreadContext.PREFIX + "." + UsernamePasswordToken.BASIC_AUTH_HEADER);
249-
250-
// basic has no auth
251-
try (TransportClient client = new TestXPackTransportClient(builder.build(), LocalStateSecurity.class)) {
252-
client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress());
253-
assertGreenClusterState(client);
254-
}
255-
256-
// enable a license that enables security
257-
License.OperationMode mode = randomFrom(License.OperationMode.GOLD, License.OperationMode.TRIAL,
258-
License.OperationMode.PLATINUM, License.OperationMode.STANDARD);
259-
enableLicensing(mode);
260-
261-
try (TransportClient client = new TestXPackTransportClient(builder.build(), LocalStateSecurity.class)) {
262-
client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress());
263-
client.admin().cluster().prepareHealth().get();
264-
fail("should not have been able to connect to a node!");
265-
} catch (NoNodeAvailableException e) {
266-
// expected
267-
}
268-
}
269-
270209
public void testNodeJoinWithoutSecurityExplicitlyEnabled() throws Exception {
271210
License.OperationMode mode = randomFrom(License.OperationMode.GOLD, License.OperationMode.PLATINUM, License.OperationMode.STANDARD);
272211
enableLicensing(mode);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RunAsIntegTests.java

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,16 @@
55
*/
66
package org.elasticsearch.xpack.security.authc;
77

8-
import org.elasticsearch.ElasticsearchSecurityException;
9-
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
10-
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
11-
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
128
import org.elasticsearch.client.Request;
139
import org.elasticsearch.client.RequestOptions;
1410
import org.elasticsearch.client.ResponseException;
15-
import org.elasticsearch.client.transport.TransportClient;
16-
import org.elasticsearch.common.settings.SecureString;
17-
import org.elasticsearch.common.settings.Settings;
18-
import org.elasticsearch.common.transport.TransportAddress;
1911
import org.elasticsearch.test.SecurityIntegTestCase;
2012
import org.elasticsearch.test.SecuritySettingsSource;
21-
import org.elasticsearch.test.SecuritySettingsSourceField;
22-
import org.elasticsearch.xpack.core.TestXPackTransportClient;
2313
import org.elasticsearch.xpack.core.security.authc.AuthenticationServiceField;
24-
import org.elasticsearch.xpack.security.LocalStateSecurity;
25-
import org.elasticsearch.xpack.core.security.SecurityField;
2614
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
2715
import org.junit.BeforeClass;
2816

29-
import java.util.Collections;
30-
import java.util.HashMap;
31-
import java.util.List;
32-
import java.util.Map;
33-
3417
import static org.elasticsearch.test.SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING;
35-
import static org.hamcrest.Matchers.containsString;
36-
import static org.hamcrest.Matchers.greaterThan;
3718
import static org.hamcrest.Matchers.is;
3819

3920
public class RunAsIntegTests extends SecurityIntegTestCase {
@@ -86,43 +67,6 @@ protected boolean transportSSLEnabled() {
8667
return false;
8768
}
8869

89-
public void testUserImpersonation() throws Exception {
90-
try (TransportClient client = getTransportClient(Settings.builder()
91-
.put(SecurityField.USER_SETTING.getKey(), TRANSPORT_CLIENT_USER + ":" +
92-
SecuritySettingsSourceField.TEST_PASSWORD).build())) {
93-
//ensure the client can connect
94-
assertBusy(() -> assertThat(client.connectedNodes().size(), greaterThan(0)));
95-
96-
// make sure the client can't get health
97-
try {
98-
client.admin().cluster().prepareHealth().get();
99-
fail("the client user should not have privileges to get the health");
100-
} catch (ElasticsearchSecurityException e) {
101-
assertThat(e.getMessage(), containsString("unauthorized"));
102-
}
103-
104-
// let's run as without authorization
105-
try {
106-
Map<String, String> headers = Collections.singletonMap(AuthenticationServiceField.RUN_AS_USER_HEADER,
107-
SecuritySettingsSource.TEST_USER_NAME);
108-
client.filterWithHeader(headers)
109-
.admin().cluster().prepareHealth().get();
110-
fail("run as should be unauthorized for the transport client user");
111-
} catch (ElasticsearchSecurityException e) {
112-
assertThat(e.getMessage(), containsString("unauthorized"));
113-
assertThat(e.getMessage(), containsString("run as"));
114-
}
115-
116-
Map<String, String> headers = new HashMap<>();
117-
headers.put("Authorization", UsernamePasswordToken.basicAuthHeaderValue(RUN_AS_USER,
118-
new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray())));
119-
headers.put(AuthenticationServiceField.RUN_AS_USER_HEADER, SecuritySettingsSource.TEST_USER_NAME);
120-
// lets set the user
121-
ClusterHealthResponse response = client.filterWithHeader(headers).admin().cluster().prepareHealth().get();
122-
assertThat(response.isTimedOut(), is(false));
123-
}
124-
}
125-
12670
public void testUserImpersonationUsingHttp() throws Exception {
12771
// use the transport client user and try to run as
12872
try {
@@ -156,29 +100,6 @@ public void testUserImpersonationUsingHttp() throws Exception {
156100
getRestClient().performRequest(requestForUserRunAsUser(SecuritySettingsSource.TEST_USER_NAME));
157101
}
158102

159-
public void testEmptyUserImpersonationHeader() throws Exception {
160-
try (TransportClient client = getTransportClient(Settings.builder()
161-
.put(SecurityField.USER_SETTING.getKey(), TRANSPORT_CLIENT_USER + ":"
162-
+ SecuritySettingsSourceField.TEST_PASSWORD).build())) {
163-
//ensure the client can connect
164-
awaitBusy(() -> {
165-
return client.connectedNodes().size() > 0;
166-
});
167-
168-
try {
169-
Map<String, String> headers = new HashMap<>();
170-
headers.put("Authorization", UsernamePasswordToken.basicAuthHeaderValue(RUN_AS_USER,
171-
new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray())));
172-
headers.put(AuthenticationServiceField.RUN_AS_USER_HEADER, "");
173-
174-
client.filterWithHeader(headers).admin().cluster().prepareHealth().get();
175-
fail("run as header should not be allowed to be empty");
176-
} catch (ElasticsearchSecurityException e) {
177-
assertThat(e.getMessage(), containsString("unable to authenticate"));
178-
}
179-
}
180-
}
181-
182103
public void testEmptyHeaderUsingHttp() throws Exception {
183104
try {
184105
getRestClient().performRequest(requestForUserRunAsUser(""));
@@ -188,29 +109,6 @@ public void testEmptyHeaderUsingHttp() throws Exception {
188109
}
189110
}
190111

191-
public void testNonExistentRunAsUser() throws Exception {
192-
try (TransportClient client = getTransportClient(Settings.builder()
193-
.put(SecurityField.USER_SETTING.getKey(), TRANSPORT_CLIENT_USER + ":" +
194-
SecuritySettingsSourceField.TEST_PASSWORD).build())) {
195-
//ensure the client can connect
196-
awaitBusy(() -> {
197-
return client.connectedNodes().size() > 0;
198-
});
199-
200-
try {
201-
Map<String, String> headers = new HashMap<>();
202-
headers.put("Authorization", UsernamePasswordToken.basicAuthHeaderValue(RUN_AS_USER,
203-
new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray())));
204-
headers.put(AuthenticationServiceField.RUN_AS_USER_HEADER, "idontexist");
205-
206-
client.filterWithHeader(headers).admin().cluster().prepareHealth().get();
207-
fail("run as header should not accept non-existent users");
208-
} catch (ElasticsearchSecurityException e) {
209-
assertThat(e.getMessage(), containsString("unauthorized"));
210-
}
211-
}
212-
}
213-
214112
public void testNonExistentRunAsUserUsingHttp() throws Exception {
215113
try {
216114
getRestClient().performRequest(requestForUserRunAsUser("idontexist"));
@@ -228,21 +126,4 @@ private static Request requestForUserRunAsUser(String user) {
228126
request.setOptions(options);
229127
return request;
230128
}
231-
232-
// build our own here to better mimic an actual client...
233-
TransportClient getTransportClient(Settings extraSettings) {
234-
NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().get();
235-
List<NodeInfo> nodes = nodeInfos.getNodes();
236-
assertTrue(nodes.isEmpty() == false);
237-
TransportAddress publishAddress = randomFrom(nodes).getTransport().address().publishAddress();
238-
String clusterName = nodeInfos.getClusterName().value();
239-
240-
Settings settings = Settings.builder()
241-
.put(extraSettings)
242-
.put("cluster.name", clusterName)
243-
.build();
244-
245-
return new TestXPackTransportClient(settings, LocalStateSecurity.class)
246-
.addTransportAddress(publishAddress);
247-
}
248129
}

0 commit comments

Comments
 (0)