From 80e9546cec826eb93bdb0e9c2d3cc1fbd3483b5c Mon Sep 17 00:00:00 2001 From: Emil Tullstedt Date: Wed, 17 Jun 2020 07:39:50 +0200 Subject: [PATCH] Settings: Add setting for hiding version number for anonymous users (#24919) * Settings: Add setting for hiding version number for anonymous users Fixes #12925 * Hide version string from footer when unavailable * Settings: Test frontend settings with hide version for anonymous users * Settings: Add hide version variable to frontend settings * Make AnonymousHideVersion non-global Signed-off-by: Arve Knudsen * Settings: Improve test neighbor friendliness, reset state before and after * Settings: Use T.Cleanup Co-authored-by: Arve Knudsen --- conf/defaults.ini | 3 + conf/sample.ini | 3 + packages/grafana-data/src/types/config.ts | 1 + pkg/api/frontendsettings.go | 18 ++- pkg/api/frontendsettings_test.go | 125 ++++++++++++++++++ pkg/api/http_server.go | 9 +- pkg/api/index.go | 7 +- pkg/setting/setting.go | 3 + public/app/core/components/Footer/Footer.tsx | 5 + public/app/plugins/panel/homelinks/module.tsx | 9 +- 10 files changed, 170 insertions(+), 13 deletions(-) create mode 100644 pkg/api/frontendsettings_test.go diff --git a/conf/defaults.ini b/conf/defaults.ini index 5be70a1cce9..545e5cd652a 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -315,6 +315,9 @@ org_name = Main Org. # specify role for unauthenticated users org_role = Viewer +# mask the Grafana version number for unauthenticated users +hide_version = false + #################################### Github Auth ######################### [auth.github] enabled = false diff --git a/conf/sample.ini b/conf/sample.ini index 919ca48c34e..5fa77743736 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -314,6 +314,9 @@ # specify role for unauthenticated users ;org_role = Viewer +# mask the Grafana version number for unauthenticated users +;hide_version = false + #################################### Github Auth ########################## [auth.github] ;enabled = false diff --git a/packages/grafana-data/src/types/config.ts b/packages/grafana-data/src/types/config.ts index d1c2dfb6bfc..7100784312f 100644 --- a/packages/grafana-data/src/types/config.ts +++ b/packages/grafana-data/src/types/config.ts @@ -20,6 +20,7 @@ export interface BuildInfo { edition: string; latestVersion: string; hasUpdate: boolean; + hideVersion: boolean; } /** diff --git a/pkg/api/frontendsettings.go b/pkg/api/frontendsettings.go index 1e6530f0132..365f3d8b87d 100644 --- a/pkg/api/frontendsettings.go +++ b/pkg/api/frontendsettings.go @@ -167,6 +167,17 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i } } + hideVersion := hs.Cfg.AnonymousHideVersion && !c.IsSignedIn + version := setting.BuildVersion + commit := setting.BuildCommit + buildstamp := setting.BuildStamp + + if hideVersion { + version = "" + commit = "" + buildstamp = 0 + } + jsonObj := map[string]interface{}{ "defaultDatasource": defaultDatasource, "datasources": datasources, @@ -197,9 +208,10 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i "disableSanitizeHtml": hs.Cfg.DisableSanitizeHtml, "pluginsToPreload": pluginsToPreload, "buildInfo": map[string]interface{}{ - "version": setting.BuildVersion, - "commit": setting.BuildCommit, - "buildstamp": setting.BuildStamp, + "hideVersion": hideVersion, + "version": version, + "commit": commit, + "buildstamp": buildstamp, "edition": hs.License.Edition(), "latestVersion": plugins.GrafanaLatestVersion, "hasUpdate": plugins.GrafanaHasUpdate, diff --git a/pkg/api/frontendsettings_test.go b/pkg/api/frontendsettings_test.go new file mode 100644 index 00000000000..090950a53e6 --- /dev/null +++ b/pkg/api/frontendsettings_test.go @@ -0,0 +1,125 @@ +package api + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "path" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/stretchr/testify/require" + + "github.com/grafana/grafana/pkg/services/rendering" + + "github.com/grafana/grafana/pkg/services/licensing" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/services/sqlstore" + + "github.com/grafana/grafana/pkg/middleware" + "gopkg.in/macaron.v1" + + "github.com/grafana/grafana/pkg/setting" +) + +func setupTestEnvironment(t *testing.T, cfg *setting.Cfg) (*macaron.Macaron, *HTTPServer) { + t.Helper() + sqlstore.InitTestDB(t) + + { + oldVersion := setting.BuildVersion + oldCommit := setting.BuildCommit + oldEnv := setting.Env + t.Cleanup(func() { + setting.BuildVersion = oldVersion + setting.BuildCommit = oldCommit + setting.Env = oldEnv + }) + } + + bus.ClearBusHandlers() + bus.AddHandler("sql", sqlstore.GetPluginSettings) + t.Cleanup(bus.ClearBusHandlers) + + r := &rendering.RenderingService{Cfg: cfg} + + hs := &HTTPServer{ + Cfg: cfg, + Bus: bus.GetBus(), + License: &licensing.OSSLicensingService{}, + RenderService: r, + } + + m := macaron.New() + m.Use(middleware.GetContextHandler(nil, nil, nil)) + m.Use(macaron.Renderer(macaron.RenderOptions{ + Directory: path.Join(setting.StaticRootPath, "views"), + IndentJSON: true, + Delims: macaron.Delims{Left: "[[", Right: "]]"}, + })) + m.Get("/api/frontend/settings/", hs.GetFrontendSettings) + + return m, hs +} + +func TestHTTPServer_GetFrontendSettings_hideVersionAnonyomus(t *testing.T) { + type buildInfo struct { + Version string `json:"version"` + Commit string `json:"commit"` + Env string `json:"env"` + } + type settings struct { + BuildInfo buildInfo `json:"buildInfo"` + } + + cfg := setting.NewCfg() + m, hs := setupTestEnvironment(t, cfg) + + req := httptest.NewRequest(http.MethodGet, "/api/frontend/settings", nil) + + setting.BuildVersion = "7.8.9" + setting.BuildCommit = "01234567" + setting.Env = "testing" + + tests := []struct { + hideVersion bool + expected settings + }{ + { + hideVersion: false, + expected: settings{ + BuildInfo: buildInfo{ + Version: setting.BuildVersion, + Commit: setting.BuildCommit, + Env: setting.Env, + }, + }, + }, + { + hideVersion: true, + expected: settings{ + BuildInfo: buildInfo{ + Version: "", + Commit: "", + Env: setting.Env, + }, + }, + }, + } + + for _, test := range tests { + hs.Cfg.AnonymousHideVersion = test.hideVersion + expected := test.expected + + recorder := httptest.NewRecorder() + m.ServeHTTP(recorder, req) + got := settings{} + err := json.Unmarshal(recorder.Body.Bytes(), &got) + require.NoError(t, err) + require.GreaterOrEqual(t, 400, recorder.Code, "status codes higher than 400 indicates a failure") + + assert.EqualValues(t, expected, got) + } +} diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 36cc9365fce..430c6766cb2 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -4,13 +4,14 @@ import ( "context" "crypto/tls" "fmt" - "github.com/grafana/grafana/pkg/services/search" "net" "net/http" "os" "path" "sync" + "github.com/grafana/grafana/pkg/services/search" + "github.com/grafana/grafana/pkg/plugins/backendplugin" "github.com/grafana/grafana/pkg/api/live" @@ -351,8 +352,10 @@ func (hs *HTTPServer) healthHandler(ctx *macaron.Context) { data := simplejson.New() data.Set("database", "ok") - data.Set("version", setting.BuildVersion) - data.Set("commit", setting.BuildCommit) + if !hs.Cfg.AnonymousHideVersion { + data.Set("version", setting.BuildVersion) + data.Set("commit", setting.BuildCommit) + } if err := bus.Dispatch(&models.GetDBHealthQuery{}); err != nil { data.Set("database", "failing") diff --git a/pkg/api/index.go b/pkg/api/index.go index 8c39e56ebea..0b2f223bc48 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -360,9 +360,14 @@ func (hs *HTTPServer) setIndexViewData(c *models.ReqContext) (*dtos.IndexViewDat }) } + helpVersion := fmt.Sprintf(`%s v%s (%s)`, setting.ApplicationName, setting.BuildVersion, setting.BuildCommit) + if hs.Cfg.AnonymousHideVersion && !c.IsSignedIn { + helpVersion = setting.ApplicationName + } + data.NavTree = append(data.NavTree, &dtos.NavLink{ Text: "Help", - SubTitle: fmt.Sprintf(`%s v%s (%s)`, setting.ApplicationName, setting.BuildVersion, setting.BuildCommit), + SubTitle: helpVersion, Id: "help", Url: "#", Icon: "question-circle", diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 7845f46b051..17c53892621 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -296,6 +296,8 @@ type Cfg struct { // Use to enable new features which may still be in alpha/beta stage. FeatureToggles map[string]bool + + AnonymousHideVersion bool } // IsExpressionsEnabled returns whether the expressions feature is enabled. @@ -873,6 +875,7 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { if err != nil { return err } + cfg.AnonymousHideVersion = iniFile.Section("auth.anonymous").Key("hide_version").MustBool(false) // auth proxy authProxy := iniFile.Section("auth.proxy") diff --git a/public/app/core/components/Footer/Footer.tsx b/public/app/core/components/Footer/Footer.tsx index 71d5dc0338b..15f93ac4454 100644 --- a/public/app/core/components/Footer/Footer.tsx +++ b/public/app/core/components/Footer/Footer.tsx @@ -38,6 +38,11 @@ export let getVersionLinks = (): FooterLink[] => { const stateInfo = licenseInfo.stateInfo ? ` (${licenseInfo.stateInfo})` : ''; links.push({ text: `${buildInfo.edition}${stateInfo}`, url: licenseInfo.licenseUrl }); + + if (buildInfo.hideVersion) { + return links; + } + links.push({ text: `v${buildInfo.version} (${buildInfo.commit})` }); if (buildInfo.hasUpdate) { diff --git a/public/app/plugins/panel/homelinks/module.tsx b/public/app/plugins/panel/homelinks/module.tsx index e6b437077f3..04059dd2535 100755 --- a/public/app/plugins/panel/homelinks/module.tsx +++ b/public/app/plugins/panel/homelinks/module.tsx @@ -70,13 +70,10 @@ export const HomeLink: FC = ({ title, url, target, icon }) => { export const VersionFooter: FC = () => { const styles = getStyles(); - const { version, commit } = config.buildInfo; + const { hideVersion, version, commit } = config.buildInfo; + const versionString = hideVersion ? '' : `Version ${version} (${commit})`; - return ( -
- Version {version} ({commit}) -
- ); + return
{versionString}
; }; export const getStyles = stylesFactory(() => {