77
88import org .apache .http .HttpHeaders ;
99import 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 ;
1310import org .elasticsearch .Version ;
11+ import org .elasticsearch .client .Request ;
12+ import org .elasticsearch .client .RequestOptions ;
1413import org .elasticsearch .client .Response ;
1514import org .elasticsearch .client .ResponseException ;
1615import org .elasticsearch .client .RestClient ;
1918
2019import java .io .IOException ;
2120import java .util .ArrayList ;
22- import java .util .Collections ;
2321import java .util .List ;
2422import java .util .Map ;
2523
2624public 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