From 39b071bc0e620b24a9dc8b13fcd6ccf5f6732ea5 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Fri, 7 Jul 2017 17:12:14 +0100 Subject: Add optional config parameter to test elasticsearch endpoint. (#6877) --- api4/elasticsearch.go | 7 ++++++- app/elasticsearch.go | 13 +++++++++++-- einterfaces/elasticsearch.go | 2 +- i18n/en.json | 4 ++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/api4/elasticsearch.go b/api4/elasticsearch.go index 05ef1f539..9eafec48b 100644 --- a/api4/elasticsearch.go +++ b/api4/elasticsearch.go @@ -19,12 +19,17 @@ func InitElasticsearch() { } func testElasticsearch(c *Context, w http.ResponseWriter, r *http.Request) { + cfg := model.ConfigFromJson(r.Body) + if cfg == nil { + cfg = utils.Cfg + } + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) return } - if err := app.TestElasticsearch(); err != nil { + if err := app.TestElasticsearch(cfg); err != nil { c.Err = err return } diff --git a/app/elasticsearch.go b/app/elasticsearch.go index 87687ddcc..ef34a6074 100644 --- a/app/elasticsearch.go +++ b/app/elasticsearch.go @@ -8,11 +8,20 @@ import ( "github.com/mattermost/platform/model" "github.com/mattermost/platform/einterfaces" + "github.com/mattermost/platform/utils" ) -func TestElasticsearch() *model.AppError { +func TestElasticsearch(cfg *model.Config) *model.AppError { + if *cfg.ElasticSearchSettings.Password == model.FAKE_SETTING { + if *cfg.ElasticSearchSettings.ConnectionUrl == *utils.Cfg.ElasticSearchSettings.ConnectionUrl && *cfg.ElasticSearchSettings.Username == *utils.Cfg.ElasticSearchSettings.Username { + *cfg.ElasticSearchSettings.Password = *utils.Cfg.ElasticSearchSettings.Password + } else { + return model.NewAppError("TestElasticsearch", "ent.elasticsearch.test_config.reenter_password", nil, "", http.StatusBadRequest) + } + } + if esI := einterfaces.GetElasticsearchInterface(); esI != nil { - if err := esI.TestConfig(); err != nil { + if err := esI.TestConfig(cfg); err != nil { return err } } else { diff --git a/einterfaces/elasticsearch.go b/einterfaces/elasticsearch.go index e4803aa88..f5babffe0 100644 --- a/einterfaces/elasticsearch.go +++ b/einterfaces/elasticsearch.go @@ -10,7 +10,7 @@ type ElasticsearchInterface interface { IndexPost(post *model.Post, teamId string) *model.AppError SearchPosts(channels *model.ChannelList, searchParams []*model.SearchParams) ([]string, *model.AppError) DeletePost(postId string) *model.AppError - TestConfig() *model.AppError + TestConfig(cfg *model.Config) *model.AppError } var theElasticsearchInterface ElasticsearchInterface diff --git a/i18n/en.json b/i18n/en.json index 88ada5ba4..d6eaffc7f 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3547,6 +3547,10 @@ "id": "ent.elasticsearch.test_config.license.error", "translation": "License does not support Elasticsearch." }, + { + "id": "ent.elasticsearch.test_config.reenter_password", + "translation": "The Elasticsearch Server URL or Username has changed. Please re-enter the Elasticsearch password to test connection." + }, { "id": "ent.emoji.licence_disable.app_error", "translation": "Custom emoji restrictions disabled by current license. Please contact your system administrator about upgrading your enterprise license." -- cgit v1.2.3-1-g7c22