2424import org .apache .http .cookie .Cookie ;
2525import org .apache .http .cookie .CookieOrigin ;
2626import org .apache .http .cookie .MalformedCookieException ;
27- import org .apache .http .entity .ContentType ;
28- import org .apache .http .entity .StringEntity ;
2927import org .apache .http .impl .client .CloseableHttpClient ;
3028import org .apache .http .impl .client .HttpClients ;
3129import org .apache .http .impl .cookie .DefaultCookieSpec ;
3937import org .apache .logging .log4j .message .ParameterizedMessage ;
4038import org .elasticsearch .ElasticsearchException ;
4139import org .elasticsearch .cli .SuppressForbidden ;
40+ import org .elasticsearch .client .Request ;
41+ import org .elasticsearch .client .RequestOptions ;
4242import org .elasticsearch .client .Response ;
4343import org .elasticsearch .common .CheckedFunction ;
4444import org .elasticsearch .common .Strings ;
8585import java .util .regex .Matcher ;
8686import java .util .regex .Pattern ;
8787
88- import static java .util .Collections .emptyMap ;
8988import static org .elasticsearch .common .xcontent .XContentHelper .convertToMap ;
9089import static org .elasticsearch .xpack .core .security .authc .support .UsernamePasswordToken .basicAuthHeaderValue ;
9190import static org .hamcrest .Matchers .contains ;
@@ -176,9 +175,9 @@ protected Settings restAdminSettings() {
176175 */
177176 @ Before
178177 public void setKibanaPassword () throws IOException {
179- final HttpEntity json = new StringEntity ( "{ \" password \" : \" " + KIBANA_PASSWORD + " \" }" , ContentType . APPLICATION_JSON );
180- final Response response = adminClient (). performRequest ( "PUT" , "/_xpack/security/user/kibana/_password" , emptyMap (), json );
181- assertOK ( response );
178+ Request request = new Request ( "PUT" , "/_xpack/security/user/kibana/_password" );
179+ request . setJsonEntity ( "{ \" password \" : \" " + KIBANA_PASSWORD + " \" }" );
180+ adminClient (). performRequest ( request );
182181 }
183182
184183 /**
@@ -188,21 +187,19 @@ public void setKibanaPassword() throws IOException {
188187 */
189188 @ Before
190189 public void setupRoleMapping () throws IOException {
191- final StringEntity json = new StringEntity ( Strings // top-level
192- .toString (XContentBuilder .builder (XContentType .JSON .xContent ())
193- .startObject ()
194- .array ("roles" , new String [] { "kibana_user" } )
195- .field ("enabled" , true )
196- .startObject ("rules" )
190+ Request request = new Request ( "PUT" , "/_xpack/security/role_mapping/thor-kibana" );
191+ request . setJsonEntity ( Strings .toString (XContentBuilder .builder (XContentType .JSON .xContent ())
192+ .startObject ()
193+ .array ("roles" , new String [] { "kibana_user" } )
194+ .field ("enabled" , true )
195+ .startObject ("rules" )
197196 .startArray ("all" )
198- .startObject ().startObject ("field" ).field ("username" , "thor" ).endObject ().endObject ()
199- .startObject ().startObject ("field" ).field ("realm.name" , "shibboleth" ).endObject ().endObject ()
197+ .startObject ().startObject ("field" ).field ("username" , "thor" ).endObject ().endObject ()
198+ .startObject ().startObject ("field" ).field ("realm.name" , "shibboleth" ).endObject ().endObject ()
200199 .endArray () // "all"
201- .endObject () // "rules"
202- .endObject ()), ContentType .APPLICATION_JSON );
203-
204- final Response response = adminClient ().performRequest ("PUT" , "/_xpack/security/role_mapping/thor-kibana" , emptyMap (), json );
205- assertOK (response );
200+ .endObject () // "rules"
201+ .endObject ()));
202+ adminClient ().performRequest (request );
206203 }
207204
208205 /**
@@ -251,10 +248,11 @@ public void testLoginUser() throws Exception {
251248 * is for the expected user with the expected name and roles.
252249 */
253250 private void verifyElasticsearchAccessToken (String accessToken ) throws IOException {
254- final BasicHeader authorization = new BasicHeader ("Authorization" , "Bearer " + accessToken );
255- final Response response = client ().performRequest ("GET" , "/_xpack/security/_authenticate" , authorization );
256- assertOK (response );
257- final Map <String , Object > map = parseResponseAsMap (response .getEntity ());
251+ Request request = new Request ("GET" , "/_xpack/security/_authenticate" );
252+ RequestOptions .Builder options = request .getOptions ().toBuilder ();
253+ options .addHeader ("Authorization" , "Bearer " + accessToken );
254+ request .setOptions (options );
255+ final Map <String , Object > map = entityAsMap (client ().performRequest (request ));
258256 assertThat (map .get ("username" ), equalTo ("thor" ));
259257 assertThat (map .get ("full_name" ), equalTo ("Thor Odinson" ));
260258 assertSingletonList (map .get ("roles" ), "kibana_user" );
@@ -272,12 +270,11 @@ private void verifyElasticsearchAccessToken(String accessToken) throws IOExcepti
272270 * can be used to get a new valid access token and refresh token.
273271 */
274272 private void verifyElasticsearchRefreshToken (String refreshToken ) throws IOException {
275- final String body = "{ \" grant_type\" :\" refresh_token\" , \" refresh_token\" :\" " + refreshToken + "\" }" ;
276- final Response response = client ().performRequest ("POST" , "/_xpack/security/oauth2/token" ,
277- emptyMap (), new StringEntity (body , ContentType .APPLICATION_JSON ), kibanaAuth ());
278- assertOK (response );
273+ Request request = new Request ("POST" , "/_xpack/security/oauth2/token" );
274+ request .setJsonEntity ("{ \" grant_type\" :\" refresh_token\" , \" refresh_token\" :\" " + refreshToken + "\" }" );
275+ kibanaAuth (request );
279276
280- final Map <String , Object > result = parseResponseAsMap ( response . getEntity ( ));
277+ final Map <String , Object > result = entityAsMap ( client (). performRequest ( request ));
281278 final Object newRefreshToken = result .get ("refresh_token" );
282279 assertThat (newRefreshToken , notNullValue ());
283280 assertThat (newRefreshToken , instanceOf (String .class ));
@@ -463,10 +460,10 @@ private String getUrl(String path) {
463460 * sends a redirect to that page.
464461 */
465462 private void httpLogin (HttpExchange http ) throws IOException {
466- final Response prepare = client (). performRequest ( "POST" , "/_xpack/security/saml/prepare" ,
467- emptyMap (), new StringEntity ( "{}" , ContentType . APPLICATION_JSON ), kibanaAuth () );
468- assertOK ( prepare );
469- final Map <String , Object > body = parseResponseAsMap ( prepare . getEntity ( ));
463+ Request request = new Request ( "POST" , "/_xpack/security/saml/prepare" );
464+ request . setJsonEntity ( "{}" );
465+ kibanaAuth ( request );
466+ final Map <String , Object > body = entityAsMap ( client (). performRequest ( request ));
470467 logger .info ("Created SAML authentication request {}" , body );
471468 http .getResponseHeaders ().add ("Set-Cookie" , REQUEST_ID_COOKIE + "=" + body .get ("id" ));
472469 http .getResponseHeaders ().add ("Location" , (String ) body .get ("redirect" ));
@@ -504,9 +501,10 @@ private Response samlAuthenticate(HttpExchange http) throws IOException {
504501 final String id = getCookie (REQUEST_ID_COOKIE , http );
505502 assertThat (id , notNullValue ());
506503
507- final String body = "{ \" content\" : \" " + saml + "\" , \" ids\" : [\" " + id + "\" ] }" ;
508- return client ().performRequest ("POST" , "/_xpack/security/saml/authenticate" ,
509- emptyMap (), new StringEntity (body , ContentType .APPLICATION_JSON ), kibanaAuth ());
504+ Request request = new Request ("POST" , "/_xpack/security/saml/authenticate" );
505+ request .setJsonEntity ("{ \" content\" : \" " + saml + "\" , \" ids\" : [\" " + id + "\" ] }" );
506+ kibanaAuth (request );
507+ return client ().performRequest (request );
510508 }
511509
512510 private List <NameValuePair > parseRequestForm (HttpExchange http ) throws IOException {
@@ -542,9 +540,11 @@ private static void assertSingletonList(Object value, String expectedElement) {
542540 assertThat (((List <?>) value ), contains (expectedElement ));
543541 }
544542
545- private static BasicHeader kibanaAuth () {
546- final String auth = UsernamePasswordToken .basicAuthHeaderValue ("kibana" , new SecureString (KIBANA_PASSWORD .toCharArray ()));
547- return new BasicHeader (UsernamePasswordToken .BASIC_AUTH_HEADER , auth );
543+ private static void kibanaAuth (Request request ) {
544+ RequestOptions .Builder options = request .getOptions ().toBuilder ();
545+ options .addHeader ("Authorization" ,
546+ UsernamePasswordToken .basicAuthHeaderValue ("kibana" , new SecureString (KIBANA_PASSWORD .toCharArray ())));
547+ request .setOptions (options );
548548 }
549549
550550 private CloseableHttpClient getHttpClient () throws Exception {
0 commit comments