|
15 | 15 | import com.unboundid.ldap.sdk.schema.Schema; |
16 | 16 | import org.elasticsearch.action.ActionListener; |
17 | 17 | import org.elasticsearch.action.support.PlainActionFuture; |
| 18 | +import org.elasticsearch.client.Client; |
18 | 19 | import org.elasticsearch.common.Strings; |
| 20 | +import org.elasticsearch.common.bytes.BytesArray; |
19 | 21 | import org.elasticsearch.common.settings.MockSecureSettings; |
20 | 22 | import org.elasticsearch.common.settings.SecureString; |
21 | 23 | import org.elasticsearch.common.settings.Settings; |
|
24 | 26 | import org.elasticsearch.env.TestEnvironment; |
25 | 27 | import org.elasticsearch.license.TestUtils; |
26 | 28 | import org.elasticsearch.license.XPackLicenseState; |
| 29 | +import org.elasticsearch.script.ScriptModule; |
| 30 | +import org.elasticsearch.script.ScriptService; |
| 31 | +import org.elasticsearch.script.mustache.MustacheScriptEngine; |
27 | 32 | import org.elasticsearch.test.ESTestCase; |
28 | 33 | import org.elasticsearch.threadpool.TestThreadPool; |
29 | 34 | import org.elasticsearch.threadpool.ThreadPool; |
|
34 | 39 | import org.elasticsearch.xpack.core.security.authc.ldap.LdapRealmSettings; |
35 | 40 | import org.elasticsearch.xpack.core.security.authc.ldap.PoolingSessionFactorySettings; |
36 | 41 | import org.elasticsearch.xpack.core.security.authc.ldap.support.LdapLoadBalancingSettings; |
| 42 | +import org.elasticsearch.xpack.core.security.authc.ldap.support.LdapMetaDataResolverSettings; |
37 | 43 | import org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings; |
38 | 44 | import org.elasticsearch.xpack.core.security.authc.support.CachingUsernamePasswordRealmSettings; |
39 | 45 | import org.elasticsearch.xpack.core.security.authc.support.DnRoleMapperSettings; |
40 | 46 | import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; |
| 47 | +import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping; |
| 48 | +import org.elasticsearch.xpack.core.security.authc.support.mapper.TemplateRoleName; |
41 | 49 | import org.elasticsearch.xpack.core.security.user.User; |
42 | 50 | import org.elasticsearch.xpack.core.ssl.SSLConfigurationSettings; |
43 | 51 | import org.elasticsearch.xpack.core.ssl.SSLService; |
44 | 52 | import org.elasticsearch.xpack.core.ssl.VerificationMode; |
45 | 53 | import org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySessionFactory.DownLevelADAuthenticator; |
46 | 54 | import org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySessionFactory.UpnADAuthenticator; |
47 | 55 | import org.elasticsearch.xpack.security.authc.support.DnRoleMapper; |
| 56 | +import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore; |
| 57 | +import org.elasticsearch.xpack.security.support.SecurityIndexManager; |
48 | 58 | import org.junit.After; |
49 | 59 | import org.junit.Before; |
50 | 60 | import org.junit.BeforeClass; |
51 | 61 |
|
52 | 62 | import java.security.AccessController; |
53 | 63 | import java.security.PrivilegedExceptionAction; |
54 | 64 | import java.util.ArrayList; |
| 65 | +import java.util.Arrays; |
55 | 66 | import java.util.Collections; |
56 | 67 | import java.util.List; |
57 | 68 | import java.util.Locale; |
58 | 69 | import java.util.Map; |
59 | 70 |
|
60 | 71 | import static org.elasticsearch.xpack.core.security.authc.RealmSettings.getFullSettingKey; |
61 | | -import static org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING; |
62 | 72 | import static org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings.URLS_SETTING; |
63 | 73 | import static org.hamcrest.Matchers.arrayContaining; |
64 | 74 | import static org.hamcrest.Matchers.arrayContainingInAnyOrder; |
|
71 | 81 | import static org.hamcrest.Matchers.notNullValue; |
72 | 82 | import static org.mockito.Matchers.any; |
73 | 83 | import static org.mockito.Matchers.eq; |
| 84 | +import static org.mockito.Mockito.mock; |
74 | 85 | import static org.mockito.Mockito.spy; |
75 | 86 | import static org.mockito.Mockito.times; |
76 | 87 | import static org.mockito.Mockito.verify; |
| 88 | +import static org.mockito.Mockito.when; |
77 | 89 |
|
78 | 90 | /** |
79 | 91 | * Active Directory Realm tests that use the UnboundID In Memory Directory Server |
@@ -354,6 +366,62 @@ public void testRealmMapsUsersToRoles() throws Exception { |
354 | 366 | assertThat(user.roles(), arrayContainingInAnyOrder(equalTo("group_role"), equalTo("user_role"))); |
355 | 367 | } |
356 | 368 |
|
| 369 | + /** |
| 370 | + * This tests template role mappings (see |
| 371 | + * {@link TemplateRoleName}) with an LDAP realm, using a additional |
| 372 | + * metadata field (see {@link LdapMetaDataResolverSettings#ADDITIONAL_META_DATA_SETTING}). |
| 373 | + */ |
| 374 | + public void testRealmWithTemplatedRoleMapping() throws Exception { |
| 375 | + final RealmConfig.RealmIdentifier realmId = realmId("testRealmWithTemplatedRoleMapping"); |
| 376 | + Settings settings = settings(realmId, Settings.builder() |
| 377 | + .put(getFullSettingKey(realmId, LdapMetaDataResolverSettings.ADDITIONAL_META_DATA_SETTING), "departmentNumber") |
| 378 | + .build()); |
| 379 | + RealmConfig config = setupRealm(realmId, settings); |
| 380 | + ActiveDirectorySessionFactory sessionFactory = new ActiveDirectorySessionFactory(config, sslService, threadPool); |
| 381 | + |
| 382 | + SecurityIndexManager mockSecurityIndex = mock(SecurityIndexManager.class); |
| 383 | + when(mockSecurityIndex.isAvailable()).thenReturn(true); |
| 384 | + when(mockSecurityIndex.isIndexUpToDate()).thenReturn(true); |
| 385 | + when(mockSecurityIndex.isMappingUpToDate()).thenReturn(true); |
| 386 | + |
| 387 | + Client mockClient = mock(Client.class); |
| 388 | + when(mockClient.threadPool()).thenReturn(threadPool); |
| 389 | + |
| 390 | + final ScriptService scriptService = new ScriptService(settings, Collections.singletonMap(MustacheScriptEngine.NAME, |
| 391 | + new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); |
| 392 | + NativeRoleMappingStore roleMapper = new NativeRoleMappingStore(settings, mockClient, mockSecurityIndex, scriptService) { |
| 393 | + @Override |
| 394 | + protected void loadMappings(ActionListener<List<ExpressionRoleMapping>> listener) { |
| 395 | + listener.onResponse( |
| 396 | + Arrays.asList( |
| 397 | + this.buildMapping("m1", new BytesArray("{" + |
| 398 | + "\"role_templates\":[{\"template\":{\"source\":\"_role_{{metadata.departmentNumber}}\"}}]," + |
| 399 | + "\"enabled\":true," + |
| 400 | + "\"rules\":{ " + |
| 401 | + " \"field\":{\"realm.name\":\"testrealmwithtemplatedrolemapping\"}" + |
| 402 | + "}}")))); |
| 403 | + } |
| 404 | + }; |
| 405 | + LdapRealm realm = new LdapRealm(config, sessionFactory, roleMapper, threadPool); |
| 406 | + realm.initialize(Collections.singleton(realm), licenseState); |
| 407 | + |
| 408 | + PlainActionFuture<AuthenticationResult> future = new PlainActionFuture<>(); |
| 409 | + realm.authenticate(new UsernamePasswordToken("CN=Thor", new SecureString(PASSWORD)), future); |
| 410 | + AuthenticationResult result = future.actionGet(); |
| 411 | + assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); |
| 412 | + User user = result.getUser(); |
| 413 | + assertThat(user, notNullValue()); |
| 414 | + assertThat(user.roles(), arrayContaining("_role_13")); |
| 415 | + |
| 416 | + future = new PlainActionFuture<>(); |
| 417 | + realm.authenticate(new UsernamePasswordToken("CN=ironman", new SecureString(PASSWORD)), future); |
| 418 | + result = future.actionGet(); |
| 419 | + assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); |
| 420 | + user = result.getUser(); |
| 421 | + assertThat(user, notNullValue()); |
| 422 | + assertThat(user.roles(), arrayContaining("_role_12")); |
| 423 | + } |
| 424 | + |
357 | 425 | public void testRealmUsageStats() throws Exception { |
358 | 426 | final RealmConfig.RealmIdentifier realmId = realmId("testRealmUsageStats"); |
359 | 427 | String loadBalanceType = randomFrom("failover", "round_robin"); |
@@ -469,7 +537,8 @@ private Settings settings(RealmConfig.RealmIdentifier realmIdentifier, Settings |
469 | 537 | builder.put(getFullSettingKey(realmIdentifier, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), |
470 | 538 | VerificationMode.CERTIFICATE); |
471 | 539 | } else { |
472 | | - builder.put(getFullSettingKey(realmIdentifier, HOSTNAME_VERIFICATION_SETTING), false); |
| 540 | + builder.put(getFullSettingKey(realmIdentifier, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), |
| 541 | + VerificationMode.NONE); |
473 | 542 | } |
474 | 543 | return builder.put(extraSettings).build(); |
475 | 544 | } |
|
0 commit comments