Skip to content

Commit 4093134

Browse files
author
Tyler J
committed
Add delegation option for any user to be able to decrypt
core: Add AnyUser field to DelegateRequest and pass to cache calls keycache: Add AnyUser parameter to AddKeyFromRecord function signature keycache_test: Add tests for AnyUser and update AddKeyFromRecord calls cryptor: Update tests to AddKeyFromRecord to reflect API update cmd/ro: Add bool flag for anyUser parameter
1 parent 577d957 commit 4093134

File tree

5 files changed

+78
-20
lines changed

5 files changed

+78
-20
lines changed

cmd/ro/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var owners, lefters, righters, inPath, labels, outPath, outEnv string
2323

2424
var uses, minUsers int
2525

26+
var anyUser bool
27+
2628
var duration, users string
2729

2830
var pollInterval time.Duration
@@ -52,6 +54,7 @@ func registerFlags() {
5254
flag.StringVar(&users, "users", "", "comma separated user list")
5355
flag.IntVar(&uses, "uses", 0, "number of delegated key uses")
5456
flag.IntVar(&minUsers, "minUsers", 2, "minimum number of delegations")
57+
flag.BoolVar(&anyUser, "anyUser", false, "whether any user can decrypt")
5558
flag.StringVar(&duration, "time", "0h", "duration of delegated key uses")
5659
flag.StringVar(&lefters, "left", "", "comma separated left owners")
5760
flag.StringVar(&righters, "right", "", "comma separated right owners")
@@ -110,6 +113,7 @@ func runDelegate() {
110113
Time: duration,
111114
Users: processCSL(users),
112115
Labels: processCSL(labels),
116+
AnyUser: anyUser,
113117
}
114118
resp, err := roServer.Delegate(req)
115119
processError(err)

core/core.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ type DelegateRequest struct {
5050
Name string
5151
Password string
5252

53-
Uses int
54-
Time string
55-
Slot string
56-
Users []string
57-
Labels []string
53+
Uses int
54+
Time string
55+
Slot string
56+
Users []string
57+
Labels []string
58+
AnyUser bool
5859
}
5960

6061
type CreateUserRequest struct {
@@ -396,7 +397,7 @@ func Delegate(jsonIn []byte) ([]byte, error) {
396397
}
397398

398399
// add signed-in record to active set
399-
if err = cache.AddKeyFromRecord(pr, s.Name, s.Password, s.Users, s.Labels, s.Uses, s.Slot, s.Time); err != nil {
400+
if err = cache.AddKeyFromRecord(pr, s.Name, s.Password, s.Users, s.Labels, s.Uses, s.Slot, s.Time, s.AnyUser); err != nil {
400401
return jsonStatusError(err)
401402
}
402403

cryptor/cryptor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestDuplicates(t *testing.T) {
107107

108108
// Delegate one key at a time and check that decryption fails.
109109
for name, pr := range recs {
110-
err = cache.AddKeyFromRecord(pr, name, "weakpassword", nil, nil, 2, "", "1h")
110+
err = cache.AddKeyFromRecord(pr, name, "weakpassword", nil, nil, 2, "", "1h", false)
111111
if err != nil {
112112
t.Fatalf("%v", err)
113113
}

keycache/keycache.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ type DelegateIndex struct {
3131

3232
// Usage holds the permissions of a delegated permission
3333
type Usage struct {
34-
Uses int // Number of uses delegated
35-
Labels []string // File labels allowed to decrypt
36-
Users []string // Set of users allows to decrypt
37-
Expiry time.Time // Expiration of usage
34+
Uses int // Number of uses delegated
35+
Labels []string // File labels allowed to decrypt
36+
Users []string // Set of users allows to decrypt
37+
Expiry time.Time // Expiration of usage
38+
AnyUser bool // True if any user is permitted, false otherwise
3839
}
3940

4041
// ActiveUser holds the information about an actively delegated key.
@@ -72,15 +73,19 @@ func (usage Usage) matchesLabel(labels []string) bool {
7273
}
7374

7475
// matches returns true if this usage applies the user and label
75-
// an empty array of Users indicates that all users are valid
76+
// also returns true if the usage is marked with AnyUser set to true
77+
// DEPRECATED: an empty array of Users indicates that all users are valid
7678
func (usage Usage) matches(user string, labels []string) bool {
7779
if !usage.matchesLabel(labels) {
7880
return false
7981
}
80-
// if usage lists no users, always match
82+
// DEPRECATED: if usage lists no users, always match
8183
if len(usage.Users) == 0 {
8284
return true
8385
}
86+
if usage.AnyUser {
87+
return true
88+
}
8489
for _, validUser := range usage.Users {
8590
if user == validUser {
8691
return true
@@ -173,7 +178,7 @@ func (cache *Cache) Refresh() {
173178
}
174179

175180
// AddKeyFromRecord decrypts a key for a given record and adds it to the cache.
176-
func (cache *Cache) AddKeyFromRecord(record passvault.PasswordRecord, name, password string, users, labels []string, uses int, slot, durationString string) (err error) {
181+
func (cache *Cache) AddKeyFromRecord(record passvault.PasswordRecord, name, password string, users, labels []string, uses int, slot, durationString string, anyUser bool) (err error) {
177182
var current ActiveUser
178183

179184
cache.Refresh()
@@ -187,6 +192,7 @@ func (cache *Cache) AddKeyFromRecord(record passvault.PasswordRecord, name, pass
187192
current.Usage.Expiry = time.Now().Add(duration)
188193
current.Usage.Users = users
189194
current.Usage.Labels = labels
195+
current.Usage.AnyUser = anyUser
190196

191197
// get decryption keys
192198
switch record.Type {

keycache/keycache_test.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestUsesFlush(t *testing.T) {
2727
// Initialize keycache and delegate the user's key to it.
2828
cache := NewCache()
2929

30-
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, nil, 2, "", "1h")
30+
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, nil, 2, "", "1h", false)
3131
if err != nil {
3232
t.Fatalf("%v", err)
3333
}
@@ -90,7 +90,7 @@ func TestTimeFlush(t *testing.T) {
9090

9191
cache := NewCache()
9292

93-
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, nil, 10, "", "1s")
93+
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, nil, 10, "", "1s", false)
9494
if err != nil {
9595
t.Fatalf("%v", err)
9696
}
@@ -129,7 +129,7 @@ func TestGoodLabel(t *testing.T) {
129129

130130
cache := NewCache()
131131

132-
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, []string{"red"}, 1, "", "1h")
132+
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, []string{"red"}, 1, "", "1h", false)
133133
if err != nil {
134134
t.Fatalf("%v", err)
135135
}
@@ -171,7 +171,7 @@ func TestBadLabel(t *testing.T) {
171171

172172
cache := NewCache()
173173

174-
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, []string{"red"}, 1, "", "1h")
174+
err = cache.AddKeyFromRecord(pr, "user", "weakpassword", nil, []string{"red"}, 1, "", "1h", false)
175175
if err != nil {
176176
t.Fatalf("%v", err)
177177
}
@@ -217,7 +217,7 @@ func TestGoodUser(t *testing.T) {
217217
pr, "user", "weakpassword",
218218
[]string{"ci", "buildeng", "user"},
219219
[]string{"red", "blue"},
220-
1, "", "1h",
220+
1, "", "1h", false,
221221
)
222222
if err != nil {
223223
t.Fatalf("%v", err)
@@ -264,7 +264,7 @@ func TestBadUser(t *testing.T) {
264264
pr, "user", "weakpassword",
265265
[]string{"ci", "buildeng", "user"},
266266
[]string{"red", "blue"},
267-
1, "", "1h",
267+
1, "", "1h", false,
268268
)
269269
if err != nil {
270270
t.Fatalf("%v", err)
@@ -291,3 +291,50 @@ func TestBadUser(t *testing.T) {
291291
t.Fatalf("Error in number of live keys %v", cache.UserKeys)
292292
}
293293
}
294+
295+
func TestAnyUser(t *testing.T) {
296+
// Initialize passvault and keycache. Delegate a key with tag and user
297+
// restrictions and verify that permissible decryption is allowed.
298+
records, err := passvault.InitFrom("memory")
299+
if err != nil {
300+
t.Fatalf("%v", err)
301+
}
302+
303+
pr, err := records.AddNewRecord("user", "weakpassword", true, passvault.DefaultRecordType)
304+
if err != nil {
305+
t.Fatalf("%v", err)
306+
}
307+
308+
cache := NewCache()
309+
310+
err = cache.AddKeyFromRecord(
311+
pr, "user", "weakpassword",
312+
nil,
313+
[]string{"red", "blue"},
314+
1, "", "1h", true,
315+
)
316+
if err != nil {
317+
t.Fatalf("%v", err)
318+
}
319+
320+
cache.Refresh()
321+
if len(cache.UserKeys) != 1 {
322+
t.Fatalf("Error in number of live keys")
323+
}
324+
325+
dummy := make([]byte, 16)
326+
pubEncryptedKey, err := pr.EncryptKey(dummy)
327+
if err != nil {
328+
t.Fatalf("%v", err)
329+
}
330+
331+
_, err = cache.DecryptKey(dummy, "user", "anybody", []string{"red"}, pubEncryptedKey)
332+
if err != nil {
333+
t.Fatalf("%v", err)
334+
}
335+
336+
cache.Refresh()
337+
if len(cache.UserKeys) != 0 {
338+
t.Fatalf("Error in number of live keys %v", cache.UserKeys)
339+
}
340+
}

0 commit comments

Comments
 (0)