forked from juju/juju
/
user.go
587 lines (522 loc) · 16.6 KB
/
user.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
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// NOTE: the users that are being stored in the database here are only
// the local users, like "admin" or "bob". In the world
// where we have external user providers hooked up, there are no records
// in the database for users that are authenticated elsewhere.
package state
import (
"crypto/rand"
"fmt"
"sort"
"strings"
"time"
"github.com/juju/errors"
"github.com/juju/mgo/v2"
"github.com/juju/mgo/v2/bson"
"github.com/juju/mgo/v2/txn"
"github.com/juju/names/v4"
"github.com/juju/utils/v2"
"github.com/DavinZhang/juju/core/permission"
)
const userGlobalKeyPrefix = "us"
func userGlobalKey(userID string) string {
return fmt.Sprintf("%s#%s", userGlobalKeyPrefix, userID)
}
func userIDFromGlobalKey(key string) string {
prefix := userGlobalKeyPrefix + "#"
return strings.TrimPrefix(key, prefix)
}
func (st *State) checkUserExists(name string) (bool, error) {
lowercaseName := strings.ToLower(name)
users, closer := st.db().GetCollection(usersC)
defer closer()
var count int
var err error
if count, err = users.FindId(lowercaseName).Count(); err != nil {
return false, err
}
return count > 0, nil
}
// AddUser adds a user to the database.
func (st *State) AddUser(name, displayName, password, creator string) (*User, error) {
return st.addUser(name, displayName, password, creator, nil)
}
// AddUserWithSecretKey adds the user with the specified name, and assigns it
// a randomly generated secret key. This secret key may be used for the user
// and controller to mutually authenticate one another, without without relying
// on TLS certificates.
//
// The new user will not have a password. A password must be set, clearing the
// secret key in the process, before the user can login normally.
func (st *State) AddUserWithSecretKey(name, displayName, creator string) (*User, error) {
secretKey, err := generateSecretKey()
if err != nil {
return nil, errors.Trace(err)
}
return st.addUser(name, displayName, "", creator, secretKey)
}
func (st *State) addUser(name, displayName, password, creator string, secretKey []byte) (*User, error) {
if !names.IsValidUserName(name) {
return nil, errors.Errorf("invalid user name %q", name)
}
lowercaseName := strings.ToLower(name)
dateCreated := st.nowToTheSecond()
user := &User{
st: st,
doc: userDoc{
DocID: lowercaseName,
Name: name,
DisplayName: displayName,
SecretKey: secretKey,
CreatedBy: creator,
DateCreated: dateCreated,
},
}
if password != "" {
salt, err := utils.RandomSalt()
if err != nil {
return nil, err
}
user.doc.PasswordHash = utils.UserPasswordHash(password, salt)
user.doc.PasswordSalt = salt
}
ops := []txn.Op{{
C: usersC,
Id: lowercaseName,
Assert: txn.DocMissing,
Insert: &user.doc,
}}
controllerUserOps := createControllerUserOps(st.ControllerUUID(),
names.NewUserTag(name),
names.NewUserTag(creator),
displayName,
dateCreated,
defaultControllerPermission)
ops = append(ops, controllerUserOps...)
err := st.db().RunTransaction(ops)
if err == txn.ErrAborted {
err = errors.Errorf("username unavailable")
}
if err != nil {
if errors.IsAlreadyExists(err) {
err = errors.Errorf("username unavailable")
}
return nil, errors.Trace(err)
}
return user, nil
}
// RemoveUser marks the user as deleted. This obviates the ability of a user
// to function, but keeps the userDoc retaining provenance, i.e. auditing.
func (st *State) RemoveUser(tag names.UserTag) error {
lowercaseName := strings.ToLower(tag.Name())
u, err := st.User(tag)
if err != nil {
return errors.Trace(err)
}
if u.IsDeleted() {
return nil
}
buildTxn := func(attempt int) ([]txn.Op, error) {
if attempt > 0 {
// If it is not our first attempt, refresh the user.
if err := u.Refresh(); err != nil {
return nil, errors.Trace(err)
}
}
ops := []txn.Op{{
Id: lowercaseName,
C: usersC,
Assert: txn.DocExists,
Update: bson.M{"$set": bson.M{"deleted": true}},
}}
return ops, nil
}
return st.db().Run(buildTxn)
}
func createInitialUserOps(controllerUUID string, user names.UserTag, password, salt string, dateCreated time.Time) []txn.Op {
lowercaseName := strings.ToLower(user.Name())
doc := userDoc{
DocID: lowercaseName,
Name: user.Name(),
DisplayName: user.Name(),
PasswordHash: utils.UserPasswordHash(password, salt),
PasswordSalt: salt,
CreatedBy: user.Name(),
DateCreated: dateCreated,
}
ops := []txn.Op{{
C: usersC,
Id: lowercaseName,
Assert: txn.DocMissing,
Insert: &doc,
}}
controllerUserOps := createControllerUserOps(controllerUUID,
names.NewUserTag(user.Name()),
names.NewUserTag(user.Name()),
user.Name(),
dateCreated,
// first user is controller admin.
permission.SuperuserAccess)
ops = append(ops, controllerUserOps...)
return ops
}
// getUser fetches information about the user with the
// given name into the provided userDoc.
func (st *State) getUser(name string, udoc *userDoc) error {
users, closer := st.db().GetCollection(usersC)
defer closer()
name = strings.ToLower(name)
err := users.Find(bson.D{{"_id", name}}).One(udoc)
if err == mgo.ErrNotFound {
err = errors.NotFoundf("user %q", name)
}
// DateCreated is inserted as UTC, but read out as local time. So we
// convert it back to UTC here.
udoc.DateCreated = udoc.DateCreated.UTC()
return err
}
// User returns the state User for the given name.
func (st *State) User(tag names.UserTag) (*User, error) {
if !tag.IsLocal() {
return nil, errors.NotFoundf("user %q", tag.Id())
}
user := &User{st: st}
if err := st.getUser(tag.Name(), &user.doc); err != nil {
return nil, errors.Trace(err)
}
if user.doc.Deleted {
// This error is returned to the apiserver and from there to the api
// client. So we don't annotate with information regarding deletion.
// TODO(redir): We'll return a deletedUserError in the future so we can
// return more appropriate errors, e.g. username not available.
return nil, newDeletedUserError(user.Name())
}
return user, nil
}
// AllUsers returns a slice of state.User. This includes all active users. If
// includeDeactivated is true it also returns inactive users. At this point it
// never returns deleted users.
func (st *State) AllUsers(includeDeactivated bool) ([]*User, error) {
var result []*User
users, closer := st.db().GetCollection(usersC)
defer closer()
var query bson.D
// TODO(redir): Provide option to retrieve deleted users in future PR.
// e.g. if !includeDelted.
// Ensure the query checks for users without the deleted attribute, and
// also that if it does that the value is not true. fwereade wanted to be
// sure we cannot miss users that previously existed without the deleted
// attr. Since this will only be in 2.0 that should never happen, but...
// belt and suspenders.
query = append(query, bson.DocElem{
"deleted", bson.D{{"$ne", true}},
})
// As above, in the case that a user previously existed and doesn't have a
// deactivated attribute, we make sure the query checks for the existence
// of the attribute, and if it exists that it is not true.
if !includeDeactivated {
query = append(query, bson.DocElem{
"deactivated", bson.D{{"$ne", true}},
})
}
iter := users.Find(query).Iter()
defer iter.Close()
var doc userDoc
for iter.Next(&doc) {
result = append(result, &User{st: st, doc: doc})
}
if err := iter.Close(); err != nil {
return nil, errors.Trace(err)
}
// Always return a predictable order, sort by Name.
sort.Sort(userList(result))
return result, nil
}
// User represents a local user in the database.
type User struct {
st *State
doc userDoc
lastLoginDoc userLastLoginDoc
}
type userDoc struct {
DocID string `bson:"_id"`
Name string `bson:"name"`
DisplayName string `bson:"displayname"`
Deactivated bool `bson:"deactivated,omitempty"`
Deleted bool `bson:"deleted,omitempty"` // Deleted users are marked deleted but not removed.
SecretKey []byte `bson:"secretkey,omitempty"`
PasswordHash string `bson:"passwordhash"`
PasswordSalt string `bson:"passwordsalt"`
CreatedBy string `bson:"createdby"`
DateCreated time.Time `bson:"datecreated"`
}
type userLastLoginDoc struct {
DocID string `bson:"_id"`
ModelUUID string `bson:"model-uuid"`
// LastLogin is updated by the apiserver whenever the user
// connects over the API. This update is not done using mgo.txn
// so this value could well change underneath a normal transaction
// and as such, it should NEVER appear in any transaction asserts.
// It is really informational only as far as everyone except the
// api server is concerned.
LastLogin time.Time `bson:"last-login"`
}
// String returns "<name>" where <name> is the Name of the user.
func (u *User) String() string {
return u.UserTag().Id()
}
// Name returns the User name.
func (u *User) Name() string {
return u.doc.Name
}
// DisplayName returns the display name of the User.
func (u *User) DisplayName() string {
return u.doc.DisplayName
}
// CreatedBy returns the name of the User that created this User.
func (u *User) CreatedBy() string {
return u.doc.CreatedBy
}
// DateCreated returns when this User was created in UTC.
func (u *User) DateCreated() time.Time {
return u.doc.DateCreated.UTC()
}
// Tag returns the Tag for the User.
func (u *User) Tag() names.Tag {
return u.UserTag()
}
// UserTag returns the Tag for the User.
func (u *User) UserTag() names.UserTag {
name := u.doc.Name
return names.NewLocalUserTag(name)
}
// LastLogin returns when this User last connected through the API in UTC.
// The resulting time will be nil if the user has never logged in. In the
// normal case, the LastLogin is the last time that the user connected through
// the API server.
func (u *User) LastLogin() (time.Time, error) {
lastLogins, closer := u.st.db().GetRawCollection(userLastLoginC)
defer closer()
var lastLogin userLastLoginDoc
err := lastLogins.FindId(u.doc.DocID).Select(bson.D{{"last-login", 1}}).One(&lastLogin)
if err != nil {
if err == mgo.ErrNotFound {
err = errors.Wrap(err, newNeverLoggedInError(u.UserTag().Name()))
}
return time.Time{}, errors.Trace(err)
}
return lastLogin.LastLogin.UTC(), nil
}
// UpdateLastLogin sets the LastLogin time of the user to be now (to the
// nearest second).
func (u *User) UpdateLastLogin() (err error) {
if err := u.ensureNotDeleted(); err != nil {
return errors.Annotate(err, "cannot update last login")
}
lastLogins, closer := u.st.db().GetCollection(userLastLoginC)
defer closer()
lastLoginsW := lastLogins.Writeable()
// Update the safe mode of the underlying session to not require
// write majority, nor sync to disk.
session := lastLoginsW.Underlying().Database.Session
session.SetSafe(&mgo.Safe{})
lastLogin := userLastLoginDoc{
DocID: u.doc.DocID,
ModelUUID: u.st.ModelUUID(),
LastLogin: u.st.nowToTheSecond(),
}
_, err = lastLoginsW.UpsertId(lastLogin.DocID, lastLogin)
return errors.Trace(err)
}
// SecretKey returns the user's secret key, if any.
func (u *User) SecretKey() []byte {
return u.doc.SecretKey
}
// SetPassword sets the password associated with the User.
func (u *User) SetPassword(password string) error {
if err := u.ensureNotDeleted(); err != nil {
return errors.Annotate(err, "cannot set password")
}
salt, err := utils.RandomSalt()
if err != nil {
return err
}
return u.SetPasswordHash(utils.UserPasswordHash(password, salt), salt)
}
// SetPasswordHash stores the hash and the salt of the
// password. If the User has a secret key set then it
// will be cleared.
func (u *User) SetPasswordHash(pwHash string, pwSalt string) error {
if err := u.ensureNotDeleted(); err != nil {
// If we do get a late set of the password this is fine b/c we have an
// explicit check before login.
return errors.Annotate(err, "cannot set password hash")
}
update := bson.D{{"$set", bson.D{
{"passwordhash", pwHash},
{"passwordsalt", pwSalt},
}}}
if u.doc.SecretKey != nil {
update = append(update,
bson.DocElem{"$unset", bson.D{{"secretkey", ""}}},
)
}
lowercaseName := strings.ToLower(u.Name())
ops := []txn.Op{{
C: usersC,
Id: lowercaseName,
Assert: txn.DocExists,
Update: update,
}}
if err := u.st.db().RunTransaction(ops); err != nil {
return errors.Annotatef(err, "cannot set password of user %q", u.Name())
}
u.doc.PasswordHash = pwHash
u.doc.PasswordSalt = pwSalt
u.doc.SecretKey = nil
return nil
}
// PasswordValid returns whether the given password is valid for the User. The
// caller should call user.Refresh before calling this.
func (u *User) PasswordValid(password string) bool {
// If the User is deactivated or deleted, there is no point in carrying on.
// Since any authentication checks are done very soon after the user is
// read from the database, there is a very small timeframe where a user
// could be disabled after it has been read but prior to being checked, but
// in practice, this isn't a problem.
if u.IsDisabled() || u.IsDeleted() {
return false
}
if u.doc.PasswordSalt != "" {
return utils.UserPasswordHash(password, u.doc.PasswordSalt) == u.doc.PasswordHash
}
return false
}
// Refresh refreshes information about the User from the state.
func (u *User) Refresh() error {
var udoc userDoc
if err := u.st.getUser(u.Name(), &udoc); err != nil {
return err
}
u.doc = udoc
return nil
}
// Disable deactivates the user. Disabled identities cannot log in.
func (u *User) Disable() error {
if err := u.ensureNotDeleted(); err != nil {
return errors.Annotate(err, "cannot disable")
}
owner, err := u.st.ControllerOwner()
if err != nil {
return errors.Trace(err)
}
if u.doc.Name == owner.Name() {
return errors.Unauthorizedf("cannot disable controller model owner")
}
return errors.Annotatef(u.setDeactivated(true), "cannot disable user %q", u.Name())
}
// Enable reactivates the user, setting disabled to false.
func (u *User) Enable() error {
if err := u.ensureNotDeleted(); err != nil {
return errors.Annotate(err, "cannot enable")
}
return errors.Annotatef(u.setDeactivated(false), "cannot enable user %q", u.Name())
}
func (u *User) setDeactivated(value bool) error {
lowercaseName := strings.ToLower(u.Name())
ops := []txn.Op{{
C: usersC,
Id: lowercaseName,
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{{"deactivated", value}}}},
}}
if err := u.st.db().RunTransaction(ops); err != nil {
if err == txn.ErrAborted {
err = fmt.Errorf("user no longer exists")
}
return err
}
u.doc.Deactivated = value
return nil
}
// IsDisabled returns whether the user is currently enabled.
func (u *User) IsDisabled() bool {
// Yes, this is a cached value, but in practice the user object is
// never held around for a long time.
return u.doc.Deactivated
}
// IsDeleted returns whether the user is currently deleted.
func (u *User) IsDeleted() bool {
return u.doc.Deleted
}
// ensureNotDeleted refreshes the user to ensure it wasn't deleted since we
// acquired it.
func (u *User) ensureNotDeleted() error {
if err := u.Refresh(); err != nil {
return errors.Trace(err)
}
if u.doc.Deleted {
return newDeletedUserError(u.Name())
}
return nil
}
// ResetPassword clears the user's password (if there is one),
// and generates a new secret key for the user.
// This must be an active user.
func (u *User) ResetPassword() ([]byte, error) {
var key []byte
buildTxn := func(attempt int) ([]txn.Op, error) {
if err := u.ensureNotDeleted(); err != nil {
return nil, errors.Trace(err)
}
if u.IsDisabled() {
return nil, fmt.Errorf("user deactivated")
}
var err error
key, err = generateSecretKey()
if err != nil {
return nil, errors.Trace(err)
}
update := bson.D{
{
"$set", bson.D{
{"secretkey", key},
},
},
{
"$unset", bson.D{
{"passwordhash", ""},
{"passwordsalt", ""},
},
},
}
lowercaseName := strings.ToLower(u.Name())
return []txn.Op{{
C: usersC,
Id: lowercaseName,
Assert: txn.DocExists,
Update: update,
}}, nil
}
if err := u.st.db().Run(buildTxn); err != nil {
return nil, errors.Annotatef(err, "cannot reset password for user %q", u.Name())
}
u.doc.SecretKey = key
u.doc.PasswordHash = ""
u.doc.PasswordSalt = ""
return key, nil
}
// generateSecretKey generates a random, 32-byte secret key.
func generateSecretKey() ([]byte, error) {
var secretKey [32]byte
if _, err := rand.Read(secretKey[:]); err != nil {
return nil, errors.Trace(err)
}
return secretKey[:], nil
}
// userList type is used to provide the methods for sorting.
type userList []*User
func (u userList) Len() int { return len(u) }
func (u userList) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
func (u userList) Less(i, j int) bool { return u[i].Name() < u[j].Name() }