55 */
66package org .elasticsearch .example .role ;
77
8- import org .apache .http .message .BasicHeader ;
8+ import org .elasticsearch .client .Request ;
9+ import org .elasticsearch .client .RequestOptions ;
910import org .elasticsearch .client .Response ;
1011import org .elasticsearch .client .ResponseException ;
1112import org .elasticsearch .common .network .NetworkModule ;
3334 * Integration test for custom roles providers.
3435 */
3536public class CustomRolesProviderIT extends ESIntegTestCase {
36-
3737 private static final String TEST_USER = "test_user" ;
3838 private static final String TEST_PWD = "change_me" ;
3939
40+ private static final RequestOptions AUTH_OPTIONS ;
41+ static {
42+ RequestOptions .Builder options = RequestOptions .DEFAULT .toBuilder ();
43+ options .addHeader (UsernamePasswordToken .BASIC_AUTH_HEADER ,
44+ basicAuthHeaderValue (TEST_USER , new SecureString (TEST_PWD .toCharArray ())));
45+ AUTH_OPTIONS = options .build ();
46+ }
47+
4048 @ Override
4149 protected Settings externalClusterClientSettings () {
4250 return Settings .builder ()
@@ -59,7 +67,9 @@ public void setupTestUser(String role) {
5967 public void testAuthorizedCustomRoleSucceeds () throws Exception {
6068 setupTestUser (ROLE_B );
6169 // roleB has all permissions on index "foo", so creating "foo" should succeed
62- Response response = getRestClient ().performRequest ("PUT" , "/" + INDEX , authHeader ());
70+ Request request = new Request ("PUT" , "/" + INDEX );
71+ request .setOptions (AUTH_OPTIONS );
72+ Response response = getRestClient ().performRequest (request );
6373 assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
6474 }
6575
@@ -71,7 +81,9 @@ public void testFirstResolvedRoleTakesPrecedence() throws Exception {
7181 setupTestUser (ROLE_A );
7282 // roleB has all permissions on index "foo", so creating "foo" should succeed
7383 try {
74- getRestClient ().performRequest ("PUT" , "/" + INDEX , authHeader ());
84+ Request request = new Request ("PUT" , "/" + INDEX );
85+ request .setOptions (AUTH_OPTIONS );
86+ getRestClient ().performRequest (request );
7587 fail (ROLE_A + " should not be authorized to create index " + INDEX );
7688 } catch (ResponseException e ) {
7789 assertThat (e .getResponse ().getStatusLine ().getStatusCode (), is (403 ));
@@ -82,15 +94,12 @@ public void testUnresolvedRoleDoesntSucceed() throws Exception {
8294 setupTestUser ("unknown" );
8395 // roleB has all permissions on index "foo", so creating "foo" should succeed
8496 try {
85- getRestClient ().performRequest ("PUT" , "/" + INDEX , authHeader ());
97+ Request request = new Request ("PUT" , "/" + INDEX );
98+ request .setOptions (AUTH_OPTIONS );
99+ getRestClient ().performRequest (request );
86100 fail (ROLE_A + " should not be authorized to create index " + INDEX );
87101 } catch (ResponseException e ) {
88102 assertThat (e .getResponse ().getStatusLine ().getStatusCode (), is (403 ));
89103 }
90104 }
91-
92- private BasicHeader authHeader () {
93- return new BasicHeader (UsernamePasswordToken .BASIC_AUTH_HEADER ,
94- basicAuthHeaderValue (TEST_USER , new SecureString (TEST_PWD .toCharArray ())));
95- }
96105}
0 commit comments