2015-05-20 14:54:26 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-04-26 03:10:32 +00:00
|
|
|
"log"
|
2015-05-20 14:54:26 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2015-08-17 19:19:55 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
|
2015-12-14 16:14:09 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
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"
|
2015-05-20 14:54:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// S3Backend is a physical backend that stores data
|
|
|
|
// within an S3 bucket.
|
|
|
|
type S3Backend struct {
|
|
|
|
bucket string
|
|
|
|
client *s3.S3
|
2016-04-26 03:10:32 +00:00
|
|
|
logger *log.Logger
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newS3Backend constructs a S3 backend using a pre-existing
|
|
|
|
// bucket. Credentials can be provided to the backend, sourced
|
|
|
|
// from the environment, AWS credential files or by IAM role.
|
2016-04-26 03:10:32 +00:00
|
|
|
func newS3Backend(conf map[string]string, logger *log.Logger) (Backend, error) {
|
2015-05-20 14:54:26 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
access_key, ok := conf["access_key"]
|
|
|
|
if !ok {
|
|
|
|
access_key = ""
|
|
|
|
}
|
|
|
|
secret_key, ok := conf["secret_key"]
|
|
|
|
if !ok {
|
|
|
|
secret_key = ""
|
|
|
|
}
|
2015-08-06 16:37:08 +00:00
|
|
|
session_token, ok := conf["session_token"]
|
|
|
|
if !ok {
|
|
|
|
session_token = ""
|
|
|
|
}
|
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
|
|
|
}
|
2015-12-17 15:19:42 +00:00
|
|
|
region := os.Getenv("AWS_DEFAULT_REGION")
|
|
|
|
if region == "" {
|
|
|
|
region = conf["region"]
|
2015-05-20 14:54:26 +00:00
|
|
|
if region == "" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := credentials.NewChainCredentials([]credentials.Provider{
|
|
|
|
&credentials.StaticProvider{Value: credentials.Value{
|
|
|
|
AccessKeyID: access_key,
|
|
|
|
SecretAccessKey: secret_key,
|
2015-07-18 06:48:23 +00:00
|
|
|
SessionToken: session_token,
|
2015-05-20 14:54:26 +00:00
|
|
|
}},
|
|
|
|
&credentials.EnvProvider{},
|
|
|
|
&credentials.SharedCredentialsProvider{Filename: "", Profile: ""},
|
2015-12-14 16:14:09 +00:00
|
|
|
&ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(session.New())},
|
2015-05-20 14:54:26 +00:00
|
|
|
})
|
|
|
|
|
2015-10-30 22:22:48 +00:00
|
|
|
s3conn := s3.New(session.New(&aws.Config{
|
2015-05-20 14:54:26 +00:00
|
|
|
Credentials: creds,
|
2015-11-04 12:34:40 +00:00
|
|
|
Endpoint: aws.String(endpoint),
|
2015-08-06 16:37:08 +00:00
|
|
|
Region: aws.String(region),
|
2015-10-30 22:22:48 +00:00
|
|
|
}))
|
2015-05-20 14:54:26 +00:00
|
|
|
|
|
|
|
_, err := s3conn.HeadBucket(&s3.HeadBucketInput{Bucket: &bucket})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to access bucket '%s': %v", bucket, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &S3Backend{
|
|
|
|
client: s3conn,
|
|
|
|
bucket: bucket,
|
2016-04-26 03:10:32 +00:00
|
|
|
logger: logger,
|
2015-05-20 14:54:26 +00:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put is used to insert or update an entry
|
|
|
|
func (s *S3Backend) Put(entry *Entry) error {
|
|
|
|
defer metrics.MeasureSince([]string{"s3", "put"}, time.Now())
|
|
|
|
|
|
|
|
_, err := s.client.PutObject(&s3.PutObjectInput{
|
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Key: aws.String(entry.Key),
|
|
|
|
Body: bytes.NewReader(entry.Value),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
|
|
|
func (s *S3Backend) Get(key string) (*Entry, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"s3", "get"}, time.Now())
|
|
|
|
|
|
|
|
resp, err := s.client.GetObject(&s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Key: aws.String(key),
|
|
|
|
})
|
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
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
data := make([]byte, *resp.ContentLength)
|
|
|
|
_, err = io.ReadFull(resp.Body, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ent := &Entry{
|
|
|
|
Key: key,
|
|
|
|
Value: data,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
|
|
|
func (s *S3Backend) Delete(key string) error {
|
|
|
|
defer metrics.MeasureSince([]string{"s3", "delete"}, time.Now())
|
|
|
|
|
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
|
|
|
})
|
2015-05-20 23:53:35 +00:00
|
|
|
|
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.
|
|
|
|
func (s *S3Backend) List(prefix string) ([]string, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"s3", "list"}, time.Now())
|
|
|
|
|
|
|
|
resp, err := s.client.ListObjects(&s3.ListObjectsInput{
|
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Prefix: aws.String(prefix),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-22 18:07:59 +00:00
|
|
|
if resp == nil {
|
|
|
|
return nil, fmt.Errorf("nil response from S3 but no error")
|
|
|
|
}
|
2015-05-20 14:54:26 +00:00
|
|
|
|
|
|
|
keys := []string{}
|
|
|
|
for _, key := range resp.Contents {
|
|
|
|
key := strings.TrimPrefix(*key.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
|
|
|
|
keys = appendIfMissing(keys, key[:i+1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendIfMissing(slice []string, i string) []string {
|
|
|
|
for _, ele := range slice {
|
|
|
|
if ele == i {
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(slice, i)
|
|
|
|
}
|