From d56d855185d2f216600fd27636e98eb54d87937c Mon Sep 17 00:00:00 2001 From: Petri-Johan Last Date: Mon, 12 Jun 2023 15:17:49 +0200 Subject: [PATCH] Add an alert banner to check for revoked licenses (#53294) --- enterprise/cmd/frontend/internal/authz/init.go | 11 +++++++++++ enterprise/internal/licensing/features.go | 5 +++++ enterprise/internal/licensing/licensing.go | 6 +----- enterprise/internal/licensing/licensing_test.go | 6 +++--- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/enterprise/cmd/frontend/internal/authz/init.go b/enterprise/cmd/frontend/internal/authz/init.go index c041bba49d4..a831a1a4c9f 100644 --- a/enterprise/cmd/frontend/internal/authz/init.go +++ b/enterprise/cmd/frontend/internal/authz/init.go @@ -79,6 +79,17 @@ func Init( return errors.Wrap(err, "Failed to createe sub-repo client") } + graphqlbackend.AlertFuncs = append(graphqlbackend.AlertFuncs, func(args graphqlbackend.AlertFuncArgs) []*graphqlbackend.Alert { + if licensing.IsLicenseValid() { + return nil + } + + return []*graphqlbackend.Alert{{ + TypeValue: graphqlbackend.AlertTypeError, + MessageValue: "To continue using Sourcegraph, a site admin must renew the Sourcegraph license (or downgrade to only using Sourcegraph Free features). Update the license key in the [**site configuration**](/site-admin/configuration).", + }} + }) + // Warn about usage of authz providers that are not enabled by the license. graphqlbackend.AlertFuncs = append(graphqlbackend.AlertFuncs, func(args graphqlbackend.AlertFuncArgs) []*graphqlbackend.Alert { // Only site admins can act on this alert, so only show it to site admins. diff --git a/enterprise/internal/licensing/features.go b/enterprise/internal/licensing/features.go index 3ce8ce3cea4..8162a69f0a1 100644 --- a/enterprise/internal/licensing/features.go +++ b/enterprise/internal/licensing/features.go @@ -141,6 +141,11 @@ func Check(feature Feature) error { if err != nil { return errors.WithMessage(err, fmt.Sprintf("checking feature %q activation", feature)) } + + if !IsLicenseValid() { + return errors.New("Sourcegraph license is no longer valid") + } + return feature.Check(info) } diff --git a/enterprise/internal/licensing/licensing.go b/enterprise/internal/licensing/licensing.go index 3380d2346f4..0e765b983b5 100644 --- a/enterprise/internal/licensing/licensing.go +++ b/enterprise/internal/licensing/licensing.go @@ -98,7 +98,7 @@ func GetConfiguredProductLicenseInfo() (*Info, error) { return info, err } -func isLicenseValid() bool { +func IsLicenseValid() bool { val := store.Get(licenseValidityStoreKey) if val.IsNil() { return true @@ -141,10 +141,6 @@ func GetConfiguredProductLicenseInfoWithSignature() (*Info, string, error) { return nil, "", err } - if !isLicenseValid() { - return nil, "", errors.New("license is not valid") - } - lastKeyText = keyText lastInfo = info lastSignature = signature diff --git a/enterprise/internal/licensing/licensing_test.go b/enterprise/internal/licensing/licensing_test.go index 0a587944780..172b825441a 100644 --- a/enterprise/internal/licensing/licensing_test.go +++ b/enterprise/internal/licensing/licensing_test.go @@ -24,18 +24,18 @@ func TestIsLicenseValid(t *testing.T) { t.Run("unset key returns true", func(t *testing.T) { cleanupStore(t, store) - require.True(t, isLicenseValid()) + require.True(t, IsLicenseValid()) }) t.Run("set false key returns false", func(t *testing.T) { cleanupStore(t, store) require.NoError(t, store.Set(licenseValidityStoreKey, false)) - require.False(t, isLicenseValid()) + require.False(t, IsLicenseValid()) }) t.Run("set true key returns true", func(t *testing.T) { cleanupStore(t, store) require.NoError(t, store.Set(licenseValidityStoreKey, true)) - require.True(t, isLicenseValid()) + require.True(t, IsLicenseValid()) }) }