Apache Nutch is an extensible and scalable web crawler
NUTCH-2757 : Indexer-elastic: add authentication options
balashashankaf999ca5af9ce
diff --git a/conf/index-writers.xml.template b/conf/index-writers.xml.templateindex 96c765e2f..4eb4386bb 100644--- a/conf/index-writers.xml.template+++ b/conf/index-writers.xml.template@@ -108,8 +108,10 @@<parameters><param name="host" value="localhost"/><param name="port" value="9200"/>- <param name="cluster" value=""/><param name="index" value="nutch"/>+ <!--Configure username and password if necessary, default user is "elastic"+ <param name="user" value="user"/>+ <param name="password" value="password"/>--><param name="max.bulk.docs" value="250"/><param name="max.bulk.size" value="2500500"/><param name="exponential.backoff.millis" value="100"/>diff --git a/src/plugin/indexer-elastic/README.md b/src/plugin/indexer-elastic/README.mdindex 0ac4f08d5..41316de36 100644--- a/src/plugin/indexer-elastic/README.md+++ b/src/plugin/indexer-elastic/README.md@@ -32,10 +32,11 @@ Parameter Name | Description | Default value--|--|--host | Comma-separated list of hostnames to send documents to using [TransportClient](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.3.0/org/elasticsearch/client/transport/TransportClient.html). Either host and port must be defined or cluster. |port | The port to connect to using [TransportClient](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.3.0/org/elasticsearch/client/transport/TransportClient.html). | 9300-cluster | The cluster name to discover. Either host and port must be defined or cluster. |index | Default index to send documents to. | nutch+user | Username for auth credentials | elastic+password | Password for auth credentials | ""max.bulk.docs | Maximum size of the bulk in number of documents. | 250max.bulk.size | Maximum size of the bulk in bytes. | 2500500exponential.backoff.millis | Initial delay for the [BulkProcessor](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.3.0/org/elasticsearch/action/bulk/BulkProcessor.html) exponential backoff policy. | 100exponential.backoff.retries | Number of times the [BulkProcessor](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.3.0/org/elasticsearch/action/bulk/BulkProcessor.html) exponential backoff policy should retry bulk operations. | 10-bulk.close.timeout | Number of seconds allowed for the [BulkProcessor](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.3.0/org/elasticsearch/action/bulk/BulkProcessor.html) to complete its last operation. | 600\ No newline at end of file+bulk.close.timeout | Number of seconds allowed for the [BulkProcessor](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.3.0/org/elasticsearch/action/bulk/BulkProcessor.html) to complete its last operation. | 600diff --git a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.java b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.javaindex d27284177..7a65d0f53 100644--- a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.java+++ b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.java@@ -19,6 +19,10 @@ package org.apache.nutch.indexwriter.elastic;public interface ElasticConstants {String HOSTS = "host";String PORT = "port";++ String USER = "user";+ String PASSWORD = "password";+String CLUSTER = "cluster";String INDEX = "index";String MAX_BULK_DOCS = "max.bulk.docs";diff --git a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.javaindex 74727a075..a4c13d349 100644--- a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java+++ b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java@@ -17,6 +17,11 @@package org.apache.nutch.indexwriter.elastic;import java.lang.invoke.MethodHandles;+import java.security.KeyManagementException;+import java.security.KeyStoreException;+import java.security.NoSuchAlgorithmException;+import java.security.cert.CertificateException;+import java.security.cert.X509Certificate;import java.time.format.DateTimeFormatter;import java.io.IOException;import java.util.AbstractMap;@@ -25,11 +30,21 @@ import java.util.List;import java.util.Map;import java.util.concurrent.TimeUnit;+import javax.net.ssl.SSLContext;+import org.apache.commons.lang.StringUtils;+import org.apache.commons.lang3.exception.ExceptionUtils;import org.apache.hadoop.conf.Configuration;import org.apache.http.Header;import org.apache.http.HttpHost;+import org.apache.http.auth.AuthScope;+import org.apache.http.auth.UsernamePasswordCredentials;+import org.apache.http.client.CredentialsProvider;+import org.apache.http.impl.client.BasicCredentialsProvider;+import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;import org.apache.http.message.BasicHeader;+import org.apache.http.ssl.SSLContextBuilder;+import org.apache.http.ssl.TrustStrategy;import org.apache.nutch.indexer.IndexWriter;import org.apache.nutch.indexer.IndexWriterParams;import org.apache.nutch.indexer.NutchDocument;@@ -47,6 +62,7 @@ import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.common.unit.ByteSizeUnit;import org.elasticsearch.common.unit.ByteSizeValue;import org.elasticsearch.common.unit.TimeValue;+import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;import org.elasticsearch.common.xcontent.XContentBuilder;import org.elasticsearch.common.xcontent.XContentFactory;import org.elasticsearch.client.RequestOptions;@@ -69,10 +85,15 @@ public class ElasticIndexWriter implements IndexWriter {private static final int DEFAULT_EXP_BACKOFF_RETRIES = 10;private static final int DEFAULT_BULK_CLOSE_TIMEOUT = 600;private static final String DEFAULT_INDEX = "nutch";+ private static final String DEFAULT_USER = "elastic";private String cluster;private String[] hosts;private int port;+ private Boolean https = null;+ private String user = null;+ private String password = null;+ private Boolean trustAllHostnames = null;private int maxBulkDocs;private int maxBulkLength;@@ -146,6 +167,14 @@ public class ElasticIndexWriter implements IndexWriter {protected RestHighLevelClient makeClient(IndexWriterParams parameters) throws IOException {hosts = parameters.getStrings(ElasticConstants.HOSTS);port = parameters.getInt(ElasticConstants.PORT, DEFAULT_PORT);++ user = parameters.get(ElasticConstants.USER, DEFAULT_USER);+ password = parameters.get(ElasticConstants.PASSWORD, "");++ final CredentialsProvider credentialsProvider =+ new BasicCredentialsProvider();+ credentialsProvider.setCredentials(AuthScope.ANY,+ new UsernamePasswordCredentials(user, password));RestHighLevelClient client = null;@@ -162,6 +191,17 @@ public class ElasticIndexWriter implements IndexWriter {} else {LOG.debug("No cluster name provided so using default");}+ //Only add username and password if password is configured+ if(StringUtils.isNotBlank(password)) {+ restClientBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {+ @Override+ public HttpAsyncClientBuilder customizeHttpClient(+ HttpAsyncClientBuilder arg0) {+ return arg0+ .setDefaultCredentialsProvider(credentialsProvider);+ }+ });+ }client = new RestHighLevelClient(restClientBuilder);} else {throw new IOException("ElasticRestClient initialization Failed!!!\\n\\nPlease Provide the hosts");