From bd8c297cbcb4798f447adc4d172298bcd6915e15 Mon Sep 17 00:00:00 2001 From: rs-eliatra Date: Tue, 22 Feb 2022 21:33:48 +0100 Subject: [PATCH] Fix 'BindException' in some tests Implement getting free port in tests: SinkProviderTLSTest and WebhookAuditLogTest Signed-off-by: rs-eliatra --- .../auditlog/sink/SinkProviderTLSTest.java | 18 ++++++++- .../auditlog/sink/WebhookAuditLogTest.java | 40 +++++++++++++------ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/opensearch/security/auditlog/sink/SinkProviderTLSTest.java b/src/test/java/org/opensearch/security/auditlog/sink/SinkProviderTLSTest.java index 4f71ca577c..65ef61f35d 100644 --- a/src/test/java/org/opensearch/security/auditlog/sink/SinkProviderTLSTest.java +++ b/src/test/java/org/opensearch/security/auditlog/sink/SinkProviderTLSTest.java @@ -16,7 +16,9 @@ package org.opensearch.security.auditlog.sink; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; +import java.net.ServerSocket; import java.security.KeyStore; import javax.net.ssl.KeyManagerFactory; @@ -59,7 +61,8 @@ public void testTlsConfigurationNoFallback() throws Exception { TestHttpHandler handler = new TestHttpHandler(); - server = ServerBootstrap.bootstrap().setListenerPort(8083).setServerInfo("Test/1.1").setSslContext(createSSLContext()).registerHandler("*", handler).create(); + int port = findFreePort(); + server = ServerBootstrap.bootstrap().setListenerPort(port).setServerInfo("Test/1.1").setSslContext(createSSLContext()).registerHandler("*", handler).create(); server.start(); @@ -71,6 +74,11 @@ public void testTlsConfigurationNoFallback() throws Exception { builder.put("plugins.security.audit.endpoints.endpoint1.config.webhook.ssl.pemtrustedcas_filepath", FileHelper.getAbsoluteFilePathFromClassPath("auditlog/root-ca.pem")); builder.put("plugins.security.audit.endpoints.endpoint2.config.webhook.ssl.pemtrustedcas_content", FileHelper.loadFile("auditlog/root-ca.pem")); + builder.put("plugins.security.audit.config.webhook.url", "https://localhost:" + port); + builder.put("plugins.security.audit.endpoints.endpoint1.config.webhook.url", "https://localhost:" + port); + builder.put("plugins.security.audit.endpoints.endpoint2.config.webhook.url", "https://localhost:" + port); + + SinkProvider provider = new SinkProvider(builder.build(), null, null, null); WebhookSink defaultSink = (WebhookSink) provider.defaultSink; Assert.assertEquals(true, defaultSink.verifySSL); @@ -141,4 +149,12 @@ private void assertStringContainsAllKeysAndValues(String in) { Assert.assertTrue(in, in.contains("8.8.8.8")); //Assert.assertTrue(in, in.contains("CN=kirk,OU=client,O=client,L=test,C=DE")); } + + private int findFreePort() { + try (ServerSocket serverSocket = new ServerSocket(0)) { + return serverSocket.getLocalPort(); + } catch (IOException e) { + throw new RuntimeException("Failed to find free port", e); + } + } } diff --git a/src/test/java/org/opensearch/security/auditlog/sink/WebhookAuditLogTest.java b/src/test/java/org/opensearch/security/auditlog/sink/WebhookAuditLogTest.java index e28f818397..2efe9ad40c 100644 --- a/src/test/java/org/opensearch/security/auditlog/sink/WebhookAuditLogTest.java +++ b/src/test/java/org/opensearch/security/auditlog/sink/WebhookAuditLogTest.java @@ -16,7 +16,9 @@ package org.opensearch.security.auditlog.sink; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; +import java.net.ServerSocket; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.security.KeyStore; @@ -222,15 +224,16 @@ public void noServerRunningHttpTest() throws Exception { public void postGetHttpTest() throws Exception { TestHttpHandler handler = new TestHttpHandler(); + int port = findFreePort(); server = ServerBootstrap.bootstrap() - .setListenerPort(8080) + .setListenerPort(port) .setServerInfo("Test/1.1") .registerHandler("*", handler) .create(); server.start(); - String url = "http://localhost:8080/endpoint"; + String url = "http://localhost:" + port + "/endpoint"; // SLACK Settings settings = Settings.builder() @@ -327,15 +330,16 @@ public void httpsTestWithoutTLSServer() throws Exception { TestHttpHandler handler = new TestHttpHandler(); + int port = findFreePort(); server = ServerBootstrap.bootstrap() - .setListenerPort(8081) + .setListenerPort(port) .setServerInfo("Test/1.1") .registerHandler("*", handler) .create(); server.start(); - String url = "https://localhost:8081/endpoint"; + String url = "https://localhost:" + port + "/endpoint"; Settings settings = Settings.builder() .put("plugins.security.audit.config.webhook.url", url) @@ -363,9 +367,9 @@ public void httpsTestWithoutTLSServer() throws Exception { public void httpsTest() throws Exception { TestHttpHandler handler = new TestHttpHandler(); - + int port = findFreePort(); server = ServerBootstrap.bootstrap() - .setListenerPort(8090) + .setListenerPort(port) .setServerInfo("Test/1.1") .setSslContext(createSSLContext()) .registerHandler("*", handler) @@ -374,7 +378,7 @@ public void httpsTest() throws Exception { server.start(); AuditMessage msg = MockAuditMessageFactory.validAuditMessage(); - String url = "https://localhost:8090/endpoint"; + String url = "https://localhost:" + port + "/endpoint"; // try with ssl verification on, no trust ca, must fail Settings settings = Settings.builder() @@ -445,8 +449,8 @@ public void httpsTest() throws Exception { @Test public void httpsTestPemDefault() throws Exception { - final int port = 8088; - TestHttpHandler handler = new TestHttpHandler(); + final int port = findFreePort(); + TestHttpHandler handler = new TestHttpHandler(); server = ServerBootstrap.bootstrap() .setListenerPort(port) @@ -561,9 +565,10 @@ public void httpsTestPemDefault() throws Exception { public void httpsTestPemEndpoint() throws Exception { TestHttpHandler handler = new TestHttpHandler(); + int port = findFreePort(); server = ServerBootstrap.bootstrap() - .setListenerPort(8091) + .setListenerPort(port) .setServerInfo("Test/1.1") .setSslContext(createSSLContext()) .registerHandler("*", handler) @@ -573,7 +578,7 @@ public void httpsTestPemEndpoint() throws Exception { AuditMessage msg = MockAuditMessageFactory.validAuditMessage(); LoggingSink fallback = new LoggingSink("test", Settings.EMPTY, null, null); - String url = "https://localhost:8091/endpoint"; + String url = "https://localhost:" + port + "/endpoint"; // test default with filepath handler.reset(); @@ -658,9 +663,10 @@ public void httpsTestPemEndpoint() throws Exception { public void httpsTestPemContentEndpoint() throws Exception { TestHttpHandler handler = new TestHttpHandler(); + int port = findFreePort(); server = ServerBootstrap.bootstrap() - .setListenerPort(8086) + .setListenerPort(port) .setServerInfo("Test/1.1") .setSslContext(createSSLContext()) .registerHandler("*", handler) @@ -670,7 +676,7 @@ public void httpsTestPemContentEndpoint() throws Exception { AuditMessage msg = MockAuditMessageFactory.validAuditMessage(); LoggingSink fallback = new LoggingSink("test", Settings.EMPTY, null, null); - String url = "https://localhost:8086/endpoint"; + String url = "https://localhost:" + port + "/endpoint"; // test with filecontent handler.reset(); @@ -731,4 +737,12 @@ private void assertStringContainsAllKeysAndValues(String in) { Assert.assertTrue(in, in.contains("8.8.8.8")); //Assert.assertTrue(in, in.contains("CN=kirk,OU=client,O=client,L=test,C=DE")); } + + private int findFreePort() { + try (ServerSocket serverSocket = new ServerSocket(0)) { + return serverSocket.getLocalPort(); + } catch (IOException e) { + throw new RuntimeException("Failed to find free port", e); + } + } }