|
1 | 1 | package org.elasticsearch.mapping;
|
2 | 2 |
|
| 3 | +import java.io.InputStream; |
3 | 4 | import java.util.ArrayList;
|
4 | 5 | import java.util.List;
|
| 6 | +import java.util.Map; |
| 7 | +import java.util.Scanner; |
5 | 8 |
|
6 | 9 | import javax.annotation.PostConstruct;
|
7 | 10 | import javax.annotation.PreDestroy;
|
|
12 | 15 | import org.elasticsearch.analysis.common.CommonAnalysisPlugin;
|
13 | 16 | import org.elasticsearch.client.Client;
|
14 | 17 | import org.elasticsearch.client.transport.TransportClient;
|
| 18 | +import org.elasticsearch.common.io.stream.InputStreamStreamInput; |
| 19 | +import org.elasticsearch.common.io.stream.StreamInput; |
15 | 20 | import org.elasticsearch.common.network.NetworkModule;
|
16 | 21 | import org.elasticsearch.common.settings.Settings;
|
17 | 22 | import org.elasticsearch.common.transport.TransportAddress;
|
|
21 | 26 | import org.elasticsearch.node.Node;
|
22 | 27 | import org.elasticsearch.plugins.Plugin;
|
23 | 28 | import org.elasticsearch.transport.MockTcpTransportPlugin;
|
| 29 | +import org.elasticsearch.transport.Netty4Plugin; |
24 | 30 | import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
25 | 31 | import org.elasticsearch.util.AddressParserUtil;
|
26 | 32 | import org.springframework.beans.factory.annotation.Value;
|
| 33 | +import org.springframework.core.io.ClassPathResource; |
27 | 34 | import org.springframework.stereotype.Component;
|
28 | 35 |
|
29 | 36 | import com.floragunn.searchguard.ssl.SearchGuardSSLPlugin;
|
30 | 37 | import com.floragunn.searchguard.ssl.util.SSLConfigConstants;
|
31 | 38 |
|
32 | 39 | import lombok.extern.slf4j.Slf4j;
|
| 40 | +import org.yaml.snakeyaml.Yaml; |
33 | 41 |
|
34 | 42 | /**
|
35 | 43 | * Prepare the node to work with elastic search.
|
@@ -59,48 +67,69 @@ public void initialize() throws Exception {
|
59 | 67 | if (this.isClient && this.isTransportClient) {
|
60 | 68 | // when these both option are set, we use a transport client
|
61 | 69 | Settings.Builder settingsBuilder = Settings.builder()
|
62 |
| - .put("cluster.name", this.clusterName); |
| 70 | + .put("cluster.name", this.clusterName); |
63 | 71 | 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); |
69 | 77 | }
|
70 | 78 | Settings settings = settingsBuilder.build();
|
71 | 79 | TransportClient transportClient;
|
72 | 80 | if (!transportSSL) {
|
73 |
| - transportClient = new PreBuiltTransportClient(settings); |
| 81 | + transportClient = new PreBuiltTransportClient(settings); |
74 | 82 | } else {
|
75 |
| - transportClient = new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class); |
| 83 | + transportClient = new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class); |
76 | 84 | }
|
77 | 85 | for (TransportAddress add : adresses) {
|
78 | 86 | transportClient.addTransportAddress(add);
|
79 | 87 | }
|
80 | 88 | 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("====================================================================================================================="); |
82 | 93 | // when only 'client' option is set, a node without data is initialized and joins the cluster
|
83 | 94 | /************************
|
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(); |
85 | 127 | 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 | + } |
101 | 130 |
|
102 | 131 | // 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(); |
104 | 133 | // }
|
105 | 134 | log.info("Initialized ElasticSearch client for cluster <" + this.clusterName + ">");
|
106 | 135 | }
|
@@ -201,7 +230,8 @@ public void setKeystorePassword(final String password) {
|
201 | 230 | this.keystorePassword = password;
|
202 | 231 | }
|
203 | 232 |
|
204 |
| - @Value("#{elasticsearchConfig['searchguard.ssl.transport.truststore_password']}") |
| 233 | + @Value("#{elasticsearchConfig['enforce_hostname_verification" + |
| 234 | + "']}") |
205 | 235 | public void setTruststorePassword(final String password) {
|
206 | 236 | this.truststorePassword = password;
|
207 | 237 | }
|
|
0 commit comments