forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentify_util.go
295 lines (258 loc) · 8.35 KB
/
identify_util.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"errors"
"fmt"
"sync"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/kbfs/tlf"
"golang.org/x/net/context"
"golang.org/x/sync/errgroup"
)
type extendedIdentify struct {
behavior keybase1.TLFIdentifyBehavior
// lock guards userBreaks and tlfBreaks
lock sync.Mutex
userBreaks chan keybase1.TLFIdentifyFailure
tlfBreaks *keybase1.TLFBreak
}
func (ei *extendedIdentify) userBreak(username libkb.NormalizedUsername, uid keybase1.UID, breaks *keybase1.IdentifyTrackBreaks) {
if ei.userBreaks == nil {
return
}
ei.userBreaks <- keybase1.TLFIdentifyFailure{
Breaks: breaks,
User: keybase1.User{
Uid: uid,
Username: string(username),
},
}
}
func (ei *extendedIdentify) makeTlfBreaksIfNeeded(
ctx context.Context, numUserInTlf int) error {
if ei.userBreaks == nil {
return nil
}
ei.lock.Lock()
defer ei.lock.Unlock()
b := &keybase1.TLFBreak{}
for i := 0; i < numUserInTlf; i++ {
select {
case ub, ok := <-ei.userBreaks:
if !ok {
return errors.New("makeTlfBreaksIfNeeded called on extendedIdentify" +
" with closed userBreaks channel.")
}
if ub.Breaks != nil {
b.Breaks = append(b.Breaks, ub)
}
case <-ctx.Done():
return ctx.Err()
}
}
ei.tlfBreaks = b
return nil
}
// getTlfBreakOrBust returns a keybase1.TLFBreak. This should only be called
// for behavior.WarningInsteadOfErrorOnBrokenTracks() == true, and after
// makeTlfBreaksIfNeeded is called, to make sure user proof breaks get
// populated in GUI mode.
//
// If called otherwise, we don't panic here anymore, since we can't panic on
// nil ei.tlfBreaks. The reason is if a previous successful identify has
// already happened recently, it could cause this identify to be skipped, which
// means ei.tlfBreaks is never populated. In this case, it's safe to return an
// empty keybase1.TLFBreak.
func (ei *extendedIdentify) getTlfBreakAndClose() keybase1.TLFBreak {
ei.lock.Lock()
defer ei.lock.Unlock()
if ei.userBreaks != nil {
close(ei.userBreaks)
ei.userBreaks = nil
}
if ei.tlfBreaks != nil {
return *ei.tlfBreaks
}
return keybase1.TLFBreak{}
}
// ctxExtendedIdentifyKeyType is a type for the context key for using
// extendedIdentify
type ctxExtendedIdentifyKeyType int
const (
// ctxExtendedIdentifyKeyType is a context key for using extendedIdentify
ctxExtendedIdentifyKey ctxExtendedIdentifyKeyType = iota
)
// ExtendedIdentifyAlreadyExists is returned when makeExtendedIdentify is
// called on a context already with extendedIdentify.
type ExtendedIdentifyAlreadyExists struct{}
func (e ExtendedIdentifyAlreadyExists) Error() string {
return "extendedIdentify already exists"
}
func makeExtendedIdentify(ctx context.Context,
behavior keybase1.TLFIdentifyBehavior) (context.Context, error) {
if _, ok := ctx.Value(ctxExtendedIdentifyKey).(*extendedIdentify); ok {
return nil, ExtendedIdentifyAlreadyExists{}
}
if !behavior.WarningInsteadOfErrorOnBrokenTracks() {
return NewContextReplayable(ctx, func(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxExtendedIdentifyKey, &extendedIdentify{
behavior: behavior,
})
}), nil
}
ch := make(chan keybase1.TLFIdentifyFailure)
return NewContextReplayable(ctx, func(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxExtendedIdentifyKey, &extendedIdentify{
behavior: behavior,
userBreaks: ch,
})
}), nil
}
func getExtendedIdentify(ctx context.Context) (ei *extendedIdentify) {
if ei, ok := ctx.Value(ctxExtendedIdentifyKey).(*extendedIdentify); ok {
return ei
}
return &extendedIdentify{
behavior: keybase1.TLFIdentifyBehavior_DEFAULT_KBFS,
}
}
// identifyUID performs identify based only on UID. It should be
// used only if the username is not known - as e.g. when rekeying.
func identifyUID(ctx context.Context, nug normalizedUsernameGetter,
identifier identifier, id keybase1.UserOrTeamID, t tlf.Type) error {
name, err := nug.GetNormalizedUsername(ctx, id)
if err != nil {
return err
}
return identifyUser(ctx, nug, identifier, name, id, t)
}
// identifyUser is the preferred way to run identifies.
func identifyUser(ctx context.Context, nug normalizedUsernameGetter,
identifier identifier, name libkb.NormalizedUsername,
id keybase1.UserOrTeamID, t tlf.Type) error {
// Check to see if identify should be skipped altogether.
ei := getExtendedIdentify(ctx)
if ei.behavior == keybase1.TLFIdentifyBehavior_CHAT_SKIP {
return nil
}
var reason string
nameAssertion := name.String()
isImplicit := false
switch t {
case tlf.Public:
if id.IsTeam() {
isImplicit = true
}
reason = "You accessed a public folder."
case tlf.Private:
if id.IsTeam() {
isImplicit = true
reason = fmt.Sprintf(
"You accessed a folder for private team %s.", nameAssertion)
} else {
reason = fmt.Sprintf(
"You accessed a private folder with %s.", nameAssertion)
}
case tlf.SingleTeam:
reason = fmt.Sprintf(
"You accessed a folder for private team %s.", nameAssertion)
nameAssertion = "team:" + nameAssertion
}
var resultName libkb.NormalizedUsername
var resultID keybase1.UserOrTeamID
if isImplicit {
assertions, extensionSuffix, err := tlf.SplitExtension(name.String())
if err != nil {
return err
}
iteamInfo, err := identifier.IdentifyImplicitTeam(
ctx, assertions, extensionSuffix, t, reason)
if err != nil {
return err
}
resultName = iteamInfo.Name
resultID = iteamInfo.TID.AsUserOrTeam()
} else {
var err error
resultName, resultID, err =
identifier.Identify(ctx, nameAssertion, reason)
if err != nil {
// Convert libkb.NoSigChainError into one we can report. (See
// KBFS-1252).
if _, ok := err.(libkb.NoSigChainError); ok {
return NoSigChainError{name}
}
return err
}
}
if resultName != name {
return fmt.Errorf("Identify returned name=%s, expected %s",
resultName, name)
}
if resultID != id {
return fmt.Errorf("Identify returned uid=%s, expected %s", resultID, id)
}
return nil
}
// identifyUserToChan calls identifyUser and plugs the result into the error channnel.
func identifyUserToChan(ctx context.Context, nug normalizedUsernameGetter,
identifier identifier, name libkb.NormalizedUsername,
id keybase1.UserOrTeamID, t tlf.Type, errChan chan error) {
errChan <- identifyUser(ctx, nug, identifier, name, id, t)
}
// identifyUsers identifies the users in the given maps.
func identifyUsers(ctx context.Context, nug normalizedUsernameGetter,
identifier identifier,
names map[keybase1.UserOrTeamID]libkb.NormalizedUsername,
t tlf.Type) error {
eg, ctx := errgroup.WithContext(ctx)
// TODO: limit the number of concurrent identifies?
// TODO: implement a version of errgroup with limited concurrency.
for id, name := range names {
// Capture range variables.
id, name := id, name
eg.Go(func() error {
return identifyUser(ctx, nug, identifier, name, id, t)
})
}
return eg.Wait()
}
// identifyUserList identifies the users in the given list.
// Only use this when the usernames are not known - like when rekeying.
func identifyUserList(ctx context.Context, nug normalizedUsernameGetter,
identifier identifier, ids []keybase1.UserOrTeamID, t tlf.Type) error {
eg, ctx := errgroup.WithContext(ctx)
// TODO: limit the number of concurrent identifies?
// TODO: implement concurrency limited version of errgroup.
for _, id := range ids {
// Capture range variable.
id := id
eg.Go(func() error {
return identifyUID(ctx, nug, identifier, id, t)
})
}
return eg.Wait()
}
// identifyUsersForTLF is a helper for identifyHandle for easier testing.
func identifyUsersForTLF(ctx context.Context, nug normalizedUsernameGetter,
identifier identifier,
names map[keybase1.UserOrTeamID]libkb.NormalizedUsername,
t tlf.Type) error {
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
ei := getExtendedIdentify(ctx)
return ei.makeTlfBreaksIfNeeded(ctx, len(names))
})
eg.Go(func() error {
return identifyUsers(ctx, nug, identifier, names, t)
})
return eg.Wait()
}
// identifyHandle identifies the canonical names in the given handle.
func identifyHandle(ctx context.Context, nug normalizedUsernameGetter, identifier identifier, h *TlfHandle) error {
return identifyUsersForTLF(ctx, nug, identifier,
h.ResolvedUsersMap(), h.Type())
}