github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/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, false, nil) 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, false, nil) 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 not embedded JSON. 36 if !isJSONMap(*sourceConfig.LogSettings.AdvancedLoggingConfig) { 37 files = append(files, *sourceConfig.LogSettings.AdvancedLoggingConfig) 38 } 39 40 files = append(files, sourceConfig.PluginSettings.SignaturePublicKeyFiles...) 41 42 for _, file := range files { 43 if err := migrateFile(file, source, destination); err != nil { 44 return err 45 } 46 } 47 48 return nil 49 } 50 51 func migrateFile(name string, source *Store, destination *Store) error { 52 fileExists, err := source.HasFile(name) 53 if err != nil { 54 return errors.Wrapf(err, "failed to check existence of %s", name) 55 } 56 57 if fileExists { 58 file, err := source.GetFile(name) 59 if err != nil { 60 return errors.Wrapf(err, "failed to migrate %s", name) 61 } 62 err = destination.SetFile(name, file) 63 if err != nil { 64 return errors.Wrapf(err, "failed to migrate %s", name) 65 } 66 } 67 68 return nil 69 }