Skip to content

Commit

Permalink
#4483: added S3 Write ACL Rule override config
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland committed Aug 2, 2022
1 parent 6c95fe8 commit 452b20c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
4 changes: 3 additions & 1 deletion exp/services/ledgerexporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/stellar/go/historyarchive"
"github.com/stellar/go/ingest/ledgerbackend"
"github.com/stellar/go/network"
Expand Down Expand Up @@ -61,7 +62,8 @@ func main() {
target, err := historyarchive.ConnectBackend(
*targetUrl,
storage.ConnectOptions{
Context: context.Background(),
Context: context.Background(),
S3WriteACL: s3.ObjectCannedACLBucketOwnerFullControl,
},
)
logFatalIf(err, "Could not connect to target")
Expand Down
4 changes: 4 additions & 0 deletions support/storage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type ConnectOptions struct {

// Wrap the Storage after connection. For example, to add a caching or introspection layer.
Wrap func(Storage) (Storage, error)

// When putting file object to s3 bucket, specify the ACL for the object.
S3WriteACL string
}

func ConnectBackend(u string, opts ConnectOptions) (Storage, error) {
Expand Down Expand Up @@ -60,6 +63,7 @@ func ConnectBackend(u string, opts ConnectOptions) (Storage, error) {
opts.S3Region,
opts.S3Endpoint,
opts.UnsignedRequests,
opts.S3WriteACL,
)

case "gcs":
Expand Down
14 changes: 13 additions & 1 deletion support/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/stellar/go/support/errors"
)

type S3Storage struct {
ctx context.Context
svc *s3.S3
svc s3iface.S3API
bucket string
prefix string
unsignedRequests bool
writeACLrule string
}

func NewS3Storage(
Expand All @@ -30,6 +32,7 @@ func NewS3Storage(
region string,
endpoint string,
unsignedRequests bool,
writeACLrule string,
) (Storage, error) {
log.WithFields(log.Fields{"bucket": bucket,
"prefix": prefix,
Expand All @@ -52,6 +55,7 @@ func NewS3Storage(
bucket: bucket,
prefix: prefix,
unsignedRequests: unsignedRequests,
writeACLrule: writeACLrule,
}
return &backend, nil
}
Expand Down Expand Up @@ -139,6 +143,13 @@ func (b *S3Storage) Size(pth string) (int64, error) {
}
}

func (b *S3Storage) GetACLWriteRule() string {
if b.writeACLrule == "" {
return s3.ObjectCannedACLPublicRead
}
return b.writeACLrule
}

func (b *S3Storage) PutFile(pth string, in io.ReadCloser) error {
var buf bytes.Buffer
_, err := buf.ReadFrom(in)
Expand All @@ -150,6 +161,7 @@ func (b *S3Storage) PutFile(pth string, in io.ReadCloser) error {
params := &s3.PutObjectInput{
Bucket: aws.String(b.bucket),
Key: aws.String(key),
ACL: aws.String(b.GetACLWriteRule()),
Body: bytes.NewReader(buf.Bytes()),
}
req, _ := b.svc.PutObjectRequest(params)
Expand Down
52 changes: 52 additions & 0 deletions support/storage/s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2016 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http:https://www.apache.org/licenses/LICENSE-2.0

package storage

import (
"context"
"testing"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

type MockS3 struct {
mock.Mock
s3iface.S3API
}

func TestWriteACLRuleOverride(t *testing.T) {

mockS3 := &MockS3{}
s3Storage := S3Storage{
ctx: context.Background(),
svc: mockS3,
bucket: "bucket",
prefix: "prefix",
unsignedRequests: false,
writeACLrule: s3.ObjectCannedACLBucketOwnerFullControl,
}

aclRule := s3Storage.GetACLWriteRule()
assert.Equal(t, aclRule, s3.ObjectCannedACLBucketOwnerFullControl)
}

func TestWriteACLRuleDefault(t *testing.T) {

mockS3 := &MockS3{}
s3Storage := S3Storage{
ctx: context.Background(),
svc: mockS3,
bucket: "bucket",
prefix: "prefix",
unsignedRequests: false,
writeACLrule: "",
}

aclRule := s3Storage.GetACLWriteRule()
assert.Equal(t, aclRule, s3.ObjectCannedACLPublicRead)
}

0 comments on commit 452b20c

Please sign in to comment.