From cad414fd4dfd10f16cd41a82d35bf944a2f2616d Mon Sep 17 00:00:00 2001 From: Conor Macpherson Date: Tue, 13 Dec 2022 13:09:20 -0400 Subject: [PATCH] Use proper error handling, fix up data structure, attempt to send telemetry data. --- api4/license.go | 59 ++++++++++++++++++++++++++------- model/license.go | 9 +++++ model/true_up_review_profile.go | 1 - 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/api4/license.go b/api4/license.go index 749fd71ead..0aa6d301b3 100644 --- a/api4/license.go +++ b/api4/license.go @@ -303,13 +303,14 @@ func getPrevTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) { func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { license := c.App.Channels().License() if license == nil { + http.Error(w, "A License is required to perform a true-up review", http.StatusBadRequest) return } userId := c.AppContext.Session().UserId subscription, err := c.App.Cloud().GetSubscription(userId) if err != nil { - fmt.Printf("1: %+v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -325,26 +326,26 @@ func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { reviewProfile.LicensedSeats = subscription.Seats reviewProfile.LicensePlan = license.SkuName + // Customer Info & Usage Analytics activeUserCount, err := c.App.Srv().GetStore().Status().GetTotalActiveUsersCount() if err != nil { - fmt.Printf("2: %+v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } - // Customer Info & Usage Analytics reviewProfile.CustomerName = license.Customer.Name reviewProfile.ActiveUsers = activeUserCount - // Webhook, call, board, playbook counts + // Webhook, calls, boards, and playbook counts var totalWebHookCount int64 = 0 incomingWebhookCount, err := c.App.Srv().Store().Webhook().GetIncomingTotal() if err != nil { - fmt.Printf("3: %+v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } outgoingWebhookCount, err := c.App.Srv().Store().Webhook().GetOutgoingTotal() if err != nil { - fmt.Printf("4: %+v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -352,12 +353,16 @@ func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { totalWebHookCount += outgoingWebhookCount reviewProfile.TotalWebhooks = totalWebHookCount - reviewProfile.TotalCalls = 0 - reviewProfile.TotalBoards = 0 - reviewProfile.TotalPlaybooks = 0 + reviewProfile.TotalCalls = 0 // TODO: Maybe from plugin? + reviewProfile.TotalBoards = 0 // TODO: Maybe from plugin? + reviewProfile.TotalPlaybooks = 0 // TODO: Maybe from plugin? // Plugin Data - trueUpReviewPlugins := model.TrueUpReviewPlugins{} + trueUpReviewPlugins := model.TrueUpReviewPlugins{ + ActivePluginNames: []string{}, + InactivePluginNames: []string{}, + } + if pluginResponse, err := c.App.GetPlugins(); err == nil { for _, plugin := range pluginResponse.Active { trueUpReviewPlugins.ActivePluginNames = append(trueUpReviewPlugins.ActivePluginNames, plugin.Name) @@ -369,12 +374,42 @@ func requestTrueUpReview(c *Context, w http.ResponseWriter, r *http.Request) { trueUpReviewPlugins.TotalInactivePlugins += 1 } } - reviewProfile.Plugins = trueUpReviewPlugins + // Authentication Data + mfaUsed := c.App.Config().ServiceSettings.EnforceMultifactorAuthentication + ldapUsed := c.App.Config().LdapSettings.Enable + samlUsed := c.App.Config().SamlSettings.Enable + openIdUsed := c.App.Config().OpenIdSettings.Enable + guessAccessAllowed := c.App.Config().GuestAccountsSettings.Enable + + authFeatures := map[string]*bool{ + model.TrueUpReviewAuthFeaturesMfa: mfaUsed, + model.TueUpReviewAuthFeaturesAdLdap: ldapUsed, + model.TrueUpReviewauthFeaturesSaml: samlUsed, + model.TrueUpReviewAuthFeatureOpenId: openIdUsed, + model.TrueUpReviewAuthFeatureGuestAccess: guessAccessAllowed, + } + + reviewProfile.AuthenticationFeatures = []string{} + for feature, used := range authFeatures { + if used != nil && *used { + reviewProfile.AuthenticationFeatures = append(reviewProfile.AuthenticationFeatures, feature) + } + } + + // Convert true up review profile struct to map + var telemetryProperties map[string]interface{} + marshalled, _ := json.Marshal(reviewProfile) + json.Unmarshal(marshalled, &telemetryProperties) + + // Send telemetry data. + telemetryService := c.App.Srv().GetTelemetryService() + telemetryService.SendTelemetry(model.TrueUpReviewTelemetryName, telemetryProperties) + json, err := json.Marshal(reviewProfile) if err != nil { - fmt.Printf("5: %+v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Write(json) diff --git a/model/license.go b/model/license.go index 94f0b81da4..ef7d2e4ffd 100644 --- a/model/license.go +++ b/model/license.go @@ -39,6 +39,15 @@ var ( sanctionedTrialDurationUpperBound = 29*(time.Hour*24) + (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59) // 696 hours (29 days) + 23 hours, 59 mins and 59 seconds ) +const ( + TrueUpReviewTelemetryName = "true-up-review-sent" + TrueUpReviewAuthFeaturesMfa = "multi_factor_authentication" + TueUpReviewAuthFeaturesAdLdap = "ad_ldap_sign_in" + TrueUpReviewauthFeaturesSaml = "saml_sign_in" + TrueUpReviewAuthFeatureOpenId = "openid_connect" + TrueUpReviewAuthFeatureGuestAccess = "guest_access" +) + type LicenseRecord struct { Id string `json:"id"` CreateAt int64 `json:"create_at"` diff --git a/model/true_up_review_profile.go b/model/true_up_review_profile.go index 67b51a3e88..4d8f8be7e9 100644 --- a/model/true_up_review_profile.go +++ b/model/true_up_review_profile.go @@ -21,7 +21,6 @@ type TrueUpReviewProfile struct { } type TrueUpReviewPlugins struct { - TotalPlugins int `json:"total_plugins"` TotalActivePlugins int `json:"total_active_plugins"` TotalInactivePlugins int `json:"total_inactive_plugins"` ActivePluginNames []string `json:"active_plugin_names"`