Skip to content

Commit 99d9a0a

Browse files
authored
Switch x-pack rolling restart to new style Requests (#32339)
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.
1 parent 4b199dd commit 99d9a0a

File tree

4 files changed

+81
-70
lines changed

4 files changed

+81
-70
lines changed

x-pack/qa/rolling-upgrade-basic/src/test/java/org/elasticsearch/upgrades/BasicLicenseUpgradeIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.upgrades;
77

8+
import org.elasticsearch.client.Request;
89
import org.elasticsearch.client.Response;
910

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

2425
private void checkBasicLicense() throws Exception {
25-
Response licenseResponse = client().performRequest("GET", "/_xpack/license");
26+
Response licenseResponse = client().performRequest(new Request("GET", "/_xpack/license"));
2627
Map<String, Object> licenseResponseMap = entityAsMap(licenseResponse);
2728
Map<String, Object> licenseMap = (Map<String, Object>) licenseResponseMap.get("license");
2829
assertEquals("basic", licenseMap.get("type"));
2930
assertEquals("active", licenseMap.get("status"));
3031
}
3132

3233
private void checkNonExpiringBasicLicense() throws Exception {
33-
Response licenseResponse = client().performRequest("GET", "/_xpack/license");
34+
Response licenseResponse = client().performRequest(new Request("GET", "/_xpack/license"));
3435
Map<String, Object> licenseResponseMap = entityAsMap(licenseResponse);
3536
Map<String, Object> licenseMap = (Map<String, Object>) licenseResponseMap.get("license");
3637
assertEquals("basic", licenseMap.get("type"));

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractUpgradeTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.upgrades;
77

8+
import org.elasticsearch.client.Request;
89
import org.elasticsearch.common.settings.Settings;
910
import org.elasticsearch.common.util.concurrent.ThreadContext;
1011
import org.elasticsearch.test.SecuritySettingsSourceField;
@@ -75,8 +76,9 @@ public void setupForTests() throws Exception {
7576
boolean success = true;
7677
for (String template : templatesToWaitFor()) {
7778
try {
78-
final boolean exists =
79-
adminClient().performRequest("HEAD", "_template/" + template).getStatusLine().getStatusCode() == 200;
79+
final boolean exists = adminClient()
80+
.performRequest(new Request("HEAD", "_template/" + template))
81+
.getStatusLine().getStatusCode() == 200;
8082
success &= exists;
8183
logger.debug("template [{}] exists [{}]", template, exists);
8284
} catch (IOException e) {

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexAuditUpgradeIT.java

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

8-
import org.apache.http.HttpEntity;
9-
import org.apache.http.entity.ContentType;
10-
import org.apache.http.entity.StringEntity;
118
import org.elasticsearch.Version;
9+
import org.elasticsearch.client.Request;
1210
import org.elasticsearch.client.Response;
1311
import org.elasticsearch.common.Booleans;
1412
import org.hamcrest.Matchers;
1513

1614
import java.io.IOException;
17-
import java.util.Collections;
1815
import java.util.List;
1916
import java.util.Map;
2017

@@ -52,7 +49,7 @@ private int expectedNumUniqueNodeNameBuckets() throws IOException {
5249
}
5350

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

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

67-
HttpEntity httpEntity = new StringEntity(
64+
Request aggRequest = new Request("GET", "/.security_audit_log*/_search");
65+
aggRequest.setJsonEntity(
6866
"{\n" +
6967
" \"aggs\" : {\n" +
7068
" \"nodes\" : {\n" +
7169
" \"terms\" : { \"field\" : \"node_name\" }\n" +
7270
" }\n" +
7371
" }\n" +
74-
"}", ContentType.APPLICATION_JSON);
75-
Response aggResponse = client().performRequest("GET", "/.security_audit_log*/_search",
76-
Collections.singletonMap("pretty", "true"), httpEntity);
72+
"}");
73+
aggRequest.addParameter("pretty", "true");
74+
Response aggResponse = client().performRequest(aggRequest);
7775
Map<String, Object> aggResponseMap = entityAsMap(aggResponse);
7876
logger.debug("aggResponse {}", aggResponseMap);
7977
Map<?, ?> aggregations = (Map<?, ?>) aggResponseMap.get("aggregations");
@@ -89,7 +87,7 @@ private void assertNumUniqueNodeNameBuckets(int numBuckets) throws Exception {
8987
* Has the master been upgraded to the new version?
9088
*/
9189
private boolean masterIsNewVersion() throws IOException {
92-
Map<?, ?> map = entityAsMap(client().performRequest("GET", "/_nodes/_master"));
90+
Map<?, ?> map = entityAsMap(client().performRequest(new Request("GET", "/_nodes/_master")));
9391
map = (Map<?, ?>) map.get("nodes");
9492
assertThat(map.values(), hasSize(1));
9593
map = (Map<?, ?>) map.values().iterator().next();

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,63 @@
77

88
import org.apache.http.HttpHeaders;
99
import org.apache.http.HttpHost;
10-
import org.apache.http.entity.ContentType;
11-
import org.apache.http.entity.StringEntity;
12-
import org.apache.http.message.BasicHeader;
1310
import org.elasticsearch.Version;
11+
import org.elasticsearch.client.Request;
12+
import org.elasticsearch.client.RequestOptions;
1413
import org.elasticsearch.client.Response;
1514
import org.elasticsearch.client.ResponseException;
1615
import org.elasticsearch.client.RestClient;
1716
import org.elasticsearch.test.rest.yaml.ObjectPath;
1817

1918
import java.io.IOException;
2019
import java.util.ArrayList;
21-
import java.util.Collections;
2220
import java.util.List;
2321
import java.util.Map;
2422

2523
public class TokenBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
2624

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

41-
StringEntity oldClusterToken = new StringEntity("{\n" +
41+
Request indexRequest1 = new Request("PUT", "token_backwards_compatibility_it/doc/old_cluster_token1");
42+
indexRequest1.setJsonEntity(
43+
"{\n" +
4244
" \"token\": \"" + token + "\"\n" +
43-
"}", ContentType.APPLICATION_JSON);
44-
Response indexResponse = client().performRequest("PUT", "token_backwards_compatibility_it/doc/old_cluster_token1",
45-
Collections.emptyMap(), oldClusterToken);
46-
assertOK(indexResponse);
45+
"}");
46+
client().performRequest(indexRequest1);
4747

48-
response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
49-
assertOK(response);
48+
Request createSecondTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
49+
createSecondTokenRequest.setEntity(createTokenRequest.getEntity());
50+
response = client().performRequest(createSecondTokenRequest);
5051
responseMap = entityAsMap(response);
5152
token = (String) responseMap.get("access_token");
5253
assertNotNull(token);
5354
assertTokenWorks(token);
54-
oldClusterToken = new StringEntity("{\n" +
55+
Request indexRequest2 = new Request("PUT", "token_backwards_compatibility_it/doc/old_cluster_token2");
56+
indexRequest2.setJsonEntity(
57+
"{\n" +
5558
" \"token\": \"" + token + "\"\n" +
56-
"}", ContentType.APPLICATION_JSON);
57-
indexResponse = client().performRequest("PUT", "token_backwards_compatibility_it/doc/old_cluster_token2",
58-
Collections.emptyMap(), oldClusterToken);
59-
assertOK(indexResponse);
59+
"}");
60+
client().performRequest(indexRequest2);
6061
}
6162

6263
public void testTokenWorksInMixedOrUpgradedCluster() throws Exception {
6364
assumeTrue("this test should only run against the mixed or upgraded cluster",
6465
CLUSTER_TYPE == ClusterType.MIXED || CLUSTER_TYPE == ClusterType.UPGRADED);
65-
Response getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token1");
66+
Response getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token1"));
6667
assertOK(getResponse);
6768
Map<String, Object> source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
6869
assertTokenWorks((String) source.get("token"));
@@ -71,39 +72,41 @@ public void testTokenWorksInMixedOrUpgradedCluster() throws Exception {
7172
public void testMixedCluster() throws Exception {
7273
assumeTrue("this test should only run against the mixed cluster", CLUSTER_TYPE == ClusterType.MIXED);
7374
assumeTrue("the master must be on the latest version before we can write", isMasterOnLatestVersion());
74-
Response getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token2");
75-
assertOK(getResponse);
75+
Response getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token2"));
7676
Map<String, Object> source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
7777
final String token = (String) source.get("token");
7878
assertTokenWorks(token);
7979

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

8586
// create token and refresh on version that supports it
86-
final StringEntity tokenPostBody = new StringEntity("{\n" +
87+
Request createTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
88+
createTokenRequest.setJsonEntity(
89+
"{\n" +
8790
" \"username\": \"test_user\",\n" +
8891
" \"password\": \"x-pack-test-password\",\n" +
8992
" \"grant_type\": \"password\"\n" +
90-
"}", ContentType.APPLICATION_JSON);
93+
"}");
9194
try (RestClient client = getRestClientForCurrentVersionNodesOnly()) {
92-
Response response = client.performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
93-
assertOK(response);
95+
Response response = client.performRequest(createTokenRequest);
9496
Map<String, Object> responseMap = entityAsMap(response);
9597
String accessToken = (String) responseMap.get("access_token");
9698
String refreshToken = (String) responseMap.get("refresh_token");
9799
assertNotNull(accessToken);
98100
assertNotNull(refreshToken);
99101
assertTokenWorks(accessToken);
100102

101-
final StringEntity tokenRefresh = new StringEntity("{\n" +
103+
Request tokenRefreshRequest = new Request("POST", "_xpack/security/oauth2/token");
104+
tokenRefreshRequest.setJsonEntity(
105+
"{\n" +
102106
" \"refresh_token\": \"" + refreshToken + "\",\n" +
103107
" \"grant_type\": \"refresh_token\"\n" +
104-
"}", ContentType.APPLICATION_JSON);
105-
response = client.performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenRefresh);
106-
assertOK(response);
108+
"}");
109+
response = client.performRequest(tokenRefreshRequest);
107110
responseMap = entityAsMap(response);
108111
String updatedAccessToken = (String) responseMap.get("access_token");
109112
String updatedRefreshToken = (String) responseMap.get("refresh_token");
@@ -118,44 +121,46 @@ public void testMixedCluster() throws Exception {
118121

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

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

133-
getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token1");
134-
assertOK(getResponse);
137+
getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token1"));
135138
source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
136139
final String workingToken = (String) source.get("token");
137140
assertTokenWorks(workingToken);
138141

139-
final StringEntity tokenPostBody = new StringEntity("{\n" +
142+
Request getTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
143+
getTokenRequest.setJsonEntity(
144+
"{\n" +
140145
" \"username\": \"test_user\",\n" +
141146
" \"password\": \"x-pack-test-password\",\n" +
142147
" \"grant_type\": \"password\"\n" +
143-
"}", ContentType.APPLICATION_JSON);
144-
Response response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
145-
assertOK(response);
148+
"}");
149+
Response response = client().performRequest(getTokenRequest);
146150
Map<String, Object> responseMap = entityAsMap(response);
147151
String accessToken = (String) responseMap.get("access_token");
148152
String refreshToken = (String) responseMap.get("refresh_token");
149153
assertNotNull(accessToken);
150154
assertNotNull(refreshToken);
151155
assertTokenWorks(accessToken);
152156

153-
final StringEntity tokenRefresh = new StringEntity("{\n" +
157+
Request refreshTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
158+
refreshTokenRequest.setJsonEntity(
159+
"{\n" +
154160
" \"refresh_token\": \"" + refreshToken + "\",\n" +
155161
" \"grant_type\": \"refresh_token\"\n" +
156-
"}", ContentType.APPLICATION_JSON);
157-
response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenRefresh);
158-
assertOK(response);
162+
"}");
163+
response = client().performRequest(refreshTokenRequest);
159164
responseMap = entityAsMap(response);
160165
String updatedAccessToken = (String) responseMap.get("access_token");
161166
String updatedRefreshToken = (String) responseMap.get("refresh_token");
@@ -168,34 +173,39 @@ public void testUpgradedCluster() throws Exception {
168173
}
169174

170175
private void assertTokenWorks(String token) throws IOException {
171-
Response authenticateResponse = client().performRequest("GET", "_xpack/security/_authenticate", Collections.emptyMap(),
172-
new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token));
176+
Request request = new Request("GET", "_xpack/security/_authenticate");
177+
RequestOptions.Builder options = request.getOptions().toBuilder();
178+
options.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
179+
request.setOptions(options);
180+
Response authenticateResponse = client().performRequest(request);
173181
assertOK(authenticateResponse);
174182
assertEquals("test_user", entityAsMap(authenticateResponse).get("username"));
175183
}
176184

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

187197
private boolean isMasterOnLatestVersion() throws Exception {
188-
Response response = client().performRequest("GET", "_cluster/state");
198+
Response response = client().performRequest(new Request("GET", "_cluster/state"));
189199
assertOK(response);
190200
final String masterNodeId = ObjectPath.createFromResponse(response).evaluate("master_node");
191-
response = client().performRequest("GET", "_nodes");
201+
response = client().performRequest(new Request("GET", "_nodes"));
192202
assertOK(response);
193203
ObjectPath objectPath = ObjectPath.createFromResponse(response);
194204
return Version.CURRENT.equals(Version.fromString(objectPath.evaluate("nodes." + masterNodeId + ".version")));
195205
}
196206

197207
private RestClient getRestClientForCurrentVersionNodesOnly() throws IOException {
198-
Response response = client().performRequest("GET", "_nodes");
208+
Response response = client().performRequest(new Request("GET", "_nodes"));
199209
assertOK(response);
200210
ObjectPath objectPath = ObjectPath.createFromResponse(response);
201211
Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");

0 commit comments

Comments
 (0)