|
8 | 8 | import com.unboundid.ldap.listener.InMemoryDirectoryServer; |
9 | 9 | import com.unboundid.ldap.sdk.LDAPConnection; |
10 | 10 | import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.common.SuppressForbidden; |
11 | 12 | import org.elasticsearch.common.settings.SecureString; |
12 | 13 | import org.elasticsearch.common.settings.Settings; |
13 | 14 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
|
17 | 18 | import org.elasticsearch.test.junit.annotations.TestLogging; |
18 | 19 | import org.elasticsearch.threadpool.TestThreadPool; |
19 | 20 | import org.elasticsearch.threadpool.ThreadPool; |
| 21 | +import org.elasticsearch.xpack.core.common.socket.SocketAccess; |
20 | 22 | import org.elasticsearch.xpack.core.security.authc.RealmConfig; |
21 | 23 | import org.elasticsearch.xpack.core.security.authc.ldap.support.LdapSearchScope; |
22 | 24 | import org.elasticsearch.xpack.core.ssl.SSLService; |
|
25 | 27 |
|
26 | 28 | import java.io.IOException; |
27 | 29 | import java.net.InetAddress; |
| 30 | +import java.net.InetSocketAddress; |
28 | 31 | import java.net.Socket; |
29 | 32 | import java.util.ArrayList; |
30 | 33 | import java.util.Arrays; |
@@ -112,7 +115,7 @@ public void testRoundRobinWithFailures() throws Exception { |
112 | 115 | // of the ldap server and the opening of the socket |
113 | 116 | logger.debug("opening mock server socket listening on [{}]", port); |
114 | 117 | Runnable runnable = () -> { |
115 | | - try (Socket socket = new MockSocket(InetAddress.getByName("localhost"), mockServerSocket.getLocalPort(), local, port)) { |
| 118 | + try (Socket socket = openMockSocket(local, mockServerSocket.getLocalPort(), local, port)) { |
116 | 119 | logger.debug("opened socket [{}]", socket); |
117 | 120 | latch.countDown(); |
118 | 121 | closeLatch.await(); |
@@ -149,6 +152,17 @@ public void testRoundRobinWithFailures() throws Exception { |
149 | 152 | } |
150 | 153 | } |
151 | 154 |
|
| 155 | + @SuppressForbidden(reason = "Allow opening socket for test") |
| 156 | + private MockSocket openMockSocket(InetAddress remoteAddress, int remotePort, InetAddress localAddress, int localPort) |
| 157 | + throws IOException { |
| 158 | + final MockSocket socket = new MockSocket(); |
| 159 | + socket.setReuseAddress(true); // allow binding even if the previous socket is in timed wait state. |
| 160 | + socket.setSoLinger(true, 0); // close immediately as we are not writing anything here. |
| 161 | + socket.bind(new InetSocketAddress(localAddress, localPort)); |
| 162 | + SocketAccess.doPrivileged(() -> socket.connect(new InetSocketAddress(localAddress, remotePort))); |
| 163 | + return socket; |
| 164 | + } |
| 165 | + |
152 | 166 | public void testFailover() throws Exception { |
153 | 167 | assumeTrue("at least one ldap server should be present for this test", ldapServers.length > 1); |
154 | 168 | logger.debug("using [{}] ldap servers, urls {}", ldapServers.length, ldapUrls()); |
|
0 commit comments