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