Skip to content

Commit 0e66ce4

Browse files
committed
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 1d4524c commit 0e66ce4

File tree

3 files changed

+78
-67
lines changed

3 files changed

+78
-67
lines changed

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,18 +5,15 @@
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.test.rest.yaml.ObjectPath;
1412
import org.elasticsearch.common.Booleans;
1513
import org.hamcrest.Matchers;
1614
import org.junit.Before;
1715

1816
import java.io.IOException;
19-
import java.util.Collections;
2017
import java.util.List;
2118
import java.util.Map;
2219

@@ -93,7 +90,7 @@ private int expectedNumUniqueNodeNameBuckets() throws IOException {
9390

9491
private void assertAuditDocsExist() throws Exception {
9592
final String type = minVersionInCluster.before(Version.V_6_0_0) ? "event" : "doc";
96-
Response response = client().performRequest("GET", "/.security_audit_log*/" + type + "/_count");
93+
Response response = client().performRequest(new Request("GET", "/.security_audit_log*/" + type + "/_count"));
9794
assertEquals(200, response.getStatusLine().getStatusCode());
9895
Map<String, Object> responseMap = entityAsMap(response);
9996
assertNotNull(responseMap.get("count"));
@@ -102,19 +99,20 @@ private void assertAuditDocsExist() throws Exception {
10299

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

108-
HttpEntity httpEntity = new StringEntity(
105+
Request aggRequest = new Request("GET", "/.security_audit_log*/_search");
106+
aggRequest.setJsonEntity(
109107
"{\n" +
110108
" \"aggs\" : {\n" +
111109
" \"nodes\" : {\n" +
112110
" \"terms\" : { \"field\" : \"node_name\" }\n" +
113111
" }\n" +
114112
" }\n" +
115-
"}", ContentType.APPLICATION_JSON);
116-
Response aggResponse = client().performRequest("GET", "/.security_audit_log*/_search",
117-
Collections.singletonMap("pretty", "true"), httpEntity);
113+
"}");
114+
aggRequest.addParameter("pretty", "true");
115+
Response aggResponse = client().performRequest(aggRequest);
118116
Map<String, Object> aggResponseMap = entityAsMap(aggResponse);
119117
logger.debug("aggResponse {}", aggResponseMap);
120118
Map<?, ?> aggregations = (Map<?, ?>) aggResponseMap.get("aggregations");
@@ -130,7 +128,7 @@ private void assertNumUniqueNodeNameBuckets(int numBuckets) throws Exception {
130128
* Has the master been upgraded to the new version?
131129
*/
132130
private boolean masterIsNewVersion() throws IOException {
133-
Map<?, ?> map = entityAsMap(client().performRequest("GET", "/_nodes/_master"));
131+
Map<?, ?> map = entityAsMap(client().performRequest(new Request("GET", "/_nodes/_master")));
134132
map = (Map<?, ?>) map.get("nodes");
135133
assertThat(map.values(), hasSize(1));
136134
map = (Map<?, ?>) map.values().iterator().next();

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

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
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;
@@ -19,51 +18,53 @@
1918

2019
import java.io.IOException;
2120
import java.util.ArrayList;
22-
import java.util.Collections;
2321
import java.util.List;
2422
import java.util.Map;
2523

2624
public class TokenBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
2725

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

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

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

6364
public void testTokenWorksInMixedOrUpgradedCluster() throws Exception {
6465
assumeTrue("this test should only run against the mixed or upgraded cluster",
6566
CLUSTER_TYPE == ClusterType.MIXED || CLUSTER_TYPE == ClusterType.UPGRADED);
66-
Response getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token1");
67+
Response getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token1"));
6768
assertOK(getResponse);
6869
Map<String, Object> source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
6970
assertTokenWorks((String) source.get("token"));
@@ -75,39 +76,42 @@ public void testMixedCluster() throws Exception {
7576
assumeFalse("can't be run twice because it invalidates a token so we skip the first attempt",
7677
Booleans.parseBoolean(System.getProperty("tests.first_round")));
7778

78-
Response getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token2");
79+
Response getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token2"));
7980
assertOK(getResponse);
8081
Map<String, Object> source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
8182
final String token = (String) source.get("token");
8283
assertTokenWorks(token);
8384

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

8991
// create token and refresh on version that supports it
90-
final StringEntity tokenPostBody = new StringEntity("{\n" +
92+
Request createTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
93+
createTokenRequest.setJsonEntity(
94+
"{\n" +
9195
" \"username\": \"test_user\",\n" +
9296
" \"password\": \"x-pack-test-password\",\n" +
9397
" \"grant_type\": \"password\"\n" +
94-
"}", ContentType.APPLICATION_JSON);
98+
"}");
9599
try (RestClient client = getRestClientForCurrentVersionNodesOnly()) {
96-
Response response = client.performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
97-
assertOK(response);
100+
Response response = client.performRequest(createTokenRequest);
98101
Map<String, Object> responseMap = entityAsMap(response);
99102
String accessToken = (String) responseMap.get("access_token");
100103
String refreshToken = (String) responseMap.get("refresh_token");
101104
assertNotNull(accessToken);
102105
assertNotNull(refreshToken);
103106
assertTokenWorks(accessToken);
104107

105-
final StringEntity tokenRefresh = new StringEntity("{\n" +
108+
Request tokenRefreshRequest = new Request("POST", "_xpack/security/oauth2/token");
109+
tokenRefreshRequest.setJsonEntity(
110+
"{\n" +
106111
" \"refresh_token\": \"" + refreshToken + "\",\n" +
107112
" \"grant_type\": \"refresh_token\"\n" +
108-
"}", ContentType.APPLICATION_JSON);
109-
response = client.performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenRefresh);
110-
assertOK(response);
113+
"}");
114+
response = client.performRequest(tokenRefreshRequest);
111115
responseMap = entityAsMap(response);
112116
String updatedAccessToken = (String) responseMap.get("access_token");
113117
String updatedRefreshToken = (String) responseMap.get("refresh_token");
@@ -122,44 +126,46 @@ public void testMixedCluster() throws Exception {
122126

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

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

137-
getResponse = client().performRequest("GET", "token_backwards_compatibility_it/doc/old_cluster_token1");
138-
assertOK(getResponse);
142+
getResponse = client().performRequest(new Request("GET", "token_backwards_compatibility_it/doc/old_cluster_token1"));
139143
source = (Map<String, Object>) entityAsMap(getResponse).get("_source");
140144
final String workingToken = (String) source.get("token");
141145
assertTokenWorks(workingToken);
142146

143-
final StringEntity tokenPostBody = new StringEntity("{\n" +
147+
Request getTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
148+
getTokenRequest.setJsonEntity(
149+
"{\n" +
144150
" \"username\": \"test_user\",\n" +
145151
" \"password\": \"x-pack-test-password\",\n" +
146152
" \"grant_type\": \"password\"\n" +
147-
"}", ContentType.APPLICATION_JSON);
148-
Response response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenPostBody);
149-
assertOK(response);
153+
"}");
154+
Response response = client().performRequest(getTokenRequest);
150155
Map<String, Object> responseMap = entityAsMap(response);
151156
String accessToken = (String) responseMap.get("access_token");
152157
String refreshToken = (String) responseMap.get("refresh_token");
153158
assertNotNull(accessToken);
154159
assertNotNull(refreshToken);
155160
assertTokenWorks(accessToken);
156161

157-
final StringEntity tokenRefresh = new StringEntity("{\n" +
162+
Request refreshTokenRequest = new Request("POST", "_xpack/security/oauth2/token");
163+
refreshTokenRequest.setJsonEntity(
164+
"{\n" +
158165
" \"refresh_token\": \"" + refreshToken + "\",\n" +
159166
" \"grant_type\": \"refresh_token\"\n" +
160-
"}", ContentType.APPLICATION_JSON);
161-
response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(), tokenRefresh);
162-
assertOK(response);
167+
"}");
168+
response = client().performRequest(refreshTokenRequest);
163169
responseMap = entityAsMap(response);
164170
String updatedAccessToken = (String) responseMap.get("access_token");
165171
String updatedRefreshToken = (String) responseMap.get("refresh_token");
@@ -172,34 +178,39 @@ public void testUpgradedCluster() throws Exception {
172178
}
173179

174180
private void assertTokenWorks(String token) throws IOException {
175-
Response authenticateResponse = client().performRequest("GET", "_xpack/security/_authenticate", Collections.emptyMap(),
176-
new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token));
181+
Request request = new Request("GET", "_xpack/security/_authenticate");
182+
RequestOptions.Builder options = request.getOptions().toBuilder();
183+
options.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
184+
request.setOptions(options);
185+
Response authenticateResponse = client().performRequest(request);
177186
assertOK(authenticateResponse);
178187
assertEquals("test_user", entityAsMap(authenticateResponse).get("username"));
179188
}
180189

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

191202
private boolean isMasterOnLatestVersion() throws Exception {
192-
Response response = client().performRequest("GET", "_cluster/state");
203+
Response response = client().performRequest(new Request("GET", "_cluster/state"));
193204
assertOK(response);
194205
final String masterNodeId = ObjectPath.createFromResponse(response).evaluate("master_node");
195-
response = client().performRequest("GET", "_nodes");
206+
response = client().performRequest(new Request("GET", "_nodes"));
196207
assertOK(response);
197208
ObjectPath objectPath = ObjectPath.createFromResponse(response);
198209
return Version.CURRENT.equals(Version.fromString(objectPath.evaluate("nodes." + masterNodeId + ".version")));
199210
}
200211

201212
private RestClient getRestClientForCurrentVersionNodesOnly() throws IOException {
202-
Response response = client().performRequest("GET", "_nodes");
213+
Response response = client().performRequest(new Request("GET", "_nodes"));
203214
assertOK(response);
204215
ObjectPath objectPath = ObjectPath.createFromResponse(response);
205216
Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");

0 commit comments

Comments
 (0)