2018-02-12 23:22:41 +00:00
|
|
|
package manta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2019-01-09 00:48:57 +00:00
|
|
|
triton "github.com/joyent/triton-go"
|
2018-02-12 23:22:41 +00:00
|
|
|
"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 *physical.PermitPool
|
|
|
|
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 {
|
2021-05-31 16:54:05 +00:00
|
|
|
return nil, fmt.Errorf("Error Creating SSH Agent Signer: %w", err)
|
2018-02-12 23:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
maxParStr, ok := conf["max_parallel"]
|
|
|
|
var maxParInt int
|
|
|
|
if ok {
|
|
|
|
maxParInt, err = strconv.Atoi(maxParStr)
|
|
|
|
if err != nil {
|
2021-05-31 16:54:05 +00:00
|
|
|
return nil, fmt.Errorf("failed parsing max_parallel parameter: %w", err)
|
2018-02-12 23:22:41 +00:00
|
|
|
}
|
|
|
|
if logger.IsDebug() {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger.Debug("max_parallel set", "max_parallel", maxParInt)
|
2018-02-12 23:22:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &triton.ClientConfig{
|
|
|
|
MantaURL: url,
|
|
|
|
AccountName: user,
|
|
|
|
Signers: []authentication.Signer{signer},
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := storage.NewClient(config)
|
|
|
|
if err != nil {
|
2021-05-31 16:54:05 +00:00
|
|
|
return nil, fmt.Errorf("failed initialising Storage client: %w", err)
|
2018-02-12 23:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &MantaBackend{
|
|
|
|
client: client,
|
|
|
|
directory: conf["directory"],
|
|
|
|
logger: logger,
|
|
|
|
permitPool: physical.NewPermitPool(maxParInt),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put is used to insert or update an entry
|
2018-02-13 15:03:12 +00:00
|
|
|
func (m *MantaBackend) Put(ctx context.Context, entry *physical.Entry) error {
|
2018-02-12 23:22:41 +00:00
|
|
|
defer metrics.MeasureSince([]string{"manta", "put"}, time.Now())
|
|
|
|
|
|
|
|
m.permitPool.Acquire()
|
|
|
|
defer m.permitPool.Release()
|
|
|
|
|
|
|
|
r := bytes.NewReader(entry.Value)
|
|
|
|
r.Seek(0, 0)
|
|
|
|
|
2018-02-13 15:03:12 +00:00
|
|
|
return m.client.Objects().Put(ctx, &storage.PutObjectInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
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
|
2018-02-13 15:03:12 +00:00
|
|
|
func (m *MantaBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
|
2018-02-12 23:22:41 +00:00
|
|
|
defer metrics.MeasureSince([]string{"manta", "get"}, time.Now())
|
|
|
|
|
|
|
|
m.permitPool.Acquire()
|
|
|
|
defer m.permitPool.Release()
|
|
|
|
|
2018-02-13 15:03:12 +00:00
|
|
|
output, err := m.client.Objects().Get(ctx, &storage.GetObjectInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
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
|
2018-02-13 15:03:12 +00:00
|
|
|
func (m *MantaBackend) Delete(ctx context.Context, key string) error {
|
2018-02-12 23:22:41 +00:00
|
|
|
defer metrics.MeasureSince([]string{"manta", "delete"}, time.Now())
|
|
|
|
|
|
|
|
m.permitPool.Acquire()
|
|
|
|
defer m.permitPool.Release()
|
|
|
|
|
|
|
|
if strings.HasSuffix(key, "/") {
|
2018-02-13 15:03:12 +00:00
|
|
|
err := m.client.Dir().Delete(ctx, &storage.DeleteDirectoryInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
DirectoryName: path.Join(mantaDefaultRootStore, m.directory, key),
|
|
|
|
ForceDelete: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-13 15:03:12 +00:00
|
|
|
err := m.client.Objects().Delete(ctx, &storage.DeleteObjectInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
ObjectPath: path.Join(mantaDefaultRootStore, m.directory, key, ".vault_value"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if errors.IsResourceNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-13 15:03:12 +00:00
|
|
|
return tryDeleteDirectory(ctx, m, path.Join(mantaDefaultRootStore, m.directory, key))
|
2018-02-12 23:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-13 15:03:12 +00:00
|
|
|
func tryDeleteDirectory(ctx context.Context, m *MantaBackend, directoryPath string) error {
|
|
|
|
objs, err := m.client.Dir().List(ctx, &storage.ListDirectoryInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
DirectoryName: directoryPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if errors.IsResourceNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if objs != nil && len(objs.Entries) == 0 {
|
2018-02-13 15:03:12 +00:00
|
|
|
err := m.client.Dir().Delete(ctx, &storage.DeleteDirectoryInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
DirectoryName: directoryPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-13 15:03:12 +00:00
|
|
|
return tryDeleteDirectory(ctx, m, path.Dir(directoryPath))
|
2018-02-12 23:22:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
2018-02-13 15:03:12 +00:00
|
|
|
func (m *MantaBackend) List(ctx context.Context, prefix string) ([]string, error) {
|
2018-02-12 23:22:41 +00:00
|
|
|
defer metrics.MeasureSince([]string{"manta", "list"}, time.Now())
|
|
|
|
|
|
|
|
m.permitPool.Acquire()
|
|
|
|
defer m.permitPool.Release()
|
|
|
|
|
2018-02-13 15:03:12 +00:00
|
|
|
objs, err := m.client.Dir().List(ctx, &storage.ListDirectoryInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
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" {
|
2018-02-13 15:03:12 +00:00
|
|
|
objs, err := m.client.Dir().List(ctx, &storage.ListDirectoryInput{
|
2018-02-12 23:22:41 +00:00
|
|
|
DirectoryName: path.Join(mantaDefaultRootStore, m.directory, prefix, obj.Name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if !errors.IsResourceNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
// We need to check to see if there is something more than just the `value` file
|
|
|
|
// if the length of the children is:
|
2018-02-12 23:22:41 +00:00
|
|
|
// > 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
|
|
|
|
}
|