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 ;
17
16
import org .elasticsearch .test .rest .yaml .ObjectPath ;
18
17
19
18
import java .io .IOException ;
20
19
import java .util .ArrayList ;
21
- import java .util .Collections ;
22
20
import java .util .List ;
23
21
import java .util .Map ;
24
22
25
23
public class TokenBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
26
24
27
25
public void testGeneratingTokenInOldCluster () throws Exception {
28
26
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 " +
30
30
" \" username\" : \" test_user\" ,\n " +
31
31
" \" password\" : \" x-pack-test-password\" ,\n " +
32
32
" \" 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 );
35
35
assertOK (response );
36
36
Map <String , Object > responseMap = entityAsMap (response );
37
37
String token = (String ) responseMap .get ("access_token" );
38
38
assertNotNull (token );
39
39
assertTokenWorks (token );
40
40
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 " +
42
44
" \" 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 );
47
47
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 );
50
51
responseMap = entityAsMap (response );
51
52
token = (String ) responseMap .get ("access_token" );
52
53
assertNotNull (token );
53
54
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 " +
55
58
" \" 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 );
60
61
}
61
62
62
63
public void testTokenWorksInMixedOrUpgradedCluster () throws Exception {
63
64
assumeTrue ("this test should only run against the mixed or upgraded cluster" ,
64
65
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" ) );
66
67
assertOK (getResponse );
67
68
Map <String , Object > source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
68
69
assertTokenWorks ((String ) source .get ("token" ));
@@ -71,39 +72,41 @@ public void testTokenWorksInMixedOrUpgradedCluster() throws Exception {
71
72
public void testMixedCluster () throws Exception {
72
73
assumeTrue ("this test should only run against the mixed cluster" , CLUSTER_TYPE == ClusterType .MIXED );
73
74
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" ));
76
76
Map <String , Object > source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
77
77
final String token = (String ) source .get ("token" );
78
78
assertTokenWorks (token );
79
79
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 );
83
84
assertTokenDoesNotWork (token );
84
85
85
86
// 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 " +
87
90
" \" username\" : \" test_user\" ,\n " +
88
91
" \" password\" : \" x-pack-test-password\" ,\n " +
89
92
" \" grant_type\" : \" password\" \n " +
90
- "}" , ContentType . APPLICATION_JSON );
93
+ "}" );
91
94
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 );
94
96
Map <String , Object > responseMap = entityAsMap (response );
95
97
String accessToken = (String ) responseMap .get ("access_token" );
96
98
String refreshToken = (String ) responseMap .get ("refresh_token" );
97
99
assertNotNull (accessToken );
98
100
assertNotNull (refreshToken );
99
101
assertTokenWorks (accessToken );
100
102
101
- final StringEntity tokenRefresh = new StringEntity ("{\n " +
103
+ Request tokenRefreshRequest = new Request ("POST" , "_xpack/security/oauth2/token" );
104
+ tokenRefreshRequest .setJsonEntity (
105
+ "{\n " +
102
106
" \" refresh_token\" : \" " + refreshToken + "\" ,\n " +
103
107
" \" 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 );
107
110
responseMap = entityAsMap (response );
108
111
String updatedAccessToken = (String ) responseMap .get ("access_token" );
109
112
String updatedRefreshToken = (String ) responseMap .get ("refresh_token" );
@@ -118,44 +121,46 @@ public void testMixedCluster() throws Exception {
118
121
119
122
public void testUpgradedCluster () throws Exception {
120
123
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" ) );
122
125
assertOK (getResponse );
123
126
Map <String , Object > source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
124
127
final String token = (String ) source .get ("token" );
125
128
126
129
// 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 );
130
134
assertOK (invalidationResponse );
131
135
assertTokenDoesNotWork (token );
132
136
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" ));
135
138
source = (Map <String , Object >) entityAsMap (getResponse ).get ("_source" );
136
139
final String workingToken = (String ) source .get ("token" );
137
140
assertTokenWorks (workingToken );
138
141
139
- final StringEntity tokenPostBody = new StringEntity ("{\n " +
142
+ Request getTokenRequest = new Request ("POST" , "_xpack/security/oauth2/token" );
143
+ getTokenRequest .setJsonEntity (
144
+ "{\n " +
140
145
" \" username\" : \" test_user\" ,\n " +
141
146
" \" password\" : \" x-pack-test-password\" ,\n " +
142
147
" \" 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 );
146
150
Map <String , Object > responseMap = entityAsMap (response );
147
151
String accessToken = (String ) responseMap .get ("access_token" );
148
152
String refreshToken = (String ) responseMap .get ("refresh_token" );
149
153
assertNotNull (accessToken );
150
154
assertNotNull (refreshToken );
151
155
assertTokenWorks (accessToken );
152
156
153
- final StringEntity tokenRefresh = new StringEntity ("{\n " +
157
+ Request refreshTokenRequest = new Request ("POST" , "_xpack/security/oauth2/token" );
158
+ refreshTokenRequest .setJsonEntity (
159
+ "{\n " +
154
160
" \" refresh_token\" : \" " + refreshToken + "\" ,\n " +
155
161
" \" 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 );
159
164
responseMap = entityAsMap (response );
160
165
String updatedAccessToken = (String ) responseMap .get ("access_token" );
161
166
String updatedRefreshToken = (String ) responseMap .get ("refresh_token" );
@@ -168,34 +173,39 @@ public void testUpgradedCluster() throws Exception {
168
173
}
169
174
170
175
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 );
173
181
assertOK (authenticateResponse );
174
182
assertEquals ("test_user" , entityAsMap (authenticateResponse ).get ("username" ));
175
183
}
176
184
177
185
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 ));
181
191
assertEquals (401 , e .getResponse ().getStatusLine ().getStatusCode ());
182
192
Response response = e .getResponse ();
183
193
assertEquals ("Bearer realm=\" security\" , error=\" invalid_token\" , error_description=\" The access token expired\" " ,
184
194
response .getHeader ("WWW-Authenticate" ));
185
195
}
186
196
187
197
private boolean isMasterOnLatestVersion () throws Exception {
188
- Response response = client ().performRequest ("GET" , "_cluster/state" );
198
+ Response response = client ().performRequest (new Request ( "GET" , "_cluster/state" ) );
189
199
assertOK (response );
190
200
final String masterNodeId = ObjectPath .createFromResponse (response ).evaluate ("master_node" );
191
- response = client ().performRequest ("GET" , "_nodes" );
201
+ response = client ().performRequest (new Request ( "GET" , "_nodes" ) );
192
202
assertOK (response );
193
203
ObjectPath objectPath = ObjectPath .createFromResponse (response );
194
204
return Version .CURRENT .equals (Version .fromString (objectPath .evaluate ("nodes." + masterNodeId + ".version" )));
195
205
}
196
206
197
207
private RestClient getRestClientForCurrentVersionNodesOnly () throws IOException {
198
- Response response = client ().performRequest ("GET" , "_nodes" );
208
+ Response response = client ().performRequest (new Request ( "GET" , "_nodes" ) );
199
209
assertOK (response );
200
210
ObjectPath objectPath = ObjectPath .createFromResponse (response );
201
211
Map <String , Object > nodesAsMap = objectPath .evaluate ("nodes" );
0 commit comments