|
13 | 13 | import org.apache.kerby.kerberos.kerb.client.KrbConfig; |
14 | 14 | import org.apache.kerby.kerberos.kerb.server.KdcConfigKey; |
15 | 15 | import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; |
16 | | -import org.apache.kerby.util.NetworkUtil; |
17 | 16 | import org.apache.logging.log4j.LogManager; |
18 | 17 | import org.apache.logging.log4j.Logger; |
19 | 18 | import org.elasticsearch.ExceptionsHelper; |
|
22 | 21 | import org.elasticsearch.test.ESTestCase; |
23 | 22 |
|
24 | 23 | import java.io.IOException; |
| 24 | +import java.net.DatagramSocket; |
| 25 | +import java.net.InetAddress; |
| 26 | +import java.net.ServerSocket; |
25 | 27 | import java.nio.charset.StandardCharsets; |
26 | 28 | import java.nio.file.Files; |
27 | 29 | import java.nio.file.Path; |
|
31 | 33 | import java.util.Locale; |
32 | 34 | import java.util.concurrent.TimeUnit; |
33 | 35 |
|
| 36 | +import javax.net.ServerSocketFactory; |
| 37 | + |
34 | 38 | /** |
35 | 39 | * Utility wrapper around Apache {@link SimpleKdcServer} backed by Unboundid |
36 | 40 | * {@link InMemoryDirectoryServer}.<br> |
@@ -127,14 +131,14 @@ private void prepareKdcServerAndStart() throws Exception { |
127 | 131 | simpleKdc.setWorkDir(workDir.toFile()); |
128 | 132 | simpleKdc.setKdcHost(host); |
129 | 133 | simpleKdc.setKdcRealm(realm); |
130 | | - if (kdcPort == 0) { |
131 | | - kdcPort = NetworkUtil.getServerPort(); |
132 | | - } |
133 | 134 | if (transport != null) { |
134 | | - if (transport.trim().equals("TCP")) { |
| 135 | + if (kdcPort == 0) { |
| 136 | + kdcPort = getServerPort(transport); |
| 137 | + } |
| 138 | + if (transport.trim().equalsIgnoreCase("TCP")) { |
135 | 139 | simpleKdc.setKdcTcpPort(kdcPort); |
136 | 140 | simpleKdc.setAllowUdp(false); |
137 | | - } else if (transport.trim().equals("UDP")) { |
| 141 | + } else if (transport.trim().equalsIgnoreCase("UDP")) { |
138 | 142 | simpleKdc.setKdcUdpPort(kdcPort); |
139 | 143 | simpleKdc.setAllowTcp(false); |
140 | 144 | } else { |
@@ -221,4 +225,21 @@ public Void run() throws Exception { |
221 | 225 | logger.info("SimpleKdcServer stoppped."); |
222 | 226 | } |
223 | 227 |
|
| 228 | + private static int getServerPort(String transport) { |
| 229 | + if (transport != null && transport.trim().equalsIgnoreCase("TCP")) { |
| 230 | + try (ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0, 1, |
| 231 | + InetAddress.getByName("127.0.0.1"))) { |
| 232 | + return serverSocket.getLocalPort(); |
| 233 | + } catch (Exception ex) { |
| 234 | + throw new RuntimeException("Failed to get a TCP server socket point"); |
| 235 | + } |
| 236 | + } else if (transport != null && transport.trim().equalsIgnoreCase("UDP")) { |
| 237 | + try (DatagramSocket socket = new DatagramSocket(0, InetAddress.getByName("127.0.0.1"))) { |
| 238 | + return socket.getLocalPort(); |
| 239 | + } catch (Exception ex) { |
| 240 | + throw new RuntimeException("Failed to get a UDP server socket point"); |
| 241 | + } |
| 242 | + } |
| 243 | + throw new IllegalArgumentException("Invalid transport: " + transport); |
| 244 | + } |
224 | 245 | } |
0 commit comments