Skip to content

Commit 0e3c799

Browse files
committed
Switch security spi example to new style Requests (#32341)
In #29623 we added `Request` object flavored requests to the low level REST client and in #30315 we deprecated the old `performRequest`s. This changes all calls in the `x-pack/qa/security-example-spi-extension` project to use the new versions.
1 parent c3ae974 commit 0e3c799

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRealmIT.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
*/
66
package org.elasticsearch.example.realm;
77

8-
import org.apache.http.message.BasicHeader;
98
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
109
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
1110
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
11+
import org.elasticsearch.client.Request;
12+
import org.elasticsearch.client.RequestOptions;
1213
import org.elasticsearch.client.Response;
1314
import org.elasticsearch.client.ResponseException;
1415
import org.elasticsearch.client.transport.NoNodeAvailableException;
@@ -50,7 +51,7 @@ protected Collection<Class<? extends Plugin>> transportClientPlugins() {
5051

5152
public void testHttpConnectionWithNoAuthentication() throws Exception {
5253
try {
53-
getRestClient().performRequest("GET", "/");
54+
getRestClient().performRequest(new Request("GET", "/"));
5455
fail("request should have failed");
5556
} catch(ResponseException e) {
5657
Response response = e.getResponse();
@@ -61,9 +62,12 @@ public void testHttpConnectionWithNoAuthentication() throws Exception {
6162
}
6263

6364
public void testHttpAuthentication() throws Exception {
64-
Response response = getRestClient().performRequest("GET", "/",
65-
new BasicHeader(CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER),
66-
new BasicHeader(CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW.toString()));
65+
Request request = new Request("GET", "/");
66+
RequestOptions.Builder options = request.getOptions().toBuilder();
67+
options.addHeader(CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER);
68+
options.addHeader(CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW.toString());
69+
request.setOptions(options);
70+
Response response = getRestClient().performRequest(request);
6771
assertThat(response.getStatusLine().getStatusCode(), is(200));
6872
}
6973

x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/role/CustomRolesProviderIT.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66
package org.elasticsearch.example.role;
77

8-
import org.apache.http.message.BasicHeader;
8+
import org.elasticsearch.client.Request;
9+
import org.elasticsearch.client.RequestOptions;
910
import org.elasticsearch.client.Response;
1011
import org.elasticsearch.client.ResponseException;
1112
import org.elasticsearch.common.network.NetworkModule;
@@ -33,10 +34,17 @@
3334
* Integration test for custom roles providers.
3435
*/
3536
public class CustomRolesProviderIT extends ESIntegTestCase {
36-
3737
private static final String TEST_USER = "test_user";
3838
private static final String TEST_PWD = "change_me";
3939

40+
private static final RequestOptions AUTH_OPTIONS;
41+
static {
42+
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
43+
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
44+
basicAuthHeaderValue(TEST_USER, new SecureString(TEST_PWD.toCharArray())));
45+
AUTH_OPTIONS = options.build();
46+
}
47+
4048
@Override
4149
protected Settings externalClusterClientSettings() {
4250
return Settings.builder()
@@ -59,7 +67,9 @@ public void setupTestUser(String role) {
5967
public void testAuthorizedCustomRoleSucceeds() throws Exception {
6068
setupTestUser(ROLE_B);
6169
// roleB has all permissions on index "foo", so creating "foo" should succeed
62-
Response response = getRestClient().performRequest("PUT", "/" + INDEX, authHeader());
70+
Request request = new Request("PUT", "/" + INDEX);
71+
request.setOptions(AUTH_OPTIONS);
72+
Response response = getRestClient().performRequest(request);
6373
assertThat(response.getStatusLine().getStatusCode(), is(200));
6474
}
6575

@@ -71,7 +81,9 @@ public void testFirstResolvedRoleTakesPrecedence() throws Exception {
7181
setupTestUser(ROLE_A);
7282
// roleB has all permissions on index "foo", so creating "foo" should succeed
7383
try {
74-
getRestClient().performRequest("PUT", "/" + INDEX, authHeader());
84+
Request request = new Request("PUT", "/" + INDEX);
85+
request.setOptions(AUTH_OPTIONS);
86+
getRestClient().performRequest(request);
7587
fail(ROLE_A + " should not be authorized to create index " + INDEX);
7688
} catch (ResponseException e) {
7789
assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403));
@@ -82,15 +94,12 @@ public void testUnresolvedRoleDoesntSucceed() throws Exception {
8294
setupTestUser("unknown");
8395
// roleB has all permissions on index "foo", so creating "foo" should succeed
8496
try {
85-
getRestClient().performRequest("PUT", "/" + INDEX, authHeader());
97+
Request request = new Request("PUT", "/" + INDEX);
98+
request.setOptions(AUTH_OPTIONS);
99+
getRestClient().performRequest(request);
86100
fail(ROLE_A + " should not be authorized to create index " + INDEX);
87101
} catch (ResponseException e) {
88102
assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403));
89103
}
90104
}
91-
92-
private BasicHeader authHeader() {
93-
return new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
94-
basicAuthHeaderValue(TEST_USER, new SecureString(TEST_PWD.toCharArray())));
95-
}
96105
}

0 commit comments

Comments
 (0)