/
register.go
797 lines (723 loc) · 25.7 KB
/
register.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package controller
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/asn1"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"text/template"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/httpbakery"
"github.com/juju/cmd/v3"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/gnuflag"
jujuhttp "github.com/juju/http/v2"
"github.com/juju/names/v5"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/yaml.v2"
"github.com/juju/juju/api"
"github.com/juju/juju/api/base"
"github.com/juju/juju/api/client/modelmanager"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
corelogger "github.com/juju/juju/core/logger"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/jujuclient"
"github.com/juju/juju/proxy/factory"
"github.com/juju/juju/rpc/params"
)
var noModelsMessage = `
There are no models available. You can add models with
"juju add-model", or you can ask an administrator or owner
of a model to grant access to that model with "juju grant".
`
// NewRegisterCommand returns a command to allow the user to register a controller.
func NewRegisterCommand() cmd.Command {
c := ®isterCommand{}
c.apiOpen = c.APIOpen
c.listModelsFunc = c.listModels
c.store = jujuclient.NewFileClientStore()
c.CanClearCurrentModel = true
return modelcmd.WrapBase(c)
}
// registerCommand logs in to a Juju controller and caches the connection
// information.
type registerCommand struct {
modelcmd.CommandBase
apiOpen api.OpenFunc
listModelsFunc func(_ jujuclient.ClientStore, controller, user string) ([]base.UserModel, error)
store jujuclient.ClientStore
arg string
replace bool
// onRunError is executed if non-nil if there is an error at the end
// of the Run method.
onRunError func()
}
var usageRegisterSummary = `
Registers a controller.`[1:]
var usageRegisterDetails = `
The register command adds details of a controller to the local system.
This is done either by completing the user registration process that
began with the 'juju add-user' command, or by providing the DNS host
name of a public controller.
To complete the user registration process, you should have been provided
with a base64-encoded blob of data (the output of 'juju add-user')
which can be copied and pasted as the <string> argument to 'register'.
You will be prompted for a password, which, once set, causes the
registration string to be voided. In order to start using Juju the user
can now either add a model or wait for a model to be shared with them.
Some machine providers will require the user to be in possession of
certain credentials in order to add a model.
If a new controller has been spun up to replace an existing one, and you want
to start using that replacement controller instead of the original one,
use the --replace option to overwrite any existing controller details based
on either a name or UUID match.
When adding a controller at a public address, authentication via some
external third party (for example Ubuntu SSO) will be required, usually
by using a web browser.
`
const usageRegisterExamples = `
juju register MFATA3JvZDAnExMxMDQuMTU0LjQyLjQ0OjE3MDcwExAxMC4xMjguMC4yOjE3MDcwBCBEFCaXerhNImkKKabuX5ULWf2Bp4AzPNJEbXVWgraLrAA=
juju register --replace MFATA3JvZDAnExMxMDQuMTU0LjQyLjQ0OjE3MDcwExAxMC4xMjguMC4yOjE3MDcwBCBEFCaXerhNImkKKabuX5ULWf2Bp4AzPNJEbXVWgraLrAA=
juju register public-controller.example.com
`
// Info implements Command.Info
// `register` may seem generic, but is seen as simple and without potential
// naming collisions in any current or planned features.
func (c *registerCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "register",
Args: "<registration string>|<controller host name>",
Purpose: usageRegisterSummary,
Doc: usageRegisterDetails,
Examples: usageRegisterExamples,
SeeAlso: []string{
"add-user",
"change-user-password",
"unregister",
},
})
}
// SetFlags implements Command.SetFlags.
func (c *registerCommand) SetFlags(f *gnuflag.FlagSet) {
c.CommandBase.SetFlags(f)
f.BoolVar(&c.replace, "replace", false, "replace any existing controller")
}
// Init implements Command.Init.
func (c *registerCommand) Init(args []string) error {
if len(args) < 1 {
return errors.New("registration data missing")
}
c.arg, args = args[0], args[1:]
if err := cmd.CheckEmpty(args); err != nil {
return errors.Trace(err)
}
return nil
}
// SetClientStore implements Command.SetClientStore.
func (c *registerCommand) SetClientStore(store jujuclient.ClientStore) {
c.store = store
}
// Run implements Command.Run.
func (c *registerCommand) Run(ctx *cmd.Context) error {
err := c.run(ctx)
if err != nil && c.onRunError != nil {
c.onRunError()
}
return err
}
func (c *registerCommand) run(ctx *cmd.Context) error {
c.store = modelcmd.QualifyingClientStore{c.store}
registrationParams, err := c.getParameters(ctx)
if err != nil {
return errors.Trace(err)
}
// Check if user is trying to register an already known controller by
// by providing the IP of one of its endpoints.
if registrationParams.publicHost != "" {
if err := ensureNotKnownEndpoint(c.store, registrationParams.publicHost); err != nil {
return errors.Trace(err)
}
}
controllerName, err := c.promptControllerName(registrationParams.defaultControllerName, ctx.Stderr, ctx.Stdin)
if err != nil {
return errors.Trace(err)
}
controllerDetails, accountDetails, err := c.controllerDetails(ctx, registrationParams, controllerName)
if err != nil {
return errors.Trace(err)
}
if err := c.updateController(
c.store,
controllerName,
controllerDetails,
accountDetails,
); err != nil {
return errors.Trace(err)
}
// Log into the controller to verify the credentials, and
// list the models available.
models, err := c.listModelsFunc(c.store, controllerName, accountDetails.User)
if err != nil {
return errors.Trace(err)
}
if err := c.SetControllerModels(c.store, controllerName, models); err != nil {
return errors.Annotate(err, "storing model details")
}
if err := c.store.SetCurrentController(controllerName); err != nil {
return errors.Trace(err)
}
fmt.Fprintf(
ctx.Stderr, "\nWelcome, %s. You are now logged into %q.\n",
friendlyUserName(accountDetails.User), controllerName,
)
return c.maybeSetCurrentModel(ctx, c.store, controllerName, accountDetails.User, models)
}
func friendlyUserName(user string) string {
u := names.NewUserTag(user)
if u.IsLocal() {
return u.Name()
}
return u.Id()
}
// controllerDetails returns controller and account details to be registered for the
// given registration parameters.
func (c *registerCommand) controllerDetails(ctx *cmd.Context, p *registrationParams, controllerName string) (jujuclient.ControllerDetails, jujuclient.AccountDetails, error) {
if p.publicHost != "" {
return c.publicControllerDetails(ctx, p.publicHost, controllerName)
}
return c.nonPublicControllerDetails(ctx, p, controllerName)
}
func cookieURL(host string) (*url.URL, error) {
if strings.Contains(host, ":") {
var err error
host, _, err = net.SplitHostPort(host)
if err != nil {
return nil, errors.Trace(err)
}
}
return url.Parse(host)
}
// publicControllerDetails returns controller and account details to be registered
// for the given public controller host name.
func (c *registerCommand) publicControllerDetails(ctx *cmd.Context, host, controllerName string) (jujuclient.ControllerDetails, jujuclient.AccountDetails, error) {
errRet := func(err error) (jujuclient.ControllerDetails, jujuclient.AccountDetails, error) {
return jujuclient.ControllerDetails{}, jujuclient.AccountDetails{}, err
}
apiAddr := host
if !strings.Contains(apiAddr, ":") {
apiAddr += ":443"
}
// Make a direct API connection because we don't yet know the
// controller UUID so can't store the thus-incomplete controller
// details to make a conventional connection.
//
// Unfortunately this means we'll connect twice to the controller
// but it's probably best to go through the conventional path the
// second time.
bclient, err := c.BakeryClient(c.store, controllerName)
if err != nil {
return errRet(errors.Trace(err))
}
cookieURL, err := cookieURL(host)
if err != nil {
return errRet(err)
}
dialOpts := api.DefaultDialOpts()
dialOpts.BakeryClient = bclient
// we set up a login provider that will first try to log in using
// oauth device flow, failing that it will try to log in using
// user-pass or macaroons.
dialOpts.LoginProvider = api.NewTryInOrderLoginProvider(
api.NewSessionTokenLoginProvider(
"",
func(format string, params ...any) error {
_, err := fmt.Fprintf(ctx.Stderr, format, params...)
return err
},
func(sessionToken string) error {
return c.store.UpdateAccount(controllerName, jujuclient.AccountDetails{
Type: jujuclient.OAuth2DeviceFlowAccountDetailsType,
SessionToken: sessionToken,
})
},
),
api.NewUserpassLoginProvider(names.UserTag{}, "", "", nil, bclient, cookieURL),
)
conn, err := c.apiOpen(&api.Info{
Addrs: []string{apiAddr},
}, dialOpts)
if err != nil {
return errRet(errors.Trace(err))
}
defer conn.Close()
user, ok := conn.AuthTag().(names.UserTag)
if !ok {
return errRet(errors.Errorf("logged in as %v, not a user", conn.AuthTag()))
}
// If we get to here, then we have a cached macaroon for the registered
// user. If we encounter an error after here, we need to clear it.
c.onRunError = func() {
if err := c.ClearControllerMacaroons(c.store, controllerName); err != nil {
logger.Errorf("failed to clear macaroon: %v", err)
}
}
return jujuclient.ControllerDetails{
APIEndpoints: []string{apiAddr},
ControllerUUID: conn.ControllerTag().Id(),
}, jujuclient.AccountDetails{
User: user.Id(),
LastKnownAccess: conn.ControllerAccess(),
}, nil
}
func getProxier(proxyConfig params.Proxy) (*jujuclient.ProxyConfWrapper, error) {
f, err := factory.NewDefaultFactory()
if err != nil {
return nil, errors.Annotate(err, "cannot create default proxy factory")
}
proxier, err := f.ProxierFromConfig(proxyConfig.Type, proxyConfig.Config)
if err != nil {
return nil, errors.Trace(err)
}
return &jujuclient.ProxyConfWrapper{Proxier: proxier}, nil
}
// nonPublicControllerDetails returns controller and account details to be registered with
// respect to the given registration parameters.
func (c *registerCommand) nonPublicControllerDetails(ctx *cmd.Context, registrationParams *registrationParams, controllerName string) (jujuclient.ControllerDetails, jujuclient.AccountDetails, error) {
errRet := func(err error) (jujuclient.ControllerDetails, jujuclient.AccountDetails, error) {
return jujuclient.ControllerDetails{}, jujuclient.AccountDetails{}, err
}
// During registration we must set a new password. This has to be done
// atomically with the clearing of the secret key.
payloadBytes, err := json.Marshal(params.SecretKeyLoginRequestPayload{
Password: registrationParams.newPassword,
})
if err != nil {
return errRet(errors.Trace(err))
}
controllerDetails := jujuclient.ControllerDetails{
APIEndpoints: registrationParams.controllerAddrs,
}
if registrationParams.proxyConfig != "" {
var proxy jujuclient.ProxyConfWrapper
if err := yaml.Unmarshal([]byte(registrationParams.proxyConfig), &proxy); err != nil {
return errRet(errors.Trace(err))
}
controllerDetails.Proxy = &proxy
}
// Make the registration call. If this is successful, the client's
// cookie jar will be populated with a macaroon that may be used
// to log in below without the user having to type in the password
// again.
req := params.SecretKeyLoginRequest{
Nonce: registrationParams.nonce[:],
User: registrationParams.userTag.String(),
PayloadCiphertext: secretbox.Seal(
nil, payloadBytes,
®istrationParams.nonce,
®istrationParams.key,
),
}
resp, err := c.secretKeyLogin(controllerDetails, req, controllerName)
if err != nil {
// If we got here and got an error, the registration token supplied
// will be expired.
// Log the error as it will be useful for debugging, but give user a
// suggestion for the way forward instead of error details.
logger.Infof("while validating secret key: %v", err)
err = errors.Errorf("Provided registration token may have been expired.\nA controller administrator must reset your user to issue a new token.\nSee %q for more information.", "juju help change-user-password")
return errRet(errors.Trace(err))
}
// Decrypt the response to authenticate the controller and
// obtain its CA certificate.
if len(resp.Nonce) != len(registrationParams.nonce) {
return errRet(errors.NotValidf("response nonce"))
}
var respNonce [24]byte
copy(respNonce[:], resp.Nonce)
payloadBytes, ok := secretbox.Open(nil, resp.PayloadCiphertext, &respNonce, ®istrationParams.key)
if !ok {
return errRet(errors.NotValidf("response payload"))
}
var responsePayload params.SecretKeyLoginResponsePayload
if err := json.Unmarshal(payloadBytes, &responsePayload); err != nil {
return errRet(errors.Annotate(err, "unmarshalling response payload"))
}
controllerDetails.ControllerUUID = responsePayload.ControllerUUID
controllerDetails.CACert = responsePayload.CACert
if responsePayload.ProxyConfig != nil {
if controllerDetails.Proxy, err = getProxier(*responsePayload.ProxyConfig); err != nil {
return errRet(errors.Annotate(err, "creating proxier from config"))
}
}
user := registrationParams.userTag.Id()
ctx.Infof("Initial password successfully set for %s.", friendlyUserName(user))
// If we get to here, then we have a cached macaroon for the registered
// user. If we encounter an error after here, we need to clear it.
c.onRunError = func() {
if err := c.ClearControllerMacaroons(c.store, controllerName); err != nil {
logger.Errorf("failed to clear macaroon: %v", err)
}
}
return controllerDetails, jujuclient.AccountDetails{
User: user,
LastKnownAccess: string(permission.LoginAccess),
}, nil
}
// updateController prompts for a controller name and updates the
// controller and account details in the given client store.
func (c *registerCommand) updateController(
store jujuclient.ClientStore,
controllerName string,
controllerDetails jujuclient.ControllerDetails,
accountDetails jujuclient.AccountDetails,
) error {
// Check that the same controller isn't already stored, so that we
// can avoid needlessly asking for a controller name in that case.
all, err := store.AllControllers()
if err != nil {
return errors.Trace(err)
}
for name, ctl := range all {
if ctl.ControllerUUID == controllerDetails.ControllerUUID {
if !c.replace || controllerName != name {
return genAlreadyRegisteredError(name, accountDetails.User)
}
break
}
}
if c.replace {
if err := store.UpdateController(controllerName, controllerDetails); err != nil {
if !errors.IsNotFound(err) {
return errors.Trace(err)
}
if err := store.AddController(controllerName, controllerDetails); err != nil {
return errors.Trace(err)
}
}
} else {
if err := store.AddController(controllerName, controllerDetails); err != nil {
return errors.Trace(err)
}
}
if err := store.UpdateAccount(controllerName, accountDetails); err != nil {
return errors.Annotatef(err, "cannot update account information: %v", err)
}
return nil
}
func (c *registerCommand) listModels(store jujuclient.ClientStore, controllerName, userName string) ([]base.UserModel, error) {
api, err := c.NewAPIRoot(store, controllerName, "")
if err != nil {
return nil, errors.Trace(err)
}
defer api.Close()
mm := modelmanager.NewClient(api)
return mm.ListModels(userName)
}
func (c *registerCommand) maybeSetCurrentModel(ctx *cmd.Context, store jujuclient.ClientStore, controllerName, userName string, models []base.UserModel) error {
if len(models) == 0 {
fmt.Fprint(ctx.Stderr, noModelsMessage)
return nil
}
// If we get to here, there is at least one model.
if len(models) == 1 {
// There is exactly one model shared,
// so set it as the current model.
model := models[0]
owner := names.NewUserTag(model.Owner)
modelName := jujuclient.JoinOwnerModelName(owner, model.Name)
err := store.SetCurrentModel(controllerName, modelName)
if err != nil {
return errors.Trace(err)
}
fmt.Fprintf(ctx.Stderr, "\nCurrent model set to %q.\n", modelName)
return nil
}
fmt.Fprintf(ctx.Stderr, `
There are %d models available. Use "juju switch" to select
one of them:
`, len(models))
user := names.NewUserTag(userName)
ownerModelNames := make(set.Strings)
otherModelNames := make(set.Strings)
for _, model := range models {
if model.Owner == userName {
ownerModelNames.Add(model.Name)
continue
}
owner := names.NewUserTag(model.Owner)
modelName := common.OwnerQualifiedModelName(model.Name, owner, user)
otherModelNames.Add(modelName)
}
for _, modelName := range ownerModelNames.SortedValues() {
fmt.Fprintf(ctx.Stderr, " - juju switch %s\n", modelName)
}
for _, modelName := range otherModelNames.SortedValues() {
fmt.Fprintf(ctx.Stderr, " - juju switch %s\n", modelName)
}
return nil
}
type registrationParams struct {
// publicHost holds the host name of a public controller.
// If this is set, all other fields will be empty.
publicHost string
defaultControllerName string
userTag names.UserTag
controllerAddrs []string
key [32]byte
nonce [24]byte
newPassword string
proxyConfig string
}
// getParameters gets all of the parameters required for registering, prompting
// the user as necessary.
func (c *registerCommand) getParameters(ctx *cmd.Context) (*registrationParams, error) {
var params registrationParams
if strings.Contains(c.arg, ".") || c.arg == "localhost" {
// Looks like a host name - no URL-encoded base64 string should
// contain a dot and every public controller name should.
// Allow localhost for development purposes.
params.publicHost = c.arg
// No need for password shenanigans if we're using a public controller.
return ¶ms, nil
}
// Decode key, username, controller addresses from the string supplied
// on the command line.
decodedData, err := base64.URLEncoding.DecodeString(c.arg)
if err != nil {
return nil, errors.Annotatef(err, "invalid registration token")
}
var info jujuclient.RegistrationInfo
if _, err := asn1.Unmarshal(decodedData, &info); err != nil {
return nil, errors.Trace(err)
}
params.controllerAddrs = info.Addrs
params.proxyConfig = info.ProxyConfig
params.userTag = names.NewUserTag(info.User)
if len(info.SecretKey) != len(params.key) {
return nil, errors.NotValidf("secret key")
}
copy(params.key[:], info.SecretKey)
params.defaultControllerName = info.ControllerName
// Prompt the user for the new password to set.
newPassword, err := c.promptNewPassword(ctx.Stderr, ctx.Stdin)
if err != nil {
return nil, errors.Trace(err)
}
params.newPassword = newPassword
// Generate a random nonce for encrypting the request.
if _, err := rand.Read(params.nonce[:]); err != nil {
return nil, errors.Trace(err)
}
return ¶ms, nil
}
func (c *registerCommand) secretKeyLogin(
controllerDetails jujuclient.ControllerDetails, request params.SecretKeyLoginRequest, controllerName string,
) (_ *params.SecretKeyLoginResponse, err error) {
cookieJar, err := c.CookieJar(c.store, controllerName)
if err != nil {
return nil, errors.Annotate(err, "getting API context")
}
buf, err := json.Marshal(&request)
if err != nil {
return nil, errors.Annotate(err, "marshalling request")
}
r := bytes.NewReader(buf)
// Determine which address to use by attempting to open an API
// connection with each of the addresses. Note that we do not
// know the CA certificate yet, so we do not want to send any
// sensitive information. We make no attempt to log in until
// we can verify the server's identity.
opts := api.DefaultDialOpts()
opts.InsecureSkipVerify = true
apiInfo := &api.Info{
Addrs: controllerDetails.APIEndpoints,
SkipLogin: true,
}
if controllerDetails.Proxy != nil {
apiInfo.Proxier = controllerDetails.Proxy.Proxier
}
conn, err := c.apiOpen(apiInfo, opts)
if err != nil {
return nil, errors.Trace(err)
}
apiAddr := conn.Addr()
defer func() {
if closeErr := conn.Close(); closeErr != nil {
if err == nil {
err = closeErr
} else {
logger.Warningf("error closing API connection: %v", closeErr)
}
}
}()
// Using the address we connected to above, perform the request.
// A success response will include a macaroon cookie that we can
// use to log in with.
urlString := fmt.Sprintf("https://%s/register", apiAddr)
httpReq, err := http.NewRequest("POST", urlString, r)
if err != nil {
return nil, errors.Trace(err)
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set(httpbakery.BakeryProtocolHeader, fmt.Sprint(bakery.LatestVersion))
httpClient := jujuhttp.NewClient(
jujuhttp.WithSkipHostnameVerification(true),
jujuhttp.WithCookieJar(cookieJar),
jujuhttp.WithLogger(logger.ChildWithLabels("http", corelogger.HTTP)),
)
httpResp, err := httpClient.Do(httpReq)
if err != nil {
return nil, errors.Trace(err)
}
defer func() { _ = httpResp.Body.Close() }()
if httpResp.StatusCode != http.StatusOK {
var resp params.ErrorResult
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return nil, errors.Trace(err)
}
return nil, resp.Error
}
var resp params.SecretKeyLoginResponse
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return nil, errors.Annotatef(err, "cannot decode login response")
}
return &resp, nil
}
func (c *registerCommand) promptNewPassword(stderr io.Writer, stdin io.Reader) (string, error) {
password, err := c.readPassword("Enter a new password: ", stderr, stdin)
if err != nil {
return "", errors.Annotatef(err, "cannot read password")
}
if password == "" {
return "", errors.NewNotValid(nil, "you must specify a non-empty password")
}
passwordConfirmation, err := c.readPassword("Confirm password: ", stderr, stdin)
if err != nil {
return "", errors.Trace(err)
}
if password != passwordConfirmation {
return "", errors.Errorf("passwords do not match")
}
return password, nil
}
func (c *registerCommand) promptControllerName(suggestedName string, stderr io.Writer, stdin io.Reader) (string, error) {
if suggestedName != "" {
if _, err := c.store.ControllerByName(suggestedName); err == nil && !c.replace {
suggestedName = ""
}
}
for {
var setMsg string
setMsg = "Enter a name for this controller: "
if suggestedName != "" {
replace := ""
if c.replace {
replace = "replace "
}
setMsg = fmt.Sprintf("Enter a name for this controller [%s%s]: ", replace, suggestedName)
}
fmt.Fprintf(stderr, setMsg)
name, err := c.readLine(stdin)
if err != nil {
return "", errors.Trace(err)
}
name = strings.TrimSpace(name)
if name == "" {
if suggestedName == "" {
fmt.Fprintln(stderr, "You must specify a non-empty controller name.")
continue
}
name = suggestedName
}
_, err = c.store.ControllerByName(name)
if err == nil && !c.replace {
fmt.Fprintf(stderr, "Controller %q already exists.\n", name)
continue
}
return name, nil
}
}
func (c *registerCommand) readPassword(prompt string, stderr io.Writer, stdin io.Reader) (string, error) {
fmt.Fprintf(stderr, "%s", prompt)
defer stderr.Write([]byte{'\n'})
if f, ok := stdin.(*os.File); ok && terminal.IsTerminal(int(f.Fd())) {
password, err := terminal.ReadPassword(int(f.Fd()))
if err != nil {
return "", errors.Trace(err)
}
return string(password), nil
}
return c.readLine(stdin)
}
func (c *registerCommand) readLine(stdin io.Reader) (string, error) {
// Read one byte at a time to avoid reading beyond the delimiter.
line, err := bufio.NewReader(byteAtATimeReader{stdin}).ReadString('\n')
if err != nil {
return "", errors.Trace(err)
}
return line[:len(line)-1], nil
}
type byteAtATimeReader struct {
io.Reader
}
func (r byteAtATimeReader) Read(out []byte) (int, error) {
return r.Reader.Read(out[:1])
}
// ensureNotKnownEndpoint checks whether any controllers in the local client
// cache contain the provided endpoint and returns an error if that is the
// case.
func ensureNotKnownEndpoint(store jujuclient.ClientStore, endpoint string) error {
existingDetails, existingName, err := store.ControllerByAPIEndpoints(endpoint)
if err != nil && !errors.IsNotFound(err) {
return errors.Trace(err)
}
if existingDetails == nil {
return nil
}
// Check if we know the username for this controller
accountDetails, err := store.AccountDetails(existingName)
if err != nil && !errors.IsNotFound(err) {
return errors.Trace(err)
}
if accountDetails != nil {
return genAlreadyRegisteredError(existingName, accountDetails.User)
}
return errors.Errorf(`This controller has already been registered on this client as %q.
To login run 'juju login -c %s'.`, existingName, existingName)
}
var alreadyRegisteredMessageT = template.Must(template.New("").Parse(`
This controller has already been registered on this client as "{{.ControllerName}}".
To login user "{{.UserName}}" run 'juju login -u {{.UserName}} -c {{.ControllerName}}'.
To update controller details and login as user "{{.UserName}}":
1. run 'juju unregister {{.ControllerName}}'
2. request from your controller admin another registration string, i.e
output from 'juju change-user-password {{.UserName}} --reset'
3. re-run 'juju register' with the registration string from (2) above.
`[1:]))
func genAlreadyRegisteredError(controller, user string) error {
var buf bytes.Buffer
if err := alreadyRegisteredMessageT.Execute(
&buf,
struct {
ControllerName string
UserName string
}{controller, user},
); err != nil {
return err
}
return errors.New(buf.String())
}