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 ;
1716import org .elasticsearch .test .rest .yaml .ObjectPath ;
1817
1918import java .io .IOException ;
2019import java .util .ArrayList ;
21- import java .util .Collections ;
2220import java .util .List ;
2321import java .util .Map ;
2422
2523public 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