Skip to content

Commit c5bc5e8

Browse files
author
Nikita Volnov
committed
Added bucket-lookup-type field to s3 config
1 parent 6223316 commit c5bc5e8

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

pkg/objstore/providers/s3/bucket_client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ func newS3Config(cfg Config) (s3.Config, error) {
3939
}
4040

4141
bucketLookupType := s3.AutoLookup
42-
if cfg.ForcePathStyle {
42+
if cfg.ForcePathStyle || cfg.BucketLookupType == PathStyleLookup {
4343
bucketLookupType = s3.PathLookup
44+
} else if cfg.BucketLookupType == VirtualHostedStyleLookup {
45+
bucketLookupType = s3.VirtualHostLookup
4446
}
4547

4648
return s3.Config{

pkg/objstore/providers/s3/config.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,25 @@ const (
3131
// SSES3 config type constant to configure S3 server side encryption with AES-256
3232
// https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html
3333
SSES3 = "SSE-S3"
34+
35+
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html#path-style-access
36+
PathStyleLookup = "path-style"
37+
38+
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html#virtual-hosted-style-access
39+
VirtualHostedStyleLookup = "virtual-hosted-style"
40+
41+
AutoLookup = "auto"
3442
)
3543

3644
var (
3745
supportedSignatureVersions = []string{SignatureVersionV4, SignatureVersionV2}
3846
supportedSSETypes = []string{SSEKMS, SSES3}
47+
supportedBucketLookupTypes = []string{PathStyleLookup, VirtualHostedStyleLookup, AutoLookup}
3948
errUnsupportedSignatureVersion = errors.New("unsupported signature version")
4049
errUnsupportedSSEType = errors.New("unsupported S3 SSE type")
4150
errInvalidSSEContext = errors.New("invalid S3 SSE encryption context")
51+
errBucketLookupConfigConflict = errors.New("cannot use s3.force-path-style = true and s3.bucket-lookup-type = virtual-hosted-style at the same time")
52+
errUnsupportedBucketLookupType = errors.New("invalid S3 bucket lookup type")
4253
)
4354

4455
// HTTPConfig stores the http.Transport configuration for the s3 minio client.
@@ -78,6 +89,7 @@ type Config struct {
7889
Insecure bool `yaml:"insecure" category:"advanced"`
7990
SignatureVersion string `yaml:"signature_version" category:"advanced"`
8091
ForcePathStyle bool `yaml:"force_path_style" category:"advanced"`
92+
BucketLookupType string `yaml:"bucket_lookup_type" category:"advanced"`
8193

8294
SSE SSEConfig `yaml:"sse"`
8395
HTTP HTTPConfig `yaml:"http"`
@@ -96,7 +108,8 @@ func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
96108
f.StringVar(&cfg.Region, prefix+"s3.region", "", "S3 region. If unset, the client will issue a S3 GetBucketLocation API call to autodetect it.")
97109
f.StringVar(&cfg.Endpoint, prefix+"s3.endpoint", "", "The S3 bucket endpoint. It could be an AWS S3 endpoint listed at https://docs.aws.amazon.com/general/latest/gr/s3.html or the address of an S3-compatible service in hostname:port format.")
98110
f.BoolVar(&cfg.Insecure, prefix+"s3.insecure", false, "If enabled, use http:// for the S3 endpoint instead of https://. This could be useful in local dev/test environments while using an S3-compatible backend storage, like Minio.")
99-
f.BoolVar(&cfg.ForcePathStyle, prefix+"s3.force-path-style", false, "Set this to `true` to force the bucket lookup to be using path-style.")
111+
f.BoolVar(&cfg.ForcePathStyle, prefix+"s3.force-path-style", false, "Deprecated, use s3.bucket-lookup-type instead. Set this to `true` to force the bucket lookup to be using path-style.")
112+
f.StringVar(&cfg.BucketLookupType, prefix+"s3.bucket-lookup-type", AutoLookup, fmt.Sprintf("S3 bucket lookup style, use one of: %v", supportedBucketLookupTypes))
100113
f.StringVar(&cfg.SignatureVersion, prefix+"s3.signature-version", SignatureVersionV4, fmt.Sprintf("The signature version to use for authenticating against S3. Supported values are: %s.", strings.Join(supportedSignatureVersions, ", ")))
101114
cfg.SSE.RegisterFlagsWithPrefix(prefix+"s3.sse.", f)
102115
cfg.HTTP.RegisterFlagsWithPrefix(prefix, f)
@@ -108,6 +121,14 @@ func (cfg *Config) Validate() error {
108121
return errUnsupportedSignatureVersion
109122
}
110123

124+
if cfg.ForcePathStyle && cfg.BucketLookupType == VirtualHostedStyleLookup {
125+
return errBucketLookupConfigConflict
126+
}
127+
128+
if !lo.Contains(supportedBucketLookupTypes, cfg.BucketLookupType) {
129+
return errUnsupportedBucketLookupType
130+
}
131+
111132
if err := cfg.SSE.Validate(); err != nil {
112133
return err
113134
}

pkg/objstore/providers/s3/config_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,44 @@ func TestParseKMSEncryptionContext(t *testing.T) {
117117
assert.NoError(t, err)
118118
assert.Equal(t, expected, actual)
119119
}
120+
121+
func TestConfig_Validate(t *testing.T) {
122+
tests := map[string]struct {
123+
setup func() *Config
124+
expected error
125+
}{
126+
"should pass with default config": {
127+
setup: func() *Config {
128+
cfg := &Config{}
129+
flagext.DefaultValues(cfg)
130+
131+
return cfg
132+
},
133+
},
134+
"should fail on invalid bucket lookup style": {
135+
setup: func() *Config {
136+
cfg := &Config{}
137+
flagext.DefaultValues(cfg)
138+
cfg.BucketLookupType = "invalid"
139+
return cfg
140+
},
141+
expected: errUnsupportedBucketLookupType,
142+
},
143+
"should fail if force-path-style conflicts with bucket-lookup-type": {
144+
setup: func() *Config {
145+
cfg := &Config{}
146+
flagext.DefaultValues(cfg)
147+
cfg.ForcePathStyle = true
148+
cfg.BucketLookupType = VirtualHostedStyleLookup
149+
return cfg
150+
},
151+
expected: errBucketLookupConfigConflict,
152+
},
153+
}
154+
155+
for testName, testData := range tests {
156+
t.Run(testName, func(t *testing.T) {
157+
assert.Equal(t, testData.expected, testData.setup().Validate())
158+
})
159+
}
160+
}

0 commit comments

Comments
 (0)