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