Skip to content

Commit

Permalink
Use onModule to inject http transport into http server module. Allow …
Browse files Browse the repository at this point in the history
…usage of http.type and sonian.elasticsearch.http.type settings. No setting for any http.type will default to JettyHttpServerTransport. This means the plugin will use Jetty by default without any configuration. All you need is a jetty.xml in your config path. Adding a jetty.xml to the classpath so server works even if no jetty.xml is specified. Reorganize elasticsearch.yml.
  • Loading branch information
Greg Butt committed Mar 21, 2015
1 parent 06614d8 commit 31e5000
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 22 deletions.
15 changes: 8 additions & 7 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ h2. Compatibility
The following table shows the versions of elasticsearch and jetty that Jetty Plugin was built with.

|_. Jetty Plugin |_. Elasticsearch |_. Jetty |
| 1.4.4 | 1.4.4 | 8.1.14.v20131031 |
| 1.2.1 | 1.2.1 | 8.1.14.v20131031 |
| 1.1.1-beta | 1.1.1 | 8.1.14.v20131031 |
| 1.1.0-beta | 1.1.0 | 8.1.14.v20131031 |
Expand Down Expand Up @@ -37,7 +38,7 @@ $ bin/plugin -url https://oss-es-plugins.s3.amazonaws.com/elasticsearch-jetty/el
The core of the plugin is JettyHttpServerTransport module that works as a replacement for NettyHttpServerTransport. To enable the elasticsearch-jetty plugin, the default netty http transport should be replaced with jetty http transport by adding the following line to @elasticsearch.yml@.

<pre>
http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransportModule
http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransport
</pre>

The elasticsearch-jetty plugin adds @Server: Jetty(8.1.4.v20120524)@ header to all responses. So, it's possible to verify that jetty plugin is running by checking the response headers using the following curl command:
Expand Down Expand Up @@ -109,15 +110,15 @@ After the login service is configured, the next step is to set security constrai
The following settings in @elasticsearch.yml@ will enable granular restrictions:

<pre>
http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransportModule
http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransport
sonian.elasticsearch.http.jetty:
config: jetty.xml,jetty-hash-auth.xml,jetty-restrict-writes.xml
</pre>

Authentication can be used with SSL connector. The following settings will restrict access to all pages and enable SSL connector:

<pre>
http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransportModule
http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransport
sonian.elasticsearch.http.jetty:
config: jetty.xml,jetty-ssl.xml,jetty-hash-auth.xml,jetty-restrict-all.xml
ssl_port: 9443
Expand All @@ -128,15 +129,15 @@ Security constraints in the elasticsearch-jetty plugin is very similar to securi

h3. Request Logging

The elasticsearch-jetty plugin contains two versions of the HTTP Server Transport: @JettyHttpServerTransport@ and @FilterHttpTransportModule@. While @JettyHttpServerTransport@ uses Jetty to handle all incoming requests, @FilterHttpTransportModule@ simply wraps another HTTP Server Transport (@JettyHttpServerTransport@ by default) to filter requests and responses. @FilterHttpTransportModule@ can be configured to pass all incoming requests through a chain of filters. The elasticsearch-jetty plugin contains one such filter that can be used for request logging.
The elasticsearch-jetty plugin contains two versions of the HTTP Server Transport: @JettyHttpServerTransport@ and @FilterHttpServerTransport@. While @JettyHttpServerTransport@ uses Jetty to handle all incoming requests, @FilterHttpServerTransport@ simply wraps another HTTP Server Transport (@JettyHttpServerTransport@ by default) to filter requests and responses. @FilterHttpServerTransport@ can be configured to pass all incoming requests through a chain of filters. The elasticsearch-jetty plugin contains one such filter that can be used for request logging.

In order to setup request logging, elasticsearch should be switched to use @FilterHttpTransportModule@. It can be done by the following setting:
In order to setup request logging, elasticsearch should be switched to use @FilterHttpServerTransport@. It can be done by the following setting:

<pre>
http.type: com.sonian.elasticsearch.http.filter.FilterHttpServerTransportModule
http.type: com.sonian.elasticsearch.http.filter.FilterHttpServerTransport
</pre>

Then @FilterHttpTransportModule@ has to be configured with an appropriate filter:
Then @FilterHttpServerTransport@ has to be configured with an appropriate filter:

<pre>
sonian.elasticsearch.http.filter:
Expand Down
11 changes: 7 additions & 4 deletions config/elasticsearch.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
cluster.name: jetty-test
# Switch to jetty transport without filter
#http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransport

# Switch to jetty transport with filter
http.type: com.sonian.elasticsearch.http.filter.FilterHttpServerTransport
# Transport Setup
sonian.elasticsearch:
# Default to jetty transport without filter
#http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransport

# Uncomment to switch to jetty transport with filter
#http.type: com.sonian.elasticsearch.http.filter.FilterHttpServerTransport

# Filter setup
sonian.elasticsearch.http.filter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public FilterHttpServerTransportModule(Settings settings) {
@SuppressWarnings({"unchecked"})
@Override
protected void configure() {
// this transport has already been bound by http server module
// bind(HttpServerTransport.class).to(FilterHttpServerTransport.class).asEagerSingleton();
Class<? extends HttpServerTransport> transport;
// This is a hack for debugging. It allows switching back to NettyHttpServer if needed.
// HttpServerTransportModule should be loaded instead of just binding HttpServerTransport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public JettyHttpServerTransportModule(Settings settings) {

@SuppressWarnings({"unchecked"})
@Override protected void configure() {
// this transport has already been bound by http server module
// bind(HttpServerTransport.class).to(JettyHttpServerTransport.class).asEagerSingleton();
bind(ESLoggerWrapper.class).asEagerSingleton();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.sonian.elasticsearch.http.jetty.JettyHttpServerTransportModule;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.http.HttpServerModule;
import org.elasticsearch.http.HttpServerTransport;
import org.elasticsearch.http.netty.NettyHttpServerTransport;
import org.elasticsearch.plugins.AbstractPlugin;

import java.util.Collection;
Expand All @@ -35,6 +35,7 @@
public class JettyPlugin extends AbstractPlugin {

private final Settings settings;
private Class<? extends HttpServerTransport> httpServerTransport;

public JettyPlugin(Settings settings) {
this.settings = settings;
Expand All @@ -51,16 +52,34 @@ public JettyPlugin(Settings settings) {
@Override
public Collection<Class<? extends Module>> modules() {
Collection<Class<? extends Module>> modules = newArrayList();

// only load plugin modules if http is enabled
if (settings.getAsBoolean("http.enabled", true)) {
Class<? extends HttpServerTransport> defaultHttpServerTransport = NettyHttpServerTransport.class;
Class<? extends HttpServerTransport> httpServerTransport = settings.getAsClass("http.type", defaultHttpServerTransport, "org.elasticsearch.http.", "HttpServerTransport");

// defer to http.type if it exists
String httpType = settings.get("http.type");
if (httpType != null) {
httpServerTransport = settings.getAsClass("http.type", null);
} else {
// default to JettyHttpServerTransport if no http.type is set
httpServerTransport = settings.getAsClass("sonian.elasticsearch.http.type", JettyHttpServerTransport.class);
}

if (httpServerTransport == JettyHttpServerTransport.class) {
modules.add(JettyHttpServerTransportModule.class);
} else if (httpServerTransport == FilterHttpServerTransport.class) {
modules.add(FilterHttpServerTransportModule.class);
} else {
// disable plugin
httpServerTransport = null;
}

}
return modules;
}

public void onModule(HttpServerModule httpServerModule) {
// override http server transport binding
if (httpServerTransport != null)
httpServerModule.setHttpServerTransport(httpServerTransport, name());
}
}
42 changes: 42 additions & 0 deletions src/main/resources/config/jetty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="ESServer" class="org.eclipse.jetty.server.Server">

<!-- ==================================================== -->
<!-- ElasticSearch Handler. -->
<!-- This handler redirects all requests to ElasticSearch -->
<!-- ==================================================== -->
<Set name="handler">
<New class="com.sonian.elasticsearch.http.jetty.handler.JettyHttpServerTransportHandler"
id="HttpServerAdapterHandler">
<Set name="transport"><Ref id="ESServerTransport"/></Set>
</New>
</Set>

<!-- ==================================================== -->
<!-- Reduce verbosity of jetty default error handler -->
<!-- ==================================================== -->
<Call name="addBean">
<Arg>
<New class="com.sonian.elasticsearch.http.jetty.error.JettyHttpServerErrorHandler"
id="HttpServerErrorHandler">
</New>
</Arg>
</Call>

<!-- ======================================== -->
<!-- Add HTTP connector -->
<!-- ======================================== -->
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<Set name="host"><Property name="jetty.bind_host"/></Set>
<Set name="port"><Property name="jetty.port"/></Set>
<Set name="maxIdleTime">600000</Set>
<Set name="Acceptors">2</Set>
</New>
</Arg>
</Call>

</Configure>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void setup() {
mockESLoggerFactory = new MockESLoggerFactory("INFO", "com.sonian.elasticsearch.http.filter.jsonlog");
ESLoggerFactory.setDefaultFactory(mockESLoggerFactory);
putDefaultSettings(ImmutableSettings.settingsBuilder()
.put("http.type", FilterHttpServerTransport.class.getName())
.put("sonian.elasticsearch.http.type", FilterHttpServerTransport.class.getName())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setup() {
mockESLoggerFactory = new MockESLoggerFactory("INFO", "com.sonian.elasticsearch.http.filter.logging");
ESLoggerFactory.setDefaultFactory(mockESLoggerFactory);
putDefaultSettings(ImmutableSettings.settingsBuilder()
.put("http.type", FilterHttpServerTransport.class.getName())
.put("sonian.elasticsearch.http.type", FilterHttpServerTransport.class.getName())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class AbstractJettyHttpServerTests {
private Settings defaultSettings = ImmutableSettings
.settingsBuilder()
.put("cluster.name", "test-cluster-" + NetworkUtils.getLocalAddress().getHostName())
.put("http.type", JettyHttpServerTransport.class.getName())
.put("sonian.elasticsearch.http.jetty.port", "9290-9300")
.put("node.local", true)
.put("gateway.type", "none")
Expand Down
Loading

0 comments on commit 31e5000

Please sign in to comment.