Skip to content

Commit

Permalink
Switch x-pack rolling restart to new style Requests (#32339)
Browse files Browse the repository at this point in the history
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:rolling-upgrade*` projects to use
the new versions.
  • Loading branch information
nik9000 authored Aug 1, 2018
1 parent 4b199dd commit 99d9a0a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.elasticsearch.upgrades;

import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;

import java.util.Map;
Expand All @@ -22,15 +23,15 @@ public void testNewClusterHasActiveNonExpiringBasic() throws Exception {
}

private void checkBasicLicense() throws Exception {
Response licenseResponse = client().performRequest("GET", "/_xpack/license");
Response licenseResponse = client().performRequest(new Request("GET", "/_xpack/license"));
Map<String, Object> licenseResponseMap = entityAsMap(licenseResponse);
Map<String, Object> licenseMap = (Map<String, Object>) licenseResponseMap.get("license");
assertEquals("basic", licenseMap.get("type"));
assertEquals("active", licenseMap.get("status"));
}

private void checkNonExpiringBasicLicense() throws Exception {
Response licenseResponse = client().performRequest("GET", "/_xpack/license");
Response licenseResponse = client().performRequest(new Request("GET", "/_xpack/license"));
Map<String, Object> licenseResponseMap = entityAsMap(licenseResponse);
Map<String, Object> licenseMap = (Map<String, Object>) licenseResponseMap.get("license");
assertEquals("basic", licenseMap.get("type"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.elasticsearch.upgrades;

import org.elasticsearch.client.Request;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.test.SecuritySettingsSourceField;
Expand Down Expand Up @@ -75,8 +76,9 @@ public void setupForTests() throws Exception {
boolean success = true;
for (String template : templatesToWaitFor()) {
try {
final boolean exists =
adminClient().performRequest("HEAD", "_template/" + template).getStatusLine().getStatusCode() == 200;
final boolean exists = adminClient()
.performRequest(new Request("HEAD", "_template/" + template))
.getStatusLine().getStatusCode() == 200;
success &= exists;
logger.debug("template [{}] exists [{}]", template, exists);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
*/
package org.elasticsearch.upgrades;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.Booleans;
import org.hamcrest.Matchers;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -52,7 +49,7 @@ private int expectedNumUniqueNodeNameBuckets() throws IOException {
}

private void assertAuditDocsExist() throws Exception {
Response response = client().performRequest("GET", "/.security_audit_log*/doc/_count");
Response response = client().performRequest(new Request("GET", "/.security_audit_log*/doc/_count"));
assertEquals(200, response.getStatusLine().getStatusCode());
Map<String, Object> responseMap = entityAsMap(response);
assertNotNull(responseMap.get("count"));
Expand All @@ -61,19 +58,20 @@ private void assertAuditDocsExist() throws Exception {

private void assertNumUniqueNodeNameBuckets(int numBuckets) throws Exception {
// call API that will hit all nodes
Map<?, ?> nodesResponse = entityAsMap(client().performRequest("GET", "/_nodes/_all/info/version"));
Map<?, ?> nodesResponse = entityAsMap(client().performRequest(new Request("GET", "/_nodes/_all/info/version")));
logger.info("all nodes {}", nodesResponse);

HttpEntity httpEntity = new StringEntity(
Request aggRequest = new Request("GET", "/.security_audit_log*/_search");
aggRequest.setJsonEntity(
"{\n" +
" \"aggs\" : {\n" +
" \"nodes\" : {\n" +
" \"terms\" : { \"field\" : \"node_name\" }\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response aggResponse = client().performRequest("GET", "/.security_audit_log*/_search",
Collections.singletonMap("pretty", "true"), httpEntity);
"}");
aggRequest.addParameter("pretty", "true");
Response aggResponse = client().performRequest(aggRequest);
Map<String, Object> aggResponseMap = entityAsMap(aggResponse);
logger.debug("aggResponse {}", aggResponseMap);
Map<?, ?> aggregations = (Map<?, ?>) aggResponseMap.get("aggregations");
Expand All @@ -89,7 +87,7 @@ private void assertNumUniqueNodeNameBuckets(int numBuckets) throws Exception {
* Has the master been upgraded to the new version?
*/
private boolean masterIsNewVersion() throws IOException {
Map<?, ?> map = entityAsMap(client().performRequest("GET", "/_nodes/_master"));
Map<?, ?> map = entityAsMap(client().performRequest(new Request("GET", "/_nodes/_master")));
map = (Map<?, ?>) map.get("nodes");
assertThat(map.values(), hasSize(1));
map = (Map<?, ?>) map.values().iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,63 @@

import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.test.rest.yaml.ObjectPath;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class TokenBackwardsCompatibilityIT extends AbstractUpgradeTestCase {

public void testGeneratingTokenInOldCluster() throws Exception {
assumeTrue("this test should only run against the old cluster", CLUSTER_TYPE == ClusterType.OLD);
final StringEntity tokenPostBody = new StringEntity("{\n" +
Request createTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
createTokenRequest.setJsonEntity(
"{\n" +
" \"username\": \"test_user\",\n" +
" \"password\": \"x-pack-test-password\",\n" +
" \"grant_type\": \"password\"\n" +
"}", ContentType.APPLICATION_JSON);
Response response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
"}");
Response response = client().performRequest(createTokenRequest);
assertOK(response);
Map<String, Object> responseMap = entityAsMap(response);
String token = (String) responseMap.get("access_token");
assertNotNull(token);
assertTokenWorks(token);

StringEntity oldClusterToken = new StringEntity("{\n" +
Request indexRequest1 = new Request("PUT", "token_backwards_compatibility_it/doc/old_cluster_token1");
indexRequest1.setJsonEntity(
"{\n" +
" \"token\": \"" + token + "\"\n" +
"}", ContentType.APPLICATION_JSON);
Response indexResponse = client().performRequest("PUT", "token_backwards_compatibility_it/doc/old_cluster_token1",
Collections.emptyMap(), oldClusterToken);
assertOK(indexResponse);
"}");
client().performRequest(indexRequest1);

response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
assertOK(response);
Request createSecondTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
createSecondTokenRequest.setEntity(createTokenRequest.getEntity());
response = client().performRequest(createSecondTokenRequest);
responseMap = entityAsMap(response);
token = (String) responseMap.get("access_token");
assertNotNull(token);
assertTokenWorks(token);
oldClusterToken = new StringEntity("{\n" +
Request indexRequest2 = new Request("PUT", "token_backwards_compatibility_it/doc/old_cluster_token2");
indexRequest2.setJsonEntity(
"{\n" +
" \"token\": \"" + token + "\"\n" +
"}", ContentType.APPLICATION_JSON);
indexResponse = client().performRequest("PUT", "token_backwards_compatibility_it/doc/old_cluster_token2",
Collections.emptyMap(), oldClusterToken);
assertOK(indexResponse);
"}");
client().performRequest(indexRequest2);
}

public void testTokenWorksInMixedOrUpgradedCluster() throws Exception {
assumeTrue("this test should only run against the mixed or upgraded cluster",
CLUSTER_TYPE == ClusterType.MIXED || CLUSTER_TYPE == ClusterType.UPGRADED);
Response getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token1");
Response getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token1"));
assertOK(getResponse);
Map<String, Object> source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
assertTokenWorks((String) source.get("token"));
Expand All @@ -71,39 +72,41 @@ public void testTokenWorksInMixedOrUpgradedCluster() throws Exception {
public void testMixedCluster() throws Exception {
assumeTrue("this test should only run against the mixed cluster", CLUSTER_TYPE == ClusterType.MIXED);
assumeTrue("the master must be on the latest version before we can write", isMasterOnLatestVersion());
Response getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token2");
assertOK(getResponse);
Response getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token2"));
Map<String, Object> source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
final String token = (String) source.get("token");
assertTokenWorks(token);

final StringEntity body = new StringEntity("{\"token\": \"" + token + "\"}", ContentType.APPLICATION_JSON);
Response invalidationResponse = client().performRequest("DELETE", "_xpack/security/oauth2/token", Collections.emptyMap(), body);
assertOK(invalidationResponse);
Request invalidateRequest = new Request("DELETE", "_xpack/security/oauth2/token");
invalidateRequest.setJsonEntity("{\"token\": \"" + token + "\"}");
invalidateRequest.addParameter("error_trace", "true");
client().performRequest(invalidateRequest);
assertTokenDoesNotWork(token);

// create token and refresh on version that supports it
final StringEntity tokenPostBody = new StringEntity("{\n" +
Request createTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
createTokenRequest.setJsonEntity(
"{\n" +
" \"username\": \"test_user\",\n" +
" \"password\": \"x-pack-test-password\",\n" +
" \"grant_type\": \"password\"\n" +
"}", ContentType.APPLICATION_JSON);
"}");
try (RestClient client = getRestClientForCurrentVersionNodesOnly()) {
Response response = client.performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
assertOK(response);
Response response = client.performRequest(createTokenRequest);
Map<String, Object> responseMap = entityAsMap(response);
String accessToken = (String) responseMap.get("access_token");
String refreshToken = (String) responseMap.get("refresh_token");
assertNotNull(accessToken);
assertNotNull(refreshToken);
assertTokenWorks(accessToken);

final StringEntity tokenRefresh = new StringEntity("{\n" +
Request tokenRefreshRequest = new Request("POST", "_xpack/security/oauth2/token");
tokenRefreshRequest.setJsonEntity(
"{\n" +
" \"refresh_token\": \"" + refreshToken + "\",\n" +
" \"grant_type\": \"refresh_token\"\n" +
"}", ContentType.APPLICATION_JSON);
response = client.performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenRefresh);
assertOK(response);
"}");
response = client.performRequest(tokenRefreshRequest);
responseMap = entityAsMap(response);
String updatedAccessToken = (String) responseMap.get("access_token");
String updatedRefreshToken = (String) responseMap.get("refresh_token");
Expand All @@ -118,44 +121,46 @@ public void testMixedCluster() throws Exception {

public void testUpgradedCluster() throws Exception {
assumeTrue("this test should only run against the mixed cluster", CLUSTER_TYPE == ClusterType.UPGRADED);
Response getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token2");
Response getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token2"));
assertOK(getResponse);
Map<String, Object> source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
final String token = (String) source.get("token");

// invalidate again since this may not have been invalidated in the mixed cluster
final StringEntity body = new StringEntity("{\"token\": \"" + token + "\"}", ContentType.APPLICATION_JSON);
Response invalidationResponse = client().performRequest("DELETE", "_xpack/security/oauth2/token",
Collections.singletonMap("error_trace", "true"), body);
Request invalidateRequest = new Request("DELETE", "_xpack/security/oauth2/token");
invalidateRequest.setJsonEntity("{\"token\": \"" + token + "\"}");
invalidateRequest.addParameter("error_trace", "true");
Response invalidationResponse = client().performRequest(invalidateRequest);
assertOK(invalidationResponse);
assertTokenDoesNotWork(token);

getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token1");
assertOK(getResponse);
getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token1"));
source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
final String workingToken = (String) source.get("token");
assertTokenWorks(workingToken);

final StringEntity tokenPostBody = new StringEntity("{\n" +
Request getTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
getTokenRequest.setJsonEntity(
"{\n" +
" \"username\": \"test_user\",\n" +
" \"password\": \"x-pack-test-password\",\n" +
" \"grant_type\": \"password\"\n" +
"}", ContentType.APPLICATION_JSON);
Response response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
assertOK(response);
"}");
Response response = client().performRequest(getTokenRequest);
Map<String, Object> responseMap = entityAsMap(response);
String accessToken = (String) responseMap.get("access_token");
String refreshToken = (String) responseMap.get("refresh_token");
assertNotNull(accessToken);
assertNotNull(refreshToken);
assertTokenWorks(accessToken);

final StringEntity tokenRefresh = new StringEntity("{\n" +
Request refreshTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
refreshTokenRequest.setJsonEntity(
"{\n" +
" \"refresh_token\": \"" + refreshToken + "\",\n" +
" \"grant_type\": \"refresh_token\"\n" +
"}", ContentType.APPLICATION_JSON);
response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenRefresh);
assertOK(response);
"}");
response = client().performRequest(refreshTokenRequest);
responseMap = entityAsMap(response);
String updatedAccessToken = (String) responseMap.get("access_token");
String updatedRefreshToken = (String) responseMap.get("refresh_token");
Expand All @@ -168,34 +173,39 @@ public void testUpgradedCluster() throws Exception {
}

private void assertTokenWorks(String token) throws IOException {
Response authenticateResponse = client().performRequest("GET", "_xpack/security/_authenticate", Collections.emptyMap(),
new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token));
Request request = new Request("GET", "_xpack/security/_authenticate");
RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
request.setOptions(options);
Response authenticateResponse = client().performRequest(request);
assertOK(authenticateResponse);
assertEquals("test_user", entityAsMap(authenticateResponse).get("username"));
}

private void assertTokenDoesNotWork(String token) {
ResponseException e = expectThrows(ResponseException.class,
() -> client().performRequest("GET", "_xpack/security/_authenticate", Collections.emptyMap(),
new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token)));
Request request = new Request("GET", "_xpack/security/_authenticate");
RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
request.setOptions(options);
ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertEquals(401, e.getResponse().getStatusLine().getStatusCode());
Response response = e.getResponse();
assertEquals("Bearer realm=\"security\", error=\"invalid_token\", error_description=\"The access token expired\"",
response.getHeader("WWW-Authenticate"));
}

private boolean isMasterOnLatestVersion() throws Exception {
Response response = client().performRequest("GET", "_cluster/state");
Response response = client().performRequest(new Request("GET", "_cluster/state"));
assertOK(response);
final String masterNodeId = ObjectPath.createFromResponse(response).evaluate("master_node");
response = client().performRequest("GET", "_nodes");
response = client().performRequest(new Request("GET", "_nodes"));
assertOK(response);
ObjectPath objectPath = ObjectPath.createFromResponse(response);
return Version.CURRENT.equals(Version.fromString(objectPath.evaluate("nodes." + masterNodeId + ".version")));
}

private RestClient getRestClientForCurrentVersionNodesOnly() throws IOException {
Response response = client().performRequest("GET", "_nodes");
Response response = client().performRequest(new Request("GET", "_nodes"));
assertOK(response);
ObjectPath objectPath = ObjectPath.createFromResponse(response);
Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");
Expand Down

0 comments on commit 99d9a0a

Please sign in to comment.