This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
driver.go
137 lines (114 loc) · 2.82 KB
/
driver.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
package main
import (
"encoding/base64"
"io/ioutil"
"path/filepath"
"strings"
"github.com/calavera/docker-volume-vault/store"
"github.com/calavera/docker-volume-vault/vault"
"github.com/docker/go-plugins-helpers/volume"
"github.com/hashicorp/vault/api"
)
type driver struct {
root string
token string
store store.Store
}
func newDriver(root, token string) *driver {
return &driver{
root: root,
token: token,
store: store.NewMemoryStore(),
}
}
func (d *driver) Create(r volume.Request) volume.Response {
vol := store.NewVolume(r.Name, d.token, r.Options)
if err := d.store.Setx(vol); err != nil {
return volume.Response{Err: err.Error()}
}
if rules, ok := r.Options["policy-rules"]; ok {
name := r.Options["policy-name"]
if name == "" {
name = "docker-policy-" + r.Name
}
token, err := d.createPolicy(name, rules)
if err != nil {
return volume.Response{Err: err.Error()}
}
vol.Token = token
d.store.Set(vol)
}
return volume.Response{}
}
func (d *driver) Get(r volume.Request) volume.Response {
return volume.Response{}
}
func (d *driver) List(r volume.Request) volume.Response {
return volume.Response{}
}
func (d *driver) Remove(r volume.Request) volume.Response {
err := d.store.Del(r.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{}
}
func (d *driver) Path(r volume.Request) volume.Response {
return volume.Response{Mountpoint: d.mountpoint(r.Name)}
}
func (d *driver) Mount(r volume.Request) volume.Response {
vol, err := d.store.Get(r.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
mount, err := vol.Mount(d.root)
if err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{Mountpoint: mount}
}
func (d driver) Unmount(r volume.Request) volume.Response {
vol, err := d.store.Get(r.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
if vol.Mounted() {
if err := vol.Unmount(); err != nil {
return volume.Response{Err: err.Error()}
}
}
return volume.Response{}
}
func (d *driver) mountpoint(name string) string {
return filepath.Join(d.root, name)
}
func (d *driver) client() (*api.Client, error) {
return vault.Client(d.token)
}
func (d *driver) createPolicy(name, policy string) (string, error) {
var rules []byte
var err error
if strings.HasPrefix(policy, "@") {
rules, err = ioutil.ReadFile(strings.TrimPrefix(policy, "@"))
} else {
rules, err = base64.StdEncoding.DecodeString(policy)
}
if err != nil {
return "", err
}
client, err := d.client()
if err != nil {
return "", err
}
if err := client.Sys().PutPolicy(name, string(rules)); err != nil {
return "", err
}
req := &api.TokenCreateRequest{
Policies: []string{name},
}
secret, err := client.Auth().Token().Create(req)
if err != nil {
return "", err
}
return secret.Auth.ClientToken, nil
}