Apache Nutch is an extensible and scalable web crawler
NUTCH-2757 - Indexer-elastic: add authentication options
balashashankae471397ddb35
diff --git a/conf/index-writers.xml.template b/conf/index-writers.xml.templateindex 4eb4386bb..aa9a90bc8 100644--- a/conf/index-writers.xml.template+++ b/conf/index-writers.xml.template@@ -109,9 +109,9 @@<param name="host" value="localhost"/><param name="port" value="9200"/><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="username" value="elastic"/>+ <param name="password" value=""/>+ <!--<param name="auth" value="false"/>--><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 41316de36..b2a875bf2 100644--- a/src/plugin/indexer-elastic/README.md+++ b/src/plugin/indexer-elastic/README.md@@ -33,8 +33,9 @@ Parameter Name | Description | Default valuehost | 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). | 9300index | Default index to send documents to. | nutch-user | Username for auth credentials | elastic+username | Username for auth credentials | elasticpassword | Password for auth credentials | ""+auth | Whether to enable HTTP basic authentication with elastic. Use `username` and `password` properties to configure your credentials. | falsemax.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. | 100diff --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 7a65d0f53..c0d1a6177 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@@ -20,10 +20,9 @@ public interface ElasticConstants {String HOSTS = "host";String PORT = "port";- String USER = "user";+ String USER = "username";String PASSWORD = "password";-- String CLUSTER = "cluster";+ String USE_AUTH = "auth";String INDEX = "index";String MAX_BULK_DOCS = "max.bulk.docs";String MAX_BULK_LENGTH = "max.bulk.size";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 5f772ba2d..a9aa35410 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@@ -86,13 +86,12 @@ public class ElasticIndexWriter implements IndexWriter {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 Boolean auth;private int maxBulkDocs;private int maxBulkLength;@@ -122,7 +121,7 @@ public class ElasticIndexWriter implements IndexWriter {*/@Overridepublic void open(IndexWriterParams parameters) throws IOException {- cluster = parameters.get(ElasticConstants.CLUSTER);+String hosts = parameters.get(ElasticConstants.HOSTS);if (StringUtils.isBlank(hosts)) {@@ -170,6 +169,7 @@ public class ElasticIndexWriter implements IndexWriter {hosts = parameters.getStrings(ElasticConstants.HOSTS);port = parameters.getInt(ElasticConstants.PORT, DEFAULT_PORT);+ auth = parameters.getBoolean(ElasticConstants.USE_AUTH, false);user = parameters.get(ElasticConstants.USER, DEFAULT_USER);password = parameters.get(ElasticConstants.PASSWORD, "");@@ -186,15 +186,7 @@ public class ElasticIndexWriter implements IndexWriter {hostsList[i++] = new HttpHost(host, port);}RestClientBuilder restClientBuilder = RestClient.builder(hostsList);- if (StringUtils.isNotBlank(cluster)) {- Header[] defaultHeaders = new Header[] {- new BasicHeader("cluster.name", cluster) };- restClientBuilder.setDefaultHeaders(defaultHeaders);- } else {- LOG.debug("No cluster name provided so using default");- }- // Only add username and password if password is configured- if (StringUtils.isNotBlank(password)) {+ if (auth) {restClientBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {@Override@@ -310,17 +302,17 @@ public class ElasticIndexWriter implements IndexWriter {public Map<String, Map.Entry<String, Object>> describe() {Map<String, Map.Entry<String, Object>> properties = new LinkedHashMap<>();- properties.put(ElasticConstants.CLUSTER, new AbstractMap.SimpleEntry<>(- "The cluster name to discover. Either host and port must be defined or cluster.",- this.cluster));- properties.put(ElasticConstants.HOSTS, new AbstractMap.SimpleEntry<>(- "Comma-separated list of hostnames to send documents to using TransportClient. "- + "Either host and port must be defined or cluster.",- this.hosts == null ? "" : String.join(",", hosts)));+ properties.put(ElasticConstants.HOSTS,+ new AbstractMap.SimpleEntry<>("Comma-separated list of hostnames",+ this.hosts == null ? "" : String.join(",", hosts)));properties.put(ElasticConstants.PORT, new AbstractMap.SimpleEntry<>(- "The port to connect to using TransportClient.", this.port));+ "The port to connect to elastic server.", this.port));properties.put(ElasticConstants.INDEX, new AbstractMap.SimpleEntry<>("Default index to send documents to.", this.defaultIndex));+ properties.put(ElasticConstants.USER, new AbstractMap.SimpleEntry<>(+ "Username for auth credentials", this.user));+ properties.put(ElasticConstants.PASSWORD, new AbstractMap.SimpleEntry<>(+ "Password for auth credentials", this.password));properties.put(ElasticConstants.MAX_BULK_DOCS,new AbstractMap.SimpleEntry<>("Maximum size of the bulk in number of documents.",