1616import com .unboundid .ldap .sdk .LDAPInterface ;
1717import com .unboundid .ldap .sdk .LDAPURL ;
1818import com .unboundid .ldap .sdk .SimpleBindRequest ;
19+
1920import org .elasticsearch .ExceptionsHelper ;
2021import org .elasticsearch .action .support .PlainActionFuture ;
2122import org .elasticsearch .common .Strings ;
5657import java .security .PrivilegedExceptionAction ;
5758import java .util .ArrayList ;
5859import java .util .List ;
60+ import java .util .Locale ;
5961import java .util .Objects ;
62+ import java .util .stream .Collectors ;
6063
6164import static org .elasticsearch .xpack .core .security .authc .RealmSettings .getFullSettingKey ;
6265import static org .elasticsearch .xpack .core .security .authc .ldap .support .SessionFactorySettings .HOSTNAME_VERIFICATION_SETTING ;
@@ -106,6 +109,18 @@ public void startLdap() throws Exception {
106109 ldapServer .startListening ();
107110 return null ;
108111 });
112+ String listenerConfig = listeners .stream ()
113+ .map (
114+ l -> String .format (
115+ Locale .ROOT ,
116+ "(%s @ %s:%d)" ,
117+ l .getListenerName (),
118+ NetworkAddress .format (resolveListenAddress (l .getListenAddress ())),
119+ ldapServer .getListenPort (l .getListenerName ())
120+ )
121+ )
122+ .collect (Collectors .joining ("," ));
123+ logger .info ("Started in-memory LDAP server [#{}] with listeners: [{}]" , i , listenerConfig );
109124 ldapServers [i ] = ldapServer ;
110125 }
111126 }
@@ -117,23 +132,29 @@ protected boolean openLdapsPort() {
117132 @ After
118133 public void stopLdap () {
119134 for (int i = 0 ; i < numberOfLdapServers ; i ++) {
135+ logger .info ("Shutting down in-memory LDAP server [#{}]" , i );
120136 ldapServers [i ].shutDown (true );
121137 }
122138 }
123139
124140 protected String [] ldapUrls () throws LDAPException {
125141 List <String > urls = new ArrayList <>(numberOfLdapServers );
126142 for (int i = 0 ; i < numberOfLdapServers ; i ++) {
127- InetAddress listenAddress = ldapServers [i ].getListenAddress ();
128- if (listenAddress == null ) {
129- listenAddress = InetAddress .getLoopbackAddress ();
130- }
143+ InetAddress listenAddress = resolveListenAddress (ldapServers [i ].getListenAddress ());
131144 LDAPURL url = new LDAPURL ("ldap" , NetworkAddress .format (listenAddress ), ldapServers [i ].getListenPort (), null , null , null , null );
132145 urls .add (url .toString ());
133146 }
134147 return urls .toArray (Strings .EMPTY_ARRAY );
135148 }
136149
150+ private InetAddress resolveListenAddress (InetAddress configuredAddress ) {
151+ InetAddress listenAddress = configuredAddress ;
152+ if (listenAddress != null ) {
153+ return listenAddress ;
154+ }
155+ return InetAddress .getLoopbackAddress ();
156+ }
157+
137158 public static Settings buildLdapSettings (String ldapUrl , String userTemplate , String groupSearchBase , LdapSearchScope scope ) {
138159 return buildLdapSettings (new String []{ldapUrl }, new String []{userTemplate }, groupSearchBase , scope );
139160 }
0 commit comments