From 9726eedbe2a03544a74dc426904423b50e308c10 Mon Sep 17 00:00:00 2001 From: TheInvincible <139259364+TheInvincibleRalph@users.noreply.github.com> Date: Wed, 16 Oct 2024 08:45:41 +0100 Subject: [PATCH] Update license.go (handled error) (#28410) Co-authored-by: Ben Schumacher --- server/.golangci.yml | 1 - server/channels/api4/license.go | 20 ++++++++++++++++---- server/i18n/en.json | 4 ++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/server/.golangci.yml b/server/.golangci.yml index 95db8828f4..6bcc8b0c93 100644 --- a/server/.golangci.yml +++ b/server/.golangci.yml @@ -76,7 +76,6 @@ issues: channels/api4/file_test.go|\ channels/api4/ip_filtering_test.go|\ channels/api4/job_test.go|\ - channels/api4/license.go|\ channels/api4/license_local.go|\ channels/api4/post_test.go|\ channels/api4/preference_test.go|\ diff --git a/server/channels/api4/license.go b/server/channels/api4/license.go index 4d48ee3b3f..277994eeaa 100644 --- a/server/channels/api4/license.go +++ b/server/channels/api4/license.go @@ -45,7 +45,9 @@ func getClientLicense(c *Context, w http.ResponseWriter, r *http.Request) { clientLicense = c.App.Srv().GetSanitizedClientLicense() } - w.Write([]byte(model.MapToJSON(clientLicense))) + if _, err := w.Write([]byte(model.MapToJSON(clientLicense))); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { @@ -93,7 +95,10 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { defer file.Close() buf := bytes.NewBuffer(nil) - io.Copy(buf, file) + if _, err := io.Copy(buf, file); err != nil { + c.Err = model.NewAppError("addLicense", "api.license.add_license.copy.app_error", nil, "", http.StatusInternalServerError).Wrap(err) + return + } licenseBytes := buf.Bytes() license, appErr := utils.LicenseValidator.LicenseFromBytes(licenseBytes) @@ -137,7 +142,11 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { if c.App.Channels().License().IsCloud() { // If cloud, invalidate the caches when a new license is loaded - defer c.App.Srv().Cloud.HandleLicenseChange() + defer func() { + if err := c.App.Srv().Cloud.HandleLicenseChange(); err != nil { + c.Logger.Warn("Error while handling license change", mlog.Err(err)) + } + }() } auditRec.Success() @@ -258,5 +267,8 @@ func getPrevTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) { clientLicense = utils.GetSanitizedClientLicense(utils.GetClientLicense(license)) } - w.Write([]byte(model.MapToJSON(clientLicense))) + w.Header().Set("Content-Type", "application/json") + if _, err := w.Write([]byte(model.MapToJSON(clientLicense))); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } diff --git a/server/i18n/en.json b/server/i18n/en.json index 80daf70ccf..393a4ae39b 100644 --- a/server/i18n/en.json +++ b/server/i18n/en.json @@ -2224,6 +2224,10 @@ "id": "api.license.add_license.array.app_error", "translation": "Empty array under 'license' in request." }, + { + "id": "api.license.add_license.copy.app_error", + "translation": "Failed to copy license." + }, { "id": "api.license.add_license.expired.app_error", "translation": "License is either expired or has not yet started."