Skip to content

Commit 159d256

Browse files
committed
[ALIEN-3355] add Netty Transport Plugin, enable HTTP && parse config file from classpath
1 parent 114cc5c commit 159d256

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

elasticsearch-annotations/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.alien4cloud</groupId>
66
<artifactId>elasticsearch-mapping-parent</artifactId>
7-
<version>6.6.2</version>
7+
<version>6.6.3-SNAPSHOT</version>
88
</parent>
99

1010
<artifactId>elasticsearch-annotations</artifactId>

elasticsearch-mapping/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.alien4cloud</groupId>
66
<artifactId>elasticsearch-mapping-parent</artifactId>
7-
<version>6.6.2</version>
7+
<version>6.6.3-SNAPSHOT</version>
88
</parent>
99

1010
<artifactId>elasticsearch-mapping</artifactId>

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/ElasticSearchClient.java

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.elasticsearch.mapping;
22

3+
import java.io.InputStream;
34
import java.util.ArrayList;
45
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Scanner;
58

69
import javax.annotation.PostConstruct;
710
import javax.annotation.PreDestroy;
@@ -12,6 +15,8 @@
1215
import org.elasticsearch.analysis.common.CommonAnalysisPlugin;
1316
import org.elasticsearch.client.Client;
1417
import org.elasticsearch.client.transport.TransportClient;
18+
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
19+
import org.elasticsearch.common.io.stream.StreamInput;
1520
import org.elasticsearch.common.network.NetworkModule;
1621
import org.elasticsearch.common.settings.Settings;
1722
import org.elasticsearch.common.transport.TransportAddress;
@@ -21,15 +26,18 @@
2126
import org.elasticsearch.node.Node;
2227
import org.elasticsearch.plugins.Plugin;
2328
import org.elasticsearch.transport.MockTcpTransportPlugin;
29+
import org.elasticsearch.transport.Netty4Plugin;
2430
import org.elasticsearch.transport.client.PreBuiltTransportClient;
2531
import org.elasticsearch.util.AddressParserUtil;
2632
import org.springframework.beans.factory.annotation.Value;
33+
import org.springframework.core.io.ClassPathResource;
2734
import org.springframework.stereotype.Component;
2835

2936
import com.floragunn.searchguard.ssl.SearchGuardSSLPlugin;
3037
import com.floragunn.searchguard.ssl.util.SSLConfigConstants;
3138

3239
import lombok.extern.slf4j.Slf4j;
40+
import org.yaml.snakeyaml.Yaml;
3341

3442
/**
3543
* Prepare the node to work with elastic search.
@@ -59,48 +67,69 @@ public void initialize() throws Exception {
5967
if (this.isClient && this.isTransportClient) {
6068
// when these both option are set, we use a transport client
6169
Settings.Builder settingsBuilder = Settings.builder()
62-
.put("cluster.name", this.clusterName);
70+
.put("cluster.name", this.clusterName);
6371
if (transportSSL) {
64-
settingsBuilder = settingsBuilder
65-
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_FILEPATH, this.keystore)
66-
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_TRUSTSTORE_FILEPATH, this.truststore)
67-
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_PASSWORD, this.keystorePassword)
68-
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_TRUSTSTORE_PASSWORD, this.truststorePassword);
72+
settingsBuilder = settingsBuilder
73+
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_FILEPATH, this.keystore)
74+
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_TRUSTSTORE_FILEPATH, this.truststore)
75+
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_PASSWORD, this.keystorePassword)
76+
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_TRUSTSTORE_PASSWORD, this.truststorePassword);
6977
}
7078
Settings settings = settingsBuilder.build();
7179
TransportClient transportClient;
7280
if (!transportSSL) {
73-
transportClient = new PreBuiltTransportClient(settings);
81+
transportClient = new PreBuiltTransportClient(settings);
7482
} else {
75-
transportClient = new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class);
83+
transportClient = new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class);
7684
}
7785
for (TransportAddress add : adresses) {
7886
transportClient.addTransportAddress(add);
7987
}
8088
this.client = transportClient;
81-
} else {
89+
} else {
90+
log.warn("=====================================================================================================================");
91+
log.warn("This embedded mode is not suitable for production, if your not in dev stage, you should start a separate JVM for ES !");
92+
log.warn("=====================================================================================================================");
8293
// when only 'client' option is set, a node without data is initialized and joins the cluster
8394
/************************
84-
this.node = NodeBuilder.nodeBuilder().client(this.isClient).clusterName(this.clusterName).local(this.isLocal).node();
95+
this.node = NodeBuilder.nodeBuilder().client(this.isClient).clusterName(this.clusterName).local(this.isLocal).node();
96+
this.client = node.client();
97+
***************************/
98+
99+
100+
Settings settings = null;
101+
ClassPathResource config = new ClassPathResource("elasticsearch.yml");
102+
if (config.exists()) {
103+
log.info("Found elasticsearch.yml in classpath, using it to configure embedded ES :");
104+
Scanner sc = new Scanner(config.getFile());
105+
while (sc.hasNextLine()) {
106+
log.info(sc.nextLine());
107+
}
108+
InputStream is = config.getInputStream();
109+
settings = Settings.builder().loadFromStream("elasticsearch.yml", is, true).build();
110+
} else {
111+
log.info("No elasticsearch.yml found in classpath, using default config !");
112+
settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), "target/eshome")
113+
.put("transport.type", MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME)
114+
.put(NetworkModule.HTTP_ENABLED.getKey(), true)
115+
.put("node.name", "alien")
116+
.put("cluster.name", this.clusterName)
117+
.put("network.host", "0.0.0.0")
118+
.build();
119+
}
120+
121+
ArrayList<Class<? extends Plugin>> plugins = new ArrayList<Class<? extends Plugin>>();
122+
plugins.add(Netty4Plugin.class);
123+
plugins.add(MockTcpTransportPlugin.class);
124+
plugins.add(CommonAnalysisPlugin.class);
125+
MockNode node = new MockNode(settings, plugins);
126+
node.start();
85127
this.client = node.client();
86-
***************************/
87-
Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), "target/eshome")
88-
.put("transport.type", MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME)
89-
.put(NetworkModule.HTTP_ENABLED.getKey(), false)
90-
.put("node.name", "alien")
91-
.put("cluster.name", this.clusterName)
92-
.build();
93-
ArrayList<Class<? extends Plugin>> plugins = new ArrayList<Class<? extends Plugin>>();
94-
plugins.add (MockTcpTransportPlugin.class);
95-
plugins.add (CommonAnalysisPlugin.class);
96-
MockNode node = new MockNode(settings, plugins);
97-
node.start();
98-
this.client = node.client();
99-
this.node = node;
100-
}
128+
this.node = node;
129+
}
101130

102131
// if (this.resetData) { // removes all indices from elastic search. For Integration testing only.
103-
// this.node.client().admin().indices().prepareDelete().execute().actionGet();
132+
// this.node.client().admin().indices().prepareDelete().execute().actionGet();
104133
// }
105134
log.info("Initialized ElasticSearch client for cluster <" + this.clusterName + ">");
106135
}
@@ -201,7 +230,8 @@ public void setKeystorePassword(final String password) {
201230
this.keystorePassword = password;
202231
}
203232

204-
@Value("#{elasticsearchConfig['searchguard.ssl.transport.truststore_password']}")
233+
@Value("#{elasticsearchConfig['enforce_hostname_verification" +
234+
"']}")
205235
public void setTruststorePassword(final String password) {
206236
this.truststorePassword = password;
207237
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.alien4cloud</groupId>
55
<artifactId>elasticsearch-mapping-parent</artifactId>
6-
<version>6.6.2</version>
6+
<version>6.6.3-SNAPSHOT</version>
77
<packaging>pom</packaging>
88

99
<name>Elastic search mapping parent</name>

0 commit comments

Comments
 (0)