Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added s3 storage path parameter #7157

Merged
merged 3 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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