2017-08-03 17:24:27 +00:00
|
|
|
package azure
|
2016-02-24 12:14:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"sort"
|
2016-06-08 16:01:43 +00:00
|
|
|
"strconv"
|
2016-02-24 12:14:23 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2016-04-25 19:53:07 +00:00
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
storage "github.com/Azure/azure-sdk-for-go/storage"
|
2016-08-19 20:45:17 +00:00
|
|
|
log "github.com/mgutz/logxi/v1"
|
|
|
|
|
2016-04-25 19:53:07 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2016-06-08 16:01:43 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2017-06-16 16:06:09 +00:00
|
|
|
cleanhttp "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-02-24 12:14:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MaxBlobSize at this time
|
|
|
|
var MaxBlobSize = 1024 * 1024 * 4
|
|
|
|
|
|
|
|
// AzureBackend is a physical backend that stores data
|
|
|
|
// within an Azure blob container.
|
|
|
|
type AzureBackend struct {
|
2017-06-16 16:06:09 +00:00
|
|
|
container *storage.Container
|
2016-08-19 20:45:17 +00:00
|
|
|
logger log.Logger
|
2017-08-03 17:24:27 +00:00
|
|
|
permitPool *physical.PermitPool
|
2016-02-24 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
// NewAzureBackend constructs an Azure backend using a pre-existing
|
2016-02-24 12:14:23 +00:00
|
|
|
// bucket. Credentials can be provided to the backend, sourced
|
|
|
|
// from the environment, AWS credential files or by IAM role.
|
2017-08-03 17:24:27 +00:00
|
|
|
func NewAzureBackend(conf map[string]string, logger log.Logger) (physical.Backend, error) {
|
2017-06-16 16:06:09 +00:00
|
|
|
name := os.Getenv("AZURE_BLOB_CONTAINER")
|
|
|
|
if name == "" {
|
|
|
|
name = conf["container"]
|
|
|
|
if name == "" {
|
2016-02-24 12:14:23 +00:00
|
|
|
return nil, fmt.Errorf("'container' must be set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
accountName := os.Getenv("AZURE_ACCOUNT_NAME")
|
|
|
|
if accountName == "" {
|
|
|
|
accountName = conf["accountName"]
|
|
|
|
if accountName == "" {
|
|
|
|
return nil, fmt.Errorf("'accountName' must be set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
accountKey := os.Getenv("AZURE_ACCOUNT_KEY")
|
|
|
|
if accountKey == "" {
|
|
|
|
accountKey = conf["accountKey"]
|
|
|
|
if accountKey == "" {
|
|
|
|
return nil, fmt.Errorf("'accountKey' must be set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := storage.NewBasicClient(accountName, accountKey)
|
|
|
|
if err != nil {
|
2017-04-17 16:15:12 +00:00
|
|
|
return nil, fmt.Errorf("failed to create Azure client: %v", err)
|
2016-02-24 12:14:23 +00:00
|
|
|
}
|
2017-06-16 16:06:09 +00:00
|
|
|
client.HTTPClient = cleanhttp.DefaultPooledClient()
|
2016-02-24 12:14:23 +00:00
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
blobClient := client.GetBlobService()
|
|
|
|
container := blobClient.GetContainerReference(name)
|
|
|
|
_, err = container.CreateIfNotExists(&storage.CreateContainerOptions{
|
|
|
|
Access: storage.ContainerAccessTypePrivate,
|
|
|
|
})
|
2017-04-17 16:15:12 +00:00
|
|
|
if err != nil {
|
2017-06-16 16:06:09 +00:00
|
|
|
return nil, fmt.Errorf("failed to create %q container: %v", name, err)
|
2017-04-17 16:15:12 +00:00
|
|
|
}
|
2016-02-24 12:14:23 +00:00
|
|
|
|
2016-06-08 16:01:43 +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() {
|
|
|
|
logger.Debug("azure: max_parallel set", "max_parallel", maxParInt)
|
|
|
|
}
|
2016-06-08 16:01:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 12:14:23 +00:00
|
|
|
a := &AzureBackend{
|
2016-06-08 16:01:43 +00:00
|
|
|
container: container,
|
|
|
|
logger: logger,
|
2017-08-03 17:24:27 +00:00
|
|
|
permitPool: physical.NewPermitPool(maxParInt),
|
2016-02-24 12:14:23 +00:00
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put is used to insert or update an entry
|
2017-08-03 17:24:27 +00:00
|
|
|
func (a *AzureBackend) Put(entry *physical.Entry) error {
|
2016-02-24 12:14:23 +00:00
|
|
|
defer metrics.MeasureSince([]string{"azure", "put"}, time.Now())
|
|
|
|
|
|
|
|
if len(entry.Value) >= MaxBlobSize {
|
2017-06-16 16:06:09 +00:00
|
|
|
return fmt.Errorf("value is bigger than the current supported limit of 4MBytes")
|
2016-02-24 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blockID := base64.StdEncoding.EncodeToString([]byte("AAAA"))
|
|
|
|
blocks := make([]storage.Block, 1)
|
|
|
|
blocks[0] = storage.Block{ID: blockID, Status: storage.BlockStatusLatest}
|
|
|
|
|
2016-06-08 16:01:43 +00:00
|
|
|
a.permitPool.Acquire()
|
|
|
|
defer a.permitPool.Release()
|
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
blob := &storage.Blob{
|
|
|
|
Container: a.container,
|
|
|
|
Name: entry.Key,
|
|
|
|
}
|
|
|
|
if err := blob.PutBlock(blockID, entry.Value, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-24 12:14:23 +00:00
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
return blob.PutBlockList(blocks, nil)
|
2016-02-24 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
2017-08-03 17:24:27 +00:00
|
|
|
func (a *AzureBackend) Get(key string) (*physical.Entry, error) {
|
2016-02-24 12:14:23 +00:00
|
|
|
defer metrics.MeasureSince([]string{"azure", "get"}, time.Now())
|
|
|
|
|
2016-06-08 16:01:43 +00:00
|
|
|
a.permitPool.Acquire()
|
|
|
|
defer a.permitPool.Release()
|
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
blob := &storage.Blob{
|
|
|
|
Container: a.container,
|
|
|
|
Name: key,
|
|
|
|
}
|
|
|
|
exists, err := blob.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-24 12:14:23 +00:00
|
|
|
if !exists {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
reader, err := blob.Get(nil)
|
2016-02-24 12:14:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-16 16:06:09 +00:00
|
|
|
defer reader.Close()
|
2016-02-24 12:14:23 +00:00
|
|
|
data, err := ioutil.ReadAll(reader)
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
ent := &physical.Entry{
|
2016-02-24 12:14:23 +00:00
|
|
|
Key: key,
|
|
|
|
Value: data,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ent, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
|
|
|
func (a *AzureBackend) Delete(key string) error {
|
|
|
|
defer metrics.MeasureSince([]string{"azure", "delete"}, time.Now())
|
2016-06-08 16:01:43 +00:00
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
blob := &storage.Blob{
|
|
|
|
Container: a.container,
|
|
|
|
Name: key,
|
|
|
|
}
|
|
|
|
|
2016-06-08 16:01:43 +00:00
|
|
|
a.permitPool.Acquire()
|
|
|
|
defer a.permitPool.Release()
|
|
|
|
|
2017-06-16 16:06:09 +00:00
|
|
|
_, err := blob.DeleteIfExists(nil)
|
2016-02-24 12:14:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
|
|
|
func (a *AzureBackend) List(prefix string) ([]string, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"azure", "list"}, time.Now())
|
|
|
|
|
2016-06-08 16:01:43 +00:00
|
|
|
a.permitPool.Acquire()
|
2017-06-16 16:06:09 +00:00
|
|
|
list, err := a.container.ListBlobs(storage.ListBlobsParameters{Prefix: prefix})
|
2016-02-24 12:14:23 +00:00
|
|
|
if err != nil {
|
|
|
|
// Break early.
|
2017-06-16 16:06:09 +00:00
|
|
|
a.permitPool.Release()
|
2016-02-24 12:14:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-16 16:06:09 +00:00
|
|
|
a.permitPool.Release()
|
2016-02-24 12:14:23 +00:00
|
|
|
|
|
|
|
keys := []string{}
|
|
|
|
for _, blob := range list.Blobs {
|
|
|
|
key := strings.TrimPrefix(blob.Name, prefix)
|
|
|
|
if i := strings.Index(key, "/"); i == -1 {
|
|
|
|
keys = append(keys, key)
|
|
|
|
} else {
|
2017-06-16 15:09:15 +00:00
|
|
|
keys = strutil.AppendIfMissing(keys, key[:i+1])
|
2016-02-24 12:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
return keys, nil
|
|
|
|
}
|