From e18b1cf89f024089b50ad7c4798eb7fb954fe874 Mon Sep 17 00:00:00 2001 From: Michael Kochell <6913320+mickmister@users.noreply.github.com> Date: Thu, 9 Jun 2022 15:57:02 -0400 Subject: [PATCH] Check for cloud license and CWS error when checking integration freemium limits (#20387) Co-authored-by: Mattermod --- api4/config_test.go | 4 ++++ app/integrations.go | 11 +++++++++-- app/plugin_test.go | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/api4/config_test.go b/api4/config_test.go index d511e17afd..629ccc690b 100644 --- a/api4/config_test.go +++ b/api4/config_test.go @@ -250,6 +250,8 @@ func TestUpdateConfig(t *testing.T) { }) t.Run("Should not be able to save config if the new config exceeds Freemium limits", func(t *testing.T) { + th.App.Srv().SetLicense(model.NewTestLicense("cloud")) + defer th.App.Srv().RemoveLicense() os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true") defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE") th.App.ReloadConfig() @@ -853,6 +855,8 @@ func TestPatchConfig(t *testing.T) { }) t.Run("Should not be able to save config if the new config exceeds Freemium limits", func(t *testing.T) { + th.App.Srv().SetLicense(model.NewTestLicense("cloud")) + defer th.App.Srv().RemoveLicense() os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true") defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE") th.App.ReloadConfig() diff --git a/app/integrations.go b/app/integrations.go index 7cb28b4afd..149bcefcc1 100644 --- a/app/integrations.go +++ b/app/integrations.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/mattermost/mattermost-server/v6/model" + "github.com/mattermost/mattermost-server/v6/shared/mlog" ) func (a *App) checkIntegrationLimitsForConfigSave(oldConfig, newConfig *model.Config) *model.AppError { @@ -73,6 +74,10 @@ func (a *App) checkIfIntegrationsMeetFreemiumLimits(originalPluginIds []string) return nil } + if a.License() == nil || !*a.License().Features.Cloud { + return nil + } + pluginIds := map[string]bool{} for _, pluginId := range originalPluginIds { if _, ok := model.InstalledIntegrationsIgnoredPlugins[pluginId]; !ok { @@ -82,7 +87,8 @@ func (a *App) checkIfIntegrationsMeetFreemiumLimits(originalPluginIds []string) limits, err := a.Cloud().GetCloudLimits("") if err != nil { - return model.NewAppError("checkIfIntegrationMeetsFreemiumLimits", "api.cloud.request_error", nil, err.Error(), http.StatusInternalServerError) + a.Log().Error("Error fetching cloud limits for enabled integrations", mlog.Err(err)) + return nil } if limits == nil || limits.Integrations == nil || limits.Integrations.Enabled == nil { @@ -91,7 +97,8 @@ func (a *App) checkIfIntegrationsMeetFreemiumLimits(originalPluginIds []string) installed, appErr := a.ch.getInstalledIntegrations() if appErr != nil { - return appErr + a.Log().Error("Failed to get installed integrations to check cloud limit", mlog.Err(appErr)) + return nil } enableCount := len(pluginIds) diff --git a/app/plugin_test.go b/app/plugin_test.go index 4dec6efd66..0cba49671a 100644 --- a/app/plugin_test.go +++ b/app/plugin_test.go @@ -7,6 +7,7 @@ import ( "bytes" "crypto/sha256" "encoding/base64" + "errors" "fmt" "io/ioutil" "net/http" @@ -1025,8 +1026,26 @@ func TestEnablePluginWithCloudLimits(t *testing.T) { checkError(t, appErr) require.Equal(t, "app.install_integration.reached_max_limit.error", appErr.Id) + th.App.Srv().RemoveLicense() + appErr = th.App.EnablePlugin("testplugin2") + checkNoError(t, appErr) + th.App.Srv().SetLicense(model.NewTestLicense("cloud")) + appErr = th.App.EnablePlugin("testplugin2") + checkError(t, appErr) + os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE") th.App.ReloadConfig() + appErr = th.App.EnablePlugin("testplugin2") + checkNoError(t, appErr) + os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true") + th.App.ReloadConfig() + appErr = th.App.EnablePlugin("testplugin2") + checkError(t, appErr) + + // Let enable succeed if a CWS error occurs + cloud = &mocks.CloudInterface{} + th.App.Srv().Cloud = cloud + cloud.Mock.On("GetCloudLimits", mock.Anything).Return(nil, errors.New("error getting limits")) appErr = th.App.EnablePlugin("testplugin2") checkNoError(t, appErr)