Skip to content

Commit

Permalink
Fix 'BindException' in some tests
Browse files Browse the repository at this point in the history
Implement getting free port in tests: SinkProviderTLSTest and WebhookAuditLogTest

Signed-off-by: rs-eliatra <[email protected]>
  • Loading branch information
rs-eliatra committed Feb 22, 2022
1 parent 6116b46 commit bd8c297
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit bd8c297

Please sign in to comment.