Skip to content

Commit

Permalink
Simple EmissaryServer Clean-up (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
sambish5 authored Nov 29, 2023
1 parent 2bf7b00 commit 5e2d65b
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions src/main/java/emissary/server/EmissaryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -119,7 +118,7 @@ public ServerCommand getServerCommand() {
/**
* Creates and starts a server that is bound into the local Namespace using DEFAULT_NAMESPACE_NAME and returned
*
*
*
*/
public Server startServer() {
// do what StartJetty and then JettyServer did to start
Expand Down Expand Up @@ -157,18 +156,18 @@ public Server startServer() {
handlers.addHandler(emissaryHandler); // not secured, no endpoints and must be loaded first
handlers.addHandler(security);

Server server = configureServer();
server.setHandler(handlers);
server.addBean(loginService);
server.setStopAtShutdown(true);
server.setStopTimeout(10000L);
if (this.cmd.shouldDumpJettyBeans()) {
LOG.info(server.dump());
Server configuredServer = configureServer();
configuredServer.setHandler(handlers);
configuredServer.addBean(loginService);
configuredServer.setStopAtShutdown(true);
configuredServer.setStopTimeout(10000L);
if (this.cmd.shouldDumpJettyBeans() && LOG.isInfoEnabled()) {
LOG.info(configuredServer.dump());
}
this.server = server;
this.server = configuredServer;
bindServer(); // emissary specific

server.start();
configuredServer.start();
// server.join(); // don't join so we can shutdown

String serverLocation = cmd.getScheme() + "://" + cmd.getHost() + ":" + cmd.getPort();
Expand All @@ -194,10 +193,9 @@ public Server startServer() {
}

LOG.info("Started EmissaryServer at {}", serverLocation);
return server;
return configuredServer;
} catch (Throwable t) {
String errorMsg = "Emissary server didn't start";
LOG.error(errorMsg, t);
throw new RuntimeException(errorMsg, t);
}
}
Expand Down Expand Up @@ -486,10 +484,10 @@ public static boolean isStarted(final String name) {
if (s != null && s.isStarted()) {
started = true;
} else {
LOG.debug("Server found but not started, name=" + name);
LOG.debug("Server found but not started, name={}", name);
}
} catch (NamespaceException ex) {
LOG.debug("No server found using name=" + name);
LOG.debug("No server found using name={}", name);
}
return started;
}
Expand Down Expand Up @@ -546,7 +544,7 @@ private LoginService buildLoginService() {
return new HashLoginService("EmissaryRealm", jettyUsersFile);
}

private void bindServer() throws UnknownHostException, AttributeInUseException {
private void bindServer() throws AttributeInUseException {
if (Namespace.exists(getDefaultNamespaceName())) {
LOG.error("EmissaryServer already bound to namespace. This should NEVER happen.");
throw new AttributeInUseException("EmissaryServer was already bound to the namespace using serverName: " + DEFAULT_NAMESPACE_NAME);
Expand Down Expand Up @@ -661,11 +659,11 @@ protected Server configureServer() throws IOException {
threadPool.setLowThreadsThreshold(lowThreads);
threadPool.setThreadsPriority(threadsPriority);

Server server = new Server(threadPool);
server.setConnectors(new Connector[] {
createServerConnector(server)
Server configuredServer = new Server(threadPool);
configuredServer.setConnectors(new Connector[] {
createServerConnector(configuredServer)
});
return server;
return configuredServer;
}

/**
Expand Down Expand Up @@ -700,16 +698,16 @@ private ServerConnector createHttpConnector(Server server) {
* @throws IOException if there is an error
*/
private ServerConnector createHttpsConnector(Server server) throws IOException {
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(cmd.getPort());
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(cmd.getPort());

HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(createSecureRequestCustomizer());
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
httpsConfig.addCustomizer(createSecureRequestCustomizer());

return new ServerConnector(server,
new SslConnectionFactory(getSslContextFactory(), HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(https_config));
new HttpConnectionFactory(httpsConfig));
}

/**
Expand All @@ -721,7 +719,7 @@ private ServerConnector createHttpsConnector(Server server) throws IOException {
* <li>getStsMaxAge() => -1 (no max age) // Strict-Transport-Security (STS) max age
* <li>isStsIncludeSubDomains() => false // include-subdomain property is sent with any STS header
* </ul>
*
*
* @return a secure request customizer
*/
private SecureRequestCustomizer createSecureRequestCustomizer() {
Expand Down

0 comments on commit 5e2d65b

Please sign in to comment.