From 4a68ba7b233f31202d94f07dc193fc599ad1b8eb Mon Sep 17 00:00:00 2001 From: Dan Cech Date: Wed, 1 Jul 2020 13:02:53 -0400 Subject: [PATCH] provide license token directly via plugin environment (#25987) * provide license token directly via plugin environment (cherry picked from commit b5ca2381bc9f4b7c5747a6d11149b6d02973ec2c) --- pkg/models/licensing.go | 2 ++ pkg/plugins/backendplugin/manager.go | 6 +++++- pkg/plugins/backendplugin/manager_test.go | 10 ++++++++-- pkg/services/licensing/oss.go | 4 ++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/models/licensing.go b/pkg/models/licensing.go index a1440de5f6d..aadc0e8b3bb 100644 --- a/pkg/models/licensing.go +++ b/pkg/models/licensing.go @@ -16,4 +16,6 @@ type Licensing interface { LicenseURL(user *SignedInUser) string StateInfo() string + + TokenRaw() string } diff --git a/pkg/plugins/backendplugin/manager.go b/pkg/plugins/backendplugin/manager.go index b58ae5bf2b3..fe47dc77ad6 100644 --- a/pkg/plugins/backendplugin/manager.go +++ b/pkg/plugins/backendplugin/manager.go @@ -100,7 +100,11 @@ func (m *manager) Register(pluginID string, factory PluginFactoryFunc) error { } if m.License.HasLicense() { - hostEnv = append(hostEnv, fmt.Sprintf("GF_ENTERPRISE_LICENSE_PATH=%s", m.Cfg.EnterpriseLicensePath)) + hostEnv = append( + hostEnv, + fmt.Sprintf("GF_ENTERPRISE_LICENSE_PATH=%s", m.Cfg.EnterpriseLicensePath), + fmt.Sprintf("GF_ENTERPRISE_LICENSE_TEXT=%s", m.License.TokenRaw()), + ) } env := pluginSettings.ToEnv("GF_PLUGIN", hostEnv) diff --git a/pkg/plugins/backendplugin/manager_test.go b/pkg/plugins/backendplugin/manager_test.go index ec62a1778ff..28fb8a737cc 100644 --- a/pkg/plugins/backendplugin/manager_test.go +++ b/pkg/plugins/backendplugin/manager_test.go @@ -251,6 +251,7 @@ func TestManager(t *testing.T) { t.Run("Plugin registration scenario when Grafana is licensed", func(t *testing.T) { ctx.license.edition = "Enterprise" ctx.license.hasLicense = true + ctx.license.tokenRaw = "testtoken" ctx.cfg.BuildVersion = "7.0.0" ctx.cfg.EnterpriseLicensePath = "/license.txt" @@ -258,8 +259,8 @@ func TestManager(t *testing.T) { require.NoError(t, err) t.Run("Should provide expected host environment variables", func(t *testing.T) { - require.Len(t, ctx.env, 3) - require.EqualValues(t, []string{"GF_VERSION=7.0.0", "GF_EDITION=Enterprise", "GF_ENTERPRISE_LICENSE_PATH=/license.txt"}, ctx.env) + require.Len(t, ctx.env, 4) + require.EqualValues(t, []string{"GF_VERSION=7.0.0", "GF_EDITION=Enterprise", "GF_ENTERPRISE_LICENSE_PATH=/license.txt", "GF_ENTERPRISE_LICENSE_TEXT=testtoken"}, ctx.env) }) }) }) @@ -383,6 +384,7 @@ func (tp *testPlugin) CallResource(ctx context.Context, req *backend.CallResourc type testLicensingService struct { edition string hasLicense bool + tokenRaw string } func (t *testLicensingService) HasLicense() bool { @@ -408,3 +410,7 @@ func (t *testLicensingService) LicenseURL(user *models.SignedInUser) string { func (t *testLicensingService) HasValidLicense() bool { return false } + +func (t *testLicensingService) TokenRaw() string { + return t.tokenRaw +} diff --git a/pkg/services/licensing/oss.go b/pkg/services/licensing/oss.go index a903567d95a..0e967840036 100644 --- a/pkg/services/licensing/oss.go +++ b/pkg/services/licensing/oss.go @@ -56,3 +56,7 @@ func (l *OSSLicensingService) Init() error { func (*OSSLicensingService) HasValidLicense() bool { return false } + +func (*OSSLicensingService) TokenRaw() string { + return "" +}