From 7319caaa63282f84a15d95c210e678e212f9284f Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Tue, 25 May 2021 15:50:11 +0100 Subject: [PATCH] API: Use SettingsProvider on GET settings handler (#34632) (#34661) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit b74a502dc466a464516aeb5c3e3f7da1ba895162) Co-authored-by: Joan López de la Franca Beltran --- pkg/api/admin.go | 17 ++--------------- pkg/api/api.go | 2 +- pkg/setting/provider.go | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/pkg/api/admin.go b/pkg/api/admin.go index 090e4f07886..643db782be2 100644 --- a/pkg/api/admin.go +++ b/pkg/api/admin.go @@ -4,23 +4,10 @@ import ( "github.com/grafana/grafana/pkg/api/response" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/models" - "github.com/grafana/grafana/pkg/setting" ) -func AdminGetSettings(c *models.ReqContext) response.Response { - settings := make(map[string]interface{}) - - for _, section := range setting.Raw.Sections() { - jsonSec := make(map[string]interface{}) - settings[section.Name()] = jsonSec - - for _, key := range section.Keys() { - keyName := key.Name() - jsonSec[keyName] = setting.RedactedValue(keyName, key.Value()) - } - } - - return response.JSON(200, settings) +func (hs *HTTPServer) AdminGetSettings(_ *models.ReqContext) response.Response { + return response.JSON(200, hs.SettingsProvider.Current()) } func AdminGetStats(c *models.ReqContext) response.Response { diff --git a/pkg/api/api.go b/pkg/api/api.go index 289f662b2f0..eed0833cbb7 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -440,7 +440,7 @@ func (hs *HTTPServer) registerRoutes() { // admin api r.Group("/api/admin", func(adminRoute routing.RouteRegister) { - adminRoute.Get("/settings", reqGrafanaAdmin, routing.Wrap(AdminGetSettings)) + adminRoute.Get("/settings", reqGrafanaAdmin, routing.Wrap(hs.AdminGetSettings)) adminRoute.Get("/stats", reqGrafanaAdmin, routing.Wrap(AdminGetStats)) adminRoute.Post("/pause-all-alerts", reqGrafanaAdmin, bind(dtos.PauseAllAlertsCommand{}), routing.Wrap(PauseAllAlerts)) diff --git a/pkg/setting/provider.go b/pkg/setting/provider.go index cddc8177981..5e387669833 100644 --- a/pkg/setting/provider.go +++ b/pkg/setting/provider.go @@ -32,7 +32,13 @@ func (v ValidationError) Error() string { // Provider is a settings provider abstraction // with thread-safety and runtime updates. type Provider interface { - // Update + // Current returns a SettingsBag with a static copy of + // the current configured pairs of key/values for each + // configuration section. + Current() SettingsBag + // Update receives a SettingsBag with the pairs of key/values + // to be updated per section and a SettingsRemovals with the + // section keys to be removed. Update(updates SettingsBag, removals SettingsRemovals) error // KeyValue returns a key-value abstraction // for the given pair of section and key. @@ -94,6 +100,19 @@ func (o OSSImpl) Init() error { return nil } +func (o OSSImpl) Current() SettingsBag { + settingsCopy := make(SettingsBag) + + for _, section := range o.Cfg.Raw.Sections() { + settingsCopy[section.Name()] = make(map[string]string) + for _, key := range section.Keys() { + settingsCopy[section.Name()][key.Name()] = RedactedValue(key.Name(), key.Value()) + } + } + + return settingsCopy +} + func (OSSImpl) Update(SettingsBag, SettingsRemovals) error { return errors.New("oss settings provider do not have support for settings updates") }