From 2c092291f5e6d4a1b8c7e519bbf0b0bce0058161 Mon Sep 17 00:00:00 2001 From: Conor Macpherson Date: Mon, 19 Dec 2022 17:11:27 -0500 Subject: [PATCH] Reorder data extraction, define review profile in-place rather than sectioned for each data domain --- api4/license.go | 54 +++++++++++++++++++++++++------------------- api4/license_test.go | 35 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/api4/license.go b/api4/license.go index 71de1ad298..6240be1190 100644 --- a/api4/license.go +++ b/api4/license.go @@ -301,31 +301,31 @@ func getPrevTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) { } func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { + // Only admins can request a true up review. + if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionManageSystem) { + c.SetPermissionError(model.PermissionManageLicenseInformation) + return + } + + if c.App.Cloud() == nil { + c.Err = model.NewAppError("requestRenewalLink", "api.license.upgrade_needed.app_error", nil, "", http.StatusForbidden) + return + } + license := c.App.Channels().License() if license == nil { http.Error(w, "A License is required to perform a true-up review", http.StatusBadRequest) return } + // Subscription Data userId := c.AppContext.Session().UserId subscription, err := c.App.Cloud().GetSubscription(userId) - if err != nil { + if err != nil || subscription == nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - reviewProfile := model.TrueUpReviewProfile{} - - // Server Data - reviewProfile.ServerId = c.App.TelemetryId() - reviewProfile.ServerVersion = model.CurrentVersion - reviewProfile.ServerInstallationType = os.Getenv(telemetry.EnvVarInstallType) - - // License Data - reviewProfile.LicenseId = license.Id - reviewProfile.LicensedSeats = subscription.Seats - reviewProfile.LicensePlan = license.SkuName - // Customer Info & Usage Analytics activeUserCount, err := c.App.Srv().Store().Status().GetTotalActiveUsersCount() if err != nil { @@ -333,9 +333,6 @@ func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { return } - reviewProfile.CustomerName = license.Customer.Name - reviewProfile.ActiveUsers = activeUserCount - // Webhook, calls, boards, and playbook counts incomingWebhookCount, err := c.App.Srv().Store().Webhook().GetIncomingTotal() if err != nil { @@ -348,9 +345,6 @@ func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { return } - reviewProfile.TotalIncomingWebhooks = incomingWebhookCount - reviewProfile.TotalOutgoingWebhooks = outgoingWebhookCount - // Plugin Data trueUpReviewPlugins := model.TrueUpReviewPlugins{ ActivePluginNames: []string{}, @@ -368,9 +362,8 @@ func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { trueUpReviewPlugins.TotalInactivePlugins += 1 } } - reviewProfile.Plugins = trueUpReviewPlugins - // Authentication Data + // Authentication Features mfaUsed := c.App.Config().ServiceSettings.EnforceMultifactorAuthentication ldapUsed := c.App.Config().LdapSettings.Enable samlUsed := c.App.Config().SamlSettings.Enable @@ -385,13 +378,28 @@ func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { model.TrueUpReviewAuthFeatureGuestAccess: guessAccessAllowed, } - reviewProfile.AuthenticationFeatures = []string{} + authFeatureList := []string{} for feature, used := range authFeatures { if used != nil && *used { - reviewProfile.AuthenticationFeatures = append(reviewProfile.AuthenticationFeatures, feature) + authFeatureList = append(authFeatureList, feature) } } + reviewProfile := model.TrueUpReviewProfile{ + ServerId: c.App.TelemetryId(), + ServerVersion: model.CurrentVersion, + ServerInstallationType: os.Getenv(telemetry.EnvVarInstallType), + LicenseId: license.Id, + LicensedSeats: subscription.Seats, + LicensePlan: license.SkuName, + CustomerName: license.Customer.Name, + ActiveUsers: activeUserCount, + TotalIncomingWebhooks: incomingWebhookCount, + TotalOutgoingWebhooks: outgoingWebhookCount, + Plugins: trueUpReviewPlugins, + AuthenticationFeatures: authFeatureList, + } + // Convert true up review profile struct to map var telemetryProperties map[string]interface{} reviewProfileJson, err := json.Marshal(reviewProfile) diff --git a/api4/license_test.go b/api4/license_test.go index d11fe1b150..f76fd4a084 100644 --- a/api4/license_test.go +++ b/api4/license_test.go @@ -330,3 +330,38 @@ func TestRequestRenewalLink(t *testing.T) { require.Equal(t, http.StatusBadRequest, resp.StatusCode) }) } + +func TestRequestTrueUpReview(t *testing.T) { + th := Setup(t) + defer th.TearDown() + + subscription := &model.Subscription{ + ID: "MySubscriptionID", + CustomerID: "MyCustomer", + ProductID: "SomeProductId", + AddOns: []string{}, + StartAt: 1000000000, + EndAt: 2000000000, + CreateAt: 1000000000, + Seats: 10, + IsFreeTrial: "true", + DNS: "some.dns.server", + TrialEndAt: 2000000000, + LastInvoice: &model.Invoice{}, + } + + th.App.Srv().SetLicense(model.NewTestLicense()) + + cloud := mocks.CloudInterface{} + cloud.Mock.On("GetSubscription", mock.Anything).Return(subscription, nil) + + cloudImpl := th.App.Srv().Cloud + th.App.Srv().Cloud = &cloud + defer func() { + th.App.Srv().Cloud = cloudImpl + }() + + resp, err := th.SystemAdminClient.DoAPIPost("/license/review", "") + require.Nil(t, err) + require.Equal(t, http.StatusOK, resp.StatusCode) +}