Skip to content

Commit 04d5dcb

Browse files
committed
Fix 'BindException' in some tests
Implement getting free port in tests: SinkProviderTLSTest and WebhookAuditLogTest Signed-off-by: rs-eliatra <rafal.stobiecki@eliatra.com>
1 parent de4bc1d commit 04d5dcb

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

src/test/java/org/opensearch/security/auditlog/sink/SinkProviderTLSTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package org.opensearch.security.auditlog.sink;
1717

1818
import java.io.FileInputStream;
19+
import java.io.IOException;
1920
import java.io.InputStream;
21+
import java.net.ServerSocket;
2022
import java.security.KeyStore;
2123

2224
import javax.net.ssl.KeyManagerFactory;
@@ -59,7 +61,8 @@ public void testTlsConfigurationNoFallback() throws Exception {
5961

6062
TestHttpHandler handler = new TestHttpHandler();
6163

62-
server = ServerBootstrap.bootstrap().setListenerPort(8083).setServerInfo("Test/1.1").setSslContext(createSSLContext()).registerHandler("*", handler).create();
64+
int port = findFreePort();
65+
server = ServerBootstrap.bootstrap().setListenerPort(port).setServerInfo("Test/1.1").setSslContext(createSSLContext()).registerHandler("*", handler).create();
6366

6467
server.start();
6568

@@ -71,6 +74,11 @@ public void testTlsConfigurationNoFallback() throws Exception {
7174
builder.put("plugins.security.audit.endpoints.endpoint1.config.webhook.ssl.pemtrustedcas_filepath", FileHelper.getAbsoluteFilePathFromClassPath("auditlog/root-ca.pem"));
7275
builder.put("plugins.security.audit.endpoints.endpoint2.config.webhook.ssl.pemtrustedcas_content", FileHelper.loadFile("auditlog/root-ca.pem"));
7376

77+
builder.put("plugins.security.audit.config.webhook.url", "https://localhost:" + port);
78+
builder.put("plugins.security.audit.endpoints.endpoint1.config.webhook.url", "https://localhost:" + port);
79+
builder.put("plugins.security.audit.endpoints.endpoint2.config.webhook.url", "https://localhost:" + port);
80+
81+
7482
SinkProvider provider = new SinkProvider(builder.build(), null, null, null);
7583
WebhookSink defaultSink = (WebhookSink) provider.defaultSink;
7684
Assert.assertEquals(true, defaultSink.verifySSL);
@@ -141,4 +149,12 @@ private void assertStringContainsAllKeysAndValues(String in) {
141149
Assert.assertTrue(in, in.contains("8.8.8.8"));
142150
//Assert.assertTrue(in, in.contains("CN=kirk,OU=client,O=client,L=test,C=DE"));
143151
}
152+
153+
private int findFreePort() {
154+
try (ServerSocket serverSocket = new ServerSocket(0)) {
155+
return serverSocket.getLocalPort();
156+
} catch (IOException e) {
157+
throw new RuntimeException("Failed to find free port", e);
158+
}
159+
}
144160
}

src/test/java/org/opensearch/security/auditlog/sink/WebhookAuditLogTest.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package org.opensearch.security.auditlog.sink;
1717

1818
import java.io.FileInputStream;
19+
import java.io.IOException;
1920
import java.io.InputStream;
21+
import java.net.ServerSocket;
2022
import java.net.URLDecoder;
2123
import java.nio.charset.StandardCharsets;
2224
import java.security.KeyStore;
@@ -222,15 +224,16 @@ public void noServerRunningHttpTest() throws Exception {
222224
public void postGetHttpTest() throws Exception {
223225
TestHttpHandler handler = new TestHttpHandler();
224226

227+
int port = findFreePort();
225228
server = ServerBootstrap.bootstrap()
226-
.setListenerPort(8080)
229+
.setListenerPort(port)
227230
.setServerInfo("Test/1.1")
228231
.registerHandler("*", handler)
229232
.create();
230233

231234
server.start();
232235

233-
String url = "http://localhost:8080/endpoint";
236+
String url = "http://localhost:" + port + "/endpoint";
234237

235238
// SLACK
236239
Settings settings = Settings.builder()
@@ -327,15 +330,16 @@ public void httpsTestWithoutTLSServer() throws Exception {
327330

328331
TestHttpHandler handler = new TestHttpHandler();
329332

333+
int port = findFreePort();
330334
server = ServerBootstrap.bootstrap()
331-
.setListenerPort(8081)
335+
.setListenerPort(port)
332336
.setServerInfo("Test/1.1")
333337
.registerHandler("*", handler)
334338
.create();
335339

336340
server.start();
337341

338-
String url = "https://localhost:8081/endpoint";
342+
String url = "https://localhost:" + port + "/endpoint";
339343

340344
Settings settings = Settings.builder()
341345
.put("plugins.security.audit.config.webhook.url", url)
@@ -363,9 +367,9 @@ public void httpsTestWithoutTLSServer() throws Exception {
363367
public void httpsTest() throws Exception {
364368

365369
TestHttpHandler handler = new TestHttpHandler();
366-
370+
int port = findFreePort();
367371
server = ServerBootstrap.bootstrap()
368-
.setListenerPort(8090)
372+
.setListenerPort(port)
369373
.setServerInfo("Test/1.1")
370374
.setSslContext(createSSLContext())
371375
.registerHandler("*", handler)
@@ -374,7 +378,7 @@ public void httpsTest() throws Exception {
374378
server.start();
375379
AuditMessage msg = MockAuditMessageFactory.validAuditMessage();
376380

377-
String url = "https://localhost:8090/endpoint";
381+
String url = "https://localhost:" + port + "/endpoint";
378382

379383
// try with ssl verification on, no trust ca, must fail
380384
Settings settings = Settings.builder()
@@ -445,8 +449,8 @@ public void httpsTest() throws Exception {
445449

446450
@Test
447451
public void httpsTestPemDefault() throws Exception {
448-
final int port = 8088;
449-
TestHttpHandler handler = new TestHttpHandler();
452+
final int port = findFreePort();
453+
TestHttpHandler handler = new TestHttpHandler();
450454

451455
server = ServerBootstrap.bootstrap()
452456
.setListenerPort(port)
@@ -561,9 +565,10 @@ public void httpsTestPemDefault() throws Exception {
561565
public void httpsTestPemEndpoint() throws Exception {
562566

563567
TestHttpHandler handler = new TestHttpHandler();
568+
int port = findFreePort();
564569

565570
server = ServerBootstrap.bootstrap()
566-
.setListenerPort(8091)
571+
.setListenerPort(port)
567572
.setServerInfo("Test/1.1")
568573
.setSslContext(createSSLContext())
569574
.registerHandler("*", handler)
@@ -573,7 +578,7 @@ public void httpsTestPemEndpoint() throws Exception {
573578
AuditMessage msg = MockAuditMessageFactory.validAuditMessage();
574579
LoggingSink fallback = new LoggingSink("test", Settings.EMPTY, null, null);
575580

576-
String url = "https://localhost:8091/endpoint";
581+
String url = "https://localhost:" + port + "/endpoint";
577582

578583
// test default with filepath
579584
handler.reset();
@@ -658,9 +663,10 @@ public void httpsTestPemEndpoint() throws Exception {
658663
public void httpsTestPemContentEndpoint() throws Exception {
659664

660665
TestHttpHandler handler = new TestHttpHandler();
666+
int port = findFreePort();
661667

662668
server = ServerBootstrap.bootstrap()
663-
.setListenerPort(8086)
669+
.setListenerPort(port)
664670
.setServerInfo("Test/1.1")
665671
.setSslContext(createSSLContext())
666672
.registerHandler("*", handler)
@@ -670,7 +676,7 @@ public void httpsTestPemContentEndpoint() throws Exception {
670676
AuditMessage msg = MockAuditMessageFactory.validAuditMessage();
671677
LoggingSink fallback = new LoggingSink("test", Settings.EMPTY, null, null);
672678

673-
String url = "https://localhost:8086/endpoint";
679+
String url = "https://localhost:" + port + "/endpoint";
674680

675681
// test with filecontent
676682
handler.reset();
@@ -731,4 +737,12 @@ private void assertStringContainsAllKeysAndValues(String in) {
731737
Assert.assertTrue(in, in.contains("8.8.8.8"));
732738
//Assert.assertTrue(in, in.contains("CN=kirk,OU=client,O=client,L=test,C=DE"));
733739
}
740+
741+
private int findFreePort() {
742+
try (ServerSocket serverSocket = new ServerSocket(0)) {
743+
return serverSocket.getLocalPort();
744+
} catch (IOException e) {
745+
throw new RuntimeException("Failed to find free port", e);
746+
}
747+
}
734748
}

0 commit comments

Comments
 (0)