-
Notifications
You must be signed in to change notification settings - Fork 10
/
fs.go
153 lines (126 loc) · 2.79 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
package main
import (
"os"
"path"
"strings"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
log "github.com/Sirupsen/logrus"
"github.com/ehazlett/libsecret"
"github.com/ehazlett/libsecret/store"
"golang.org/x/net/context"
)
type FS struct {
mountpoint string
volumeName string
conn *fuse.Conn
errChan chan (error)
server *fs.Server
store store.SecretStore
files map[string]*File
tick *time.Ticker
}
func NewFS(mountpoint string, storeBackend store.Backend, storeAddr string, storeOpts map[string]interface{}) (*FS, error) {
c := make(chan error)
go func() {
err := <-c
log.Errorf("fs: %s", err.Error())
}()
storeConfig := &store.Config{
StoreOpts: storeOpts,
}
secretStore, err := libsecret.NewSecretStore(storeBackend, storeAddr, storeConfig)
if err != nil {
return nil, err
}
return &FS{
mountpoint: mountpoint,
errChan: c,
store: secretStore,
files: map[string]*File{},
}, nil
}
func (f *FS) Mount(volumeName string) error {
log.Debugf("setting up fuse: volume=%s", volumeName)
c, err := fuse.Mount(
f.mountpoint,
fuse.FSName("libsecret"),
fuse.Subtype("libsecretfs"),
fuse.LocalVolume(),
fuse.VolumeName(volumeName),
)
if err != nil {
return err
}
srv := fs.New(c, nil)
f.server = srv
f.volumeName = volumeName
f.conn = c
go func() {
err = f.server.Serve(f)
if err != nil {
f.errChan <- err
}
}()
// check if the mount process has an error to report
log.Debug("waiting for mount")
<-c.Ready
if err := c.MountError; err != nil {
return err
}
return nil
}
func (f *FS) Root() (fs.Node, error) {
return &Dir{f, f.server, f.volumeName}, nil
}
type Dir struct {
fs *FS
fuse *fs.Server
basePath string
}
func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = 1
a.Mode = os.ModeDir | 0555
a.Valid = time.Second * 1
return nil
}
func (d *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
secretPath := strings.TrimPrefix(path.Join(d.basePath, name), "/")
log.Debugf("looking up secret: path=%s", secretPath)
s, err := d.fs.store.Get(secretPath)
if err != nil {
log.Warn(err)
return &Dir{d.fs, d.fuse, path.Join(d.basePath, name)}, nil
}
f := &File{
d.fs,
d.fuse,
name,
path.Join(d.basePath, name),
s.Value.(string),
}
return f, nil
}
func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
return nil, nil
}
type File struct {
fs *FS
fuse *fs.Server
name string
path string
content string
}
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
log.Debugf("path: %s", f.path)
f.fs.files[f.path] = f
a.Inode = 2
a.Mode = 0444
a.Size = uint64(len(f.content))
a.Valid = time.Second * 1
return nil
}
func (f *File) ReadAll(ctx context.Context) ([]byte, error) {
return []byte(f.content), nil
}