This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
/
fs.go
514 lines (470 loc) · 14.7 KB
/
fs.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
// Copyright 2015 Square Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"github.com/square/go-sq-metrics"
"github.com/square/keywhiz-fs/log"
"golang.org/x/sys/unix"
)
const (
fsVersion = "2.0"
fuseEISDIR = fuse.Status(unix.EISDIR)
)
// Initialized via ldflags
var (
buildRevision = "unknown"
buildTime = "0"
buildMachine = "unknown"
)
// StatusInfo contains debug info accessible via `.json/status`.
type StatusInfo struct {
BuildRevision string `json:"build_revision"`
BuildMachine string `json:"build_machine"`
BuildTime time.Time `json:"build_time"`
StartTime time.Time `json:"start_time"`
RuntimeVersion string `json:"runtime_version"`
ServerURL string `json:"server_url"`
ClientParams httpClientParams `json:"client_params"`
}
// KeywhizFs is the central struct for dispatching filesystem operations.
type KeywhizFs struct {
pathfs.FileSystem
*log.Logger
Client *Client
Cache *Cache
Metrics *sqmetrics.SquareMetrics
StartTime time.Time
Ownership Ownership
Timeout time.Duration
}
// prettyContext pretty-prints a FUSE context for log output.
func prettyContext(context *fuse.Context) string {
if context == nil {
return "nil"
}
return fmt.Sprintf("Context{Uid: %d, Gid: %d, Pid: %d}", context.Uid, context.Gid, context.Pid)
}
func (kwfs KeywhizFs) statusJSON() []byte {
// Convert buildTime (seconds since epoch) into an actual time.Time object,
// makes for nicer JSON marshalling (and matches mount time format).
seconds, err := strconv.ParseInt(buildTime, 10, 64)
panicOnError(err)
status, err := json.Marshal(
StatusInfo{
BuildRevision: buildRevision,
BuildMachine: buildMachine,
BuildTime: time.Unix(seconds, 0),
StartTime: kwfs.StartTime,
RuntimeVersion: runtime.Version(),
ServerURL: kwfs.Client.url.String(),
ClientParams: kwfs.Client.params,
})
panicOnError(err)
return status
}
func (kwfs KeywhizFs) metricsJSON() []byte {
if kwfs.Metrics != nil {
metrics := kwfs.Metrics.SerializeMetrics()
data, err := json.Marshal(metrics)
if err == nil {
return data
}
kwfs.Warnf("Error serializing metrics: %v", err)
}
return []byte{}
}
func (kwfs KeywhizFs) profile(name string) []byte {
var b bytes.Buffer
// Set "1" to enable human-readable debug output
err := pprof.Lookup(name).WriteTo(&b, 1)
if err != nil {
kwfs.Warnf("Error writing profile: %v", err)
}
return b.Bytes()
}
// NewKeywhizFs readies a KeywhizFs struct and its parent filesystem objects.
func NewKeywhizFs(client *Client, ownership Ownership, timeouts Timeouts, metrics *sqmetrics.SquareMetrics, logConfig log.Config) (kwfs *KeywhizFs, root nodefs.Node, err error) {
logger := log.New("kwfs", logConfig)
cache := NewCache(client, timeouts, logConfig, nil)
defaultfs := pathfs.NewDefaultFileSystem() // Returns ENOSYS by default
readonlyfs := pathfs.NewReadonlyFileSystem(defaultfs) // R/W calls return EPERM
kwfs = &KeywhizFs{readonlyfs, logger, client, cache, metrics, time.Now(), ownership, 2 * timeouts.MaxWait}
nfs := pathfs.NewPathNodeFs(kwfs, nil)
nfs.SetDebug(logConfig.Debug)
return kwfs, nfs.Root(), nil
}
// GetAttr is a FUSE function which tells FUSE which files and directories exist.
//
// name is empty when getting information on the base directory
func (kwfs KeywhizFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
ret := make(chan struct {
*fuse.Attr
fuse.Status
}, 1)
go func() {
defer close(ret)
attr, status := kwfs.getAttr(name, context)
ret <- struct {
*fuse.Attr
fuse.Status
}{attr, status}
}()
select {
case out := <-ret:
return out.Attr, out.Status
case <-time.After(kwfs.Timeout):
kwfs.Errorf("Operation timed out: GetAttr(\"%s\", %s)", name, prettyContext(context))
kwfs.logGoroutines()
return nil, fuse.EIO
}
}
func (kwfs KeywhizFs) getAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
kwfs.Debugf("GetAttr called with '%v'", name)
var attr *fuse.Attr
switch {
case name == "": // Base directory
attr = kwfs.directoryAttr(1, 0755) // Writability necessary for .clear_cache
case name == ".version":
size := uint64(len(fsVersion))
attr = kwfs.fileAttr(size, 0444)
case name == ".clear_cache":
attr = kwfs.fileAttr(0, 0440)
case name == ".running":
size := uint64(len(running()))
attr = kwfs.fileAttr(size, 0444)
case name == ".json":
attr = kwfs.directoryAttr(1, 0700)
case name == ".json/status":
size := uint64(len(kwfs.statusJSON()))
attr = kwfs.fileAttr(size, 0444)
case name == ".json/metrics":
size := uint64(len(kwfs.metricsJSON()))
attr = kwfs.fileAttr(size, 0444)
case name == ".json/secret":
attr = kwfs.directoryAttr(0, 0700)
case name == ".json/secrets":
data, ok := kwfs.Client.RawSecretList()
if ok {
size := uint64(len(data))
attr = kwfs.fileAttr(size, 0400)
}
case name == ".json/server_status":
data, err := kwfs.Client.ServerStatus()
if err == nil {
size := uint64(len(data))
attr = kwfs.fileAttr(size, 0444)
}
case strings.HasPrefix(name, ".json/secret/"):
sname := name[len(".json/secret/"):]
data, err := kwfs.Client.RawSecret(sname)
if err == nil {
size := uint64(len(data))
attr = kwfs.fileAttr(size, 0400)
}
case name == ".pprof":
attr = kwfs.directoryAttr(1, 0700)
case name == ".pprof/heap":
size := uint64(len(kwfs.profile("heap")))
attr = kwfs.fileAttr(size, 0444)
case name == ".pprof/goroutine":
size := uint64(len(kwfs.profile("goroutine")))
attr = kwfs.fileAttr(size, 0444)
case name == ".pprof/threadcreate":
size := uint64(len(kwfs.profile("threadcreate")))
attr = kwfs.fileAttr(size, 0444)
case name == ".pprof/block":
size := uint64(len(kwfs.profile("block")))
attr = kwfs.fileAttr(size, 0444)
default:
secret, ok := kwfs.Cache.Secret(name)
if ok {
attr = kwfs.secretAttr(secret)
}
}
if attr != nil {
return attr, fuse.OK
}
return nil, fuse.ENOENT
}
// Open is a FUSE function where an in-memory open file struct is constructed.
func (kwfs KeywhizFs) Open(name string, flags uint32, context *fuse.Context) (nodefs.File, fuse.Status) {
ret := make(chan struct {
nodefs.File
fuse.Status
}, 1)
go func() {
defer close(ret)
file, status := kwfs.open(name, flags, context)
ret <- struct {
nodefs.File
fuse.Status
}{file, status}
}()
select {
case out := <-ret:
return out.File, out.Status
case <-time.After(kwfs.Timeout):
kwfs.Errorf("Operation timed out: Open(\"%s\", %d, %s)", name, flags, prettyContext(context))
kwfs.logGoroutines()
return nil, fuse.EIO
}
}
func (kwfs KeywhizFs) open(name string, flags uint32, context *fuse.Context) (nodefs.File, fuse.Status) {
kwfs.Debugf("Open called with '%v'", name)
var file nodefs.File
switch {
case name == "", name == ".json", name == ".json/secret", name == ".pprof":
return nil, fuseEISDIR
case name == ".version":
file = nodefs.NewDataFile([]byte(fsVersion))
case name == ".json/status":
file = nodefs.NewDataFile(kwfs.statusJSON())
case name == ".json/metrics":
file = nodefs.NewDataFile(kwfs.metricsJSON())
case name == ".clear_cache":
file = nodefs.NewDevNullFile()
case name == ".running":
file = nodefs.NewDataFile(running())
case name == ".json/secrets":
data, ok := kwfs.Client.RawSecretList()
if ok {
file = nodefs.NewDataFile(data)
}
case name == ".json/server_status":
data, err := kwfs.Client.ServerStatus()
if err == nil {
file = nodefs.NewDataFile(data)
}
case strings.HasPrefix(name, ".json/secret/"):
sname := name[len(".json/secret/"):]
data, err := kwfs.Client.RawSecret(sname)
if err == nil {
file = nodefs.NewDataFile(data)
kwfs.Debugf("Access to %s by uid %d, with gid %d", sname, context.Uid, context.Gid)
}
case name == ".pprof/heap":
file = nodefs.NewDataFile(kwfs.profile("heap"))
case name == ".pprof/goroutine":
file = nodefs.NewDataFile(kwfs.profile("goroutine"))
case name == ".pprof/threadcreate":
file = nodefs.NewDataFile(kwfs.profile("threadcreate"))
case name == ".pprof/block":
file = nodefs.NewDataFile(kwfs.profile("block"))
default:
secret, ok := kwfs.Cache.Secret(name)
if ok {
file = nodefs.NewDataFile(secret.Content)
kwfs.Debugf("Access to %s by uid %d, with gid %d", name, context.Uid, context.Gid)
}
}
if file != nil {
file = nodefs.NewReadOnlyFile(file)
attr, status := kwfs.GetAttr(name, context)
if status != fuse.OK {
return nil, fuse.ENOENT
}
file = NewAttrFile(file, attr)
kwfs.Debugf("Open returning '%s': '%s'", name, file.String())
return file, fuse.OK
}
return nil, fuse.ENOENT
}
// OpenDir is a FUSE function called when performing a directory listing.
func (kwfs KeywhizFs) OpenDir(name string, context *fuse.Context) (stream []fuse.DirEntry, code fuse.Status) {
ret := make(chan struct {
Stream []fuse.DirEntry
Status fuse.Status
}, 1)
go func() {
defer close(ret)
stream, status := kwfs.openDir(name, context)
ret <- struct {
Stream []fuse.DirEntry
Status fuse.Status
}{stream, status}
}()
select {
case out := <-ret:
return out.Stream, out.Status
case <-time.After(kwfs.Timeout):
kwfs.Errorf("Operation timed out: OpenDir(\"%s\", %s)", name, prettyContext(context))
kwfs.logGoroutines()
return nil, fuse.EIO
}
}
func (kwfs KeywhizFs) logGoroutines() {
var buffer bytes.Buffer
profile := pprof.Lookup("goroutine")
if profile == nil {
kwfs.Errorf("unable to fetch goroutine profile\n")
return
}
err := profile.WriteTo(&buffer, 1)
if err != nil {
kwfs.Errorf("failed to write goroutine dump: %s\n", err)
} else {
kwfs.Errorf("goroutine dump:\n%s\n", string(buffer.Bytes()))
}
}
func (kwfs KeywhizFs) openDir(name string, context *fuse.Context) (stream []fuse.DirEntry, code fuse.Status) {
kwfs.Debugf("OpenDir called with '%v'", name)
var entries []fuse.DirEntry
switch name {
case "": // Base directory
entries = kwfs.secretsDirListing(
fuse.DirEntry{Name: ".clear_cache", Mode: fuse.S_IFREG},
fuse.DirEntry{Name: ".json", Mode: fuse.S_IFDIR},
fuse.DirEntry{Name: ".pprof", Mode: fuse.S_IFDIR},
fuse.DirEntry{Name: ".running", Mode: fuse.S_IFREG},
fuse.DirEntry{Name: ".version", Mode: fuse.S_IFREG})
case ".json":
entries = []fuse.DirEntry{
{Name: "metrics", Mode: fuse.S_IFREG},
{Name: "secret", Mode: fuse.S_IFDIR},
{Name: "secrets", Mode: fuse.S_IFREG},
{Name: "status", Mode: fuse.S_IFREG},
{Name: "server_status", Mode: fuse.S_IFREG},
}
case ".json/secret":
entries = kwfs.secretsDirListing()
case ".pprof":
entries = []fuse.DirEntry{
fuse.DirEntry{Name: "heap", Mode: fuse.S_IFREG},
fuse.DirEntry{Name: "goroutine", Mode: fuse.S_IFREG},
fuse.DirEntry{Name: "threadcreate", Mode: fuse.S_IFREG},
fuse.DirEntry{Name: "block", Mode: fuse.S_IFREG},
}
}
if len(entries) == 0 {
return entries, fuse.ENOENT
}
return entries, fuse.OK
}
// Unlink is a FUSE function called when an object is deleted.
func (kwfs KeywhizFs) Unlink(name string, context *fuse.Context) fuse.Status {
kwfs.Debugf("Unlink called with '%v'", name)
if name == ".clear_cache" {
kwfs.Cache.Clear()
return fuse.OK
}
return fuse.EACCES
}
// StatFs is a FUSE function called to provide information about the filesystem
// We return zeros, which makes "df" think this is a dummy fs, which it is.
func (kwfs KeywhizFs) StatFs(name string) *fuse.StatfsOut {
kwfs.Debugf("StatFs called with '%v'", name)
return &fuse.StatfsOut{}
}
// secretsDirListing produces directory entries containing all secret files. Extra entries passed
// to this function are included.
func (kwfs KeywhizFs) secretsDirListing(extraEntries ...fuse.DirEntry) []fuse.DirEntry {
secrets := kwfs.Cache.SecretList()
entries := make([]fuse.DirEntry, 0, len(secrets)+len(extraEntries))
for _, s := range secrets {
entries = append(entries, fuse.DirEntry{Name: s.Name, Mode: fuse.S_IFREG})
}
entries = append(entries, extraEntries...)
return entries
}
// secretAttr constructs a fuse.Attr based on a given Secret.
func (kwfs KeywhizFs) secretAttr(s *Secret) *fuse.Attr {
created := uint64(s.CreatedAt.Unix())
attr := &fuse.Attr{
Size: s.Length,
// The resolution for nsec time (uint32) is too small.
Atime: created,
Mtime: created,
Ctime: created,
Mode: s.ModeValue(),
Nlink: 1,
}
attr.Uid = kwfs.Ownership.Uid
attr.Gid = kwfs.Ownership.Gid
if s.Owner != "" {
attr.Uid = lookupUid(s.Owner)
}
if s.Group != "" {
attr.Gid = lookupGid(s.Group)
}
return attr
}
// fileAttr constructs a generic file fuse.Attr with the given parameters.
func (kwfs KeywhizFs) fileAttr(size uint64, mode uint32) *fuse.Attr {
created := uint64(kwfs.StartTime.Unix())
attr := fuse.Attr{
Size: size,
Atime: created,
Mtime: created,
Ctime: created,
Mode: fuse.S_IFREG | mode,
Nlink: 1,
}
attr.Uid = kwfs.Ownership.Uid
attr.Gid = kwfs.Ownership.Gid
return &attr
}
// directoryAttr constructs a generic directory fuse.Attr with the given parameters.
func (kwfs KeywhizFs) directoryAttr(subdirCount, mode uint32) *fuse.Attr {
// 4K is typically the minimum size of inode storage for a directory.
const directoryInodeSize = 4096
created := uint64(kwfs.StartTime.Unix())
attr := fuse.Attr{
Size: directoryInodeSize,
Atime: created,
Mtime: created,
Ctime: created,
Mode: fuse.S_IFDIR | mode,
Nlink: 2 + subdirCount, // '.', '..', and any other subdirectories
}
attr.Uid = kwfs.Ownership.Uid
attr.Gid = kwfs.Ownership.Gid
return &attr
}
// NewAttrFile wraps a File so all GetAttr operations return the passed in value
func NewAttrFile(f nodefs.File, attr *fuse.Attr) nodefs.File {
return &attrFile{File: f, attr: attr}
}
type attrFile struct {
nodefs.File
attr *fuse.Attr
}
func (f *attrFile) InnerFile() nodefs.File {
return f.File
}
func (f *attrFile) String() string {
return fmt.Sprintf("modeFile(%s, %#o)", f.File.String(), f.attr.Mode)
}
func (f *attrFile) GetAttr(out *fuse.Attr) fuse.Status {
*out = *f.attr
return fuse.OK
}
// running provides a formatted string with the current process ID.
func running() []byte {
return []byte(fmt.Sprintf("pid=%d", os.Getpid()))
}
func (kwfs KeywhizFs) String() string {
return "keywhiz-fs"
}