7
7
8
8
import org .apache .http .HttpHeaders ;
9
9
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 ;
13
10
import org .elasticsearch .Version ;
11
+ import org .elasticsearch .client .Request ;
12
+ import org .elasticsearch .client .RequestOptions ;
14
13
import org .elasticsearch .client .Response ;
15
14
import org .elasticsearch .client .ResponseException ;
16
15
import org .elasticsearch .client .RestClient ;
19
18
20
19
import java .io .IOException ;
21
20
import java .util .ArrayList ;
22
- import java .util .Collections ;
23
21
import java .util .List ;
24
22
import java .util .Map ;
25
23
26
24
public class TokenBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
27
25
28
26
public void testGeneratingTokenInOldCluster () throws Exception {
29
27
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 " +
31
31
" \" username\" : \" test_user\" ,\n " +
32
32
" \" password\" : \" x-pack-test-password\" ,\n " +
33
33
" \" 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 );
36
36
assertOK (response );
37
37
Map <String , Object > responseMap = entityAsMap (response );
38
38
String token = (String ) responseMap .get ("access_token" );
39
39
assertNotNull (token );
40
40
assertTokenWorks (token );
41
41
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 " +
43
45
" \" 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 );
48
48
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 );
51
52
responseMap = entityAsMap (response );
52
53
token = (String ) responseMap .get ("access_token" );
53
54
assertNotNull (token );
54
55
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 " +
56
59
" \" 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 );
61
62
}
62
63
63
64
public void testTokenWorksInMixedOrUpgradedCluster () throws Exception {
64
65
assumeTrue ("this test should only run against the mixed or upgraded cluster" ,
65
66
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" ) );
67
68
assertOK (getResponse );
68
69
Map <String , Object > source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
69
70
assertTokenWorks ((String ) source .get ("token" ));
@@ -75,39 +76,42 @@ public void testMixedCluster() throws Exception {
75
76
assumeFalse ("can't be run twice because it invalidates a token so we skip the first attempt" ,
76
77
Booleans .parseBoolean (System .getProperty ("tests.first_round" )));
77
78
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" ) );
79
80
assertOK (getResponse );
80
81
Map <String , Object > source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
81
82
final String token = (String ) source .get ("token" );
82
83
assertTokenWorks (token );
83
84
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 );
87
89
assertTokenDoesNotWork (token );
88
90
89
91
// 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 " +
91
95
" \" username\" : \" test_user\" ,\n " +
92
96
" \" password\" : \" x-pack-test-password\" ,\n " +
93
97
" \" grant_type\" : \" password\" \n " +
94
- "}" , ContentType . APPLICATION_JSON );
98
+ "}" );
95
99
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 );
98
101
Map <String , Object > responseMap = entityAsMap (response );
99
102
String accessToken = (String ) responseMap .get ("access_token" );
100
103
String refreshToken = (String ) responseMap .get ("refresh_token" );
101
104
assertNotNull (accessToken );
102
105
assertNotNull (refreshToken );
103
106
assertTokenWorks (accessToken );
104
107
105
- final StringEntity tokenRefresh = new StringEntity ("{\n " +
108
+ Request tokenRefreshRequest = new Request ("POST" , "_xpack/security/oauth2/token" );
109
+ tokenRefreshRequest .setJsonEntity (
110
+ "{\n " +
106
111
" \" refresh_token\" : \" " + refreshToken + "\" ,\n " +
107
112
" \" 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 );
111
115
responseMap = entityAsMap (response );
112
116
String updatedAccessToken = (String ) responseMap .get ("access_token" );
113
117
String updatedRefreshToken = (String ) responseMap .get ("refresh_token" );
@@ -122,44 +126,46 @@ public void testMixedCluster() throws Exception {
122
126
123
127
public void testUpgradedCluster () throws Exception {
124
128
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" ) );
126
130
assertOK (getResponse );
127
131
Map <String , Object > source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
128
132
final String token = (String ) source .get ("token" );
129
133
130
134
// 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 );
134
139
assertOK (invalidationResponse );
135
140
assertTokenDoesNotWork (token );
136
141
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" ));
139
143
source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
140
144
final String workingToken = (String ) source .get ("token" );
141
145
assertTokenWorks (workingToken );
142
146
143
- final StringEntity tokenPostBody = new StringEntity ("{\n " +
147
+ Request getTokenRequest = new Request ("POST" , "_xpack/security/oauth2/token" );
148
+ getTokenRequest .setJsonEntity (
149
+ "{\n " +
144
150
" \" username\" : \" test_user\" ,\n " +
145
151
" \" password\" : \" x-pack-test-password\" ,\n " +
146
152
" \" 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 );
150
155
Map <String , Object > responseMap = entityAsMap (response );
151
156
String accessToken = (String ) responseMap .get ("access_token" );
152
157
String refreshToken = (String ) responseMap .get ("refresh_token" );
153
158
assertNotNull (accessToken );
154
159
assertNotNull (refreshToken );
155
160
assertTokenWorks (accessToken );
156
161
157
- final StringEntity tokenRefresh = new StringEntity ("{\n " +
162
+ Request refreshTokenRequest = new Request ("POST" , "_xpack/security/oauth2/token" );
163
+ refreshTokenRequest .setJsonEntity (
164
+ "{\n " +
158
165
" \" refresh_token\" : \" " + refreshToken + "\" ,\n " +
159
166
" \" 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 );
163
169
responseMap = entityAsMap (response );
164
170
String updatedAccessToken = (String ) responseMap .get ("access_token" );
165
171
String updatedRefreshToken = (String ) responseMap .get ("refresh_token" );
@@ -172,34 +178,39 @@ public void testUpgradedCluster() throws Exception {
172
178
}
173
179
174
180
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 );
177
186
assertOK (authenticateResponse );
178
187
assertEquals ("test_user" , entityAsMap (authenticateResponse ).get ("username" ));
179
188
}
180
189
181
190
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 ));
185
196
assertEquals (401 , e .getResponse ().getStatusLine ().getStatusCode ());
186
197
Response response = e .getResponse ();
187
198
assertEquals ("Bearer realm=\" security\" , error=\" invalid_token\" , error_description=\" The access token expired\" " ,
188
199
response .getHeader ("WWW-Authenticate" ));
189
200
}
190
201
191
202
private boolean isMasterOnLatestVersion () throws Exception {
192
- Response response = client ().performRequest ("GET" , "_cluster/state" );
203
+ Response response = client ().performRequest (new Request ( "GET" , "_cluster/state" ) );
193
204
assertOK (response );
194
205
final String masterNodeId = ObjectPath .createFromResponse (response ).evaluate ("master_node" );
195
- response = client ().performRequest ("GET" , "_nodes" );
206
+ response = client ().performRequest (new Request ( "GET" , "_nodes" ) );
196
207
assertOK (response );
197
208
ObjectPath objectPath = ObjectPath .createFromResponse (response );
198
209
return Version .CURRENT .equals (Version .fromString (objectPath .evaluate ("nodes." + masterNodeId + ".version" )));
199
210
}
200
211
201
212
private RestClient getRestClientForCurrentVersionNodesOnly () throws IOException {
202
- Response response = client ().performRequest ("GET" , "_nodes" );
213
+ Response response = client ().performRequest (new Request ( "GET" , "_nodes" ) );
203
214
assertOK (response );
204
215
ObjectPath objectPath = ObjectPath .createFromResponse (response );
205
216
Map <String , Object > nodesAsMap = objectPath .evaluate ("nodes" );
0 commit comments