2017-08-03 17:24:27 +00:00
|
|
|
package swift
|
2016-05-16 22:29:23 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-05-16 22:29:23 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
2016-06-08 16:20:21 +00:00
|
|
|
"strconv"
|
2016-05-16 22:29:23 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2016-06-08 16:20:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2017-06-16 15:09:15 +00:00
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2017-08-03 17:24:27 +00:00
|
|
|
"github.com/hashicorp/vault/physical"
|
2016-05-16 22:29:23 +00:00
|
|
|
"github.com/ncw/swift"
|
|
|
|
)
|
|
|
|
|
2018-01-20 01:44:24 +00:00
|
|
|
// Verify SwiftBackend satisfies the correct interfaces
|
|
|
|
var _ physical.Backend = (*SwiftBackend)(nil)
|
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
// SwiftBackend is a physical backend that stores data
|
|
|
|
// within an OpenStack Swift container.
|
|
|
|
type SwiftBackend struct {
|
2016-06-08 16:20:21 +00:00
|
|
|
container string
|
|
|
|
client *swift.Connection
|
2016-08-19 20:45:17 +00:00
|
|
|
logger log.Logger
|
2017-08-03 17:24:27 +00:00
|
|
|
permitPool *physical.PermitPool
|
2016-05-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
// NewSwiftBackend constructs a Swift backend using a pre-existing
|
2016-05-16 22:29:23 +00:00
|
|
|
// container. Credentials can be provided to the backend, sourced
|
|
|
|
// from the environment.
|
2017-08-03 17:24:27 +00:00
|
|
|
func NewSwiftBackend(conf map[string]string, logger log.Logger) (physical.Backend, error) {
|
2017-06-05 14:48:39 +00:00
|
|
|
var ok bool
|
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
username := os.Getenv("OS_USERNAME")
|
|
|
|
if username == "" {
|
|
|
|
username = conf["username"]
|
|
|
|
if username == "" {
|
|
|
|
return nil, fmt.Errorf("missing username")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
password := os.Getenv("OS_PASSWORD")
|
|
|
|
if password == "" {
|
|
|
|
password = conf["password"]
|
|
|
|
if password == "" {
|
|
|
|
return nil, fmt.Errorf("missing password")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
authUrl := os.Getenv("OS_AUTH_URL")
|
|
|
|
if authUrl == "" {
|
|
|
|
authUrl = conf["auth_url"]
|
|
|
|
if authUrl == "" {
|
|
|
|
return nil, fmt.Errorf("missing auth_url")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
container := os.Getenv("OS_CONTAINER")
|
|
|
|
if container == "" {
|
|
|
|
container = conf["container"]
|
|
|
|
if container == "" {
|
|
|
|
return nil, fmt.Errorf("missing container")
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 15:59:44 +00:00
|
|
|
project := os.Getenv("OS_PROJECT_NAME")
|
|
|
|
if project == "" {
|
2017-06-05 14:48:39 +00:00
|
|
|
if project, ok = conf["project"]; !ok {
|
2017-04-17 15:59:44 +00:00
|
|
|
// Check for KeyStone naming prior to V3
|
2017-06-05 14:48:39 +00:00
|
|
|
project = os.Getenv("OS_TENANT_NAME")
|
2017-04-17 15:59:44 +00:00
|
|
|
if project == "" {
|
|
|
|
project = conf["tenant"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := os.Getenv("OS_USER_DOMAIN_NAME")
|
|
|
|
if domain == "" {
|
|
|
|
domain = conf["domain"]
|
|
|
|
}
|
|
|
|
projectDomain := os.Getenv("OS_PROJECT_DOMAIN_NAME")
|
|
|
|
if projectDomain == "" {
|
|
|
|
projectDomain = conf["project-domain"]
|
2016-05-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := swift.Connection{
|
2017-04-17 15:59:44 +00:00
|
|
|
Domain: domain,
|
|
|
|
UserName: username,
|
|
|
|
ApiKey: password,
|
|
|
|
AuthUrl: authUrl,
|
|
|
|
Tenant: project,
|
|
|
|
TenantDomain: projectDomain,
|
|
|
|
Transport: cleanhttp.DefaultPooledTransport(),
|
2016-05-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := c.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = c.Container(container)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf(fmt.Sprintf("Unable to access container %q: {{err}}", container), err)
|
2016-05-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 16:20:21 +00:00
|
|
|
maxParStr, ok := conf["max_parallel"]
|
|
|
|
var maxParInt int
|
|
|
|
if ok {
|
|
|
|
maxParInt, err = strconv.Atoi(maxParStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("failed parsing max_parallel parameter: {{err}}", err)
|
|
|
|
}
|
2016-08-19 20:45:17 +00:00
|
|
|
if logger.IsDebug() {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger.Debug("max_parallel set", "max_parallel", maxParInt)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2016-06-08 16:20:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
s := &SwiftBackend{
|
2016-06-08 16:20:21 +00:00
|
|
|
client: &c,
|
|
|
|
container: container,
|
|
|
|
logger: logger,
|
2017-08-03 17:24:27 +00:00
|
|
|
permitPool: physical.NewPermitPool(maxParInt),
|
2016-05-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put is used to insert or update an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *SwiftBackend) Put(ctx context.Context, entry *physical.Entry) error {
|
2016-05-16 22:29:23 +00:00
|
|
|
defer metrics.MeasureSince([]string{"swift", "put"}, time.Now())
|
|
|
|
|
2016-06-08 16:20:21 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
err := s.client.ObjectPutBytes(s.container, entry.Key, entry.Value, "")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *SwiftBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
|
2016-05-16 22:29:23 +00:00
|
|
|
defer metrics.MeasureSince([]string{"swift", "get"}, time.Now())
|
|
|
|
|
2016-06-08 16:20:21 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
//Do a list of names with the key first since eventual consistency means
|
|
|
|
//it might be deleted, but a node might return a read of bytes which fails
|
|
|
|
//the physical test
|
|
|
|
list, err := s.client.ObjectNames(s.container, &swift.ObjectsOpts{Prefix: key})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if 0 == len(list) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
data, err := s.client.ObjectGetBytes(s.container, key)
|
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-03 17:24:27 +00:00
|
|
|
ent := &physical.Entry{
|
2016-05-16 22:29:23 +00:00
|
|
|
Key: key,
|
|
|
|
Value: data,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *SwiftBackend) Delete(ctx context.Context, key string) error {
|
2016-05-16 22:29:23 +00:00
|
|
|
defer metrics.MeasureSince([]string{"swift", "delete"}, time.Now())
|
|
|
|
|
2016-06-08 16:20:21 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
err := s.client.ObjectDelete(s.container, key)
|
|
|
|
|
|
|
|
if err != nil && err != swift.ObjectNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *SwiftBackend) List(ctx context.Context, prefix string) ([]string, error) {
|
2016-05-16 22:29:23 +00:00
|
|
|
defer metrics.MeasureSince([]string{"swift", "list"}, time.Now())
|
|
|
|
|
2016-06-08 16:20:21 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2016-05-16 22:29:23 +00:00
|
|
|
list, err := s.client.ObjectNamesAll(s.container, &swift.ObjectsOpts{Prefix: prefix})
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := []string{}
|
|
|
|
for _, key := range list {
|
|
|
|
key := strings.TrimPrefix(key, prefix)
|
|
|
|
|
|
|
|
if i := strings.Index(key, "/"); i == -1 {
|
|
|
|
// Add objects only from the current 'folder'
|
|
|
|
keys = append(keys, key)
|
|
|
|
} else if i != -1 {
|
|
|
|
// Add truncated 'folder' paths
|
2017-06-16 15:09:15 +00:00
|
|
|
keys = strutil.AppendIfMissing(keys, key[:i+1])
|
2016-05-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|