12
12
import org .elasticsearch .common .settings .Settings ;
13
13
import org .elasticsearch .common .util .concurrent .ThreadContext ;
14
14
import org .elasticsearch .common .util .concurrent .UncategorizedExecutionException ;
15
+ import org .elasticsearch .env .Environment ;
15
16
import org .elasticsearch .env .TestEnvironment ;
16
17
import org .elasticsearch .threadpool .TestThreadPool ;
17
18
import org .elasticsearch .threadpool .ThreadPool ;
@@ -59,9 +60,7 @@ public boolean enableWarningsCheck() {
59
60
60
61
@ SuppressWarnings ("unchecked" )
61
62
public void testAdAuth () throws Exception {
62
- RealmConfig config = new RealmConfig ("ad-test" ,
63
- buildAdSettings (AD_LDAP_URL , AD_DOMAIN , false ),
64
- globalSettings , TestEnvironment .newEnvironment (globalSettings ), new ThreadContext (Settings .EMPTY ));
63
+ RealmConfig config = configureRealm ("ad-test" , buildAdSettings (AD_LDAP_URL , AD_DOMAIN , false ));
65
64
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
66
65
67
66
String userName = "ironman" ;
@@ -82,11 +81,21 @@ public void testAdAuth() throws Exception {
82
81
}
83
82
}
84
83
84
+ private RealmConfig configureRealm (String name , Settings settings ) {
85
+ final Environment env = TestEnvironment .newEnvironment (globalSettings );
86
+ final Settings mergedSettings = Settings .builder ()
87
+ .put (settings )
88
+ .normalizePrefix ("xpack.security.authc.realms." + name + "." )
89
+ .put (globalSettings )
90
+ .build ();
91
+ this .sslService = new SSLService (mergedSettings , env );
92
+ return new RealmConfig (name , settings , globalSettings , env , new ThreadContext (globalSettings ));
93
+ }
94
+
85
95
@ SuppressWarnings ("unchecked" )
86
96
public void testNetbiosAuth () throws Exception {
87
97
final String adUrl = randomFrom (AD_LDAP_URL , AD_LDAP_GC_URL );
88
- RealmConfig config = new RealmConfig ("ad-test" , buildAdSettings (adUrl , AD_DOMAIN , false ), globalSettings ,
89
- TestEnvironment .newEnvironment (globalSettings ), new ThreadContext (globalSettings ));
98
+ RealmConfig config = configureRealm ("ad-test" , buildAdSettings (adUrl , AD_DOMAIN , false ));
90
99
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
91
100
92
101
String userName = "ades\\ ironman" ;
@@ -108,8 +117,7 @@ public void testNetbiosAuth() throws Exception {
108
117
}
109
118
110
119
public void testAdAuthAvengers () throws Exception {
111
- RealmConfig config = new RealmConfig ("ad-test" , buildAdSettings (AD_LDAP_URL , AD_DOMAIN , false ), globalSettings ,
112
- TestEnvironment .newEnvironment (globalSettings ), new ThreadContext (globalSettings ));
120
+ RealmConfig config = configureRealm ("ad-test" , buildAdSettings (AD_LDAP_URL , AD_DOMAIN , false ));
113
121
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
114
122
115
123
String [] users = new String []{"cap" , "hawkeye" , "hulk" , "ironman" , "thor" , "blackwidow" };
@@ -126,8 +134,7 @@ public void testAdAuthAvengers() throws Exception {
126
134
public void testAuthenticate () throws Exception {
127
135
Settings settings = buildAdSettings (AD_LDAP_URL , AD_DOMAIN , "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" ,
128
136
LdapSearchScope .ONE_LEVEL , false );
129
- RealmConfig config = new RealmConfig ("ad-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
130
- new ThreadContext (globalSettings ));
137
+ RealmConfig config = configureRealm ("ad-test" , settings );
131
138
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
132
139
133
140
String userName = "hulk" ;
@@ -151,8 +158,7 @@ public void testAuthenticate() throws Exception {
151
158
public void testAuthenticateBaseUserSearch () throws Exception {
152
159
Settings settings = buildAdSettings (AD_LDAP_URL , AD_DOMAIN , "CN=Bruce Banner, CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" ,
153
160
LdapSearchScope .BASE , false );
154
- RealmConfig config = new RealmConfig ("ad-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
155
- new ThreadContext (globalSettings ));
161
+ RealmConfig config = configureRealm ("ad-test" , settings );
156
162
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
157
163
158
164
String userName = "hulk" ;
@@ -180,8 +186,7 @@ public void testAuthenticateBaseGroupSearch() throws Exception {
180
186
"CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" )
181
187
.put (ActiveDirectorySessionFactorySettings .AD_GROUP_SEARCH_SCOPE_SETTING , LdapSearchScope .BASE )
182
188
.build ();
183
- RealmConfig config = new RealmConfig ("ad-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
184
- new ThreadContext (globalSettings ));
189
+ RealmConfig config = configureRealm ("ad-test" , settings );
185
190
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
186
191
187
192
String userName = "hulk" ;
@@ -198,8 +203,7 @@ public void testAuthenticateBaseGroupSearch() throws Exception {
198
203
public void testAuthenticateWithUserPrincipalName () throws Exception {
199
204
Settings settings = buildAdSettings (AD_LDAP_URL , AD_DOMAIN , "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" ,
200
205
LdapSearchScope .ONE_LEVEL , false );
201
- RealmConfig config = new RealmConfig ("ad-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
202
- new ThreadContext (globalSettings ));
206
+ RealmConfig config = configureRealm ("ad-test" , settings );
203
207
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
204
208
205
209
//Login with the UserPrincipalName
@@ -220,8 +224,7 @@ public void testAuthenticateWithUserPrincipalName() throws Exception {
220
224
public void testAuthenticateWithSAMAccountName () throws Exception {
221
225
Settings settings = buildAdSettings (AD_LDAP_URL , AD_DOMAIN , "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" ,
222
226
LdapSearchScope .ONE_LEVEL , false );
223
- RealmConfig config = new RealmConfig ("ad-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
224
- new ThreadContext (globalSettings ));
227
+ RealmConfig config = configureRealm ("ad-test" , settings );
225
228
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
226
229
227
230
//login with sAMAccountName
@@ -247,8 +250,7 @@ public void testCustomUserFilter() throws Exception {
247
250
.put (ActiveDirectorySessionFactorySettings .AD_USER_SEARCH_FILTER_SETTING ,
248
251
"(&(objectclass=user)(userPrincipalName={0}@ad.test.elasticsearch.com))" )
249
252
.build ();
250
- RealmConfig config = new RealmConfig ("ad-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
251
- new ThreadContext (globalSettings ));
253
+ RealmConfig config = configureRealm ("ad-test" , settings );
252
254
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
253
255
254
256
//Login with the UserPrincipalName
@@ -284,8 +286,7 @@ public void testStandardLdapConnection() throws Exception {
284
286
.putList ("ssl.certificate_authorities" , certificatePaths )
285
287
.build ();
286
288
}
287
- RealmConfig config = new RealmConfig ("ad-as-ldap-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
288
- new ThreadContext (globalSettings ));
289
+ RealmConfig config = configureRealm ("ad-as-ldap-test" , settings );
289
290
LdapSessionFactory sessionFactory = new LdapSessionFactory (config , sslService , threadPool );
290
291
291
292
String user = "Bruce Banner" ;
@@ -348,8 +349,7 @@ public void testStandardLdapWithAttributeGroups() throws Exception {
348
349
.putList ("ssl.certificate_authorities" , certificatePaths )
349
350
.build ();
350
351
}
351
- RealmConfig config = new RealmConfig ("ad-as-ldap-test" , settings , globalSettings , TestEnvironment .newEnvironment (globalSettings ),
352
- new ThreadContext (globalSettings ));
352
+ RealmConfig config = configureRealm ("ad-as-ldap-test" , settings );
353
353
LdapSessionFactory sessionFactory = new LdapSessionFactory (config , sslService , threadPool );
354
354
355
355
String user = "Bruce Banner" ;
@@ -366,9 +366,7 @@ public void testStandardLdapWithAttributeGroups() throws Exception {
366
366
}
367
367
368
368
public void testADLookup () throws Exception {
369
- RealmConfig config = new RealmConfig ("ad-test" ,
370
- buildAdSettings (AD_LDAP_URL , AD_DOMAIN , false , true ),
371
- globalSettings , TestEnvironment .newEnvironment (globalSettings ), new ThreadContext (Settings .EMPTY ));
369
+ RealmConfig config = configureRealm ("ad-test" , buildAdSettings (AD_LDAP_URL , AD_DOMAIN , false , true ));
372
370
try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory (config , sslService , threadPool )) {
373
371
374
372
List <String > users = randomSubsetOf (Arrays .asList ("cap" , "hawkeye" , "hulk" , "ironman" , "thor" , "blackwidow" ,
0 commit comments