github.com/mattermost/mattermost-server/server/v8@v8.0.0-20230610055354-a6d1d38b273d/config/migrate.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package config
     5  
     6  import (
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // Migrate migrates SAML keys, certificates, and other config files from one store to another given their data source names.
    11  func Migrate(from, to string) error {
    12  	source, err := NewStoreFromDSN(from, false, nil, false)
    13  	if err != nil {
    14  		return errors.Wrapf(err, "failed to access source config %s", from)
    15  	}
    16  	defer source.Close()
    17  
    18  	destination, err := NewStoreFromDSN(to, false, nil, true)
    19  	if err != nil {
    20  		return errors.Wrapf(err, "failed to access destination config %s", to)
    21  	}
    22  	defer destination.Close()
    23  
    24  	sourceConfig := source.Get()
    25  	if _, _, err = destination.Set(sourceConfig); err != nil {
    26  		return errors.Wrapf(err, "failed to set config")
    27  	}
    28  
    29  	files := []string{
    30  		*sourceConfig.SamlSettings.IdpCertificateFile,
    31  		*sourceConfig.SamlSettings.PublicCertificateFile,
    32  		*sourceConfig.SamlSettings.PrivateKeyFile,
    33  	}
    34  
    35  	// Only migrate advanced logging config if it is a filespec.
    36  	dsn := sourceConfig.LogSettings.GetAdvancedLoggingConfig()
    37  	cfgSource, err := NewLogConfigSrc(dsn, source)
    38  	if err == nil && cfgSource.GetType() == LogConfigSrcTypeFile {
    39  		files = append(files, string(dsn))
    40  	}
    41  
    42  	files = append(files, sourceConfig.PluginSettings.SignaturePublicKeyFiles...)
    43  
    44  	for _, file := range files {
    45  		if err := migrateFile(file, source, destination); err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  func migrateFile(name string, source *Store, destination *Store) error {
    54  	fileExists, err := source.HasFile(name)
    55  	if err != nil {
    56  		return errors.Wrapf(err, "failed to check existence of %s", name)
    57  	}
    58  
    59  	if fileExists {
    60  		file, err := source.GetFile(name)
    61  		if err != nil {
    62  			return errors.Wrapf(err, "failed to migrate %s", name)
    63  		}
    64  		err = destination.SetFile(name, file)
    65  		if err != nil {
    66  			return errors.Wrapf(err, "failed to migrate %s", name)
    67  		}
    68  	}
    69  
    70  	return nil
    71  }