5
5
*/
6
6
package org .elasticsearch .example .role ;
7
7
8
- import org .apache .http .message .BasicHeader ;
8
+ import org .elasticsearch .client .Request ;
9
+ import org .elasticsearch .client .RequestOptions ;
9
10
import org .elasticsearch .client .Response ;
10
11
import org .elasticsearch .client .ResponseException ;
11
12
import org .elasticsearch .common .network .NetworkModule ;
33
34
* Integration test for custom roles providers.
34
35
*/
35
36
public class CustomRolesProviderIT extends ESIntegTestCase {
36
-
37
37
private static final String TEST_USER = "test_user" ;
38
38
private static final String TEST_PWD = "change_me" ;
39
39
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
+
40
48
@ Override
41
49
protected Settings externalClusterClientSettings () {
42
50
return Settings .builder ()
@@ -59,7 +67,9 @@ public void setupTestUser(String role) {
59
67
public void testAuthorizedCustomRoleSucceeds () throws Exception {
60
68
setupTestUser (ROLE_B );
61
69
// 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 );
63
73
assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
64
74
}
65
75
@@ -71,7 +81,9 @@ public void testFirstResolvedRoleTakesPrecedence() throws Exception {
71
81
setupTestUser (ROLE_A );
72
82
// roleB has all permissions on index "foo", so creating "foo" should succeed
73
83
try {
74
- getRestClient ().performRequest ("PUT" , "/" + INDEX , authHeader ());
84
+ Request request = new Request ("PUT" , "/" + INDEX );
85
+ request .setOptions (AUTH_OPTIONS );
86
+ getRestClient ().performRequest (request );
75
87
fail (ROLE_A + " should not be authorized to create index " + INDEX );
76
88
} catch (ResponseException e ) {
77
89
assertThat (e .getResponse ().getStatusLine ().getStatusCode (), is (403 ));
@@ -82,15 +94,12 @@ public void testUnresolvedRoleDoesntSucceed() throws Exception {
82
94
setupTestUser ("unknown" );
83
95
// roleB has all permissions on index "foo", so creating "foo" should succeed
84
96
try {
85
- getRestClient ().performRequest ("PUT" , "/" + INDEX , authHeader ());
97
+ Request request = new Request ("PUT" , "/" + INDEX );
98
+ request .setOptions (AUTH_OPTIONS );
99
+ getRestClient ().performRequest (request );
86
100
fail (ROLE_A + " should not be authorized to create index " + INDEX );
87
101
} catch (ResponseException e ) {
88
102
assertThat (e .getResponse ().getStatusLine ().getStatusCode (), is (403 ));
89
103
}
90
104
}
91
-
92
- private BasicHeader authHeader () {
93
- return new BasicHeader (UsernamePasswordToken .BASIC_AUTH_HEADER ,
94
- basicAuthHeaderValue (TEST_USER , new SecureString (TEST_PWD .toCharArray ())));
95
- }
96
105
}
0 commit comments