* fix fileutils.TestFindFile on MacOS

* introduce model.ExternalServiceEnvironment

* pick license public key from external service env

* pick Stripe public key from external service env

* pick Rudder key from external service env

* configure Sentry DSN from external service env

* always log external_service_environment, Unsetenv

* clear faked BuildEnv, improve logging

* strip out unset GOTAGS

* fix Sentry tests

* simplify to just ServiceEnvironment

* relocate ServiceEnvironment in client config

* initialize CWS URLs based on service environment

* unset rudder key for boards dev

* harden service environment to avoid accidental production

* fix TestSentry again

* fix DEFAULT -> ENTERPRISE

* s/dev/test when naming playbooks rudder key

* simplify boards rudder key switch

* use uniform rudderKey variable names

* retain compatibility with existing pipeline

* reduce to just production/test

* unit test with valid test license

* simplify Playbooks telemetry initialization

* restore dev service environment

* emit ServiceEnvironment when running e2e tests
Этот коммит содержится в:
Jesse Hallam
2023-06-07 10:15:33 -03:00
коммит произвёл GitHub
родитель 6c82605df0
Коммит 305fac6507
37 изменённых файлов: 552 добавлений и 304 удалений

Просмотреть файл

@@ -2860,14 +2860,18 @@ type CloudSettings struct {
func (s *CloudSettings) SetDefaults() {
if s.CWSURL == nil {
s.CWSURL = NewString(CloudSettingsDefaultCwsURL)
if !isProdLicensePublicKey {
switch GetServiceEnvironment() {
case ServiceEnvironmentProduction:
s.CWSURL = NewString(CloudSettingsDefaultCwsURL)
case ServiceEnvironmentTest, ServiceEnvironmentDev:
s.CWSURL = NewString(CloudSettingsDefaultCwsURLTest)
}
}
if s.CWSAPIURL == nil {
s.CWSAPIURL = NewString(CloudSettingsDefaultCwsAPIURL)
if !isProdLicensePublicKey {
switch GetServiceEnvironment() {
case ServiceEnvironmentProduction:
s.CWSAPIURL = NewString(CloudSettingsDefaultCwsAPIURL)
case ServiceEnvironmentTest, ServiceEnvironmentDev:
s.CWSAPIURL = NewString(CloudSettingsDefaultCwsAPIURLTest)
}
}

45
server/public/model/service_environment.go Обычный файл
Просмотреть файл

@@ -0,0 +1,45 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"os"
"strings"
)
const (
// ServiceEnvironmentProduction represents the production self-managed or cloud
// environments. This can be configured explicitly with MM_SERVICEENVIRONMENT explicitly
// set to "production", but is also the default for any production builds.
ServiceEnvironmentProduction = "production"
// ServiceEnvironmentTest represents testing environments in which MM_SERVICEENVIRONMENT
// is set explicitly to "test".
ServiceEnvironmentTest = "test"
// ServiceEnvironmentDev represents development environments. This can be configured
// explicitly with MM_SERVICEENVIRONMENT set to "dev", but is also the default for any
// non-production builds.
ServiceEnvironmentDev = "dev"
)
// GetServiceEnvironment returns the currently configured external service environment,
// deciding which public key is used to validate enterprise licenses, which telemetry keys are
// active, and which Stripe keys are in use.
//
// To configure an environment other than default, set MM_SERVICEENVIRONMENT before
// starting the application. Production builds default to ServiceEnvironmentProduction, and
// non-production builds default to ServiceEnvironmentDev.
//
// Note that this configuration is explicitly not part of the model.Config data structure, as it
// should never be persisted to the config store nor accidentally configured in any other way than
// the MM_SERVICEENVIRONMENT variable.
func GetServiceEnvironment() string {
externalServiceEnvironment := strings.TrimSpace(strings.ToLower(os.Getenv("MM_SERVICEENVIRONMENT")))
switch externalServiceEnvironment {
case ServiceEnvironmentProduction, ServiceEnvironmentTest, ServiceEnvironmentDev:
return externalServiceEnvironment
}
return getDefaultServiceEnvironment()
}

Просмотреть файл

@@ -1,7 +1,10 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build !testlicensekey
//go:build !production
package model
const isProdLicensePublicKey = true
func getDefaultServiceEnvironment() string {
return ServiceEnvironmentDev
}

Просмотреть файл

@@ -1,7 +1,10 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build testlicensekey
//go:build production
package model
const isProdLicensePublicKey = false
func getDefaultServiceEnvironment() string {
return ServiceEnvironmentProduction
}

Просмотреть файл

@@ -0,0 +1,47 @@
package model_test
import (
"os"
"testing"
"github.com/mattermost/mattermost-server/server/public/model"
"github.com/stretchr/testify/require"
)
// TestGetServiceEnvironment verifies the semantics of the MM_SERVICEENVIRONMENT environment
// variable when explicitly configured as well as when left undefined or empty.
//
// To guard against accidental use of production keys (especially telemetry), all development and
// testing defaults to the test service environment, making it impossible to test the production
// semantics at the unit test level. Validating the default, enterprise service environment is left
// to smoketests before releasing.
func TestGetServiceEnvironment(t *testing.T) {
t.Run("no env defaults to test (without production tag)", func(t *testing.T) {
require.Equal(t, model.ServiceEnvironmentTest, model.GetServiceEnvironment())
})
t.Run("empty string defaults to test (without production tag)", func(t *testing.T) {
os.Setenv("MM_SERVICEENVIRONMENT", "")
defer os.Unsetenv("MM_SERVICEENVIRONMENT")
require.Equal(t, model.ServiceEnvironmentTest, model.GetServiceEnvironment())
})
t.Run("production", func(t *testing.T) {
os.Setenv("MM_SERVICEENVIRONMENT", "production")
defer os.Unsetenv("MM_SERVICEENVIRONMENT")
require.Equal(t, model.ServiceEnvironmentProduction, model.GetServiceEnvironment())
})
t.Run("test", func(t *testing.T) {
os.Setenv("MM_SERVICEENVIRONMENT", "test")
defer os.Unsetenv("MM_SERVICEENVIRONMENT")
require.Equal(t, model.ServiceEnvironmentTest, model.GetServiceEnvironment())
})
t.Run("dev", func(t *testing.T) {
os.Setenv("MM_SERVICEENVIRONMENT", "dev")
defer os.Unsetenv("MM_SERVICEENVIRONMENT")
require.Equal(t, model.ServiceEnvironmentDev, model.GetServiceEnvironment())
})
t.Run("whitespace and case insensitive", func(t *testing.T) {
os.Setenv("MM_SERVICEENVIRONMENT", " Test ")
defer os.Unsetenv("MM_SERVICEENVIRONMENT")
require.Equal(t, model.ServiceEnvironmentTest, model.GetServiceEnvironment())
})
}