Skip to content

Commit 1a53cd1

Browse files
committed
Fix building AD URL from domain name (#31849)
The steps to read the settings and build URLs happen in a non-obvious order, which meant that we would build the default URL (from the domain name, and port) before we'd actually read the port settings. This would cause the URL to always have a port of `0`. Relates: bccf988
1 parent 359d259 commit 1a53cd1

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactory.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.unboundid.ldap.sdk.LDAPException;
1212
import com.unboundid.ldap.sdk.LDAPInterface;
1313
import com.unboundid.ldap.sdk.SearchResultEntry;
14+
import com.unboundid.ldap.sdk.ServerSet;
1415
import com.unboundid.ldap.sdk.SimpleBindRequest;
1516
import com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl;
1617
import org.apache.logging.log4j.Logger;
@@ -62,8 +63,6 @@ class ActiveDirectorySessionFactory extends PoolingSessionFactory {
6263
final DownLevelADAuthenticator downLevelADAuthenticator;
6364
final UpnADAuthenticator upnADAuthenticator;
6465

65-
private final int ldapPort;
66-
6766
ActiveDirectorySessionFactory(RealmConfig config, SSLService sslService, ThreadPool threadPool) throws LDAPException {
6867
super(config, sslService, new ActiveDirectoryGroupsResolver(config.settings()),
6968
ActiveDirectorySessionFactorySettings.POOL_ENABLED,
@@ -85,7 +84,7 @@ class ActiveDirectorySessionFactory extends PoolingSessionFactory {
8584
+ "] setting for active directory");
8685
}
8786
String domainDN = buildDnFromDomain(domainName);
88-
ldapPort = ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING.get(settings);
87+
final int ldapPort = ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING.get(settings);
8988
final int ldapsPort = ActiveDirectorySessionFactorySettings.AD_LDAPS_PORT_SETTING.get(settings);
9089
final int gcLdapPort = ActiveDirectorySessionFactorySettings.AD_GC_LDAP_PORT_SETTING.get(settings);
9190
final int gcLdapsPort = ActiveDirectorySessionFactorySettings.AD_GC_LDAPS_PORT_SETTING.get(settings);
@@ -102,7 +101,7 @@ class ActiveDirectorySessionFactory extends PoolingSessionFactory {
102101
@Override
103102
protected List<String> getDefaultLdapUrls(Settings settings) {
104103
return Collections.singletonList("ldap://" + settings.get(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING) +
105-
":" + ldapPort);
104+
":" + ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING.get(settings));
106105
}
107106

108107
@Override
@@ -197,6 +196,11 @@ static String getBindDN(Settings settings) {
197196
return bindDN;
198197
}
199198

199+
// Exposed for testing
200+
ServerSet getServerSet() {
201+
return super.serverSet;
202+
}
203+
200204
ADAuthenticator getADAuthenticator(String username) {
201205
if (username.indexOf('\\') > 0) {
202206
return downLevelADAuthenticator;

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRealmTests.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
99
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
1010
import com.unboundid.ldap.sdk.Attribute;
11+
import com.unboundid.ldap.sdk.FailoverServerSet;
1112
import com.unboundid.ldap.sdk.LDAPException;
1213
import com.unboundid.ldap.sdk.LDAPURL;
14+
import com.unboundid.ldap.sdk.SingleServerSet;
1315
import com.unboundid.ldap.sdk.schema.Schema;
1416
import org.elasticsearch.action.ActionListener;
1517
import org.elasticsearch.action.support.PlainActionFuture;
@@ -29,6 +31,7 @@
2931
import org.elasticsearch.xpack.core.security.authc.ldap.ActiveDirectorySessionFactorySettings;
3032
import org.elasticsearch.xpack.core.security.authc.ldap.LdapRealmSettings;
3133
import org.elasticsearch.xpack.core.security.authc.ldap.PoolingSessionFactorySettings;
34+
import org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings;
3235
import org.elasticsearch.xpack.core.security.authc.support.CachingUsernamePasswordRealmSettings;
3336
import org.elasticsearch.xpack.core.security.authc.support.DnRoleMapperSettings;
3437
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
@@ -52,9 +55,11 @@
5255
import static org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings.URLS_SETTING;
5356
import static org.hamcrest.Matchers.arrayContaining;
5457
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
58+
import static org.hamcrest.Matchers.arrayWithSize;
5559
import static org.hamcrest.Matchers.containsString;
5660
import static org.hamcrest.Matchers.equalTo;
5761
import static org.hamcrest.Matchers.hasEntry;
62+
import static org.hamcrest.Matchers.instanceOf;
5863
import static org.hamcrest.Matchers.is;
5964
import static org.hamcrest.Matchers.notNullValue;
6065
import static org.mockito.Matchers.any;
@@ -354,6 +359,48 @@ public void testCustomSearchFilters() throws Exception {
354359
assertEquals("(objectClass=down level)", sessionFactory.downLevelADAuthenticator.getUserSearchFilter());
355360
}
356361

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+
357404
private Settings settings() throws Exception {
358405
return settings(Settings.EMPTY);
359406
}

0 commit comments

Comments
 (0)