/
change_password.go
324 lines (289 loc) · 9.05 KB
/
change_password.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package user
import (
"bufio"
"fmt"
"io"
"os"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/httpbakery"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/names/v5"
"golang.org/x/crypto/ssh/terminal"
"github.com/juju/juju/api"
"github.com/juju/juju/api/authentication"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/juju"
"github.com/juju/juju/jujuclient"
)
const userChangePasswordDoc = `
The user is, by default, the current user. The latter can be confirmed with
the ` + "`juju show-user`" + ` command.
If no controller is specified, the current controller will be used.
A controller administrator can change the password for another user
by providing desired username as an argument.
A controller administrator can also reset the password with a --reset option.
This will invalidate any passwords that were previously set
and registration strings that were previously issued for a user.
This option will issue a new registration string to be used with
` + "`juju register`" + `.`
const userChangePasswordExamples = `
juju change-user-password
juju change-user-password bob
juju change-user-password bob --reset
juju change-user-password -c another-known-controller
juju change-user-password bob --controller another-known-controller
`
func NewChangePasswordCommand() cmd.Command {
var cmd changePasswordCommand
cmd.newAPIConnection = juju.NewAPIConnection
return modelcmd.WrapController(&cmd)
}
// changePasswordCommand changes the password for a user.
type changePasswordCommand struct {
modelcmd.ControllerCommandBase
newAPIConnection func(juju.NewAPIConnectionParams) (api.Connection, error)
api ChangePasswordAPI
// Input arguments
User string
Reset bool
noPrompt bool
// Internally initialised and used during run
controllerName string
userTag names.UserTag
accountDetails *jujuclient.AccountDetails
}
func (c *changePasswordCommand) SetFlags(f *gnuflag.FlagSet) {
f.BoolVar(&c.Reset, "reset", false, "Reset user password")
f.BoolVar(&c.noPrompt, "no-prompt", false, "don't prompt for password and just read a line from stdin")
}
// Info implements Command.Info.
func (c *changePasswordCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "change-user-password",
Args: "[username]",
Purpose: "Changes the password for the current or specified Juju user.",
Doc: userChangePasswordDoc,
Examples: userChangePasswordExamples,
SeeAlso: []string{
"add-user",
"register",
},
})
}
// Init implements Command.Init.
func (c *changePasswordCommand) Init(args []string) error {
var err error
c.User, err = cmd.ZeroOrOneArgs(args)
if err != nil {
return errors.Trace(err)
}
return nil
}
// ChangePasswordAPI defines the usermanager API methods that the change
// password command uses.
type ChangePasswordAPI interface {
SetPassword(username, password string) error
ResetPassword(username string) ([]byte, error)
Close() error
}
// Run implements Command.Run.
func (c *changePasswordCommand) Run(ctx *cmd.Context) error {
if err := c.prepareRun(); err != nil {
return errors.Trace(err)
}
if c.api == nil {
api, err := c.NewUserManagerAPIClient()
if err != nil {
return errors.Trace(err)
}
c.api = api
defer c.api.Close()
}
if c.Reset {
if c.User == "" || (c.accountDetails != nil && c.User == c.accountDetails.User) {
ctx.Infof("You cannot reset your own password.\nIf you want to change it, please call `juju change-user-password` without --reset option.")
return nil
}
return c.resetUserPassword(ctx)
}
return c.updateUserPassword(ctx)
}
func (c *changePasswordCommand) prepareRun() error {
err := c.ensureControllerName()
if err != nil {
return errors.Trace(err)
}
c.accountDetails, err = c.ClientStore().AccountDetails(c.controllerName)
if err != nil {
return errors.Trace(err)
}
if c.User != "" {
if !names.IsValidUserName(c.User) {
return errors.NotValidf("user name %q", c.User)
}
c.userTag = names.NewUserTag(c.User)
if c.userTag.Id() != c.accountDetails.User {
// The account details don't correspond to the username
// being changed, so we don't need to update the account
// locally.
c.accountDetails = nil
}
} else {
if !names.IsValidUser(c.accountDetails.User) {
return errors.Errorf("invalid user in account %q", c.accountDetails.User)
}
c.userTag = names.NewUserTag(c.accountDetails.User)
if !c.userTag.IsLocal() {
operation := "change"
if c.Reset {
operation = "reset"
}
return errors.Errorf("cannot %v password for external user %q", operation, c.userTag)
}
}
return nil
}
func (c *changePasswordCommand) ensureControllerName() error {
controllerName, err := c.ControllerName()
if err != nil {
return errors.Trace(err)
}
c.controllerName = controllerName
return nil
}
func (c *changePasswordCommand) resetUserPassword(ctx *cmd.Context) error {
key, err := c.api.ResetPassword(c.userTag.Id())
if err != nil {
return block.ProcessBlockedError(err, block.BlockChange)
}
ctx.Infof("Password for %q has been reset.", c.User)
base64RegistrationData, err := generateUserControllerAccessToken(
&c.ControllerCommandBase,
c.userTag.Id(),
key,
)
if err != nil {
return errors.Annotate(err, "generating controller user access token")
}
ctx.Infof("Ask the user to run:\n juju register %s\n", base64RegistrationData)
return nil
}
func (c *changePasswordCommand) updateUserPassword(ctx *cmd.Context) error {
var err error
var newPassword string
if c.noPrompt {
fmt.Fprintln(ctx.Stderr, "reading password from stdin...")
newPassword, err = readLine(ctx.Stdin)
if err != nil {
return errors.Trace(err)
}
} else {
newPassword, err = readAndConfirmPassword(ctx)
if err != nil {
return errors.Trace(err)
}
}
if newPassword == "" {
return errors.Errorf("password cannot be empty")
}
if err := c.api.SetPassword(c.userTag.Id(), newPassword); err != nil {
return block.ProcessBlockedError(err, block.BlockChange)
}
if c.accountDetails == nil {
ctx.Infof("Password for %q has been changed.", c.User)
} else {
if c.accountDetails.Password != "" {
// Log back in with macaroon authentication, so we can
// discard the password without having to log back in
// immediately.
if err := c.recordMacaroon(newPassword); err != nil {
return errors.Annotate(err, "recording macaroon")
}
// Wipe the password from disk. In the event of an
// error occurring after SetPassword and before the
// account details being updated, the user will be
// able to recover by running "juju login".
c.accountDetails.Password = ""
if err := c.ClientStore().UpdateAccount(c.controllerName, *c.accountDetails); err != nil {
return errors.Annotate(err, "failed to update client credentials")
}
}
ctx.Infof("Your password has been changed.")
}
return nil
}
func (c *changePasswordCommand) recordMacaroon(password string) error {
accountDetails := &jujuclient.AccountDetails{User: c.accountDetails.User}
args, err := c.NewAPIConnectionParams(
c.ClientStore(), c.controllerName, "", accountDetails,
)
if err != nil {
return errors.Trace(err)
}
args.DialOpts.BakeryClient.InteractionMethods = []httpbakery.Interactor{
authentication.NewInteractor(accountDetails.User, func(string) (string, error) {
return password, nil
}),
httpbakery.WebBrowserInteractor{},
}
api, err := c.newAPIConnection(args)
if err != nil {
return errors.Annotate(err, "connecting to API")
}
return api.Close()
}
func readAndConfirmPassword(ctx *cmd.Context) (string, error) {
// Don't add the carriage returns before readPassword, but add
// them directly after the readPassword so any errors are output
// on their own lines.
//
// TODO(axw) retry/loop on failure
fmt.Fprint(ctx.Stderr, "new password: ")
password, err := readPassword(ctx.Stdin)
fmt.Fprint(ctx.Stderr, "\n")
if err != nil {
return "", errors.Trace(err)
}
if password == "" {
return "", errors.Errorf("you must enter a password")
}
fmt.Fprint(ctx.Stderr, "type new password again: ")
verify, err := readPassword(ctx.Stdin)
fmt.Fprint(ctx.Stderr, "\n")
if err != nil {
return "", errors.Trace(err)
}
if password != verify {
return "", errors.New("Passwords do not match")
}
return password, nil
}
func readPassword(stdin io.Reader) (string, error) {
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 readLine(stdin)
}
func 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])
}