Skip to content

Commit

Permalink
Added s3 storage path parameter (#7157)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelvocks authored and jefferai committed Jul 24, 2019
1 parent 6fe8995 commit 8bcc19c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
31 changes: 26 additions & 5 deletions physical/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
"io"
"net/http"
"os"
"path"
"sort"
"strconv"
"strings"
"time"

log "github.com/hashicorp/go-hclog"

metrics "github.com/armon/go-metrics"
"github.com/armon/go-metrics"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/errwrap"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-cleanhttp"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/helper/awsutil"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/parseutil"
Expand All @@ -34,6 +34,7 @@ var _ physical.Backend = (*S3Backend)(nil)
// within an S3 bucket.
type S3Backend struct {
bucket string
path string
kmsKeyId string
client *s3.S3
logger log.Logger
Expand All @@ -52,6 +53,8 @@ func NewS3Backend(conf map[string]string, logger log.Logger) (physical.Backend,
}
}

path := conf["path"]

accessKey, ok := conf["access_key"]
if !ok {
accessKey = ""
Expand Down Expand Up @@ -144,6 +147,7 @@ func NewS3Backend(conf map[string]string, logger log.Logger) (physical.Backend,
s := &S3Backend{
client: s3conn,
bucket: bucket,
path: path,
kmsKeyId: kmsKeyId,
logger: logger,
permitPool: physical.NewPermitPool(maxParInt),
Expand All @@ -158,9 +162,12 @@ func (s *S3Backend) Put(ctx context.Context, entry *physical.Entry) error {
s.permitPool.Acquire()
defer s.permitPool.Release()

// Setup key
key := path.Join(s.path, entry.Key)

putObjectInput := &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(entry.Key),
Key: aws.String(key),
Body: bytes.NewReader(entry.Value),
}

Expand All @@ -185,6 +192,9 @@ func (s *S3Backend) Get(ctx context.Context, key string) (*physical.Entry, error
s.permitPool.Acquire()
defer s.permitPool.Release()

// Setup key
key = path.Join(s.path, key)

resp, err := s.client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
Expand Down Expand Up @@ -230,6 +240,9 @@ func (s *S3Backend) Delete(ctx context.Context, key string) error {
s.permitPool.Acquire()
defer s.permitPool.Release()

// Setup key
key = path.Join(s.path, key)

_, err := s.client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
Expand All @@ -250,6 +263,14 @@ func (s *S3Backend) List(ctx context.Context, prefix string) ([]string, error) {
s.permitPool.Acquire()
defer s.permitPool.Release()

// Setup prefix
prefix = path.Join(s.path, prefix)

// Validate prefix is ending with a "/"
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}

params := &s3.ListObjectsV2Input{
Bucket: aws.String(s.bucket),
Prefix: aws.String(prefix),
Expand Down
1 change: 1 addition & 0 deletions physical/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func DoS3BackendTest(t *testing.T, kmsKeyId string) {
b, err := NewS3Backend(map[string]string{
"bucket": bucket,
"kmsKeyId": kmsKeyId,
"path": "test/vault",
}, logger)
if err != nil {
t.Fatalf("err: %s", err)
Expand Down
3 changes: 3 additions & 0 deletions website/source/docs/configuration/storage/s3.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ cause Vault to attempt to retrieve credentials from the AWS metadata service.
permissions for this key. You can use `alias/aws/s3` to specify the default
key for the account.

- `path` `(string: "")` - Specifies the path in the S3 Bucket where Vault
data will be stored.

## `s3` Examples

### Default Example
Expand Down

0 comments on commit 8bcc19c

Please sign in to comment.