mirror of
https://github.com/mattermost/mattermost.git
synced 2026-09-19 10:12:47 +08:00
* Generalize the file storage Test Connection endpoint Replaces the S3-only /api/v4/file/s3_test handler with a backend-agnostic POST /api/v4/file/test that validates mandatory fields per driver and runs a write/read/delete probe against the configured backend. The legacy /file/s3_test route stays as a thin wrapper so existing clients keep working. The driver switch validates S3 and Azure mandatory fields explicitly, treats Local as a no-op (no required credentials), and rejects unknown or empty driver names with a 400 and a specific error code so admins get a useful message instead of a generic backend failure. Reuses config.Desanitize (renamed from the package-private desanitize) so the FakeSetting placeholder swap for secrets is shared with the PUT /api/v4/config save path. Adding a new driver-secret in the future only requires touching config.Desanitize once. Desanitize is also made nil-safe on every pointer dereference so callers can hand it a partial config without first running SetDefaults(). Mattermost-redux and the webapp client gain a corresponding TestFileStoreConnection method that the admin console action layer calls instead of the deprecated S3-specific method. ------ AI assisted commit * Wire Azure Blob Storage into the file storage admin console Adds the Azure Blob Storage option to the File Storage panel in the System Console. Selecting it enables Azure-specific fields for the storage account name, container, optional path prefix, shared key, optional endpoint override, secure-connections toggle, and request timeout. The fields are hidden and disabled when the driver is set to Local or S3, matching the existing pattern. Help text and placeholders are added in the webapp i18n catalog so admins see the same field labels documented in the admin guide. The same set of fields is repeated for the Files Export panel when DedicatedExportStore is enabled, keeping the export backend configurable independently of the primary file store. ------ AI assisted commit * Document /api/v4/file/test in the OpenAPI spec Adds the new backend-agnostic file storage Test Connection endpoint to the public OpenAPI surface. The request body is optional: callers that omit it test the running server configuration, callers that include a full AdminConfig test the supplied configuration without persisting anything. The deprecated /api/v4/file/s3_test endpoint is left unchanged in the spec for the existing S3-only flow. ------ AI assisted commit * Add UI-only Cypress coverage for the Azure file storage panel Adds a Cypress spec that drives the System Console File Storage panel, switches the driver to Azure Blob Storage, fills in the Azure fields, and asserts the expected fields appear (and S3 fields are hidden). The spec is UI-only and does not depend on an Azure backend or Azurite, so it can run in CI without external infrastructure. Updates the existing environment_spec.js so it tolerates the new Azure option in the driver dropdown. ------ AI assisted commit * Nil-guard file storage mandatory-field checks CheckMandatoryS3Fields and CheckMandatoryAzureFields built a FileBackendSettings via NewFileBackendSettingsFromConfig before validating, but that constructor dereferences pointers unconditionally and would panic if a caller skipped the api handler's reflective nil check. Validate the required pointers directly against FileSettings instead, dropping the throwaway constructor call so the methods are safe to call from any path. ------ AI assisted commit * Check permission before validating file settings The /file/test handler ran checkHasNilFields before SessionHasPermissionTo, so an unauthorized caller posting a partial config got a 400, leaking config shape, rather than a 403. Swap the two blocks so the permission decision happens first. ------ AI assisted commit * Preserve FakeSetting when desanitize has no actual The Azure access key, export Azure access key, and S3 secret access key branches in Desanitize reassigned target to actual without checking actual for nil. When the running config had no value, the FakeSetting placeholder in target was replaced with nil, dropping the field from the round-trip. Guard the assignment so the placeholder stays in place when actual is unset. ------ AI assisted commit --------- Co-authored-by: Mattermost Build <build@mattermost.com>
271 lines
11 KiB
Go
271 lines
11 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
|
)
|
|
|
|
// marshalConfig converts the given configuration into JSON bytes for persistence.
|
|
func marshalConfig(cfg *model.Config) ([]byte, error) {
|
|
return json.MarshalIndent(cfg, "", " ")
|
|
}
|
|
|
|
// Desanitize replaces fake settings with their actual values. Safe to call on
|
|
// partial configs: every pointer dereference is nil-guarded so callers do not
|
|
// need to run SetDefaults() first.
|
|
func Desanitize(actual, target *model.Config) {
|
|
if target.LdapSettings.BindPassword != nil && *target.LdapSettings.BindPassword == model.FakeSetting && actual.LdapSettings.BindPassword != nil {
|
|
*target.LdapSettings.BindPassword = *actual.LdapSettings.BindPassword
|
|
}
|
|
|
|
if target.FileSettings.PublicLinkSalt != nil && *target.FileSettings.PublicLinkSalt == model.FakeSetting && actual.FileSettings.PublicLinkSalt != nil {
|
|
*target.FileSettings.PublicLinkSalt = *actual.FileSettings.PublicLinkSalt
|
|
}
|
|
if target.FileSettings.AmazonS3SecretAccessKey != nil && *target.FileSettings.AmazonS3SecretAccessKey == model.FakeSetting && actual.FileSettings.AmazonS3SecretAccessKey != nil {
|
|
target.FileSettings.AmazonS3SecretAccessKey = actual.FileSettings.AmazonS3SecretAccessKey
|
|
}
|
|
if target.FileSettings.ExportAmazonS3SecretAccessKey != nil && *target.FileSettings.ExportAmazonS3SecretAccessKey == model.FakeSetting && actual.FileSettings.ExportAmazonS3SecretAccessKey != nil {
|
|
target.FileSettings.ExportAmazonS3SecretAccessKey = actual.FileSettings.ExportAmazonS3SecretAccessKey
|
|
}
|
|
if target.FileSettings.AzureAccessKey != nil && *target.FileSettings.AzureAccessKey == model.FakeSetting && actual.FileSettings.AzureAccessKey != nil {
|
|
target.FileSettings.AzureAccessKey = actual.FileSettings.AzureAccessKey
|
|
}
|
|
if target.FileSettings.ExportAzureAccessKey != nil && *target.FileSettings.ExportAzureAccessKey == model.FakeSetting && actual.FileSettings.ExportAzureAccessKey != nil {
|
|
target.FileSettings.ExportAzureAccessKey = actual.FileSettings.ExportAzureAccessKey
|
|
}
|
|
|
|
if target.EmailSettings.SMTPPassword != nil && *target.EmailSettings.SMTPPassword == model.FakeSetting {
|
|
target.EmailSettings.SMTPPassword = actual.EmailSettings.SMTPPassword
|
|
}
|
|
|
|
if target.GitLabSettings.Secret != nil && *target.GitLabSettings.Secret == model.FakeSetting {
|
|
target.GitLabSettings.Secret = actual.GitLabSettings.Secret
|
|
}
|
|
|
|
if target.GoogleSettings.Secret != nil && *target.GoogleSettings.Secret == model.FakeSetting {
|
|
target.GoogleSettings.Secret = actual.GoogleSettings.Secret
|
|
}
|
|
|
|
if target.Office365Settings.Secret != nil && *target.Office365Settings.Secret == model.FakeSetting {
|
|
target.Office365Settings.Secret = actual.Office365Settings.Secret
|
|
}
|
|
|
|
if target.OpenIdSettings.Secret != nil && *target.OpenIdSettings.Secret == model.FakeSetting {
|
|
target.OpenIdSettings.Secret = actual.OpenIdSettings.Secret
|
|
}
|
|
|
|
if target.SqlSettings.DataSource != nil && *target.SqlSettings.DataSource == model.FakeSetting && actual.SqlSettings.DataSource != nil {
|
|
*target.SqlSettings.DataSource = *actual.SqlSettings.DataSource
|
|
}
|
|
if target.SqlSettings.AtRestEncryptKey != nil && *target.SqlSettings.AtRestEncryptKey == model.FakeSetting {
|
|
target.SqlSettings.AtRestEncryptKey = actual.SqlSettings.AtRestEncryptKey
|
|
}
|
|
|
|
if target.ElasticsearchSettings.Password != nil && *target.ElasticsearchSettings.Password == model.FakeSetting && actual.ElasticsearchSettings.Password != nil {
|
|
*target.ElasticsearchSettings.Password = *actual.ElasticsearchSettings.Password
|
|
}
|
|
|
|
if len(target.SqlSettings.DataSourceReplicas) == len(actual.SqlSettings.DataSourceReplicas) {
|
|
for i, value := range target.SqlSettings.DataSourceReplicas {
|
|
if value == model.FakeSetting {
|
|
target.SqlSettings.DataSourceReplicas[i] = actual.SqlSettings.DataSourceReplicas[i]
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(target.SqlSettings.DataSourceSearchReplicas) == len(actual.SqlSettings.DataSourceSearchReplicas) {
|
|
for i, value := range target.SqlSettings.DataSourceSearchReplicas {
|
|
if value == model.FakeSetting {
|
|
target.SqlSettings.DataSourceSearchReplicas[i] = actual.SqlSettings.DataSourceSearchReplicas[i]
|
|
}
|
|
}
|
|
}
|
|
|
|
if target.MessageExportSettings.GlobalRelaySettings != nil &&
|
|
target.MessageExportSettings.GlobalRelaySettings.SMTPPassword != nil &&
|
|
*target.MessageExportSettings.GlobalRelaySettings.SMTPPassword == model.FakeSetting &&
|
|
actual.MessageExportSettings.GlobalRelaySettings != nil &&
|
|
actual.MessageExportSettings.GlobalRelaySettings.SMTPPassword != nil {
|
|
*target.MessageExportSettings.GlobalRelaySettings.SMTPPassword = *actual.MessageExportSettings.GlobalRelaySettings.SMTPPassword
|
|
}
|
|
|
|
if target.ServiceSettings.SplitKey != nil && *target.ServiceSettings.SplitKey == model.FakeSetting && actual.ServiceSettings.SplitKey != nil {
|
|
*target.ServiceSettings.SplitKey = *actual.ServiceSettings.SplitKey
|
|
}
|
|
|
|
if target.ServiceSettings.GoogleDeveloperKey != nil && *target.ServiceSettings.GoogleDeveloperKey == model.FakeSetting {
|
|
target.ServiceSettings.GoogleDeveloperKey = actual.ServiceSettings.GoogleDeveloperKey
|
|
}
|
|
|
|
if target.ServiceSettings.GiphySdkKey != nil && *target.ServiceSettings.GiphySdkKey == model.FakeSetting {
|
|
target.ServiceSettings.GiphySdkKey = actual.ServiceSettings.GiphySdkKey
|
|
}
|
|
|
|
if target.CacheSettings.RedisPassword != nil && *target.CacheSettings.RedisPassword == model.FakeSetting {
|
|
target.CacheSettings.RedisPassword = actual.CacheSettings.RedisPassword
|
|
}
|
|
|
|
if target.AutoTranslationSettings.LibreTranslate != nil &&
|
|
target.AutoTranslationSettings.LibreTranslate.APIKey != nil &&
|
|
*target.AutoTranslationSettings.LibreTranslate.APIKey == model.FakeSetting {
|
|
target.AutoTranslationSettings.LibreTranslate.APIKey = actual.AutoTranslationSettings.LibreTranslate.APIKey
|
|
}
|
|
|
|
for id, settings := range target.PluginSettings.Plugins {
|
|
for k, v := range settings {
|
|
if v == model.FakeSetting {
|
|
settings[k] = actual.PluginSettings.Plugins[id][k]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// fixConfig patches invalid or missing data in the configuration.
|
|
func fixConfig(cfg *model.Config) {
|
|
// Ensure SiteURL has no trailing slash.
|
|
if strings.HasSuffix(*cfg.ServiceSettings.SiteURL, "/") {
|
|
*cfg.ServiceSettings.SiteURL = strings.TrimRight(*cfg.ServiceSettings.SiteURL, "/")
|
|
}
|
|
|
|
// Ensure the directory for a local file store has a trailing slash.
|
|
if *cfg.FileSettings.DriverName == model.ImageDriverLocal {
|
|
if *cfg.FileSettings.Directory != "" && !strings.HasSuffix(*cfg.FileSettings.Directory, "/") {
|
|
*cfg.FileSettings.Directory += "/"
|
|
}
|
|
}
|
|
|
|
fixInvalidLocales(cfg)
|
|
}
|
|
|
|
// fixInvalidLocales checks and corrects the given config for invalid locale-related settings.
|
|
func fixInvalidLocales(cfg *model.Config) bool {
|
|
var changed bool
|
|
|
|
locales := i18n.GetSupportedLocales()
|
|
if _, ok := locales[*cfg.LocalizationSettings.DefaultServerLocale]; !ok {
|
|
mlog.Warn("DefaultServerLocale must be one of the supported locales. Setting DefaultServerLocale to en as default value.", mlog.String("locale", *cfg.LocalizationSettings.DefaultServerLocale))
|
|
*cfg.LocalizationSettings.DefaultServerLocale = model.DefaultLocale
|
|
changed = true
|
|
}
|
|
|
|
if _, ok := locales[*cfg.LocalizationSettings.DefaultClientLocale]; !ok {
|
|
mlog.Warn("DefaultClientLocale must be one of the supported locales. Setting DefaultClientLocale to en as default value.", mlog.String("locale", *cfg.LocalizationSettings.DefaultClientLocale))
|
|
*cfg.LocalizationSettings.DefaultClientLocale = model.DefaultLocale
|
|
changed = true
|
|
}
|
|
|
|
if *cfg.LocalizationSettings.AvailableLocales != "" {
|
|
isDefaultClientLocaleInAvailableLocales := false
|
|
for word := range strings.SplitSeq(*cfg.LocalizationSettings.AvailableLocales, ",") {
|
|
if _, ok := locales[word]; !ok {
|
|
*cfg.LocalizationSettings.AvailableLocales = ""
|
|
isDefaultClientLocaleInAvailableLocales = true
|
|
mlog.Warn("AvailableLocales must include DefaultClientLocale. Setting AvailableLocales to all locales as default value.")
|
|
changed = true
|
|
break
|
|
}
|
|
|
|
if word == *cfg.LocalizationSettings.DefaultClientLocale {
|
|
isDefaultClientLocaleInAvailableLocales = true
|
|
}
|
|
}
|
|
|
|
availableLocales := *cfg.LocalizationSettings.AvailableLocales
|
|
|
|
if !isDefaultClientLocaleInAvailableLocales {
|
|
availableLocales += "," + *cfg.LocalizationSettings.DefaultClientLocale
|
|
mlog.Warn("Adding DefaultClientLocale to AvailableLocales.")
|
|
changed = true
|
|
}
|
|
|
|
*cfg.LocalizationSettings.AvailableLocales = strings.Join(utils.RemoveDuplicatesFromStringArray(strings.Split(availableLocales, ",")), ",")
|
|
}
|
|
|
|
return changed
|
|
}
|
|
|
|
// Merge merges two configs together. The receiver's values are overwritten with the patch's
|
|
// values except when the patch's values are nil.
|
|
func Merge(cfg *model.Config, patch *model.Config, mergeConfig *utils.MergeConfig) (*model.Config, error) {
|
|
return utils.Merge(cfg, patch, mergeConfig)
|
|
}
|
|
|
|
func IsDatabaseDSN(dsn string) bool {
|
|
return strings.HasPrefix(dsn, "postgres://") ||
|
|
strings.HasPrefix(dsn, "postgresql://")
|
|
}
|
|
|
|
func isJSONMap(data []byte) bool {
|
|
var m map[string]any
|
|
err := json.Unmarshal(data, &m)
|
|
return err == nil
|
|
}
|
|
|
|
func GetValueByPath(path []string, obj any) (any, bool) {
|
|
r := reflect.ValueOf(obj)
|
|
var val reflect.Value
|
|
if r.Kind() == reflect.Map {
|
|
val = r.MapIndex(reflect.ValueOf(path[0]))
|
|
if val.IsValid() {
|
|
val = val.Elem()
|
|
}
|
|
} else {
|
|
val = r.FieldByName(path[0])
|
|
}
|
|
|
|
if !val.IsValid() {
|
|
return nil, false
|
|
}
|
|
|
|
switch {
|
|
case len(path) == 1:
|
|
return val.Interface(), true
|
|
case val.Kind() == reflect.Struct:
|
|
return GetValueByPath(path[1:], val.Interface())
|
|
case val.Kind() == reflect.Map:
|
|
remainingPath := strings.Join(path[1:], ".")
|
|
mapIter := val.MapRange()
|
|
for mapIter.Next() {
|
|
key := mapIter.Key().String()
|
|
if strings.HasPrefix(remainingPath, key) {
|
|
i := strings.Count(key, ".") + 2 // number of dots + a dot on each side
|
|
mapVal := mapIter.Value()
|
|
// if no sub field path specified, return the object
|
|
if len(path[i:]) == 0 {
|
|
return mapVal.Interface(), true
|
|
}
|
|
data := mapVal.Interface()
|
|
if mapVal.Kind() == reflect.Pointer {
|
|
data = mapVal.Elem().Interface() // if value is a pointer, dereference it
|
|
}
|
|
// pass subpath
|
|
return GetValueByPath(path[i:], data)
|
|
}
|
|
}
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func equal(oldCfg, newCfg *model.Config) (bool, error) {
|
|
oldCfgBytes, err := json.Marshal(oldCfg)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to marshal old config: %w", err)
|
|
}
|
|
newCfgBytes, err := json.Marshal(newCfg)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to marshal new config: %w", err)
|
|
}
|
|
return !bytes.Equal(oldCfgBytes, newCfgBytes), nil
|
|
}
|