From 738c6d5e65425e8c6e93a3e95fe222cac030edb2 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 18 Jul 2017 12:05:07 +0000
Subject: [PATCH] Bug 18950 - Elasticsearch - Add password access for admin
 functions

To test:
1 - Load 'Search engine configuration' from admin page
http://localhost:8081/cgi-bin/koha/admin/searchengine/elasticsearch/mappings.pl
2 - Verify this works as before the patch
3 - Add <elasticsearch_admin_password> to koha-conf.xml
4 - Reload search config page, note additional option to reset mappings
5 - Attempt reset with bad password
6 - Attempt reset with correct password
7 - Attempt a reindex with bad password
8 - Attempt a reindex with correct password
---
 admin/searchengine/elasticsearch/mappings.pl       | 24 ++++++++++++++++------
 debian/scripts/koha-create                         |  3 +++
 debian/templates/koha-conf-site.xml.in             |  4 ++++
 .../admin/searchengine/elasticsearch/mappings.tt   | 21 ++++++++++++++++++-
 4 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl
index 7d63002..c06be81 100755
--- a/admin/searchengine/elasticsearch/mappings.pl
+++ b/admin/searchengine/elasticsearch/mappings.pl
@@ -22,6 +22,7 @@ use C4::Output;
 use C4::Auth;
 
 use Koha::SearchEngine::Elasticsearch;
+use Koha::SearchEngine::Elasticsearch::Indexer;
 use Koha::SearchMarcMaps;
 use Koha::SearchFields;
 
@@ -37,6 +38,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
 
 my $index = $input->param('index') || 'biblios';
 my $op    = $input->param('op')    || 'list';
+my $action = $input->param('action') || '';
 my @messages;
 
 my $database = Koha::Database->new();
@@ -97,12 +99,21 @@ if ( $op eq 'edit' ) {
         $schema->storage->txn_commit;
     }
 }
-elsif( $op eq 'reset' ) {
-    # TODO Move this feature to the interface
-    my $sure = $input->param('i_know_what_i_am_doing');
-    if ( $sure ) {
-        Koha::SearchMarcMaps->search->delete;
-        Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
+elsif( $op eq 'admin') {
+    if( $input->param('admin_password') eq C4::Context->config('elasticsearch_admin_password') ){
+        if ( $action eq 'reset' ) {
+            Koha::SearchMarcMaps->search->delete;
+            Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
+            $template->param( mappings_reset => 1 );
+        }
+        elsif ( $action eq 'reindex' ) {
+            my $reindex_bibs = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' })->do_reindex( undef, undef, 0, [] );
+            my $reindex_auths = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'authorities' })->do_reindex( undef, undef, 0, [] );
+            $template->param( reindex_launched => 1 );
+        }
+    }
+    else {
+        $template->param( admin_badpassword => 1 );
     }
 }
 
@@ -139,6 +150,7 @@ $template->param(
     indexes           => \@indexes,
     all_search_fields => \@all_search_fields,
     messages          => \@messages,
+    show_admin_options => C4::Context->config('elasticsearch_admin_password') ? 1 : 0,
 );
 
 output_html_with_http_headers $input, $cookie, $template->output;
diff --git a/debian/scripts/koha-create b/debian/scripts/koha-create
index ba4f043..0b927be 100755
--- a/debian/scripts/koha-create
+++ b/debian/scripts/koha-create
@@ -106,6 +106,7 @@ generate_config_file() {
         -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
         -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
         -e "s/__API_SECRET__/$API_SECRET/g" \
+        -e "s/__ES_ADMIN_PASSWORD__/$ES_ADMIN_PASSWORD/g" \
         -e "s/__DB_NAME__/$mysqldb/g" \
         -e "s/__DB_HOST__/$mysqlhost/g" \
         -e "s/__DB_USER__/$mysqluser/g" \
@@ -406,6 +407,8 @@ UPLOAD_DIR="uploads"
 UPLOAD_PATH=""
 # Generate a randomizaed API secret
 API_SECRET="$(pwgen -s 64 1)"
+# Generate a randomizaed ES Admin Password secret
+ES_ADMIN_PASSWORD="$(pwgen -s 16 1)"
 # SRU server variables
 ENABLE_SRU="no"
 SRU_SERVER_PORT=""
diff --git a/debian/templates/koha-conf-site.xml.in b/debian/templates/koha-conf-site.xml.in
index 8e8204f..e990b33 100644
--- a/debian/templates/koha-conf-site.xml.in
+++ b/debian/templates/koha-conf-site.xml.in
@@ -326,6 +326,10 @@ __END_SRU_PUBLICSERVER__
      <server>localhost:9200</server>
      <index_name>koha___KOHASITE__</index_name>
  </elasticsearch>
+ <!-- Uncomment the following line to allow access to reindexing tools via Search
+ Configuration page in staff interface
+ <elasticsearch_admin_password>__ES_ADMIN_PASSWORD__</elasticsearch_admin_password>
+ -->
 
 </config>
 </yazgfs>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
index 25535d9..5736703 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
@@ -1,3 +1,4 @@
+[% USE Koha %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; Elastic Search mappings</title>
 [% INCLUDE 'doc-head-close.inc' %]
@@ -88,7 +89,25 @@ a.add, a.delete {
 
     <h1>Search engine configuration</h1>
     <div class="warning">
-        Warning: Any modification in these configurations will need a total reindexation to be fully taken into account !
+        Warning: Any modification in these configurations will need a total reindexation to be fully taken into account ! <br/>
+        [% IF (show_admin_options) %]
+            [% IF (admin_badpassword) %]<p>The password you entered was incorrect!</p>[% END %]
+            [% IF (reindex_launched) %]<p>A rebuild has been completed<p/>
+            [% ELSIF (mappings_reset) %]<p>Mappings have been reset<p/>
+            [% ELSE %]
+                <form method="post">
+                    <label for="admin_password">Enter the Elasticsearch admin password and select action</labe;>
+                    <input type="text" name="admin_password" value="" id="admin_password"/>
+                    <input type="hidden" name="op" value="admin" />
+                    <select name="action">
+                       <option value="reindex">Launch reindex</option>
+                       <option value="reset">Reset mappings</option>
+                    </select>
+                    <input type="submit" value="Submit" />
+                </form>
+            [% END %]
+        [% END %]
+
     </div>
     [% IF errors %]
         <div class="error">
-- 
2.1.4