Skip to content

Commit f012ea4

Browse files
tvernumelasticsearchmachine
authored andcommitted
[TEST] Add logging when LDAP server starts/stops (elastic#75322)
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: elastic#75176
1 parent d117e7f commit f012ea4

File tree

1 file changed

+25
-4
lines changed
  • x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support

1 file changed

+25
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.unboundid.ldap.sdk.LDAPInterface;
1717
import com.unboundid.ldap.sdk.LDAPURL;
1818
import com.unboundid.ldap.sdk.SimpleBindRequest;
19+
1920
import org.elasticsearch.ExceptionsHelper;
2021
import org.elasticsearch.action.support.PlainActionFuture;
2122
import org.elasticsearch.common.Strings;
@@ -56,7 +57,9 @@
5657
import java.security.PrivilegedExceptionAction;
5758
import java.util.ArrayList;
5859
import java.util.List;
60+
import java.util.Locale;
5961
import java.util.Objects;
62+
import java.util.stream.Collectors;
6063

6164
import static org.elasticsearch.xpack.core.security.authc.RealmSettings.getFullSettingKey;
6265
import 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

Comments
 (0)