github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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 "github.com/pkg/errors"
     7  
     8  // Migrate migrates SAML keys, certificates, and other config files from one store to another given their data source names.
     9  func Migrate(from, to string) error {
    10  	source, err := NewStore(from, false)
    11  	if err != nil {
    12  		return errors.Wrapf(err, "failed to access source config %s", from)
    13  	}
    14  	defer source.Close()
    15  
    16  	destination, err := NewStore(to, false)
    17  	if err != nil {
    18  		return errors.Wrapf(err, "failed to access destination config %s", to)
    19  	}
    20  	defer destination.Close()
    21  
    22  	sourceConfig := source.Get()
    23  	if _, err = destination.Set(sourceConfig); err != nil {
    24  		return errors.Wrapf(err, "failed to set config")
    25  	}
    26  
    27  	files := []string{
    28  		*sourceConfig.SamlSettings.IdpCertificateFile,
    29  		*sourceConfig.SamlSettings.PublicCertificateFile,
    30  		*sourceConfig.SamlSettings.PrivateKeyFile,
    31  	}
    32  
    33  	// Only migrate advanced logging config if it is not embedded JSON.
    34  	if !IsJsonMap(*sourceConfig.LogSettings.AdvancedLoggingConfig) {
    35  		files = append(files, *sourceConfig.LogSettings.AdvancedLoggingConfig)
    36  	}
    37  
    38  	files = append(files, sourceConfig.PluginSettings.SignaturePublicKeyFiles...)
    39  
    40  	for _, file := range files {
    41  		if err := migrateFile(file, source, destination); err != nil {
    42  			return err
    43  		}
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func migrateFile(name string, source Store, destination Store) error {
    50  	fileExists, err := source.HasFile(name)
    51  	if err != nil {
    52  		return errors.Wrapf(err, "failed to check existence of %s", name)
    53  	}
    54  
    55  	if fileExists {
    56  		file, err := source.GetFile(name)
    57  		if err != nil {
    58  			return errors.Wrapf(err, "failed to migrate %s", name)
    59  		}
    60  		err = destination.SetFile(name, file)
    61  		if err != nil {
    62  			return errors.Wrapf(err, "failed to migrate %s", name)
    63  		}
    64  	}
    65  
    66  	return nil
    67  }