github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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 and certificates 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  
    15  	destination, err := NewStore(to, false)
    16  	if err != nil {
    17  		return errors.Wrapf(err, "failed to access destination config %s", to)
    18  	}
    19  
    20  	sourceConfig := source.Get()
    21  	if _, err = destination.Set(sourceConfig); err != nil {
    22  		return errors.Wrapf(err, "failed to set config")
    23  	}
    24  
    25  	files := []string{*sourceConfig.SamlSettings.IdpCertificateFile, *sourceConfig.SamlSettings.PublicCertificateFile,
    26  		*sourceConfig.SamlSettings.PrivateKeyFile}
    27  
    28  	files = append(files, sourceConfig.PluginSettings.SignaturePublicKeyFiles...)
    29  
    30  	for _, file := range files {
    31  		err = migrateFile(file, source, destination)
    32  
    33  		if err != nil {
    34  			return err
    35  		}
    36  	}
    37  	return nil
    38  }
    39  
    40  func migrateFile(name string, source Store, destination Store) error {
    41  	fileExists, err := source.HasFile(name)
    42  	if err != nil {
    43  		return errors.Wrapf(err, "failed to check existence of %s", name)
    44  	}
    45  
    46  	if fileExists {
    47  		file, err := source.GetFile(name)
    48  		if err != nil {
    49  			return errors.Wrapf(err, "failed to migrate %s", name)
    50  		}
    51  		err = destination.SetFile(name, file)
    52  		if err != nil {
    53  			return errors.Wrapf(err, "failed to migrate %s", name)
    54  		}
    55  	}
    56  
    57  	return nil
    58  }