Skip to content

Commit 5485b6e

Browse files
committed
Move enforcement of Users/AnyUser flag to keycache, pass through error
1 parent d97f953 commit 5485b6e

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

core/core.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,6 @@ func Delegate(jsonIn []byte) ([]byte, error) {
403403
}
404404
}
405405

406-
// Ensure a list of Users is given or the AnyUser flag is set
407-
if (s.Users == nil || len(s.Users) == 0) && s.AnyUser == false {
408-
err = errors.New("Must provide a list of Users or set the AnyUser flag to true")
409-
return jsonStatusError(err)
410-
}
411-
412406
// Find password record for user and verify that their password
413407
// matches. If not found then add a new entry for this user.
414408
pr, found := records.GetRecord(s.Name)

keycache/keycache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (cache *Cache) Refresh() {
176176
func (cache *Cache) AddKeyFromRecord(record passvault.PasswordRecord, name, password, slot string, usage *Usage) (err error) {
177177
var current ActiveUser
178178

179+
// Ensure a list of Users is given or the AnyUser flag is set
180+
if (usage.Users == nil || len(usage.Users) == 0) && usage.AnyUser == false {
181+
return errors.New("Must provide a list of Users or set the AnyUser flag to true")
182+
}
179183
cache.Refresh()
180184
current.Usage = *usage
181185

keycache/keycache_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,45 @@ func TestAnyUserNotDefaultBehavior(t *testing.T) {
371371

372372
cache := NewCache()
373373

374+
// Ensure we can't provide a nil list of Users *and* have a false AnyUser flag
374375
duration, _ := time.ParseDuration("1h")
375376
err = cache.AddKeyFromRecord(
376377
pr, "user", "weakpassword", "",
377378
&Usage{
378379
1, []string{"red", "blue"},
379-
nil,
380+
nil, // Set a nil list of users
380381
time.Now().Add(duration),
381382
false, // Set AnyUser flag to false
382383
},
383384
)
385+
if err == nil {
386+
t.Fatalf("Should have seen error with Users=nil and AnyUser=false")
387+
}
384388

389+
// Ensure we can't provide an empty list of Users either
390+
err = cache.AddKeyFromRecord(
391+
pr, "user", "weakpassword", "",
392+
&Usage{
393+
1, []string{"red", "blue"},
394+
[]string{}, // Set an empty list of users
395+
time.Now().Add(duration),
396+
false, // Set AnyUser flag to false
397+
},
398+
)
399+
if err == nil {
400+
t.Fatalf("Should have seen error with Users=[]string{} and AnyUser=false")
401+
}
402+
403+
// Ensure we only the specified user can decrypt when AnyUser is false
404+
err = cache.AddKeyFromRecord(
405+
pr, "user", "weakpassword", "",
406+
&Usage{
407+
1, []string{"red", "blue"},
408+
[]string{"alice"}, // Set a valid list of users
409+
time.Now().Add(duration),
410+
false, // Set AnyUser flag to false
411+
},
412+
)
385413
if err != nil {
386414
t.Fatalf("%v", err)
387415
}
@@ -407,4 +435,16 @@ func TestAnyUserNotDefaultBehavior(t *testing.T) {
407435
if len(cache.UserKeys) != 1 {
408436
t.Fatalf("Error in number of live keys %v", cache.UserKeys)
409437
}
438+
439+
// Sanity check to make sure our user can still decrpyt
440+
_, err = cache.DecryptKey(dummy, "user", "alice", []string{"red"}, pubEncryptedKey)
441+
if err != nil {
442+
t.Fatalf("%v", err)
443+
}
444+
445+
cache.Refresh()
446+
if len(cache.UserKeys) != 0 {
447+
t.Fatalf("Error in number of live keys %v", cache.UserKeys)
448+
}
449+
410450
}

0 commit comments

Comments
 (0)