From 738c6d5e65425e8c6e93a3e95fe222cac030edb2 Mon Sep 17 00:00:00 2001 From: Nick Clemens 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 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__ localhost:9200 koha___KOHASITE__ + 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' %] Koha › Administration › Elastic Search mappings [% INCLUDE 'doc-head-close.inc' %] @@ -88,7 +89,25 @@ a.add, a.delete {

Search engine configuration

- 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 !
+ [% IF (show_admin_options) %] + [% IF (admin_badpassword) %]

The password you entered was incorrect!

[% END %] + [% IF (reindex_launched) %]

A rebuild has been completed

+ [% ELSIF (mappings_reset) %]

Mappings have been reset

+ [% ELSE %] +

+
[% IF errors %]
-- 2.1.4