Commit 42ccc44a by Richard Ding Committed by Madhan Neethiraj

ATLAS-2087: Allow Atlas server to bind on a specific address

parent f59284ad
...@@ -33,6 +33,10 @@ import org.slf4j.Logger; ...@@ -33,6 +33,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler; import org.slf4j.bridge.SLF4JBridgeHandler;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Iterator; import java.util.Iterator;
/** /**
...@@ -105,6 +109,17 @@ public final class Atlas { ...@@ -105,6 +109,17 @@ public final class Atlas {
setApplicationHome(); setApplicationHome();
Configuration configuration = ApplicationProperties.get(); Configuration configuration = ApplicationProperties.get();
final String enableTLSFlag = configuration.getString(SecurityProperties.TLS_ENABLED); final String enableTLSFlag = configuration.getString(SecurityProperties.TLS_ENABLED);
final String appHost = configuration.getString(SecurityProperties.BIND_ADDRESS, EmbeddedServer.ATLAS_DEFAULT_BIND_ADDRESS);
if (!isLocalAddress(InetAddress.getByName(appHost))) {
String msg =
"Failed to start Atlas server. Address " + appHost
+ " does not belong to this host. Correct configuration parameter: "
+ SecurityProperties.BIND_ADDRESS;
LOG.error(msg);
throw new IOException(msg);
}
final int appPort = getApplicationPort(cmd, enableTLSFlag, configuration); final int appPort = getApplicationPort(cmd, enableTLSFlag, configuration);
System.setProperty(AtlasConstants.SYSTEM_PROPERTY_APP_PORT, String.valueOf(appPort)); System.setProperty(AtlasConstants.SYSTEM_PROPERTY_APP_PORT, String.valueOf(appPort));
final boolean enableTLS = isTLSEnabled(enableTLSFlag, appPort); final boolean enableTLS = isTLSEnabled(enableTLSFlag, appPort);
...@@ -112,7 +127,7 @@ public final class Atlas { ...@@ -112,7 +127,7 @@ public final class Atlas {
showStartupInfo(buildConfiguration, enableTLS, appPort); showStartupInfo(buildConfiguration, enableTLS, appPort);
server = EmbeddedServer.newServer(appPort, appPath, enableTLS); server = EmbeddedServer.newServer(appHost, appPort, appPath, enableTLS);
installLogBridge(); installLogBridge();
server.start(); server.start();
...@@ -164,6 +179,21 @@ public final class Atlas { ...@@ -164,6 +179,21 @@ public final class Atlas {
System.getProperty(SecurityProperties.TLS_ENABLED, (appPort % 1000) == 443 ? "true" : "false") : enableTLSFlag); System.getProperty(SecurityProperties.TLS_ENABLED, (appPort % 1000) == 443 ? "true" : "false") : enableTLSFlag);
} }
private static boolean isLocalAddress(InetAddress addr) {
// Check if the address is any local or loop back
boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
// Check if the address is defined on any interface
if (!local) {
try {
local = NetworkInterface.getByInetAddress(addr) != null;
} catch (SocketException e) {
local = false;
}
}
return local;
}
private static void showStartupInfo(PropertiesConfiguration buildConfiguration, boolean enableTLS, int appPort) { private static void showStartupInfo(PropertiesConfiguration buildConfiguration, boolean enableTLS, int appPort) {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
buffer.append("\n############################################"); buffer.append("\n############################################");
......
...@@ -41,9 +41,11 @@ import java.util.concurrent.TimeUnit; ...@@ -41,9 +41,11 @@ import java.util.concurrent.TimeUnit;
public class EmbeddedServer { public class EmbeddedServer {
public static final Logger LOG = LoggerFactory.getLogger(EmbeddedServer.class); public static final Logger LOG = LoggerFactory.getLogger(EmbeddedServer.class);
public static final String ATLAS_DEFAULT_BIND_ADDRESS = "0.0.0.0";
protected final Server server; protected final Server server;
public EmbeddedServer(int port, String path) throws IOException { public EmbeddedServer(String host, int port, String path) throws IOException {
int queueSize = AtlasConfiguration.WEBSERVER_QUEUE_SIZE.getInt(); int queueSize = AtlasConfiguration.WEBSERVER_QUEUE_SIZE.getInt();
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize); LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize);
...@@ -54,7 +56,7 @@ public class EmbeddedServer { ...@@ -54,7 +56,7 @@ public class EmbeddedServer {
new ExecutorThreadPool(minThreads, maxThreads, keepAliveTime, TimeUnit.SECONDS, queue); new ExecutorThreadPool(minThreads, maxThreads, keepAliveTime, TimeUnit.SECONDS, queue);
server = new Server(pool); server = new Server(pool);
Connector connector = getConnector(port); Connector connector = getConnector(host, port);
server.addConnector(connector); server.addConnector(connector);
WebAppContext application = getWebAppContext(path); WebAppContext application = getWebAppContext(path);
...@@ -69,15 +71,16 @@ public class EmbeddedServer { ...@@ -69,15 +71,16 @@ public class EmbeddedServer {
return application; return application;
} }
public static EmbeddedServer newServer(int port, String path, boolean secure) throws IOException { public static EmbeddedServer newServer(String host, int port, String path, boolean secure)
throws IOException {
if (secure) { if (secure) {
return new SecureEmbeddedServer(port, path); return new SecureEmbeddedServer(host, port, path);
} else { } else {
return new EmbeddedServer(port, path); return new EmbeddedServer(host, port, path);
} }
} }
protected Connector getConnector(int port) throws IOException { protected Connector getConnector(String host, int port) throws IOException {
HttpConfiguration http_config = new HttpConfiguration(); HttpConfiguration http_config = new HttpConfiguration();
// this is to enable large header sizes when Kerberos is enabled with AD // this is to enable large header sizes when Kerberos is enabled with AD
final int bufferSize = AtlasConfiguration.WEBSERVER_REQUEST_BUFFER_SIZE.getInt();; final int bufferSize = AtlasConfiguration.WEBSERVER_REQUEST_BUFFER_SIZE.getInt();;
...@@ -86,7 +89,7 @@ public class EmbeddedServer { ...@@ -86,7 +89,7 @@ public class EmbeddedServer {
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(http_config)); ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(http_config));
connector.setPort(port); connector.setPort(port);
connector.setHost("0.0.0.0"); connector.setHost(host);
return connector; return connector;
} }
......
...@@ -60,8 +60,8 @@ public class SecureEmbeddedServer extends EmbeddedServer { ...@@ -60,8 +60,8 @@ public class SecureEmbeddedServer extends EmbeddedServer {
private static final Logger LOG = LoggerFactory.getLogger(SecureEmbeddedServer.class); private static final Logger LOG = LoggerFactory.getLogger(SecureEmbeddedServer.class);
public SecureEmbeddedServer(int port, String path) throws IOException { public SecureEmbeddedServer(String host, int port, String path) throws IOException {
super(port, path); super(host, port, path);
} }
protected Connector getConnector(int port) throws IOException { protected Connector getConnector(int port) throws IOException {
......
...@@ -55,7 +55,7 @@ public class AtlasAuthenticationKerberosFilterTest extends BaseSecurityTest { ...@@ -55,7 +55,7 @@ public class AtlasAuthenticationKerberosFilterTest extends BaseSecurityTest {
class TestEmbeddedServer extends EmbeddedServer { class TestEmbeddedServer extends EmbeddedServer {
public TestEmbeddedServer(int port, String path) throws IOException { public TestEmbeddedServer(int port, String path) throws IOException {
super(port, path); super(ATLAS_DEFAULT_BIND_ADDRESS, port, path);
} }
Server getServer() { Server getServer() {
......
...@@ -49,7 +49,7 @@ public class BaseSSLAndKerberosTest extends BaseSecurityTest { ...@@ -49,7 +49,7 @@ public class BaseSSLAndKerberosTest extends BaseSecurityTest {
class TestSecureEmbeddedServer extends SecureEmbeddedServer { class TestSecureEmbeddedServer extends SecureEmbeddedServer {
public TestSecureEmbeddedServer(int port, String path) throws IOException { public TestSecureEmbeddedServer(int port, String path) throws IOException {
super(port, path); super(ATLAS_DEFAULT_BIND_ADDRESS, port, path);
} }
public Server getServer() { public Server getServer() {
......
...@@ -52,7 +52,7 @@ public class SSLTest extends BaseSSLAndKerberosTest { ...@@ -52,7 +52,7 @@ public class SSLTest extends BaseSSLAndKerberosTest {
class TestSecureEmbeddedServer extends SecureEmbeddedServer { class TestSecureEmbeddedServer extends SecureEmbeddedServer {
public TestSecureEmbeddedServer(int port, String path) throws IOException { public TestSecureEmbeddedServer(int port, String path) throws IOException {
super(port, path); super(ATLAS_DEFAULT_BIND_ADDRESS, port, path);
} }
public Server getServer() { public Server getServer() {
......
...@@ -49,7 +49,8 @@ public class SecureEmbeddedServerTest extends SecureEmbeddedServerTestBase { ...@@ -49,7 +49,8 @@ public class SecureEmbeddedServerTest extends SecureEmbeddedServerTestBase {
ApplicationProperties.forceReload(); ApplicationProperties.forceReload();
SecureEmbeddedServer secureEmbeddedServer = null; SecureEmbeddedServer secureEmbeddedServer = null;
try { try {
secureEmbeddedServer = new SecureEmbeddedServer(21443, TestUtils.getWarPath()) { secureEmbeddedServer = new SecureEmbeddedServer(ATLAS_DEFAULT_HOST_ADDRESS,
21443, TestUtils.getWarPath()) {
@Override @Override
protected PropertiesConfiguration getConfiguration() { protected PropertiesConfiguration getConfiguration() {
return configuration; return configuration;
......
...@@ -105,7 +105,8 @@ public class SecureEmbeddedServerTestBase { ...@@ -105,7 +105,8 @@ public class SecureEmbeddedServerTestBase {
originalConf = System.getProperty("atlas.conf"); originalConf = System.getProperty("atlas.conf");
System.clearProperty("atlas.conf"); System.clearProperty("atlas.conf");
ApplicationProperties.forceReload(); ApplicationProperties.forceReload();
secureEmbeddedServer = new SecureEmbeddedServer(securePort, TestUtils.getWarPath()); secureEmbeddedServer = new SecureEmbeddedServer(
EmbeddedServer.ATLAS_DEFAULT_BIND_ADDRESS, securePort, TestUtils.getWarPath());
secureEmbeddedServer.server.start(); secureEmbeddedServer.server.start();
Assert.fail("Should have thrown an exception"); Assert.fail("Should have thrown an exception");
...@@ -132,7 +133,8 @@ public class SecureEmbeddedServerTestBase { ...@@ -132,7 +133,8 @@ public class SecureEmbeddedServerTestBase {
configuration.setProperty(CERT_STORES_CREDENTIAL_PROVIDER_PATH, providerUrl); configuration.setProperty(CERT_STORES_CREDENTIAL_PROVIDER_PATH, providerUrl);
try { try {
secureEmbeddedServer = new SecureEmbeddedServer(securePort, TestUtils.getWarPath()) { secureEmbeddedServer = new SecureEmbeddedServer(
EmbeddedServer.ATLAS_DEFAULT_BIND_ADDRESS, securePort, TestUtils.getWarPath()) {
@Override @Override
protected PropertiesConfiguration getConfiguration() { protected PropertiesConfiguration getConfiguration() {
return configuration; return configuration;
...@@ -159,7 +161,8 @@ public class SecureEmbeddedServerTestBase { ...@@ -159,7 +161,8 @@ public class SecureEmbeddedServerTestBase {
setupCredentials(); setupCredentials();
try { try {
secureEmbeddedServer = new SecureEmbeddedServer(securePort, TestUtils.getWarPath()) { secureEmbeddedServer = new SecureEmbeddedServer(
EmbeddedServer.ATLAS_DEFAULT_BIND_ADDRESS, securePort, TestUtils.getWarPath()) {
@Override @Override
protected PropertiesConfiguration getConfiguration() { protected PropertiesConfiguration getConfiguration() {
return configuration; return configuration;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment