From fe148af455e90e77c46a3e5852e2c4d9bfc3f9ca Mon Sep 17 00:00:00 2001 From: Keepers Date: Mon, 3 Oct 2022 15:37:20 -0600 Subject: [PATCH] normalize bucket naming during config lookup (#1025) ## Description Ensure s3 bucket name inputs are normalized in all uses. Currently, the normalized format is applied when using storage, but not when passing around --bucket flag values elsewhere. ## Type of change - [x] :bug: Bugfix ## Issue(s) * #975 ## Test Plan - [x] :muscle: Manual - [x] :zap: Unit test --- src/cli/config/storage.go | 4 ++++ src/internal/common/buckets.go | 13 +++++++++++++ src/internal/common/buckets_test.go | 30 +++++++++++++++++++++++++++++ src/pkg/storage/s3.go | 6 +++--- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/internal/common/buckets.go create mode 100644 src/internal/common/buckets_test.go diff --git a/src/cli/config/storage.go b/src/cli/config/storage.go index abb3640fd..bc71a40b8 100644 --- a/src/cli/config/storage.go +++ b/src/cli/config/storage.go @@ -57,6 +57,10 @@ func configureStorage( return store, errors.Wrap(err, "reading s3 configs from corso config file") } + if b, ok := overrides[storage.Bucket]; ok { + overrides[storage.Bucket] = common.NormalizeBucket(b) + } + if err := mustMatchConfig(vpr, s3Overrides(overrides)); err != nil { return store, errors.Wrap(err, "verifying s3 configs in corso config file") } diff --git a/src/internal/common/buckets.go b/src/internal/common/buckets.go new file mode 100644 index 000000000..73e207d6e --- /dev/null +++ b/src/internal/common/buckets.go @@ -0,0 +1,13 @@ +package common + +import "strings" + +// NormalizeBuckets ensures that bucket names are cleaned and +// standardized according to the downstream needs of minio. +// +// Any url prefixing to location the bucket (ex: s3://bckt) +// will be removed, leaving only the bucket name (bckt). +// Corso should only utilize or store the normalized name. +func NormalizeBucket(b string) string { + return strings.TrimPrefix(b, "s3://") +} diff --git a/src/internal/common/buckets_test.go b/src/internal/common/buckets_test.go new file mode 100644 index 000000000..dbc14d704 --- /dev/null +++ b/src/internal/common/buckets_test.go @@ -0,0 +1,30 @@ +package common_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/common" +) + +type CommonBucketsSuite struct { + suite.Suite + ctx context.Context +} + +func TestCommonBucketsSuite(t *testing.T) { + suite.Run(t, new(CommonBucketsSuite)) +} + +func (suite *CommonBucketsSuite) TestDoesThings() { + t := suite.T() + trimmablePrefixes := []string{"s3://"} + + for _, pfx := range trimmablePrefixes { + assert.Equal(t, "fnords", common.NormalizeBucket(pfx+"fnords")) + assert.Equal(t, "smarf", "smarf") + } +} diff --git a/src/pkg/storage/s3.go b/src/pkg/storage/s3.go index 367d3ce93..cc20e2a36 100644 --- a/src/pkg/storage/s3.go +++ b/src/pkg/storage/s3.go @@ -1,9 +1,9 @@ package storage import ( - "strings" - "github.com/pkg/errors" + + "github.com/alcionai/corso/src/internal/common" ) type S3Config struct { @@ -28,7 +28,7 @@ const ( func (c S3Config) Normalize() S3Config { return S3Config{ - Bucket: strings.TrimPrefix(c.Bucket, "s3://"), + Bucket: common.NormalizeBucket(c.Bucket), Endpoint: c.Endpoint, Prefix: c.Prefix, }