Skip to content

Commit 1e9144d

Browse files
authored
Switch remaining x-pack tests to new style Requests (elastic#33108)
In elastic#29623 we added `Request` object flavored requests to the low level REST client and in elastic#30315 we deprecated the old `performRequest`s. This changes all calls in the `x-pack/qa/saml-idp-tests` and `x-pack/qa/security-setup-password-tests` projects to use the new versions.
1 parent dd4a8dc commit 1e9144d

File tree

3 files changed

+50
-46
lines changed

3 files changed

+50
-46
lines changed

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ public void testMonitoringBulk() throws Exception {
120120

121121
// REST is the realistic way that these operations happen, so it's the most realistic way to integration test it too
122122
// Use Monitoring Bulk API to index 3 documents
123-
//final Response bulkResponse = getRestClient().performRequest("POST", "/_xpack/monitoring/_bulk",
124-
// parameters, createBulkEntity());
123+
//final Request bulkRequest = new Request("POST", "/_xpack/monitoring/_bulk");
124+
//<<add all parameters>
125+
//bulkRequest.setJsonEntity(createBulkEntity());
126+
//final Response bulkResponse = getRestClient().performRequest(request);
125127

126128
final MonitoringBulkResponse bulkResponse =
127129
new MonitoringBulkRequestBuilder(client())

x-pack/qa/saml-idp-tests/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticationIT.java

+38-38
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import org.apache.http.cookie.Cookie;
2525
import org.apache.http.cookie.CookieOrigin;
2626
import org.apache.http.cookie.MalformedCookieException;
27-
import org.apache.http.entity.ContentType;
28-
import org.apache.http.entity.StringEntity;
2927
import org.apache.http.impl.client.CloseableHttpClient;
3028
import org.apache.http.impl.client.HttpClients;
3129
import org.apache.http.impl.cookie.DefaultCookieSpec;
@@ -39,6 +37,8 @@
3937
import org.apache.logging.log4j.message.ParameterizedMessage;
4038
import org.elasticsearch.ElasticsearchException;
4139
import org.elasticsearch.cli.SuppressForbidden;
40+
import org.elasticsearch.client.Request;
41+
import org.elasticsearch.client.RequestOptions;
4242
import org.elasticsearch.client.Response;
4343
import org.elasticsearch.common.CheckedFunction;
4444
import org.elasticsearch.common.Strings;
@@ -85,7 +85,6 @@
8585
import java.util.regex.Matcher;
8686
import java.util.regex.Pattern;
8787

88-
import static java.util.Collections.emptyMap;
8988
import static org.elasticsearch.common.xcontent.XContentHelper.convertToMap;
9089
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
9190
import static org.hamcrest.Matchers.contains;
@@ -176,9 +175,9 @@ protected Settings restAdminSettings() {
176175
*/
177176
@Before
178177
public void setKibanaPassword() throws IOException {
179-
final HttpEntity json = new StringEntity("{ \"password\" : \"" + KIBANA_PASSWORD + "\" }", ContentType.APPLICATION_JSON);
180-
final Response response = adminClient().performRequest("PUT", "/_xpack/security/user/kibana/_password", emptyMap(), json);
181-
assertOK(response);
178+
Request request = new Request("PUT", "/_xpack/security/user/kibana/_password");
179+
request.setJsonEntity("{ \"password\" : \"" + KIBANA_PASSWORD + "\" }");
180+
adminClient().performRequest(request);
182181
}
183182

184183
/**
@@ -188,21 +187,19 @@ public void setKibanaPassword() throws IOException {
188187
*/
189188
@Before
190189
public void setupRoleMapping() throws IOException {
191-
final StringEntity json = new StringEntity(Strings // top-level
192-
.toString(XContentBuilder.builder(XContentType.JSON.xContent())
193-
.startObject()
194-
.array("roles", new String[] { "kibana_user"} )
195-
.field("enabled", true)
196-
.startObject("rules")
190+
Request request = new Request("PUT", "/_xpack/security/role_mapping/thor-kibana");
191+
request.setJsonEntity(Strings.toString(XContentBuilder.builder(XContentType.JSON.xContent())
192+
.startObject()
193+
.array("roles", new String[] { "kibana_user"} )
194+
.field("enabled", true)
195+
.startObject("rules")
197196
.startArray("all")
198-
.startObject().startObject("field").field("username", "thor").endObject().endObject()
199-
.startObject().startObject("field").field("realm.name", "shibboleth").endObject().endObject()
197+
.startObject().startObject("field").field("username", "thor").endObject().endObject()
198+
.startObject().startObject("field").field("realm.name", "shibboleth").endObject().endObject()
200199
.endArray() // "all"
201-
.endObject() // "rules"
202-
.endObject()), ContentType.APPLICATION_JSON);
203-
204-
final Response response = adminClient().performRequest("PUT", "/_xpack/security/role_mapping/thor-kibana", emptyMap(), json);
205-
assertOK(response);
200+
.endObject() // "rules"
201+
.endObject()));
202+
adminClient().performRequest(request);
206203
}
207204

208205
/**
@@ -251,10 +248,11 @@ public void testLoginUser() throws Exception {
251248
* is for the expected user with the expected name and roles.
252249
*/
253250
private void verifyElasticsearchAccessToken(String accessToken) throws IOException {
254-
final BasicHeader authorization = new BasicHeader("Authorization", "Bearer " + accessToken);
255-
final Response response = client().performRequest("GET", "/_xpack/security/_authenticate", authorization);
256-
assertOK(response);
257-
final Map<String, Object> map = parseResponseAsMap(response.getEntity());
251+
Request request = new Request("GET", "/_xpack/security/_authenticate");
252+
RequestOptions.Builder options = request.getOptions().toBuilder();
253+
options.addHeader("Authorization", "Bearer " + accessToken);
254+
request.setOptions(options);
255+
final Map<String, Object> map = entityAsMap(client().performRequest(request));
258256
assertThat(map.get("username"), equalTo("thor"));
259257
assertThat(map.get("full_name"), equalTo("Thor Odinson"));
260258
assertSingletonList(map.get("roles"), "kibana_user");
@@ -272,12 +270,11 @@ private void verifyElasticsearchAccessToken(String accessToken) throws IOExcepti
272270
* can be used to get a new valid access token and refresh token.
273271
*/
274272
private void verifyElasticsearchRefreshToken(String refreshToken) throws IOException {
275-
final String body = "{ \"grant_type\":\"refresh_token\", \"refresh_token\":\"" + refreshToken + "\" }";
276-
final Response response = client().performRequest("POST", "/_xpack/security/oauth2/token",
277-
emptyMap(), new StringEntity(body, ContentType.APPLICATION_JSON), kibanaAuth());
278-
assertOK(response);
273+
Request request = new Request("POST", "/_xpack/security/oauth2/token");
274+
request.setJsonEntity("{ \"grant_type\":\"refresh_token\", \"refresh_token\":\"" + refreshToken + "\" }");
275+
kibanaAuth(request);
279276

280-
final Map<String, Object> result = parseResponseAsMap(response.getEntity());
277+
final Map<String, Object> result = entityAsMap(client().performRequest(request));
281278
final Object newRefreshToken = result.get("refresh_token");
282279
assertThat(newRefreshToken, notNullValue());
283280
assertThat(newRefreshToken, instanceOf(String.class));
@@ -463,10 +460,10 @@ private String getUrl(String path) {
463460
* sends a redirect to that page.
464461
*/
465462
private void httpLogin(HttpExchange http) throws IOException {
466-
final Response prepare = client().performRequest("POST", "/_xpack/security/saml/prepare",
467-
emptyMap(), new StringEntity("{}", ContentType.APPLICATION_JSON), kibanaAuth());
468-
assertOK(prepare);
469-
final Map<String, Object> body = parseResponseAsMap(prepare.getEntity());
463+
Request request = new Request("POST", "/_xpack/security/saml/prepare");
464+
request.setJsonEntity("{}");
465+
kibanaAuth(request);
466+
final Map<String, Object> body = entityAsMap(client().performRequest(request));
470467
logger.info("Created SAML authentication request {}", body);
471468
http.getResponseHeaders().add("Set-Cookie", REQUEST_ID_COOKIE + "=" + body.get("id"));
472469
http.getResponseHeaders().add("Location", (String) body.get("redirect"));
@@ -504,9 +501,10 @@ private Response samlAuthenticate(HttpExchange http) throws IOException {
504501
final String id = getCookie(REQUEST_ID_COOKIE, http);
505502
assertThat(id, notNullValue());
506503

507-
final String body = "{ \"content\" : \"" + saml + "\", \"ids\": [\"" + id + "\"] }";
508-
return client().performRequest("POST", "/_xpack/security/saml/authenticate",
509-
emptyMap(), new StringEntity(body, ContentType.APPLICATION_JSON), kibanaAuth());
504+
Request request = new Request("POST", "/_xpack/security/saml/authenticate");
505+
request.setJsonEntity("{ \"content\" : \"" + saml + "\", \"ids\": [\"" + id + "\"] }");
506+
kibanaAuth(request);
507+
return client().performRequest(request);
510508
}
511509

512510
private List<NameValuePair> parseRequestForm(HttpExchange http) throws IOException {
@@ -542,9 +540,11 @@ private static void assertSingletonList(Object value, String expectedElement) {
542540
assertThat(((List<?>) value), contains(expectedElement));
543541
}
544542

545-
private static BasicHeader kibanaAuth() {
546-
final String auth = UsernamePasswordToken.basicAuthHeaderValue("kibana", new SecureString(KIBANA_PASSWORD.toCharArray()));
547-
return new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, auth);
543+
private static void kibanaAuth(Request request) {
544+
RequestOptions.Builder options = request.getOptions().toBuilder();
545+
options.addHeader("Authorization",
546+
UsernamePasswordToken.basicAuthHeaderValue("kibana", new SecureString(KIBANA_PASSWORD.toCharArray())));
547+
request.setOptions(options);
548548
}
549549

550550
private CloseableHttpClient getHttpClient() throws Exception {

x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolIT.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
*/
66
package org.elasticsearch.xpack.security.authc.esnative.tool;
77

8-
import org.apache.http.message.BasicHeader;
98
import org.elasticsearch.cli.MockTerminal;
9+
import org.elasticsearch.client.Request;
10+
import org.elasticsearch.client.RequestOptions;
1011
import org.elasticsearch.client.Response;
1112
import org.elasticsearch.common.Strings;
1213
import org.elasticsearch.common.SuppressForbidden;
@@ -52,7 +53,7 @@ public void testSetupPasswordToolAutoSetup() throws Exception {
5253
final Path configPath = PathUtils.get(testConfigDir);
5354
setSystemPropsForTool(configPath);
5455

55-
Response nodesResponse = client().performRequest("GET", "/_nodes/http");
56+
Response nodesResponse = client().performRequest(new Request("GET", "/_nodes/http"));
5657
Map<String, Object> nodesMap = entityAsMap(nodesResponse);
5758

5859
Map<String,Object> nodes = (Map<String,Object>) nodesMap.get("nodes");
@@ -102,10 +103,11 @@ public void testSetupPasswordToolAutoSetup() throws Exception {
102103
final String basicHeader = "Basic " +
103104
Base64.getEncoder().encodeToString((entry.getKey() + ":" + entry.getValue()).getBytes(StandardCharsets.UTF_8));
104105
try {
105-
Response authenticateResponse = client().performRequest("GET", "/_xpack/security/_authenticate",
106-
new BasicHeader("Authorization", basicHeader));
107-
assertEquals(200, authenticateResponse.getStatusLine().getStatusCode());
108-
Map<String, Object> userInfoMap = entityAsMap(authenticateResponse);
106+
Request request = new Request("GET", "/_xpack/security/_authenticate");
107+
RequestOptions.Builder options = request.getOptions().toBuilder();
108+
options.addHeader("Authorization", basicHeader);
109+
request.setOptions(options);
110+
Map<String, Object> userInfoMap = entityAsMap(client().performRequest(request));
109111
assertEquals(entry.getKey(), userInfoMap.get("username"));
110112
} catch (IOException e) {
111113
throw new UncheckedIOException(e);

0 commit comments

Comments
 (0)