From 650ad3b3aeafeb5fbcd8b4e5e9264a8c21bc9268 Mon Sep 17 00:00:00 2001 From: Nick Misasi Date: Mon, 4 Apr 2022 09:48:14 -0400 Subject: [PATCH] [MM-42822] Check for and return HTTP 451 for all trial requests (#19873) * Check for and return HTTP 451 for all trial requests * Use constant in another spot too * add a test * Update test name --- api4/license_test.go | 30 ++++++++++++++++++++++++++++++ app/license.go | 5 +++++ i18n/en.json | 4 ++++ 3 files changed, 39 insertions(+) diff --git a/api4/license_test.go b/api4/license_test.go index 4e61cfdf6e..919d04156c 100644 --- a/api4/license_test.go +++ b/api4/license_test.go @@ -255,6 +255,36 @@ func TestRequestTrialLicense(t *testing.T) { CheckBadRequestStatus(t, resp) }) + t.Run("returns status 451 when it receives status 451", func(t *testing.T) { + nUsers := 1 + license := model.NewTestLicense() + license.Features.Users = model.NewInt(nUsers) + licenseJSON, jsonErr := json.Marshal(license) + require.NoError(t, jsonErr) + testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + res.WriteHeader(http.StatusUnavailableForLegalReasons) + })) + defer testServer.Close() + + mockLicenseValidator := mocks2.LicenseValidatorIface{} + defer testutils.ResetLicenseValidator() + + mockLicenseValidator.On("ValidateLicense", mock.Anything).Return(true, string(licenseJSON)) + utils.LicenseValidator = &mockLicenseValidator + licenseManagerMock := &mocks.LicenseInterface{} + licenseManagerMock.On("CanStartTrial").Return(true, nil).Once() + th.App.Srv().LicenseManager = licenseManagerMock + + defer func(requestTrialURL string) { + app.RequestTrialURL = requestTrialURL + }(app.RequestTrialURL) + app.RequestTrialURL = testServer.URL + + resp, err := th.SystemAdminClient.RequestTrialLicense(nUsers) + require.Error(t, err) + require.Equal(t, resp.StatusCode, 451) + }) + th.App.Srv().LicenseManager = nil t.Run("trial license should fail if LicenseManager is nil", func(t *testing.T) { resp, err := th.SystemAdminClient.RequestTrialLicense(1) diff --git a/app/license.go b/app/license.go index 538fe4fec1..2d3d948190 100644 --- a/app/license.go +++ b/app/license.go @@ -340,6 +340,11 @@ func (s *Server) RequestTrialLicense(trialRequest *model.TrialLicenseRequest) *m } defer resp.Body.Close() + // CloudFlare sitting in front of the Customer Portal will block this request with a 451 response code in the event that the request originates from a country sanctioned by the U.S. Government. + if resp.StatusCode == http.StatusUnavailableForLegalReasons { + return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.embargoed", nil, "Request for trial license came from an embargoed country", http.StatusUnavailableForLegalReasons) + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.app_error", nil, fmt.Sprintf("Unexpected HTTP status code %q returned by server", resp.Status), http.StatusInternalServerError) diff --git a/i18n/en.json b/i18n/en.json index 03c5a400ba..6abd1ce53f 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -2005,6 +2005,10 @@ "id": "api.license.request_trial_license.app_error", "translation": "Unable to get a trial license, please try again or contact with support@mattermost.com." }, + { + "id": "api.license.request_trial_license.embargoed", + "translation": "We were unable to process the request due to limitations for embargoed countries. [Learn more in our documentation](https://mattermost.com/pl/limitations-for-embargoed-countries), or reach out to legal@mattermost.com for questions around export limitations." + }, { "id": "api.license.request_trial_license.fail_get_user_count.app_error", "translation": "Unable to get a trial license, please try again or contact with support@mattermost.com. Cannot obtain the number of registered users."