24
24
import org .apache .http .cookie .Cookie ;
25
25
import org .apache .http .cookie .CookieOrigin ;
26
26
import org .apache .http .cookie .MalformedCookieException ;
27
- import org .apache .http .entity .ContentType ;
28
- import org .apache .http .entity .StringEntity ;
29
27
import org .apache .http .impl .client .CloseableHttpClient ;
30
28
import org .apache .http .impl .client .HttpClients ;
31
29
import org .apache .http .impl .cookie .DefaultCookieSpec ;
39
37
import org .apache .logging .log4j .message .ParameterizedMessage ;
40
38
import org .elasticsearch .ElasticsearchException ;
41
39
import org .elasticsearch .cli .SuppressForbidden ;
40
+ import org .elasticsearch .client .Request ;
41
+ import org .elasticsearch .client .RequestOptions ;
42
42
import org .elasticsearch .client .Response ;
43
43
import org .elasticsearch .common .CheckedFunction ;
44
44
import org .elasticsearch .common .Strings ;
85
85
import java .util .regex .Matcher ;
86
86
import java .util .regex .Pattern ;
87
87
88
- import static java .util .Collections .emptyMap ;
89
88
import static org .elasticsearch .common .xcontent .XContentHelper .convertToMap ;
90
89
import static org .elasticsearch .xpack .core .security .authc .support .UsernamePasswordToken .basicAuthHeaderValue ;
91
90
import static org .hamcrest .Matchers .contains ;
@@ -176,9 +175,9 @@ protected Settings restAdminSettings() {
176
175
*/
177
176
@ Before
178
177
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 );
182
181
}
183
182
184
183
/**
@@ -188,21 +187,19 @@ public void setKibanaPassword() throws IOException {
188
187
*/
189
188
@ Before
190
189
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" )
197
196
.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 ()
200
199
.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 );
206
203
}
207
204
208
205
/**
@@ -251,10 +248,11 @@ public void testLoginUser() throws Exception {
251
248
* is for the expected user with the expected name and roles.
252
249
*/
253
250
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 ));
258
256
assertThat (map .get ("username" ), equalTo ("thor" ));
259
257
assertThat (map .get ("full_name" ), equalTo ("Thor Odinson" ));
260
258
assertSingletonList (map .get ("roles" ), "kibana_user" );
@@ -272,12 +270,11 @@ private void verifyElasticsearchAccessToken(String accessToken) throws IOExcepti
272
270
* can be used to get a new valid access token and refresh token.
273
271
*/
274
272
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 );
279
276
280
- final Map <String , Object > result = parseResponseAsMap ( response . getEntity ( ));
277
+ final Map <String , Object > result = entityAsMap ( client (). performRequest ( request ));
281
278
final Object newRefreshToken = result .get ("refresh_token" );
282
279
assertThat (newRefreshToken , notNullValue ());
283
280
assertThat (newRefreshToken , instanceOf (String .class ));
@@ -463,10 +460,10 @@ private String getUrl(String path) {
463
460
* sends a redirect to that page.
464
461
*/
465
462
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 ));
470
467
logger .info ("Created SAML authentication request {}" , body );
471
468
http .getResponseHeaders ().add ("Set-Cookie" , REQUEST_ID_COOKIE + "=" + body .get ("id" ));
472
469
http .getResponseHeaders ().add ("Location" , (String ) body .get ("redirect" ));
@@ -504,9 +501,10 @@ private Response samlAuthenticate(HttpExchange http) throws IOException {
504
501
final String id = getCookie (REQUEST_ID_COOKIE , http );
505
502
assertThat (id , notNullValue ());
506
503
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 );
510
508
}
511
509
512
510
private List <NameValuePair > parseRequestForm (HttpExchange http ) throws IOException {
@@ -542,9 +540,11 @@ private static void assertSingletonList(Object value, String expectedElement) {
542
540
assertThat (((List <?>) value ), contains (expectedElement ));
543
541
}
544
542
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 );
548
548
}
549
549
550
550
private CloseableHttpClient getHttpClient () throws Exception {
0 commit comments