Skip to content

Commit 219be1b

Browse files
devmentalitysimonswine
authored andcommitted
Added bucket-lookup-type field to s3 config (grafana#3788)
* Added bucket-lookup-type field to s3 config * Review fixes: validation & deprecation warning * Update reference docs * Formatted code --------- Co-authored-by: Christian Simon <[email protected]>
1 parent bcf4c28 commit 219be1b

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

cmd/pyroscope/help-all.txt.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,14 +901,16 @@ Usage of ./pyroscope:
901901
JSON either from a Google Developers Console client_credentials.json file, or a Google Developers service account key. Needs to be valid JSON, not a filesystem path.
902902
-storage.s3.access-key-id string
903903
S3 access key ID
904+
-storage.s3.bucket-lookup-type string
905+
S3 bucket lookup style, use one of: [path-style virtual-hosted-style auto] (default "auto")
904906
-storage.s3.bucket-name string
905907
S3 bucket name
906908
-storage.s3.endpoint string
907909
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.
908910
-storage.s3.expect-continue-timeout duration
909911
The time to wait for a server's first response headers after fully writing the request headers if the request has an Expect header. 0 to send the request body immediately. (default 1s)
910912
-storage.s3.force-path-style
911-
Set this to `true` to force the bucket lookup to be using path-style.
913+
Deprecated, use s3.bucket-lookup-type instead. Set this to `true` to force the bucket lookup to be using path-style.
912914
-storage.s3.http.idle-conn-timeout duration
913915
The time an idle connection will remain idle before closing. (default 1m30s)
914916
-storage.s3.http.insecure-skip-verify

pkg/objstore/providers/s3/bucket_client.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package s3
77

88
import (
99
"github.com/go-kit/log"
10+
"github.com/go-kit/log/level"
1011
"github.com/prometheus/common/model"
1112
"github.com/thanos-io/objstore"
1213
"github.com/thanos-io/objstore/providers/s3"
@@ -19,6 +20,8 @@ func NewBucketClient(cfg Config, name string, logger log.Logger) (objstore.Bucke
1920
return nil, err
2021
}
2122

23+
warnForDeprecatedConfigFields(cfg, logger)
24+
2225
return s3.NewBucketWithConfig(logger, s3Cfg, name)
2326
}
2427

@@ -29,6 +32,8 @@ func NewBucketReaderClient(cfg Config, name string, logger log.Logger) (objstore
2932
return nil, err
3033
}
3134

35+
warnForDeprecatedConfigFields(cfg, logger)
36+
3237
return s3.NewBucketWithConfig(logger, s3Cfg, name)
3338
}
3439

@@ -39,8 +44,10 @@ func newS3Config(cfg Config) (s3.Config, error) {
3944
}
4045

4146
bucketLookupType := s3.AutoLookup
42-
if cfg.ForcePathStyle {
47+
if cfg.ForcePathStyle || cfg.BucketLookupType == PathStyleLookup {
4348
bucketLookupType = s3.PathLookup
49+
} else if cfg.BucketLookupType == VirtualHostedStyleLookup {
50+
bucketLookupType = s3.VirtualHostLookup
4451
}
4552

4653
return s3.Config{
@@ -67,3 +74,9 @@ func newS3Config(cfg Config) (s3.Config, error) {
6774
SignatureV2: cfg.SignatureVersion == SignatureVersionV2,
6875
}, nil
6976
}
77+
78+
func warnForDeprecatedConfigFields(cfg Config, logger log.Logger) {
79+
if cfg.ForcePathStyle {
80+
level.Warn(logger).Log("msg", "S3 bucket client config has a deprecated s3.force-path-style flag set. Please, use s3.bucket-lookup-type instead.")
81+
}
82+
}

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 != AutoLookup && cfg.BucketLookupType != PathStyleLookup {
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+
}

pkg/phlare/phlare.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ import (
3131
"github.com/grafana/dskit/signals"
3232
"github.com/grafana/dskit/spanprofiler"
3333
wwtracing "github.com/grafana/dskit/tracing"
34-
"github.com/grafana/pyroscope-go"
3534
grpcgw "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
3635
"github.com/opentracing/opentracing-go"
3736
"github.com/prometheus/client_golang/prometheus"
3837
"github.com/prometheus/common/version"
3938
"github.com/samber/lo"
4039
"google.golang.org/grpc/health"
4140

41+
"github.com/grafana/pyroscope-go"
42+
4243
"github.com/grafana/pyroscope/pkg/api"
4344
apiversion "github.com/grafana/pyroscope/pkg/api/version"
4445
"github.com/grafana/pyroscope/pkg/cfg"

0 commit comments

Comments
 (0)