Skip to content

Make s3 bucket lookup type configurable #4794

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

Merged
merged 2 commits into from
Sep 7, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* [FEATURE] Create OpenTelemetry Bridge for Tracing. Now cortex can send traces to multiple destinations using OTEL Collectors. #4834
* [FEATURE] Added `-api.http-request-headers-to-log` allowing for the addition of HTTP Headers to logs #4803
* [FEATURE] Distributor: Added a new limit `-validation.max-labels-size-bytes` allowing to limit the combined size of labels for each timeseries. #4848
* [FEATURE] Storage/Bucket: Added `-*.s3.bucket-lookup-type` allowing to configure the s3 bucket lookup type. #4794
* [BUGFIX] Memberlist: Add join with no retrying when starting service. #4804
* [BUGFIX] Ruler: Fix /ruler/rule_groups returns YAML with extra fields. #4767

Expand Down
5 changes: 5 additions & 0 deletions docs/blocks-storage/querier.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ blocks_storage:
# CLI flag: -blocks-storage.s3.signature-version
[signature_version: <string> | default = "v4"]

# The s3 bucket lookup style. Supported values are: auto, virtual-hosted,
# path.
# CLI flag: -blocks-storage.s3.bucket-lookup-type
[bucket_lookup_type: <string> | default = "auto"]

# The s3_sse_config configures the S3 server-side encryption.
# The CLI flags prefix for this block config is: blocks-storage
[sse: <s3_sse_config>]
Expand Down
5 changes: 5 additions & 0 deletions docs/blocks-storage/store-gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ blocks_storage:
# CLI flag: -blocks-storage.s3.signature-version
[signature_version: <string> | default = "v4"]

# The s3 bucket lookup style. Supported values are: auto, virtual-hosted,
# path.
# CLI flag: -blocks-storage.s3.bucket-lookup-type
[bucket_lookup_type: <string> | default = "auto"]

# The s3_sse_config configures the S3 server-side encryption.
# The CLI flags prefix for this block config is: blocks-storage
[sse: <s3_sse_config>]
Expand Down
15 changes: 15 additions & 0 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,11 @@ s3:
# CLI flag: -ruler-storage.s3.signature-version
[signature_version: <string> | default = "v4"]

# The s3 bucket lookup style. Supported values are: auto, virtual-hosted,
# path.
# CLI flag: -ruler-storage.s3.bucket-lookup-type
[bucket_lookup_type: <string> | default = "auto"]

# The s3_sse_config configures the S3 server-side encryption.
# The CLI flags prefix for this block config is: ruler-storage
[sse: <s3_sse_config>]
Expand Down Expand Up @@ -1839,6 +1844,11 @@ s3:
# CLI flag: -alertmanager-storage.s3.signature-version
[signature_version: <string> | default = "v4"]

# The s3 bucket lookup style. Supported values are: auto, virtual-hosted,
# path.
# CLI flag: -alertmanager-storage.s3.bucket-lookup-type
[bucket_lookup_type: <string> | default = "auto"]

# The s3_sse_config configures the S3 server-side encryption.
# The CLI flags prefix for this block config is: alertmanager-storage
[sse: <s3_sse_config>]
Expand Down Expand Up @@ -3081,6 +3091,11 @@ s3:
# CLI flag: -blocks-storage.s3.signature-version
[signature_version: <string> | default = "v4"]

# The s3 bucket lookup style. Supported values are: auto, virtual-hosted,
# path.
# CLI flag: -blocks-storage.s3.bucket-lookup-type
[bucket_lookup_type: <string> | default = "auto"]

# The s3_sse_config configures the S3 server-side encryption.
# The CLI flags prefix for this block config is: blocks-storage
[sse: <s3_sse_config>]
Expand Down
11 changes: 6 additions & 5 deletions integration/e2ecortex/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func NewS3Client(cfg s3.Config) (*S3Client, error) {

func NewS3ClientForMinio(minio *e2e.HTTPService, bucketName string) (*S3Client, error) {
return NewS3Client(s3.Config{
Endpoint: minio.HTTPEndpoint(),
BucketName: bucketName,
SecretAccessKey: flagext.Secret{Value: e2edb.MinioSecretKey},
AccessKeyID: e2edb.MinioAccessKey,
Insecure: true,
Endpoint: minio.HTTPEndpoint(),
BucketName: bucketName,
SecretAccessKey: flagext.Secret{Value: e2edb.MinioSecretKey},
AccessKeyID: e2edb.MinioAccessKey,
Insecure: true,
BucketLookupType: s3.BucketAutoLookup,
})
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/cortex/cortex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func TestCortex(t *testing.T) {
Bucket: bucket.Config{
Backend: bucket.S3,
S3: s3.Config{
Endpoint: "localhost",
Endpoint: "localhost",
BucketLookupType: s3.BucketPathLookup,
},
},
},
Expand All @@ -71,7 +72,8 @@ func TestCortex(t *testing.T) {
Bucket: bucket.Config{
Backend: bucket.S3,
S3: s3.Config{
Endpoint: "localhost",
Endpoint: "localhost",
BucketLookupType: s3.BucketPathLookup,
},
},
BucketStore: tsdb.BucketStoreConfig{
Expand Down
7 changes: 6 additions & 1 deletion pkg/storage/bucket/s3/bucket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func newS3Config(cfg Config) (s3.Config, error) {
if err != nil {
return s3.Config{}, err
}
bucketLookupType, err := cfg.bucketLookupType()
if err != nil {
return s3.Config{}, err
}

return s3.Config{
Bucket: cfg.BucketName,
Expand All @@ -53,6 +57,7 @@ func newS3Config(cfg Config) (s3.Config, error) {
Transport: cfg.HTTP.Transport,
},
// Enforce signature version 2 if CLI flag is set
SignatureV2: cfg.SignatureVersion == SignatureVersionV2,
SignatureV2: cfg.SignatureVersion == SignatureVersionV2,
BucketLookupType: bucketLookupType,
}, nil
}
24 changes: 24 additions & 0 deletions pkg/storage/bucket/s3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ const (
// SSES3 config type constant to configure S3 server side encryption with AES-256
// https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html
SSES3 = "SSE-S3"

BucketAutoLookup = "auto"
BucketVirtualHostLookup = "virtual-hosted"
BucketPathLookup = "path"
)

var (
supportedSignatureVersions = []string{SignatureVersionV4, SignatureVersionV2}
supportedSSETypes = []string{SSEKMS, SSES3}
supportedBucketLookupTypes = []string{BucketAutoLookup, BucketVirtualHostLookup, BucketPathLookup}
errUnsupportedSignatureVersion = errors.New("unsupported signature version")
errUnsupportedSSEType = errors.New("unsupported S3 SSE type")
errInvalidSSEContext = errors.New("invalid S3 SSE encryption context")
errInvalidBucketLookupType = errors.New("invalid bucket lookup type")
)

// HTTPConfig stores the http.Transport configuration for the s3 minio client.
Expand All @@ -59,6 +65,7 @@ type Config struct {
AccessKeyID string `yaml:"access_key_id"`
Insecure bool `yaml:"insecure"`
SignatureVersion string `yaml:"signature_version"`
BucketLookupType string `yaml:"bucket_lookup_type"`

SSE SSEConfig `yaml:"sse"`
HTTP HTTPConfig `yaml:"http"`
Expand All @@ -78,6 +85,7 @@ func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
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.")
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.")
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, ", ")))
f.StringVar(&cfg.BucketLookupType, prefix+"s3.bucket-lookup-type", BucketAutoLookup, fmt.Sprintf("The s3 bucket lookup style. Supported values are: %s.", strings.Join(supportedBucketLookupTypes, ", ")))
cfg.SSE.RegisterFlagsWithPrefix(prefix+"s3.sse.", f)
cfg.HTTP.RegisterFlagsWithPrefix(prefix, f)
}
Expand All @@ -87,6 +95,9 @@ func (cfg *Config) Validate() error {
if !util.StringsContain(supportedSignatureVersions, cfg.SignatureVersion) {
return errUnsupportedSignatureVersion
}
if !util.StringsContain(supportedBucketLookupTypes, cfg.BucketLookupType) {
return errInvalidBucketLookupType
}

if err := cfg.SSE.Validate(); err != nil {
return err
Expand All @@ -95,6 +106,19 @@ func (cfg *Config) Validate() error {
return nil
}

func (cfg *Config) bucketLookupType() (s3.BucketLookupType, error) {
switch cfg.BucketLookupType {
case BucketVirtualHostLookup:
return s3.VirtualHostLookup, nil
case BucketPathLookup:
return s3.PathLookup, nil
case BucketAutoLookup:
return s3.AutoLookup, nil
default:
return s3.AutoLookup, errInvalidBucketLookupType
}
}

// SSEConfig configures S3 server side encryption
// struct that is going to receive user input (through config file or CLI)
type SSEConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/storage/bucket/s3/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// defaultConfig should match the default flag values defined in RegisterFlagsWithPrefix.
var defaultConfig = Config{
SignatureVersion: SignatureVersionV4,
BucketLookupType: BucketAutoLookup,
HTTP: HTTPConfig{
Config: bucket_http.Config{
IdleConnTimeout: 90 * time.Second,
Expand Down Expand Up @@ -53,6 +54,7 @@ secret_access_key: test-secret-access-key
access_key_id: test-access-key-id
insecure: true
signature_version: test-signature-version
bucket_lookup_type: virtual-hosted
sse:
type: test-type
kms_key_id: test-kms-key-id
Expand All @@ -75,6 +77,7 @@ http:
AccessKeyID: "test-access-key-id",
Insecure: true,
SignatureVersion: "test-signature-version",
BucketLookupType: BucketVirtualHostLookup,
SSE: SSEConfig{
Type: "test-type",
KMSKeyID: "test-kms-key-id",
Expand Down
13 changes: 7 additions & 6 deletions pkg/storage/bucket/sse_bucket_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ func TestSSEBucketClient_Upload_ShouldInjectCustomSSEConfig(t *testing.T) {
defer srv.Close()

s3Cfg := s3.Config{
Endpoint: srv.Listener.Addr().String(),
Region: "test",
BucketName: "test-bucket",
SecretAccessKey: flagext.Secret{Value: "test"},
AccessKeyID: "test",
Insecure: true,
Endpoint: srv.Listener.Addr().String(),
Region: "test",
BucketName: "test-bucket",
SecretAccessKey: flagext.Secret{Value: "test"},
AccessKeyID: "test",
Insecure: true,
BucketLookupType: s3.BucketPathLookup,
}

s3Client, err := s3.NewBucketClient(s3Cfg, "test", log.NewNopLogger())
Expand Down