Switch remaining x-pack tests to new style Requests (#33108)
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/saml-idp-tests` and `x-pack/qa/security-setup-password-tests` projects to use the new versions.
This commit is contained in:
parent
dd4a8dc444
commit
1e9144d8e6
|
@ -120,8 +120,10 @@ public class MonitoringIT extends ESSingleNodeTestCase {
|
||||||
|
|
||||||
// REST is the realistic way that these operations happen, so it's the most realistic way to integration test it too
|
// REST is the realistic way that these operations happen, so it's the most realistic way to integration test it too
|
||||||
// Use Monitoring Bulk API to index 3 documents
|
// Use Monitoring Bulk API to index 3 documents
|
||||||
//final Response bulkResponse = getRestClient().performRequest("POST", "/_xpack/monitoring/_bulk",
|
//final Request bulkRequest = new Request("POST", "/_xpack/monitoring/_bulk");
|
||||||
// parameters, createBulkEntity());
|
//<<add all parameters>
|
||||||
|
//bulkRequest.setJsonEntity(createBulkEntity());
|
||||||
|
//final Response bulkResponse = getRestClient().performRequest(request);
|
||||||
|
|
||||||
final MonitoringBulkResponse bulkResponse =
|
final MonitoringBulkResponse bulkResponse =
|
||||||
new MonitoringBulkRequestBuilder(client())
|
new MonitoringBulkRequestBuilder(client())
|
||||||
|
|
|
@ -24,8 +24,6 @@ import org.apache.http.client.utils.URLEncodedUtils;
|
||||||
import org.apache.http.cookie.Cookie;
|
import org.apache.http.cookie.Cookie;
|
||||||
import org.apache.http.cookie.CookieOrigin;
|
import org.apache.http.cookie.CookieOrigin;
|
||||||
import org.apache.http.cookie.MalformedCookieException;
|
import org.apache.http.cookie.MalformedCookieException;
|
||||||
import org.apache.http.entity.ContentType;
|
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.impl.cookie.DefaultCookieSpec;
|
import org.apache.http.impl.cookie.DefaultCookieSpec;
|
||||||
|
@ -39,6 +37,8 @@ import org.apache.http.util.CharArrayBuffer;
|
||||||
import org.apache.logging.log4j.message.ParameterizedMessage;
|
import org.apache.logging.log4j.message.ParameterizedMessage;
|
||||||
import org.elasticsearch.ElasticsearchException;
|
import org.elasticsearch.ElasticsearchException;
|
||||||
import org.elasticsearch.cli.SuppressForbidden;
|
import org.elasticsearch.cli.SuppressForbidden;
|
||||||
|
import org.elasticsearch.client.Request;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
import org.elasticsearch.client.Response;
|
import org.elasticsearch.client.Response;
|
||||||
import org.elasticsearch.common.CheckedFunction;
|
import org.elasticsearch.common.CheckedFunction;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
|
@ -85,7 +85,6 @@ import java.util.concurrent.ExecutorService;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static java.util.Collections.emptyMap;
|
|
||||||
import static org.elasticsearch.common.xcontent.XContentHelper.convertToMap;
|
import static org.elasticsearch.common.xcontent.XContentHelper.convertToMap;
|
||||||
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
|
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
|
||||||
import static org.hamcrest.Matchers.contains;
|
import static org.hamcrest.Matchers.contains;
|
||||||
|
@ -176,9 +175,9 @@ public class SamlAuthenticationIT extends ESRestTestCase {
|
||||||
*/
|
*/
|
||||||
@Before
|
@Before
|
||||||
public void setKibanaPassword() throws IOException {
|
public void setKibanaPassword() throws IOException {
|
||||||
final HttpEntity json = new StringEntity("{ \"password\" : \"" + KIBANA_PASSWORD + "\" }", ContentType.APPLICATION_JSON);
|
Request request = new Request("PUT", "/_xpack/security/user/kibana/_password");
|
||||||
final Response response = adminClient().performRequest("PUT", "/_xpack/security/user/kibana/_password", emptyMap(), json);
|
request.setJsonEntity("{ \"password\" : \"" + KIBANA_PASSWORD + "\" }");
|
||||||
assertOK(response);
|
adminClient().performRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -188,21 +187,19 @@ public class SamlAuthenticationIT extends ESRestTestCase {
|
||||||
*/
|
*/
|
||||||
@Before
|
@Before
|
||||||
public void setupRoleMapping() throws IOException {
|
public void setupRoleMapping() throws IOException {
|
||||||
final StringEntity json = new StringEntity(Strings // top-level
|
Request request = new Request("PUT", "/_xpack/security/role_mapping/thor-kibana");
|
||||||
.toString(XContentBuilder.builder(XContentType.JSON.xContent())
|
request.setJsonEntity(Strings.toString(XContentBuilder.builder(XContentType.JSON.xContent())
|
||||||
.startObject()
|
.startObject()
|
||||||
.array("roles", new String[] { "kibana_user"} )
|
.array("roles", new String[] { "kibana_user"} )
|
||||||
.field("enabled", true)
|
.field("enabled", true)
|
||||||
.startObject("rules")
|
.startObject("rules")
|
||||||
.startArray("all")
|
.startArray("all")
|
||||||
.startObject().startObject("field").field("username", "thor").endObject().endObject()
|
.startObject().startObject("field").field("username", "thor").endObject().endObject()
|
||||||
.startObject().startObject("field").field("realm.name", "shibboleth").endObject().endObject()
|
.startObject().startObject("field").field("realm.name", "shibboleth").endObject().endObject()
|
||||||
.endArray() // "all"
|
.endArray() // "all"
|
||||||
.endObject() // "rules"
|
.endObject() // "rules"
|
||||||
.endObject()), ContentType.APPLICATION_JSON);
|
.endObject()));
|
||||||
|
adminClient().performRequest(request);
|
||||||
final Response response = adminClient().performRequest("PUT", "/_xpack/security/role_mapping/thor-kibana", emptyMap(), json);
|
|
||||||
assertOK(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -251,10 +248,11 @@ public class SamlAuthenticationIT extends ESRestTestCase {
|
||||||
* is for the expected user with the expected name and roles.
|
* is for the expected user with the expected name and roles.
|
||||||
*/
|
*/
|
||||||
private void verifyElasticsearchAccessToken(String accessToken) throws IOException {
|
private void verifyElasticsearchAccessToken(String accessToken) throws IOException {
|
||||||
final BasicHeader authorization = new BasicHeader("Authorization", "Bearer " + accessToken);
|
Request request = new Request("GET", "/_xpack/security/_authenticate");
|
||||||
final Response response = client().performRequest("GET", "/_xpack/security/_authenticate", authorization);
|
RequestOptions.Builder options = request.getOptions().toBuilder();
|
||||||
assertOK(response);
|
options.addHeader("Authorization", "Bearer " + accessToken);
|
||||||
final Map<String, Object> map = parseResponseAsMap(response.getEntity());
|
request.setOptions(options);
|
||||||
|
final Map<String, Object> map = entityAsMap(client().performRequest(request));
|
||||||
assertThat(map.get("username"), equalTo("thor"));
|
assertThat(map.get("username"), equalTo("thor"));
|
||||||
assertThat(map.get("full_name"), equalTo("Thor Odinson"));
|
assertThat(map.get("full_name"), equalTo("Thor Odinson"));
|
||||||
assertSingletonList(map.get("roles"), "kibana_user");
|
assertSingletonList(map.get("roles"), "kibana_user");
|
||||||
|
@ -272,12 +270,11 @@ public class SamlAuthenticationIT extends ESRestTestCase {
|
||||||
* can be used to get a new valid access token and refresh token.
|
* can be used to get a new valid access token and refresh token.
|
||||||
*/
|
*/
|
||||||
private void verifyElasticsearchRefreshToken(String refreshToken) throws IOException {
|
private void verifyElasticsearchRefreshToken(String refreshToken) throws IOException {
|
||||||
final String body = "{ \"grant_type\":\"refresh_token\", \"refresh_token\":\"" + refreshToken + "\" }";
|
Request request = new Request("POST", "/_xpack/security/oauth2/token");
|
||||||
final Response response = client().performRequest("POST", "/_xpack/security/oauth2/token",
|
request.setJsonEntity("{ \"grant_type\":\"refresh_token\", \"refresh_token\":\"" + refreshToken + "\" }");
|
||||||
emptyMap(), new StringEntity(body, ContentType.APPLICATION_JSON), kibanaAuth());
|
kibanaAuth(request);
|
||||||
assertOK(response);
|
|
||||||
|
|
||||||
final Map<String, Object> result = parseResponseAsMap(response.getEntity());
|
final Map<String, Object> result = entityAsMap(client().performRequest(request));
|
||||||
final Object newRefreshToken = result.get("refresh_token");
|
final Object newRefreshToken = result.get("refresh_token");
|
||||||
assertThat(newRefreshToken, notNullValue());
|
assertThat(newRefreshToken, notNullValue());
|
||||||
assertThat(newRefreshToken, instanceOf(String.class));
|
assertThat(newRefreshToken, instanceOf(String.class));
|
||||||
|
@ -463,10 +460,10 @@ public class SamlAuthenticationIT extends ESRestTestCase {
|
||||||
* sends a redirect to that page.
|
* sends a redirect to that page.
|
||||||
*/
|
*/
|
||||||
private void httpLogin(HttpExchange http) throws IOException {
|
private void httpLogin(HttpExchange http) throws IOException {
|
||||||
final Response prepare = client().performRequest("POST", "/_xpack/security/saml/prepare",
|
Request request = new Request("POST", "/_xpack/security/saml/prepare");
|
||||||
emptyMap(), new StringEntity("{}", ContentType.APPLICATION_JSON), kibanaAuth());
|
request.setJsonEntity("{}");
|
||||||
assertOK(prepare);
|
kibanaAuth(request);
|
||||||
final Map<String, Object> body = parseResponseAsMap(prepare.getEntity());
|
final Map<String, Object> body = entityAsMap(client().performRequest(request));
|
||||||
logger.info("Created SAML authentication request {}", body);
|
logger.info("Created SAML authentication request {}", body);
|
||||||
http.getResponseHeaders().add("Set-Cookie", REQUEST_ID_COOKIE + "=" + body.get("id"));
|
http.getResponseHeaders().add("Set-Cookie", REQUEST_ID_COOKIE + "=" + body.get("id"));
|
||||||
http.getResponseHeaders().add("Location", (String) body.get("redirect"));
|
http.getResponseHeaders().add("Location", (String) body.get("redirect"));
|
||||||
|
@ -504,9 +501,10 @@ public class SamlAuthenticationIT extends ESRestTestCase {
|
||||||
final String id = getCookie(REQUEST_ID_COOKIE, http);
|
final String id = getCookie(REQUEST_ID_COOKIE, http);
|
||||||
assertThat(id, notNullValue());
|
assertThat(id, notNullValue());
|
||||||
|
|
||||||
final String body = "{ \"content\" : \"" + saml + "\", \"ids\": [\"" + id + "\"] }";
|
Request request = new Request("POST", "/_xpack/security/saml/authenticate");
|
||||||
return client().performRequest("POST", "/_xpack/security/saml/authenticate",
|
request.setJsonEntity("{ \"content\" : \"" + saml + "\", \"ids\": [\"" + id + "\"] }");
|
||||||
emptyMap(), new StringEntity(body, ContentType.APPLICATION_JSON), kibanaAuth());
|
kibanaAuth(request);
|
||||||
|
return client().performRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<NameValuePair> parseRequestForm(HttpExchange http) throws IOException {
|
private List<NameValuePair> parseRequestForm(HttpExchange http) throws IOException {
|
||||||
|
@ -542,9 +540,11 @@ public class SamlAuthenticationIT extends ESRestTestCase {
|
||||||
assertThat(((List<?>) value), contains(expectedElement));
|
assertThat(((List<?>) value), contains(expectedElement));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BasicHeader kibanaAuth() {
|
private static void kibanaAuth(Request request) {
|
||||||
final String auth = UsernamePasswordToken.basicAuthHeaderValue("kibana", new SecureString(KIBANA_PASSWORD.toCharArray()));
|
RequestOptions.Builder options = request.getOptions().toBuilder();
|
||||||
return new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, auth);
|
options.addHeader("Authorization",
|
||||||
|
UsernamePasswordToken.basicAuthHeaderValue("kibana", new SecureString(KIBANA_PASSWORD.toCharArray())));
|
||||||
|
request.setOptions(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CloseableHttpClient getHttpClient() throws Exception {
|
private CloseableHttpClient getHttpClient() throws Exception {
|
||||||
|
|
|
@ -5,8 +5,9 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.security.authc.esnative.tool;
|
package org.elasticsearch.xpack.security.authc.esnative.tool;
|
||||||
|
|
||||||
import org.apache.http.message.BasicHeader;
|
|
||||||
import org.elasticsearch.cli.MockTerminal;
|
import org.elasticsearch.cli.MockTerminal;
|
||||||
|
import org.elasticsearch.client.Request;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
import org.elasticsearch.client.Response;
|
import org.elasticsearch.client.Response;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.SuppressForbidden;
|
import org.elasticsearch.common.SuppressForbidden;
|
||||||
|
@ -52,7 +53,7 @@ public class SetupPasswordToolIT extends ESRestTestCase {
|
||||||
final Path configPath = PathUtils.get(testConfigDir);
|
final Path configPath = PathUtils.get(testConfigDir);
|
||||||
setSystemPropsForTool(configPath);
|
setSystemPropsForTool(configPath);
|
||||||
|
|
||||||
Response nodesResponse = client().performRequest("GET", "/_nodes/http");
|
Response nodesResponse = client().performRequest(new Request("GET", "/_nodes/http"));
|
||||||
Map<String, Object> nodesMap = entityAsMap(nodesResponse);
|
Map<String, Object> nodesMap = entityAsMap(nodesResponse);
|
||||||
|
|
||||||
Map<String,Object> nodes = (Map<String,Object>) nodesMap.get("nodes");
|
Map<String,Object> nodes = (Map<String,Object>) nodesMap.get("nodes");
|
||||||
|
@ -102,10 +103,11 @@ public class SetupPasswordToolIT extends ESRestTestCase {
|
||||||
final String basicHeader = "Basic " +
|
final String basicHeader = "Basic " +
|
||||||
Base64.getEncoder().encodeToString((entry.getKey() + ":" + entry.getValue()).getBytes(StandardCharsets.UTF_8));
|
Base64.getEncoder().encodeToString((entry.getKey() + ":" + entry.getValue()).getBytes(StandardCharsets.UTF_8));
|
||||||
try {
|
try {
|
||||||
Response authenticateResponse = client().performRequest("GET", "/_xpack/security/_authenticate",
|
Request request = new Request("GET", "/_xpack/security/_authenticate");
|
||||||
new BasicHeader("Authorization", basicHeader));
|
RequestOptions.Builder options = request.getOptions().toBuilder();
|
||||||
assertEquals(200, authenticateResponse.getStatusLine().getStatusCode());
|
options.addHeader("Authorization", basicHeader);
|
||||||
Map<String, Object> userInfoMap = entityAsMap(authenticateResponse);
|
request.setOptions(options);
|
||||||
|
Map<String, Object> userInfoMap = entityAsMap(client().performRequest(request));
|
||||||
assertEquals(entry.getKey(), userInfoMap.get("username"));
|
assertEquals(entry.getKey(), userInfoMap.get("username"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
|
|
Loading…
Reference in New Issue