-
Notifications
You must be signed in to change notification settings - Fork 42
/
load.go
409 lines (377 loc) · 10.5 KB
/
load.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2022 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"
"net/url"
"os"
"strings"
"github.com/nats-io/nsc/v2/cmd/store"
"github.com/nats-io/nsc/v2/home"
"github.com/nats-io/jsm.go/natscontext"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
"github.com/spf13/cobra"
)
func createLoadCmd() *cobra.Command {
var params LoadParams
cmd := &cobra.Command{
Use: "load",
Short: "install entities for an operator, account and key",
RunE: func(cmd *cobra.Command, args []string) error {
// Note: Need to initialize the operator and environment
// before the RunAction command since that is dependent
// on an operator being available.
if err := params.setupHomeDir(); err != nil {
return err
}
if err := params.addOperator(); err != nil {
switch err.Error() {
case "operator not found":
cmd.Printf("Unable to find operator %q\n\n", params.operatorName)
cmd.Printf("If you have used this operator, please enter:")
cmd.Printf("`nsc env -s /path/to/storedir`\n")
cmd.Printf("or define the operator endpoint environment:\n")
cmd.Printf("`NSC_<OPERATOR_NAME>_OPERATOR=http/s://host:port/jwt/v1/operator`\n\n")
case "bad operator version":
_ = JWTUpgradeBannerJWT(1)
default:
}
return err
}
err := RunAction(cmd, args, ¶ms)
if err != nil {
return err
}
return err
},
}
cmd.Flags().StringVarP(¶ms.resourceURL, "profile", "", "", "Profile URL to initialize NSC and NATS CLI env")
cmd.Flags().StringVarP(¶ms.accountServerURL, "url", "", "", "URL of the account server")
cmd.Flags().StringVarP(¶ms.accountSeed, "seed", "", "", "Seed of the account used to create users")
cmd.Flags().StringVarP(¶ms.username, "user", "", "default", "Default username")
return cmd
}
func init() {
rootCmd.AddCommand(createLoadCmd())
}
// LoadParams prepares an NSC environment and NATS Cli context based
// on the URL of and Account seed. Requires NATS CLI to be in PATH
// to complete the setup of a user profile.
type LoadParams struct {
r *store.Report
resourceURL string
operatorName string
operator *jwt.OperatorClaims
accountName string
accountSeed string
accountPublicKey string
account *jwt.AccountClaims
accountKeyPair nkeys.KeyPair
accountServerURL string
username string
contextName string
userCreds string
}
func (p *LoadParams) setupHomeDir() error {
tc := GetConfig()
sr := tc.StoreRoot
if sr == "" {
sr = home.NscDataHome(home.StoresSubDirName)
}
sr, err := Expand(sr)
if err != nil {
return err
}
if err := MaybeMakeDir(sr); err != nil {
return err
}
if err := tc.ContextConfig.setStoreRoot(sr); err != nil {
return err
}
if err := tc.Save(); err != nil {
return err
}
return nil
}
func (p *LoadParams) addOperator() error {
if p.resourceURL != "" {
u, err := url.Parse(p.resourceURL)
if err != nil {
return err
}
p.operatorName = u.Hostname()
qparams := u.Query()
p.accountSeed = qparams.Get("secret")
username := qparams.Get("user")
if username != "" {
p.username = username
}
ko, err := FindKnownOperator(p.operatorName)
if err != nil {
return err
}
if ko == nil {
return fmt.Errorf("operator not found")
}
p.accountServerURL = ko.AccountServerURL
}
if p.accountServerURL == "" {
return fmt.Errorf("missing account server URL")
}
// Fetch the Operator JWT from the URL to get its claims
// and setup the local store.
data, err := LoadFromURL(p.accountServerURL)
if err != nil {
return err
}
opJWT, err := jwt.ParseDecoratedJWT(data)
if err != nil {
return err
}
operator, err := jwt.DecodeOperatorClaims(opJWT)
if err != nil {
return err
}
p.operator = operator
if operator.AccountServerURL == "" {
return fmt.Errorf("error importing operator %q - it doesn't define an account server url", p.operatorName)
}
// Store the Operator locally.
var (
onk store.NamedKey
s *store.Store
)
onk.Name = p.operatorName
ts, err := GetConfig().LoadStore(onk.Name)
if err == nil {
tso, err := ts.ReadOperatorClaim()
if err == nil {
if tso.Subject == operator.Subject {
s = ts
} else {
return fmt.Errorf("error a different operator named %q already exists -- specify --dir to create at a different location", onk.Name)
}
}
}
if s == nil {
s, err = store.CreateStore(onk.Name, GetConfig().StoreRoot, &onk)
if err != nil {
return err
}
}
if err := s.StoreRaw([]byte(opJWT)); err != nil {
return err
}
return nil
}
func (p *LoadParams) addAccount(ctx ActionCtx) error {
r := p.r
// Take the seed and generate the public key for the Account then store it.
// The key is needed to be able to create local user creds as well to configure
// the context for the NATS CLI.
operator := p.operator
seed := p.accountSeed
if !strings.HasPrefix(seed, "SA") {
return fmt.Errorf("expected account seed to initialize")
}
akp, err := nkeys.FromSeed([]byte(seed))
if err != nil {
return fmt.Errorf("failed to parse account name as an nkey: %w", err)
}
p.accountKeyPair = akp
publicAccount, err := akp.PublicKey()
if err != nil {
return err
}
p.accountPublicKey = publicAccount
// Fetch Account JWT from URL.
accountURL := fmt.Sprintf("%s/accounts/%s", operator.AccountServerURL, publicAccount)
data, err := LoadFromURL(accountURL)
if err != nil {
return err
}
accJWT, err := jwt.ParseDecoratedJWT(data)
if err != nil {
return err
}
account, err := jwt.DecodeAccountClaims(accJWT)
if err != nil {
return err
}
p.account = account
p.accountName = account.Name
current := GetConfig()
err = current.ContextConfig.Update(current.StoreRoot, p.operatorName, "")
if err != nil {
return err
}
// Store the key and JWT.
_, err = store.StoreKey(akp)
if err != nil {
return err
}
sctx := ctx.StoreCtx()
err = sctx.SetContext(p.accountName, p.accountPublicKey)
if err != nil {
return err
}
ts, err := current.LoadStore(p.operatorName)
if err != nil {
return err
}
rs, err := ts.StoreClaim([]byte(accJWT))
if rs != nil {
r.Add(rs)
}
if err != nil {
r.AddFromError(err)
}
return nil
}
func (p *LoadParams) addUser(ctx ActionCtx) error {
current := GetConfig()
err := current.ContextConfig.Update(current.StoreRoot, p.operatorName, p.accountName)
if err != nil {
return err
}
sctx := ctx.StoreCtx()
err = sctx.SetContext(p.accountName, p.accountPublicKey)
if err != nil {
return err
}
ts, err := current.LoadStore(p.operatorName)
if err != nil {
return err
}
r := p.r
// NOTE: Stamp the KeyStore env to use the Operator that is being setup.
sctx.KeyStore.Env = p.operatorName
// Check if username is already setup, if so then just find the creds instead.
userFields := []string{store.Accounts, p.accountName, store.Users, store.JwtName(p.username)}
if ts.Has(userFields...) {
r.AddWarning("the user %q already exists", p.username)
creds := sctx.KeyStore.CalcUserCredsPath(p.accountName, p.username)
if _, err := os.Stat(creds); os.IsNotExist(err) {
r.AddFromError(fmt.Errorf("user %q credentials not found at %q", p.username, creds))
return err
} else {
p.userCreds = creds
}
return nil
}
kp, err := nkeys.CreatePair(nkeys.PrefixByteUser)
if err != nil {
return err
}
pub, err := kp.PublicKey()
if err != nil {
return err
}
uc := jwt.NewUserClaims(pub)
uc.Name = p.username
uc.SetScoped(signerKeyIsScoped(ctx, p.accountName, p.accountKeyPair))
userJWT, err := uc.Encode(p.accountKeyPair)
if err != nil {
return err
}
st, err := ts.StoreClaim([]byte(userJWT))
if st != nil {
r.Add(st)
}
if err != nil {
p.r.AddFromError(err)
return err
}
_, err = sctx.KeyStore.Store(kp)
if err != nil {
return err
}
r.AddOK("generated and stored user key %q", uc.Subject)
// Store user credentials.
userSeed, err := kp.Seed()
if err != nil {
return err
}
creds, err := jwt.FormatUserConfig(userJWT, userSeed)
if err != nil {
return err
}
ks := sctx.KeyStore
path, err := ks.MaybeStoreUserCreds(p.accountName, p.username, creds)
if err != nil {
return err
}
p.userCreds = path
r.AddOK("generated and stored user credentials at %q", path)
return nil
}
func (p *LoadParams) configureNSCEnv() error {
// Change the current context to the one from load.
current := GetConfig()
if err := current.ContextConfig.Update(current.StoreRoot, p.operatorName, p.accountName); err != nil {
return err
}
if err := current.Save(); err != nil {
return err
}
return nil
}
func (p *LoadParams) configureNATSCLI() error {
p.contextName = fmt.Sprintf("%s_%s_%s", p.operatorName, p.accountName, p.username)
// Replace this to use instead use the natscontext library.
var servers string
if len(p.operator.OperatorServiceURLs) > 0 {
servers = strings.Join(p.operator.OperatorServiceURLs, ",")
}
// Finally, store the NATS context used by the NATS CLI.
config, err := natscontext.New(p.contextName, false,
natscontext.WithServerURL(servers),
natscontext.WithCreds(p.userCreds),
natscontext.WithDescription(fmt.Sprintf("%s (%s)", p.operatorName, p.operator.Name)),
)
if err != nil {
return err
}
config.Save(p.contextName)
// Switch to use that context as well.
err = natscontext.SelectContext(p.contextName)
if err != nil {
return err
}
return nil
}
// Run executes the load profile command.
func (p *LoadParams) Run(ctx ActionCtx) (store.Status, error) {
r := store.NewDetailedReport(false)
p.r = r
if err := p.configureNSCEnv(); err != nil {
return nil, err
}
if err := p.addAccount(ctx); err != nil {
return nil, err
}
if err := p.addUser(ctx); err != nil {
return nil, err
}
if err := p.configureNATSCLI(); err != nil {
return nil, err
}
r.AddOK("created nats context %q", p.contextName)
return r, nil
}
func (p *LoadParams) SetDefaults(ctx ActionCtx) error { return nil }
func (p *LoadParams) PreInteractive(ctx ActionCtx) error { return nil }
func (p *LoadParams) Load(ctx ActionCtx) error { return nil }
func (p *LoadParams) PostInteractive(ctx ActionCtx) error { return nil }
func (p *LoadParams) Validate(ctx ActionCtx) error { return nil }