forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter_kbpki.go
486 lines (448 loc) · 15.9 KB
/
reporter_kbpki.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
// 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 (
"fmt"
"strconv"
"sync"
"time"
"github.com/keybase/client/go/kbfs/idutil"
"github.com/keybase/client/go/kbfs/kbfsmd"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/kbfs/tlfhandle"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
const (
// error param keys
errorParamTlf = "tlf"
errorParamMode = "mode"
errorParamFeature = "feature"
errorParamUsername = "username"
errorParamRekeySelf = "rekeyself"
errorParamUsageBytes = "usageBytes"
errorParamLimitBytes = "limitBytes"
errorParamUsageFiles = "usageFiles"
errorParamLimitFiles = "limitFiles"
errorParamRenameOldFilename = "oldFilename"
errorParamFoldersCreated = "foldersCreated"
errorParamFolderLimit = "folderLimit"
errorParamApplicationExecPath = "applicationExecPath"
// error operation modes
errorModeRead = "read"
errorModeWrite = "write"
// features that aren't ready yet
errorFeatureFileLimit = "2gbFileLimit"
errorFeatureDirLimit = "512kbDirLimit"
)
const connectionStatusConnected keybase1.FSStatusCode = keybase1.FSStatusCode_START
const connectionStatusDisconnected keybase1.FSStatusCode = keybase1.FSStatusCode_ERROR
// noErrorNames are lookup names that should not result in an error
// notification. These should all be reserved or illegal Keybase
// usernames that will never be associated with a real account.
var noErrorNames = map[string]bool{
"objects": true, // git shells
"gemfile": true, // rvm
"Gemfile": true, // rvm
"devfs": true, // lsof? KBFS-823
"_mtn": true, // emacs on Linux
"_MTN": true, // emacs on Linux
"docker-machine": true, // docker shell stuff
"HEAD": true, // git shell
"Keybase.app": true, // some OSX mount thing
"DCIM": true, // looking for digital pic folder
"Thumbs.db": true, // Windows mounts
"config": true, // Windows, possibly 7-Zip?
"m4root": true, // OS X, iMovie?
"BDMV": true, // OS X, iMovie?
"node_modules": true, // Some npm shell configuration
"folder": true, // Dolphin? keybase/client#7304
"avchd": true, // Sony PlayMemories Home, keybase/client#6801
"avchd_bk": true, // Sony PlayMemories Home, keybase/client#6801
"sony": true, // Sony PlayMemories Home, keybase/client#6801
}
// ReporterKBPKI implements the Notify function of the Reporter
// interface in addition to embedding ReporterSimple for error
// tracking. Notify will make RPCs to the keybase daemon.
type ReporterKBPKI struct {
*ReporterSimple
config Config
log logger.Logger
notifyBuffer chan *keybase1.FSNotification
notifyPathBuffer chan string
notifySyncBuffer chan *keybase1.FSPathSyncStatus
canceler func()
lastNotifyPathLock sync.Mutex
lastNotifyPath string
}
// NewReporterKBPKI creates a new ReporterKBPKI.
func NewReporterKBPKI(config Config, maxErrors, bufSize int) *ReporterKBPKI {
r := &ReporterKBPKI{
ReporterSimple: NewReporterSimple(config.Clock(), maxErrors),
config: config,
log: config.MakeLogger(""),
notifyBuffer: make(chan *keybase1.FSNotification, bufSize),
notifyPathBuffer: make(chan string, 1),
notifySyncBuffer: make(chan *keybase1.FSPathSyncStatus, 1),
}
var ctx context.Context
ctx, r.canceler = context.WithCancel(context.Background())
go r.send(ctx)
return r
}
// ReportErr implements the Reporter interface for ReporterKBPKI.
func (r *ReporterKBPKI) ReportErr(ctx context.Context,
tlfName tlf.CanonicalName, t tlf.Type, mode ErrorModeType, err error) {
r.ReporterSimple.ReportErr(ctx, tlfName, t, mode, err)
// Fire off error popups
params := make(map[string]string)
filename := ""
var code keybase1.FSErrorType = -1
switch e := errors.Cause(err).(type) {
case tlfhandle.ReadAccessError:
code = keybase1.FSErrorType_ACCESS_DENIED
params[errorParamMode] = errorModeRead
filename = e.Filename
case tlfhandle.WriteAccessError:
code = keybase1.FSErrorType_ACCESS_DENIED
params[errorParamUsername] = e.User.String()
params[errorParamMode] = errorModeWrite
filename = e.Filename
case WriteUnsupportedError:
code = keybase1.FSErrorType_ACCESS_DENIED
params[errorParamMode] = errorModeWrite
filename = e.Filename
case UnverifiableTlfUpdateError:
code = keybase1.FSErrorType_REVOKED_DATA_DETECTED
case idutil.NoCurrentSessionError:
code = keybase1.FSErrorType_NOT_LOGGED_IN
case NeedSelfRekeyError:
code = keybase1.FSErrorType_REKEY_NEEDED
params[errorParamRekeySelf] = "true"
case NeedOtherRekeyError:
code = keybase1.FSErrorType_REKEY_NEEDED
params[errorParamRekeySelf] = "false"
case kbfsmd.NewMetadataVersionError:
code = keybase1.FSErrorType_OLD_VERSION
err = OutdatedVersionError{}
case kbfsmd.NewMerkleVersionError:
code = keybase1.FSErrorType_OLD_VERSION
err = OutdatedVersionError{}
case NewDataVersionError:
code = keybase1.FSErrorType_OLD_VERSION
err = OutdatedVersionError{}
case OverQuotaWarning:
code = keybase1.FSErrorType_OVER_QUOTA
params[errorParamUsageBytes] = strconv.FormatInt(e.UsageBytes, 10)
params[errorParamLimitBytes] = strconv.FormatInt(e.LimitBytes, 10)
case *ErrDiskLimitTimeout:
if !e.reportable {
return
}
code = keybase1.FSErrorType_DISK_LIMIT_REACHED
params[errorParamUsageBytes] = strconv.FormatInt(e.usageBytes, 10)
params[errorParamLimitBytes] =
strconv.FormatFloat(e.limitBytes, 'f', 0, 64)
params[errorParamUsageFiles] = strconv.FormatInt(e.usageFiles, 10)
params[errorParamLimitFiles] =
strconv.FormatFloat(e.limitFiles, 'f', 0, 64)
case idutil.NoSigChainError:
code = keybase1.FSErrorType_NO_SIG_CHAIN
params[errorParamUsername] = e.User.String()
case kbfsmd.ServerErrorTooManyFoldersCreated:
code = keybase1.FSErrorType_TOO_MANY_FOLDERS
params[errorParamFolderLimit] = strconv.FormatUint(e.Limit, 10)
params[errorParamFoldersCreated] = strconv.FormatUint(e.Created, 10)
case RenameAcrossDirsError:
if len(e.ApplicationExecPath) > 0 {
code = keybase1.FSErrorType_EXDEV_NOT_SUPPORTED
params[errorParamApplicationExecPath] = e.ApplicationExecPath
}
case OfflineArchivedError:
code = keybase1.FSErrorType_OFFLINE_ARCHIVED
case OfflineUnsyncedError:
code = keybase1.FSErrorType_OFFLINE_UNSYNCED
}
if code < 0 && err == context.DeadlineExceeded {
code = keybase1.FSErrorType_TIMEOUT
// Workaround for DESKTOP-2442
filename = string(tlfName)
}
if code >= 0 {
n := errorNotification(err, code, tlfName, t, mode, filename, params)
r.Notify(ctx, n)
}
}
// Notify implements the Reporter interface for ReporterKBPKI.
//
// TODO: might be useful to get the debug tags out of ctx and store
// them in the notifyBuffer as well so that send() can put
// them back in its context.
func (r *ReporterKBPKI) Notify(ctx context.Context, notification *keybase1.FSNotification) {
select {
case r.notifyBuffer <- notification:
default:
r.log.CDebugf(ctx, "ReporterKBPKI: notify buffer full, dropping %+v",
notification)
}
}
func (r *ReporterKBPKI) setLastNotifyPath(p string) (same bool) {
r.lastNotifyPathLock.Lock()
defer r.lastNotifyPathLock.Unlock()
same = p == r.lastNotifyPath
r.lastNotifyPath = p
return same
}
// NotifyPathUpdated implements the Reporter interface for ReporterKBPKI.
//
// TODO: might be useful to get the debug tags out of ctx and store
// them in the notifyPathBuffer as well so that send() can put
// them back in its context.
func (r *ReporterKBPKI) NotifyPathUpdated(ctx context.Context, path string) {
sameAsLast := r.setLastNotifyPath(path)
select {
case r.notifyPathBuffer <- path:
default:
if sameAsLast {
r.log.CDebugf(ctx,
"ReporterKBPKI: notify path buffer full, dropping %s", path)
} else {
// This should be rare; it only happens when user switches from one
// TLF to another, and we happen to have an update from the old TLF
// in the buffer before switching subscribed TLF.
r.log.CDebugf(ctx,
"ReporterKBPKI: notify path buffer full, but path is "+
"different from last one, so send in a goroutine %s", path)
go func() { r.notifyPathBuffer <- path }()
}
}
}
// NotifySyncStatus implements the Reporter interface for ReporterKBPKI.
//
// TODO: might be useful to get the debug tags out of ctx and store
// them in the notifyBuffer as well so that send() can put
// them back in its context.
func (r *ReporterKBPKI) NotifySyncStatus(ctx context.Context,
status *keybase1.FSPathSyncStatus) {
select {
case r.notifySyncBuffer <- status:
default:
r.log.CDebugf(ctx, "ReporterKBPKI: notify sync buffer full, "+
"dropping %+v", status)
}
}
// Shutdown implements the Reporter interface for ReporterKBPKI.
func (r *ReporterKBPKI) Shutdown() {
r.canceler()
close(r.notifyBuffer)
close(r.notifySyncBuffer)
}
const reporterSendInterval = time.Second
// send takes notifications out of notifyBuffer, notifyPathBuffer, and
// notifySyncBuffer and sends them to the keybase daemon.
func (r *ReporterKBPKI) send(ctx context.Context) {
sendTicker := time.NewTicker(reporterSendInterval)
defer sendTicker.Stop()
for {
select {
case notification, ok := <-r.notifyBuffer:
if !ok {
return
}
nt := notification.NotificationType
st := notification.StatusCode
// Only these notifications are used in frontend:
// https://github.com/keybase/client/blob/0d63795105f64289ba4ef20fbefe56aad91bc7e9/shared/util/kbfs-notifications.js#L142-L154
if nt != keybase1.FSNotificationType_REKEYING &&
nt != keybase1.FSNotificationType_INITIALIZED &&
nt != keybase1.FSNotificationType_CONNECTION &&
st != keybase1.FSStatusCode_ERROR {
continue
}
// Send them right away rather than staging it and waiting for the
// ticker, since each of them can be distinct from each other.
if err := r.config.KeybaseService().Notify(ctx,
notification); err != nil {
r.log.CDebugf(ctx, "ReporterDaemon: error sending "+
"notification: %s", err)
}
case <-sendTicker.C:
select {
case path, ok := <-r.notifyPathBuffer:
if !ok {
return
}
if err := r.config.KeybaseService().NotifyPathUpdated(
ctx, path); err != nil {
r.log.CDebugf(ctx, "ReporterDaemon: error sending "+
"notification for path: %s", err)
}
default:
}
select {
case status, ok := <-r.notifySyncBuffer:
if !ok {
return
}
if err := r.config.KeybaseService().NotifySyncStatus(ctx,
status); err != nil {
r.log.CDebugf(ctx, "ReporterDaemon: error sending "+
"sync status: %s", err)
}
default:
}
case <-ctx.Done():
return
}
}
}
// writeNotification creates FSNotifications from paths for file
// write events.
func writeNotification(file path, finish bool) *keybase1.FSNotification {
n := baseNotification(file, finish)
if file.Tlf.Type() == tlf.Public {
n.NotificationType = keybase1.FSNotificationType_SIGNING
} else {
n.NotificationType = keybase1.FSNotificationType_ENCRYPTING
}
return n
}
// readNotification creates FSNotifications from paths for file
// read events.
func readNotification(file path, finish bool) *keybase1.FSNotification {
n := baseNotification(file, finish)
if file.Tlf.Type() == tlf.Public {
n.NotificationType = keybase1.FSNotificationType_VERIFYING
} else {
n.NotificationType = keybase1.FSNotificationType_DECRYPTING
}
return n
}
// rekeyNotification creates FSNotifications from TlfHandles for rekey
// events.
func rekeyNotification(ctx context.Context, config Config, handle *tlfhandle.Handle, finish bool) *keybase1.FSNotification {
code := keybase1.FSStatusCode_START
if finish {
code = keybase1.FSStatusCode_FINISH
}
return &keybase1.FSNotification{
FolderType: handle.Type().FolderType(),
Filename: string(handle.GetCanonicalPath()),
StatusCode: code,
NotificationType: keybase1.FSNotificationType_REKEYING,
}
}
func baseFileEditNotification(file path, writer keybase1.UID,
localTime time.Time) *keybase1.FSNotification {
n := baseNotification(file, true)
n.WriterUid = writer
n.LocalTime = keybase1.ToTime(localTime)
return n
}
// fileCreateNotification creates FSNotifications from paths for file
// create events.
func fileCreateNotification(file path, writer keybase1.UID,
localTime time.Time) *keybase1.FSNotification {
n := baseFileEditNotification(file, writer, localTime)
n.NotificationType = keybase1.FSNotificationType_FILE_CREATED
return n
}
// fileModifyNotification creates FSNotifications from paths for file
// modification events.
func fileModifyNotification(file path, writer keybase1.UID,
localTime time.Time) *keybase1.FSNotification {
n := baseFileEditNotification(file, writer, localTime)
n.NotificationType = keybase1.FSNotificationType_FILE_MODIFIED
return n
}
// fileDeleteNotification creates FSNotifications from paths for file
// delete events.
func fileDeleteNotification(file path, writer keybase1.UID,
localTime time.Time) *keybase1.FSNotification {
n := baseFileEditNotification(file, writer, localTime)
n.NotificationType = keybase1.FSNotificationType_FILE_DELETED
return n
}
// fileRenameNotification creates FSNotifications from paths for file
// rename events.
func fileRenameNotification(oldFile path, newFile path, writer keybase1.UID,
localTime time.Time) *keybase1.FSNotification {
n := baseFileEditNotification(newFile, writer, localTime)
n.NotificationType = keybase1.FSNotificationType_FILE_RENAMED
n.Params = map[string]string{errorParamRenameOldFilename: oldFile.CanonicalPathString()}
return n
}
// connectionNotification creates FSNotifications based on whether
// or not KBFS is online.
func connectionNotification(status keybase1.FSStatusCode) *keybase1.FSNotification {
// TODO finish placeholder
return &keybase1.FSNotification{
NotificationType: keybase1.FSNotificationType_CONNECTION,
StatusCode: status,
}
}
// baseNotification creates a basic FSNotification without a
// NotificationType from a path.
func baseNotification(file path, finish bool) *keybase1.FSNotification {
code := keybase1.FSStatusCode_START
if finish {
code = keybase1.FSStatusCode_FINISH
}
return &keybase1.FSNotification{
Filename: file.CanonicalPathString(),
StatusCode: code,
}
}
// errorNotification creates FSNotifications for errors.
func errorNotification(err error, errType keybase1.FSErrorType,
tlfName tlf.CanonicalName, t tlf.Type, mode ErrorModeType,
filename string, params map[string]string) *keybase1.FSNotification {
if tlfName != "" {
params[errorParamTlf] = string(tlfName)
}
var nType keybase1.FSNotificationType
switch mode {
case ReadMode:
params[errorParamMode] = errorModeRead
if t == tlf.Public {
nType = keybase1.FSNotificationType_VERIFYING
} else {
nType = keybase1.FSNotificationType_DECRYPTING
}
case WriteMode:
params[errorParamMode] = errorModeWrite
if t == tlf.Public {
nType = keybase1.FSNotificationType_SIGNING
} else {
nType = keybase1.FSNotificationType_ENCRYPTING
}
default:
panic(fmt.Sprintf("Unknown mode: %v", mode))
}
return &keybase1.FSNotification{
FolderType: t.FolderType(),
Filename: filename,
StatusCode: keybase1.FSStatusCode_ERROR,
Status: err.Error(),
ErrorType: errType,
Params: params,
NotificationType: nType,
}
}
func mdReadSuccessNotification(handle *tlfhandle.Handle,
public bool) *keybase1.FSNotification {
params := make(map[string]string)
if handle != nil {
params[errorParamTlf] = string(handle.GetCanonicalName())
}
return &keybase1.FSNotification{
FolderType: handle.Type().FolderType(),
Filename: string(handle.GetCanonicalPath()),
StatusCode: keybase1.FSStatusCode_START,
NotificationType: keybase1.FSNotificationType_MD_READ_SUCCESS,
Params: params,
}
}