|
8 | 8 | import com.unboundid.ldap.listener.InMemoryDirectoryServer; |
9 | 9 | import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; |
10 | 10 | import com.unboundid.ldap.sdk.Attribute; |
| 11 | +import com.unboundid.ldap.sdk.FailoverServerSet; |
11 | 12 | import com.unboundid.ldap.sdk.LDAPException; |
12 | 13 | import com.unboundid.ldap.sdk.LDAPURL; |
| 14 | +import com.unboundid.ldap.sdk.SingleServerSet; |
13 | 15 | import com.unboundid.ldap.sdk.schema.Schema; |
14 | 16 | import org.elasticsearch.action.ActionListener; |
15 | 17 | import org.elasticsearch.action.support.PlainActionFuture; |
|
29 | 31 | import org.elasticsearch.xpack.core.security.authc.ldap.ActiveDirectorySessionFactorySettings; |
30 | 32 | import org.elasticsearch.xpack.core.security.authc.ldap.LdapRealmSettings; |
31 | 33 | import org.elasticsearch.xpack.core.security.authc.ldap.PoolingSessionFactorySettings; |
| 34 | +import org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings; |
32 | 35 | import org.elasticsearch.xpack.core.security.authc.support.CachingUsernamePasswordRealmSettings; |
33 | 36 | import org.elasticsearch.xpack.core.security.authc.support.DnRoleMapperSettings; |
34 | 37 | import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; |
|
52 | 55 | import static org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings.URLS_SETTING; |
53 | 56 | import static org.hamcrest.Matchers.arrayContaining; |
54 | 57 | import static org.hamcrest.Matchers.arrayContainingInAnyOrder; |
| 58 | +import static org.hamcrest.Matchers.arrayWithSize; |
55 | 59 | import static org.hamcrest.Matchers.containsString; |
56 | 60 | import static org.hamcrest.Matchers.equalTo; |
57 | 61 | import static org.hamcrest.Matchers.hasEntry; |
| 62 | +import static org.hamcrest.Matchers.instanceOf; |
58 | 63 | import static org.hamcrest.Matchers.is; |
59 | 64 | import static org.hamcrest.Matchers.notNullValue; |
60 | 65 | import static org.mockito.Matchers.any; |
@@ -354,6 +359,48 @@ public void testCustomSearchFilters() throws Exception { |
354 | 359 | assertEquals("(objectClass=down level)", sessionFactory.downLevelADAuthenticator.getUserSearchFilter()); |
355 | 360 | } |
356 | 361 |
|
| 362 | + public void testBuildUrlFromDomainNameAndDefaultPort() throws Exception { |
| 363 | + Settings settings = Settings.builder() |
| 364 | + .put(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING, "ad.test.elasticsearch.com") |
| 365 | + .build(); |
| 366 | + RealmConfig config = new RealmConfig("testBuildUrlFromDomainNameAndDefaultPort", settings, globalSettings, |
| 367 | + TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); |
| 368 | + ActiveDirectorySessionFactory sessionFactory = new ActiveDirectorySessionFactory(config, sslService, threadPool); |
| 369 | + assertSingleLdapServer(sessionFactory, "ad.test.elasticsearch.com", 389); |
| 370 | + } |
| 371 | + |
| 372 | + public void testBuildUrlFromDomainNameAndCustomPort() throws Exception { |
| 373 | + Settings settings = Settings.builder() |
| 374 | + .put(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING, "ad.test.elasticsearch.com") |
| 375 | + .put(ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING.getKey(), 10389) |
| 376 | + .build(); |
| 377 | + RealmConfig config = new RealmConfig("testBuildUrlFromDomainNameAndCustomPort", settings, globalSettings, |
| 378 | + TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); |
| 379 | + ActiveDirectorySessionFactory sessionFactory = new ActiveDirectorySessionFactory(config, sslService, threadPool); |
| 380 | + assertSingleLdapServer(sessionFactory, "ad.test.elasticsearch.com", 10389); |
| 381 | + } |
| 382 | + |
| 383 | + public void testUrlConfiguredInSettings() throws Exception { |
| 384 | + Settings settings = Settings.builder() |
| 385 | + .put(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING, "ad.test.elasticsearch.com") |
| 386 | + .put(SessionFactorySettings.URLS_SETTING, "ldap://ad01.testing.elastic.co:20389/") |
| 387 | + .build(); |
| 388 | + RealmConfig config = new RealmConfig("testBuildUrlFromDomainNameAndCustomPort", settings, globalSettings, |
| 389 | + TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); |
| 390 | + ActiveDirectorySessionFactory sessionFactory = new ActiveDirectorySessionFactory(config, sslService, threadPool); |
| 391 | + assertSingleLdapServer(sessionFactory, "ad01.testing.elastic.co", 20389); |
| 392 | + } |
| 393 | + |
| 394 | + private void assertSingleLdapServer(ActiveDirectorySessionFactory sessionFactory, String hostname, int port) { |
| 395 | + assertThat(sessionFactory.getServerSet(), instanceOf(FailoverServerSet.class)); |
| 396 | + FailoverServerSet fss = (FailoverServerSet) sessionFactory.getServerSet(); |
| 397 | + assertThat(fss.getServerSets(), arrayWithSize(1)); |
| 398 | + assertThat(fss.getServerSets()[0], instanceOf(SingleServerSet.class)); |
| 399 | + SingleServerSet sss = (SingleServerSet) fss.getServerSets()[0]; |
| 400 | + assertThat(sss.getAddress(), equalTo(hostname)); |
| 401 | + assertThat(sss.getPort(), equalTo(port)); |
| 402 | + } |
| 403 | + |
357 | 404 | private Settings settings() throws Exception { |
358 | 405 | return settings(Settings.EMPTY); |
359 | 406 | } |
|
0 commit comments