2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
package s3
|
2015-05-20 14:54:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-05-20 14:54:26 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-03-29 17:27:27 +00:00
|
|
|
"net/http"
|
2015-05-20 14:54:26 +00:00
|
|
|
"os"
|
2019-07-24 16:48:26 +00:00
|
|
|
"path"
|
2015-05-20 14:54:26 +00:00
|
|
|
"sort"
|
2017-03-26 18:32:26 +00:00
|
|
|
"strconv"
|
2015-05-20 14:54:26 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-24 16:48:26 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2015-06-03 19:02:49 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-10-30 22:22:48 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2015-06-03 19:02:49 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2019-07-24 16:48:26 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2021-07-21 00:42:00 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/awsutil"
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2015-05-20 14:54:26 +00:00
|
|
|
)
|
|
|
|
|
2018-01-20 01:44:24 +00:00
|
|
|
// Verify S3Backend satisfies the correct interfaces
|
|
|
|
var _ physical.Backend = (*S3Backend)(nil)
|
|
|
|
|
2015-05-20 14:54:26 +00:00
|
|
|
// S3Backend is a physical backend that stores data
|
|
|
|
// within an S3 bucket.
|
|
|
|
type S3Backend struct {
|
2017-03-29 17:27:27 +00:00
|
|
|
bucket string
|
2019-07-24 16:48:26 +00:00
|
|
|
path string
|
2019-01-26 21:48:08 +00:00
|
|
|
kmsKeyId string
|
2017-03-29 17:27:27 +00:00
|
|
|
client *s3.S3
|
|
|
|
logger log.Logger
|
2017-08-03 17:24:27 +00:00
|
|
|
permitPool *physical.PermitPool
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
// NewS3Backend constructs a S3 backend using a pre-existing
|
2015-05-20 14:54:26 +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 NewS3Backend(conf map[string]string, logger log.Logger) (physical.Backend, error) {
|
2015-12-17 15:19:42 +00:00
|
|
|
bucket := os.Getenv("AWS_S3_BUCKET")
|
|
|
|
if bucket == "" {
|
|
|
|
bucket = conf["bucket"]
|
2015-11-06 13:05:29 +00:00
|
|
|
if bucket == "" {
|
|
|
|
return nil, fmt.Errorf("'bucket' must be set")
|
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 16:48:26 +00:00
|
|
|
path := conf["path"]
|
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
accessKey, ok := conf["access_key"]
|
2015-05-20 14:54:26 +00:00
|
|
|
if !ok {
|
2016-05-03 21:00:16 +00:00
|
|
|
accessKey = ""
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
2016-05-03 21:00:16 +00:00
|
|
|
secretKey, ok := conf["secret_key"]
|
2015-05-20 14:54:26 +00:00
|
|
|
if !ok {
|
2016-05-03 21:00:16 +00:00
|
|
|
secretKey = ""
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
2016-05-03 21:00:16 +00:00
|
|
|
sessionToken, ok := conf["session_token"]
|
2015-08-06 16:37:08 +00:00
|
|
|
if !ok {
|
2016-05-03 21:00:16 +00:00
|
|
|
sessionToken = ""
|
2015-08-06 16:37:08 +00:00
|
|
|
}
|
2015-12-17 15:19:42 +00:00
|
|
|
endpoint := os.Getenv("AWS_S3_ENDPOINT")
|
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = conf["endpoint"]
|
2015-11-04 12:34:40 +00:00
|
|
|
}
|
2017-07-31 22:27:16 +00:00
|
|
|
region := os.Getenv("AWS_REGION")
|
2015-12-17 15:19:42 +00:00
|
|
|
if region == "" {
|
2017-07-31 22:27:16 +00:00
|
|
|
region = os.Getenv("AWS_DEFAULT_REGION")
|
2015-05-20 14:54:26 +00:00
|
|
|
if region == "" {
|
2017-07-31 22:27:16 +00:00
|
|
|
region = conf["region"]
|
|
|
|
if region == "" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-03 17:11:00 +00:00
|
|
|
s3ForcePathStyleStr, ok := conf["s3_force_path_style"]
|
|
|
|
if !ok {
|
|
|
|
s3ForcePathStyleStr = "false"
|
|
|
|
}
|
|
|
|
s3ForcePathStyleBool, err := parseutil.ParseBool(s3ForcePathStyleStr)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("invalid boolean set for s3_force_path_style: %q", s3ForcePathStyleStr)
|
2018-01-03 17:11:00 +00:00
|
|
|
}
|
|
|
|
disableSSLStr, ok := conf["disable_ssl"]
|
|
|
|
if !ok {
|
|
|
|
disableSSLStr = "false"
|
|
|
|
}
|
|
|
|
disableSSLBool, err := parseutil.ParseBool(disableSSLStr)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("invalid boolean set for disable_ssl: %q", disableSSLStr)
|
2018-01-03 17:11:00 +00:00
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
|
2016-05-03 21:00:16 +00:00
|
|
|
credsConfig := &awsutil.CredentialsConfig{
|
|
|
|
AccessKey: accessKey,
|
|
|
|
SecretKey: secretKey,
|
|
|
|
SessionToken: sessionToken,
|
2020-09-28 21:06:49 +00:00
|
|
|
Logger: logger,
|
2016-05-03 21:00:16 +00:00
|
|
|
}
|
|
|
|
creds, err := credsConfig.GenerateCredentialChain()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
|
2017-03-29 17:27:27 +00:00
|
|
|
pooledTransport := cleanhttp.DefaultPooledTransport()
|
|
|
|
pooledTransport.MaxIdleConnsPerHost = consts.ExpirationRestoreWorkerCount
|
|
|
|
|
2020-01-09 22:58:33 +00:00
|
|
|
sess, err := session.NewSession(&aws.Config{
|
2015-05-20 14:54:26 +00:00
|
|
|
Credentials: creds,
|
2017-03-29 17:27:27 +00:00
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Transport: pooledTransport,
|
|
|
|
},
|
2018-01-03 17:11:00 +00:00
|
|
|
Endpoint: aws.String(endpoint),
|
|
|
|
Region: aws.String(region),
|
|
|
|
S3ForcePathStyle: aws.Bool(s3ForcePathStyleBool),
|
|
|
|
DisableSSL: aws.Bool(disableSSLBool),
|
2020-01-09 22:58:33 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s3conn := s3.New(sess)
|
2015-05-20 14:54:26 +00:00
|
|
|
|
2017-06-20 00:16:41 +00:00
|
|
|
_, err = s3conn.ListObjects(&s3.ListObjectsInput{Bucket: &bucket})
|
2015-05-20 14:54:26 +00:00
|
|
|
if err != nil {
|
2021-05-31 16:54:05 +00:00
|
|
|
return nil, fmt.Errorf("unable to access bucket %q in region %q: %w", bucket, region, err)
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:32:26 +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)
|
2017-03-26 18:32:26 +00:00
|
|
|
}
|
|
|
|
if logger.IsDebug() {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger.Debug("max_parallel set", "max_parallel", maxParInt)
|
2017-03-26 18:32:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 21:48:08 +00:00
|
|
|
kmsKeyId, ok := conf["kms_key_id"]
|
|
|
|
if !ok {
|
|
|
|
kmsKeyId = ""
|
|
|
|
}
|
2019-02-08 14:12:55 +00:00
|
|
|
|
2015-05-20 14:54:26 +00:00
|
|
|
s := &S3Backend{
|
2017-03-29 17:27:27 +00:00
|
|
|
client: s3conn,
|
|
|
|
bucket: bucket,
|
2019-07-24 16:48:26 +00:00
|
|
|
path: path,
|
2019-01-26 21:48:08 +00:00
|
|
|
kmsKeyId: kmsKeyId,
|
2017-03-29 17:27:27 +00:00
|
|
|
logger: logger,
|
2017-08-03 17:24:27 +00:00
|
|
|
permitPool: physical.NewPermitPool(maxParInt),
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put is used to insert or update an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *S3Backend) Put(ctx context.Context, entry *physical.Entry) error {
|
2015-05-20 14:54:26 +00:00
|
|
|
defer metrics.MeasureSince([]string{"s3", "put"}, time.Now())
|
|
|
|
|
2017-03-26 18:32:26 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2019-07-24 16:48:26 +00:00
|
|
|
// Setup key
|
|
|
|
key := path.Join(s.path, entry.Key)
|
|
|
|
|
2019-01-26 21:48:08 +00:00
|
|
|
putObjectInput := &s3.PutObjectInput{
|
2015-05-20 14:54:26 +00:00
|
|
|
Bucket: aws.String(s.bucket),
|
2019-07-24 16:48:26 +00:00
|
|
|
Key: aws.String(key),
|
2015-05-20 14:54:26 +00:00
|
|
|
Body: bytes.NewReader(entry.Value),
|
2019-01-26 21:48:08 +00:00
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
|
2019-01-26 21:48:08 +00:00
|
|
|
if s.kmsKeyId != "" {
|
|
|
|
putObjectInput.ServerSideEncryption = aws.String("aws:kms")
|
|
|
|
putObjectInput.SSEKMSKeyId = aws.String(s.kmsKeyId)
|
|
|
|
}
|
2019-02-08 14:12:55 +00:00
|
|
|
|
2019-01-26 21:48:08 +00:00
|
|
|
_, err := s.client.PutObject(putObjectInput)
|
2015-05-20 14:54:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *S3Backend) Get(ctx context.Context, key string) (*physical.Entry, error) {
|
2015-05-20 14:54:26 +00:00
|
|
|
defer metrics.MeasureSince([]string{"s3", "get"}, time.Now())
|
|
|
|
|
2017-03-26 18:32:26 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2019-07-24 16:48:26 +00:00
|
|
|
// Setup key
|
|
|
|
key = path.Join(s.path, key)
|
|
|
|
|
2015-05-20 14:54:26 +00:00
|
|
|
resp, err := s.client.GetObject(&s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Key: aws.String(key),
|
|
|
|
})
|
2018-03-30 17:03:33 +00:00
|
|
|
if resp != nil && resp.Body != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
2015-05-21 20:15:21 +00:00
|
|
|
if awsErr, ok := err.(awserr.RequestFailure); ok {
|
2015-05-20 14:54:26 +00:00
|
|
|
// Return nil on 404s, error on anything else
|
2015-05-21 20:15:21 +00:00
|
|
|
if awsErr.StatusCode() == 404 {
|
2015-05-20 14:54:26 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-03-26 18:32:26 +00:00
|
|
|
return nil, err
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
2016-04-22 18:07:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
return nil, fmt.Errorf("got nil response from S3 but no error")
|
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
|
2018-03-30 16:42:48 +00:00
|
|
|
data := bytes.NewBuffer(nil)
|
|
|
|
if resp.ContentLength != nil {
|
|
|
|
data = bytes.NewBuffer(make([]byte, 0, *resp.ContentLength))
|
|
|
|
}
|
|
|
|
_, err = io.Copy(data, resp.Body)
|
2015-05-20 14:54:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:18:45 +00:00
|
|
|
// Strip path prefix
|
|
|
|
if s.path != "" {
|
|
|
|
key = strings.TrimPrefix(key, s.path+"/")
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
ent := &physical.Entry{
|
2015-05-20 14:54:26 +00:00
|
|
|
Key: key,
|
2018-03-30 16:42:48 +00:00
|
|
|
Value: data.Bytes(),
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *S3Backend) Delete(ctx context.Context, key string) error {
|
2015-05-20 14:54:26 +00:00
|
|
|
defer metrics.MeasureSince([]string{"s3", "delete"}, time.Now())
|
|
|
|
|
2017-03-26 18:32:26 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2019-07-24 16:48:26 +00:00
|
|
|
// Setup key
|
|
|
|
key = path.Join(s.path, key)
|
|
|
|
|
2015-05-20 23:53:35 +00:00
|
|
|
_, err := s.client.DeleteObject(&s3.DeleteObjectInput{
|
2015-05-20 14:54:26 +00:00
|
|
|
Bucket: aws.String(s.bucket),
|
2015-05-20 23:53:35 +00:00
|
|
|
Key: aws.String(key),
|
2015-05-20 14:54:26 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
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 *S3Backend) List(ctx context.Context, prefix string) ([]string, error) {
|
2015-05-20 14:54:26 +00:00
|
|
|
defer metrics.MeasureSince([]string{"s3", "list"}, time.Now())
|
|
|
|
|
2017-03-26 18:32:26 +00:00
|
|
|
s.permitPool.Acquire()
|
|
|
|
defer s.permitPool.Release()
|
|
|
|
|
2019-07-24 16:48:26 +00:00
|
|
|
// Setup prefix
|
|
|
|
prefix = path.Join(s.path, prefix)
|
|
|
|
|
2019-12-04 14:18:45 +00:00
|
|
|
// Validate prefix (if present) is ending with a "/"
|
|
|
|
if prefix != "" && !strings.HasSuffix(prefix, "/") {
|
2019-07-24 16:48:26 +00:00
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:15:48 +00:00
|
|
|
params := &s3.ListObjectsV2Input{
|
2017-06-16 15:09:15 +00:00
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Prefix: aws.String(prefix),
|
|
|
|
Delimiter: aws.String("/"),
|
2016-04-22 18:07:59 +00:00
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
|
|
|
|
keys := []string{}
|
2017-01-03 16:15:48 +00:00
|
|
|
|
|
|
|
err := s.client.ListObjectsV2Pages(params,
|
|
|
|
func(page *s3.ListObjectsV2Output, lastPage bool) bool {
|
2017-06-05 14:54:26 +00:00
|
|
|
if page != nil {
|
2017-06-16 15:09:15 +00:00
|
|
|
// Add truncated 'folder' paths
|
|
|
|
for _, commonPrefix := range page.CommonPrefixes {
|
|
|
|
// Avoid panic
|
|
|
|
if commonPrefix == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
commonPrefix := strings.TrimPrefix(*commonPrefix.Prefix, prefix)
|
|
|
|
keys = append(keys, commonPrefix)
|
|
|
|
}
|
|
|
|
// Add objects only from the current 'folder'
|
2017-06-05 14:54:26 +00:00
|
|
|
for _, key := range page.Contents {
|
|
|
|
// Avoid panic
|
|
|
|
if key == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key := strings.TrimPrefix(*key.Key, prefix)
|
2017-06-16 15:09:15 +00:00
|
|
|
keys = append(keys, key)
|
2017-01-03 16:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|