-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmanta.go
275 lines (236 loc) · 6.63 KB
/
manta.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package manta
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
metrics "github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/permitpool"
"github.com/hashicorp/vault/sdk/physical"
triton "github.com/joyent/triton-go"
"github.com/joyent/triton-go/authentication"
"github.com/joyent/triton-go/errors"
"github.com/joyent/triton-go/storage"
)
const mantaDefaultRootStore = "/stor"
type MantaBackend struct {
logger log.Logger
permitPool *permitpool.Pool
client *storage.StorageClient
directory string
}
func NewMantaBackend(conf map[string]string, logger log.Logger) (physical.Backend, error) {
user := os.Getenv("MANTA_USER")
if user == "" {
user = conf["user"]
}
keyId := os.Getenv("MANTA_KEY_ID")
if keyId == "" {
keyId = conf["key_id"]
}
url := os.Getenv("MANTA_URL")
if url == "" {
url = conf["url"]
} else {
url = "https://us-east.manta.joyent.com"
}
subuser := os.Getenv("MANTA_SUBUSER")
if subuser == "" {
if confUser, ok := conf["subuser"]; ok {
subuser = confUser
}
}
input := authentication.SSHAgentSignerInput{
KeyID: keyId,
AccountName: user,
Username: subuser,
}
signer, err := authentication.NewSSHAgentSigner(input)
if err != nil {
return nil, fmt.Errorf("Error Creating SSH Agent Signer: %w", err)
}
maxParStr, ok := conf["max_parallel"]
var maxParInt int
if ok {
maxParInt, err = strconv.Atoi(maxParStr)
if err != nil {
return nil, fmt.Errorf("failed parsing max_parallel parameter: %w", err)
}
if logger.IsDebug() {
logger.Debug("max_parallel set", "max_parallel", maxParInt)
}
}
config := &triton.ClientConfig{
MantaURL: url,
AccountName: user,
Signers: []authentication.Signer{signer},
}
client, err := storage.NewClient(config)
if err != nil {
return nil, fmt.Errorf("failed initialising Storage client: %w", err)
}
return &MantaBackend{
client: client,
directory: conf["directory"],
logger: logger,
permitPool: permitpool.New(maxParInt),
}, nil
}
// Put is used to insert or update an entry
func (m *MantaBackend) Put(ctx context.Context, entry *physical.Entry) error {
defer metrics.MeasureSince([]string{"manta", "put"}, time.Now())
if err := m.permitPool.Acquire(ctx); err != nil {
return err
}
defer m.permitPool.Release()
r := bytes.NewReader(entry.Value)
r.Seek(0, 0)
return m.client.Objects().Put(ctx, &storage.PutObjectInput{
ObjectPath: path.Join(mantaDefaultRootStore, m.directory, entry.Key, ".vault_value"),
ObjectReader: r,
ContentLength: uint64(len(entry.Value)),
ForceInsert: true,
})
}
// Get is used to fetch an entry
func (m *MantaBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
defer metrics.MeasureSince([]string{"manta", "get"}, time.Now())
if err := m.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer m.permitPool.Release()
output, err := m.client.Objects().Get(ctx, &storage.GetObjectInput{
ObjectPath: path.Join(mantaDefaultRootStore, m.directory, key, ".vault_value"),
})
if err != nil {
if strings.Contains(err.Error(), "ResourceNotFound") {
return nil, nil
}
return nil, err
}
defer output.ObjectReader.Close()
data := make([]byte, output.ContentLength)
_, err = io.ReadFull(output.ObjectReader, data)
if err != nil {
return nil, err
}
ent := &physical.Entry{
Key: key,
Value: data,
}
return ent, nil
}
// Delete is used to permanently delete an entry
func (m *MantaBackend) Delete(ctx context.Context, key string) error {
defer metrics.MeasureSince([]string{"manta", "delete"}, time.Now())
if err := m.permitPool.Acquire(ctx); err != nil {
return err
}
defer m.permitPool.Release()
if strings.HasSuffix(key, "/") {
err := m.client.Dir().Delete(ctx, &storage.DeleteDirectoryInput{
DirectoryName: path.Join(mantaDefaultRootStore, m.directory, key),
ForceDelete: true,
})
if err != nil {
return err
}
} else {
err := m.client.Objects().Delete(ctx, &storage.DeleteObjectInput{
ObjectPath: path.Join(mantaDefaultRootStore, m.directory, key, ".vault_value"),
})
if err != nil {
if errors.IsResourceNotFound(err) {
return nil
}
return err
}
return tryDeleteDirectory(ctx, m, path.Join(mantaDefaultRootStore, m.directory, key))
}
return nil
}
func tryDeleteDirectory(ctx context.Context, m *MantaBackend, directoryPath string) error {
objs, err := m.client.Dir().List(ctx, &storage.ListDirectoryInput{
DirectoryName: directoryPath,
})
if err != nil {
if errors.IsResourceNotFound(err) {
return nil
}
return err
}
if objs != nil && len(objs.Entries) == 0 {
err := m.client.Dir().Delete(ctx, &storage.DeleteDirectoryInput{
DirectoryName: directoryPath,
})
if err != nil {
return err
}
return tryDeleteDirectory(ctx, m, path.Dir(directoryPath))
}
return nil
}
// List is used to list all the keys under a given
// prefix, up to the next prefix.
func (m *MantaBackend) List(ctx context.Context, prefix string) ([]string, error) {
defer metrics.MeasureSince([]string{"manta", "list"}, time.Now())
if err := m.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer m.permitPool.Release()
objs, err := m.client.Dir().List(ctx, &storage.ListDirectoryInput{
DirectoryName: path.Join(mantaDefaultRootStore, m.directory, prefix),
})
if err != nil {
if errors.IsResourceNotFound(err) {
return []string{}, nil
}
return nil, err
}
keys := []string{}
for _, obj := range objs.Entries {
if obj.Type == "directory" {
objs, err := m.client.Dir().List(ctx, &storage.ListDirectoryInput{
DirectoryName: path.Join(mantaDefaultRootStore, m.directory, prefix, obj.Name),
})
if err != nil {
if !errors.IsResourceNotFound(err) {
return nil, err
}
}
// We need to check to see if there is something more than just the `value` file
// if the length of the children is:
// > 1 and includes the value `index` then we need to add foo and foo/
// = 1 and the value is `index` then we need to add foo
// = 1 and the value is not `index` then we need to add foo/
if len(objs.Entries) == 1 {
if objs.Entries[0].Name != ".vault_value" {
keys = append(keys, fmt.Sprintf("%s/", obj.Name))
} else {
keys = append(keys, obj.Name)
}
} else if len(objs.Entries) > 1 {
for _, childObj := range objs.Entries {
if childObj.Name == ".vault_value" {
keys = append(keys, obj.Name)
} else {
keys = append(keys, fmt.Sprintf("%s/", obj.Name))
}
}
} else {
keys = append(keys, obj.Name)
}
}
}
sort.Strings(keys)
return keys, nil
}