forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.go
256 lines (231 loc) · 8.3 KB
/
migrations.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package config
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/hashicorp/go-version"
"github.com/ory/hydra/x"
"github.com/pydio/cells/common"
)
type (
configMigration func(config *Config) (bool, error)
)
var (
configsMigrations []configMigration
configKeysRenames = map[string]string{
"services/pydio.api.websocket": "services/" + common.SERVICE_GATEWAY_NAMESPACE_ + common.SERVICE_WEBSOCKET,
"services/pydio.grpc.gateway.data": "services/" + common.SERVICE_GATEWAY_DATA,
"services/pydio.grpc.gateway.proxy": "services/" + common.SERVICE_GATEWAY_PROXY,
"services/pydio.rest.gateway.dav": "services/" + common.SERVICE_GATEWAY_DAV,
"services/pydio.rest.gateway.wopi": "services/" + common.SERVICE_GATEWAY_WOPI,
"ports/micro.api": "ports/" + common.SERVICE_MICRO_API,
"services/micro.api": "services/" + common.SERVICE_MICRO_API,
"services/pydio.api.front-plugins": "services/" + common.SERVICE_WEB_NAMESPACE_ + common.SERVICE_FRONT_STATICS,
}
)
func init() {
configsMigrations = append(configsMigrations, renameServices1, setDefaultConfig, forceDefaultConfig, dsnRemoveAllowNativePassword, updateLeCaURL)
}
// UpgradeConfigsIfRequired applies all registered configMigration functions
// Returns true if there was a change and save is required, error if something nasty happened
func UpgradeConfigsIfRequired(config *Config) (bool, error) {
var save bool
for _, m := range configsMigrations {
s, e := m(config)
if e != nil {
return false, e
}
if s {
save = true
}
}
return save, nil
}
func renameServices1(config *Config) (bool, error) {
var save bool
for oldPath, newPath := range configKeysRenames {
oldPaths := strings.Split(oldPath, "/")
val := config.Get(oldPaths...)
var data interface{}
if e := val.Scan(&data); e == nil && data != nil {
fmt.Printf("[Configs] Upgrading: renaming key %s to %s\n", oldPath, newPath)
config.Set(val, strings.Split(newPath, "/")...)
config.Del(oldPaths...)
save = true
}
}
return save, nil
}
func setDefaultConfig(config *Config) (bool, error) {
var save bool
oauthSrv := common.SERVICE_WEB_NAMESPACE_ + common.SERVICE_OAUTH
secret, err := x.GenerateSecret(32)
if err != nil {
return false, err
}
var syncRedirects = []string{
"http://localhost:3000/servers/callback", // SYNC UX DEBUG PORT
"http://localhost:[3636-3666]/servers/callback",
}
oAuthSyncConfig := map[string]interface{}{
"client_id": "cells-sync",
"client_name": "CellsSync Application",
"grant_types": []string{"authorization_code", "refresh_token"},
"redirect_uris": syncRedirects,
"response_types": []string{"code", "token", "id_token"},
"scope": "openid email profile pydio offline",
}
external := config.Get("defaults", "url").String("")
oAuthCecConfig := map[string]interface{}{
"client_id": "cells-client",
"client_name": "Cells Client CLI Tool",
"grant_types": []string{"authorization_code", "refresh_token"},
"redirect_uris": []string{
"http://localhost:3000/servers/callback",
external + "/oauth2/oob",
},
"response_types": []string{"code", "token", "id_token"},
"scope": "openid email profile pydio offline",
}
oAuthMobileConfig := map[string]interface{}{
"client_id": "cells-mobile",
"client_name": "Mobile Applications",
"grant_types": []string{"authorization_code", "refresh_token"},
"redirect_uris": []string{
"cellsauth://callback",
},
"response_types": []string{"code", "token", "id_token"},
"scope": "openid email profile pydio offline",
}
configKeys := map[string]interface{}{
"frontend/plugin/editor.libreoffice/LIBREOFFICE_HOST": "localhost",
"frontend/plugin/editor.libreoffice/LIBREOFFICE_PORT": "9980",
"frontend/plugin/editor.libreoffice/LIBREOFFICE_SSL": true,
"services/" + oauthSrv + "/cors/public": map[string]interface{}{
"allowedOrigins": "*",
},
"services/" + oauthSrv + "/secret": string(secret),
"services/" + oauthSrv + "/staticClients": []map[string]interface{}{
oAuthSyncConfig,
oAuthCecConfig,
oAuthMobileConfig,
},
}
for path, def := range configKeys {
paths := strings.Split(path, "/")
val := config.Get(paths...)
var data interface{}
if e := val.Scan(&data); e == nil && data == nil {
fmt.Printf("[Configs] Upgrading: setting default config %s to %v\n", path, def)
config.Set(def, paths...)
save = true
}
}
return save, nil
}
// updateLeCaURL changes the URL of acme API endpoint for Let's Encrypt certificate generation to v2 if it is used.
func updateLeCaURL(config *Config) (bool, error) {
caURLKey := "cert/proxy/caUrl"
caURLOldValue := "https://acme-v01.api.letsencrypt.org/directory"
caURLNewValue := "https://acme-v02.api.letsencrypt.org/directory"
paths := strings.Split(caURLKey, "/")
val := config.Get(paths...)
var data interface{}
save := false
if e := val.Scan(&data); e == nil && data != nil {
ov := data.(string)
if ov == caURLOldValue {
fmt.Printf("[Configs] Upgrading: rather use acme v2 API to generate Let's Encrypt certificates\n")
config.Set(caURLNewValue, paths...)
save = true
}
}
return save, nil
}
func forceDefaultConfig(config *Config) (bool, error) {
var save bool
authSrv := common.SERVICE_GRPC_NAMESPACE_ + common.SERVICE_AUTH
oauthSrv := common.SERVICE_WEB_NAMESPACE_ + common.SERVICE_OAUTH
srvUrl := config.Get("defaults", "url").String("")
if srvUrl == "" {
return false, nil
}
// Easy finding usage of srvUrl
configKeys := map[string]interface{}{
"services/" + authSrv + "/dex/issuer": srvUrl + "/auth/dex",
"services/" + authSrv + "/dex/web/http": srvUrl + "/auth/dex",
"services/" + oauthSrv + "/issuer": srvUrl + "/oidc/",
}
for path, def := range configKeys {
paths := strings.Split(path, "/")
val := config.Get(paths...)
var data interface{}
if val.Scan(&data); data != def {
fmt.Printf("[Configs] Upgrading: setting default config %s to %v\n", path, def)
config.Set(def, paths...)
save = true
}
}
// Special case for srvUrl/oauth2/oob url
statics := config.Get("services", oauthSrv, "staticClients")
var data []map[string]interface{}
if err := statics.Scan(&data); err == nil {
var saveStatics bool
for _, static := range data {
if redirs, ok := static["redirect_uris"].([]interface{}); ok {
var newRedirs []string
for _, redir := range redirs {
if strings.HasSuffix(redir.(string), "/oauth2/oob") && redir.(string) != srvUrl+"/oauth2/oob" {
newRedirs = append(newRedirs, srvUrl+"/oauth2/oob")
saveStatics = true
} else {
newRedirs = append(newRedirs, redir.(string))
}
}
static["redirect_uris"] = newRedirs
}
}
if saveStatics {
fmt.Println("[Configs] Upgrading: updating out-of-band redirect URI")
config.Set(data, "services", oauthSrv, "staticClients")
save = true
}
}
return save, nil
}
// dsnRemoveAllowNativePassword removes this part from default DSN
func dsnRemoveAllowNativePassword(config *Config) (bool, error) {
testFile := filepath.Join(ApplicationWorkingDir(ApplicationDirServices), common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_CONFIG, "version")
if data, e := ioutil.ReadFile(testFile); e == nil && len(data) > 0 {
ref, _ := version.NewVersion("1.4.1")
if v, e2 := version.NewVersion(strings.TrimSpace(string(data))); e2 == nil && v.LessThan(ref) {
dbId := config.Get("defaults", "database").String("")
if dbId != "" {
if dsn := config.Get("databases", dbId, "dsn").String(""); dsn != "" && strings.Contains(dsn, "allowNativePasswords=false\u0026") {
dsn = strings.Replace(dsn, "allowNativePasswords=false\u0026", "", 1)
fmt.Println("[Configs] Upgrading DSN to support new MySQL authentication plugin")
config.Set(dsn, "databases", dbId, "dsn")
return true, nil
}
}
}
}
return false, nil
}
// Do not append to the standard migration, it is called directly inside the
// Vault/once.Do() routine, otherwise it locks config
func migrateVault(vault *Config, defaultConfig *Config) bool {
var save bool
for _, path := range registeredVaultKeys {
confValue := defaultConfig.Get(path...).String("")
if confValue != "" && vault.Get(confValue).String("") == "" {
u := NewKeyForSecret()
fmt.Printf("[Configs] Upgrading %s to vault key %s\n", strings.Join(path, "/"), u)
vaultSource.Set(u, confValue, true)
defaultConfig.Set(u, path...)
save = true
}
}
return save
}