-
Notifications
You must be signed in to change notification settings - Fork 42
/
migrate.go
374 lines (338 loc) · 9.23 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Copyright 2018-2019 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
cli "github.com/nats-io/cliprompts/v2"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
"github.com/spf13/cobra"
"github.com/nats-io/nsc/v2/cmd/store"
)
func createMigrateCmd() *cobra.Command {
var params MigrateCmdParams
var cmd = &cobra.Command{
Hidden: true,
Short: "Migrate an account to the current operator",
Example: "migrate --url <path or url to account jwt>",
Use: `migrate`,
Args: MaxArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, ¶ms); err != nil {
return err
}
return nil
},
}
cmd.Flags().StringVarP(¶ms.url, "url", "u", "", "path or url to import jwt from")
cmd.Flags().StringVarP(¶ms.storeDir, "operator-dir", "", "", "path to an operator dir - all accounts are migrated")
cmd.Flags().BoolVarP(¶ms.overwrite, "force", "F", false, "overwrite accounts with the same name")
return cmd
}
func init() {
GetRootCmd().AddCommand(createMigrateCmd())
}
type MigrateCmdParams struct {
url string
storeDir string
overwrite bool
Jobs []*MigrateJob
}
func (p *MigrateCmdParams) SetDefaults(ctx ActionCtx) error {
if p.url != "" && p.storeDir != "" {
return fmt.Errorf("specify one of --url or --store-dir")
}
return nil
}
func (p *MigrateCmdParams) PreInteractive(ctx ActionCtx) error {
ok, err := cli.Confirm("migrate all accounts under a particular operator", true)
if err != nil {
return err
}
if ok {
p.storeDir, err = cli.Prompt("specify the directory for the operator", "", cli.Val(func(v string) error {
_, err := store.LoadStore(v)
return err
}))
if err != nil {
return err
}
} else {
p.url, err = cli.Prompt("account jwt url/or path ", p.url, cli.Val(func(v string) error {
// we expect either a file or url
if IsURL(v) {
return nil
}
v, err := Expand(v)
if err != nil {
return err
}
_, err = os.Stat(v)
return err
}))
if err != nil {
return err
}
}
return nil
}
func (p *MigrateCmdParams) Load(ctx ActionCtx) error {
var err error
if p.storeDir != "" {
p.storeDir, err = Expand(p.storeDir)
if err != nil {
return err
}
s, err := store.LoadStore(p.storeDir)
if err != nil {
return fmt.Errorf("error loading operator %#q: %v", p.storeDir, err)
}
names, err := s.ListSubContainers(store.Accounts)
if err != nil {
return fmt.Errorf("error listing accounts in %#q: %v", p.storeDir, err)
}
for _, n := range names {
mj := NewMigrateJob(filepath.Join(p.storeDir, store.Accounts, n, store.JwtName(n)), p.overwrite)
p.Jobs = append(p.Jobs, &mj)
}
} else {
mj := NewMigrateJob(p.url, p.overwrite)
p.Jobs = append(p.Jobs, &mj)
}
for _, j := range p.Jobs {
j.Load(ctx)
}
return nil
}
func (p *MigrateCmdParams) PostInteractive(ctx ActionCtx) error {
for _, j := range p.Jobs {
if j.OK() {
j.PostInteractive(ctx)
}
}
return nil
}
func (p *MigrateCmdParams) Validate(ctx ActionCtx) error {
for _, j := range p.Jobs {
if j.OK() {
j.Validate(ctx)
}
}
return nil
}
func (p *MigrateCmdParams) Run(ctx ActionCtx) (store.Status, error) {
var jobs store.MultiJob
for _, j := range p.Jobs {
if j.OK() {
j.Run(ctx)
}
jobs = append(jobs, j.status)
}
m, err := jobs.Summary()
if m != "" {
ctx.CurrentCmd().Println(m)
}
return jobs, err
}
type MigrateJob struct {
accountToken string
claim *jwt.AccountClaims
url string
isFileImport bool
operator string
migratedUsers []*jwt.UserClaims
overwrite bool
status store.Status
}
func NewMigrateJob(url string, overwrite bool) MigrateJob {
return MigrateJob{url: url, overwrite: overwrite, status: &store.Report{}}
}
func (j *MigrateJob) OK() bool {
code := j.status.Code()
return code == store.OK || code == store.NONE
}
func (j *MigrateJob) getAccountKeys() []string {
var keys []string
keys = append(keys, j.claim.Subject)
keys = append(keys, j.claim.SigningKeys.Keys()...)
return keys
}
func (j *MigrateJob) Load(ctx ActionCtx) {
if j.url == "" {
j.status = store.ErrorStatus("an url or path to the account jwt is required")
return
}
data, err := LoadFromFileOrURL(j.url)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("error loading from %#q: %v", j.url, err))
return
}
j.isFileImport = !IsURL(j.url)
j.accountToken, err = jwt.ParseDecoratedJWT(data)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("error parsing JWT: %v", err))
return
}
j.claim, err = jwt.DecodeAccountClaims(j.accountToken)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("error decoding JWT: %v", err))
return
}
}
func (j *MigrateJob) PostInteractive(ctx ActionCtx) {
if ctx.StoreCtx().Store.HasAccount(j.claim.Name) && !j.overwrite {
aac, err := ctx.StoreCtx().Store.ReadAccountClaim(j.claim.Name)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("error reading account JWT: %v", err))
return
}
j.overwrite = aac.Subject == j.claim.Subject
if !j.overwrite {
j.overwrite, err = cli.Confirm("account %q already exists under the current operator, replace it", false)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
}
}
}
func (j *MigrateJob) Validate(ctx ActionCtx) {
if j.isFileImport {
parent := ctx.StoreCtx().Store.Dir
// it is already determined to be a file
fp, err := Expand(j.url)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
if strings.HasPrefix(fp, parent) {
j.status = store.ErrorStatus(fmt.Sprintf("cannot migrate %q onto itself", fp))
return
}
}
if !j.overwrite && ctx.StoreCtx().Store.HasAccount(j.claim.Name) {
j.status = store.ErrorStatus(fmt.Sprintf("account %q already exists, specify --force to overwrite", j.claim.Name))
return
}
keys := j.getAccountKeys()
var hasOne bool
for _, k := range keys {
kp, _ := ctx.StoreCtx().KeyStore.GetKeyPair(k)
if kp != nil {
hasOne = true
break
}
}
if !hasOne {
j.status = store.ErrorStatus(fmt.Sprintf("unable to find an account key for %q - need one of %s", j.claim.Name, strings.Join(keys, ", ")))
return
}
}
func (j *MigrateJob) Run(ctx ActionCtx) {
ctx.CurrentCmd().SilenceUsage = true
j.operator = ctx.StoreCtx().Operator.Name
token, err := jwt.ParseDecoratedJWT([]byte(j.accountToken))
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
ac, err := jwt.DecodeAccountClaims(token)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
if ctx.StoreCtx().Store.IsManaged() {
var keys []string
keys = append(keys, ac.Subject)
keys = append(keys, ac.SigningKeys.Keys()...)
// need to sign it with any key we can get
var kp nkeys.KeyPair
for _, k := range keys {
kp, _ = ctx.StoreCtx().KeyStore.GetKeyPair(k)
if kp != nil {
break
}
}
if kp == nil {
j.status = store.ErrorStatus(fmt.Sprintf("unable to find any account keys - need any of %s", strings.Join(keys, ", ")))
return
}
j.accountToken, err = ac.Encode(kp)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
}
remote, err := ctx.StoreCtx().Store.StoreClaim([]byte(j.accountToken))
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("failed to migrate %q: %v", ac.Name, err))
return
}
if j.isFileImport {
udir := filepath.Join(filepath.Dir(j.url), store.Users)
fi, err := os.Stat(udir)
if err == nil && fi.IsDir() {
dirEntries, err := os.ReadDir(udir)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
for _, v := range dirEntries {
n := v.Name()
if !v.IsDir() && filepath.Ext(n) == ".jwt" {
up := filepath.Join(udir, n)
d, err := Read(up)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
s, err := jwt.ParseDecoratedJWT(d)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
uc, err := jwt.DecodeUserClaims(s)
if err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
if err := ctx.StoreCtx().Store.StoreRaw([]byte(s)); err != nil {
j.status = store.ErrorStatus(fmt.Sprintf("%v", err))
return
}
j.migratedUsers = append(j.migratedUsers, uc)
}
}
}
}
m := fmt.Sprintf("migrated %q to operator %q", j.claim.Name, j.operator)
um := fmt.Sprintf("%d users migrated", len(j.migratedUsers))
if len(j.migratedUsers) == 0 {
um = "no users migrated"
}
if !j.isFileImport {
um = ""
}
j.status = store.OKStatus(fmt.Sprintf("%s [%s]", m, um))
if remote != nil {
si, ok := j.status.(*store.Report)
if ok {
si.Details = append(si.Details, remote)
}
}
}