Skip to content

Commit

Permalink
[TEST] Add logging when LDAP server starts/stops (#75322)
Browse files Browse the repository at this point in the history
This commit adds logging to the in memory LDAP server in LdapTestCase
that indicates when the server starts and stops and the addresses on
which it is listening.

Relates: #75176
  • Loading branch information
tvernum authored Jul 14, 2021
1 parent 71e0583 commit f1aa1cf
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.unboundid.ldap.sdk.LDAPInterface;
import com.unboundid.ldap.sdk.LDAPURL;
import com.unboundid.ldap.sdk.SimpleBindRequest;

import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -57,7 +58,9 @@
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.elasticsearch.xpack.core.security.authc.RealmSettings.getFullSettingKey;
import static org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING;
Expand Down Expand Up @@ -107,6 +110,18 @@ public void startLdap() throws Exception {
ldapServer.startListening();
return null;
});
String listenerConfig = listeners.stream()
.map(
l -> String.format(
Locale.ROOT,
"(%s @ %s:%d)",
l.getListenerName(),
NetworkAddress.format(resolveListenAddress(l.getListenAddress())),
ldapServer.getListenPort(l.getListenerName())
)
)
.collect(Collectors.joining(","));
logger.info("Started in-memory LDAP server [#{}] with listeners: [{}]", i, listenerConfig);
ldapServers[i] = ldapServer;
}
}
Expand All @@ -118,23 +133,29 @@ protected boolean openLdapsPort() {
@After
public void stopLdap() {
for (int i = 0; i < numberOfLdapServers; i++) {
logger.info("Shutting down in-memory LDAP server [#{}]", i);
ldapServers[i].shutDown(true);
}
}

protected String[] ldapUrls() throws LDAPException {
List<String> urls = new ArrayList<>(numberOfLdapServers);
for (int i = 0; i < numberOfLdapServers; i++) {
InetAddress listenAddress = ldapServers[i].getListenAddress();
if (listenAddress == null) {
listenAddress = InetAddress.getLoopbackAddress();
}
InetAddress listenAddress = resolveListenAddress(ldapServers[i].getListenAddress());
LDAPURL url = new LDAPURL("ldap", NetworkAddress.format(listenAddress), ldapServers[i].getListenPort(), null, null, null, null);
urls.add(url.toString());
}
return urls.toArray(Strings.EMPTY_ARRAY);
}

private InetAddress resolveListenAddress(InetAddress configuredAddress) {
InetAddress listenAddress = configuredAddress;
if (listenAddress != null) {
return listenAddress;
}
return InetAddress.getLoopbackAddress();
}

public static Settings buildLdapSettings(String ldapUrl, String userTemplate, String groupSearchBase, LdapSearchScope scope) {
return buildLdapSettings(new String[]{ldapUrl}, new String[]{userTemplate}, groupSearchBase, scope);
}
Expand Down

0 comments on commit f1aa1cf

Please sign in to comment.