Skip to content
Open
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
7 changes: 6 additions & 1 deletion client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/thanos-io/objstore"
"github.com/thanos-io/objstore/clientutil"
"github.com/thanos-io/objstore/providers/azure"
"github.com/thanos-io/objstore/providers/bos"
"github.com/thanos-io/objstore/providers/cos"
Expand Down Expand Up @@ -37,8 +38,12 @@ type BucketConfig struct {
// NOTE: confContentYaml can contain secrets.
func NewBucket(logger log.Logger, confContentYaml []byte, component string, wrapRoundtripper func(http.RoundTripper) http.RoundTripper) (objstore.Bucket, error) {
level.Info(logger).Log("msg", "loading bucket configuration")
trimmedYaml, err := clientutil.TrimExtraFields(confContentYaml)
if err != nil {
return nil, errors.Wrap(err, "trimming extra fields failed")
}
bucketConf := &BucketConfig{}
if err := yaml.UnmarshalStrict(confContentYaml, bucketConf); err != nil {
if err := yaml.UnmarshalStrict(trimmedYaml, bucketConf); err != nil {
return nil, errors.Wrap(err, "parsing config YAML file")
}

Expand Down
28 changes: 28 additions & 0 deletions clientutil/trimconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package clientutil

import (
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

func TrimExtraFields(confContentYaml []byte) ([]byte, error) {
var raw map[string]interface{}
if err := yaml.Unmarshal(confContentYaml, &raw); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal YAML")
}
allowed := map[string]bool{
"type": true,
"config": true,
"prefix": true,
}
filtered := make(map[string]interface{})
for key, value := range raw {
if allowed[key] {
filtered[key] = value
}
}
return yaml.Marshal(filtered)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt it kinda weird that we marshal it back to yaml just to parse it again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. Lets keep this simple and handle hedging_config field only seperately.

}
77 changes: 77 additions & 0 deletions clientutil/trimconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package clientutil

import (
"reflect"
"testing"

"gopkg.in/yaml.v2"
)

func TestTrimExtraFields(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]interface{}
expectErr bool
}{
{
name: "YAML with extra field",
input: `
type: "s3"
config:
key1: "value1"
key2: "value2"
prefix: "/path/to/bucket"
hedging_config:
enabled: false
up_to: 3
quantile: 0.9
`,
expected: map[string]interface{}{
"type": "s3",
"config": map[interface{}]interface{}{"key1": "value1", "key2": "value2"},
"prefix": "/path/to/bucket",
},
expectErr: false,
},
{
name: "YAML without extra field",
input: `
type: "s3"
config:
key: "value"
prefix: "/path/to/bucket"
`,
expected: map[string]interface{}{
"type": "s3",
"config": map[interface{}]interface{}{"key": "value"},
"prefix": "/path/to/bucket",
},
expectErr: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
output, err := TrimExtraFields([]byte(tc.input))
if (err != nil) != tc.expectErr {
t.Fatalf("TrimExtraFields() error = %v, expectErr %v", err, tc.expectErr)
}
if err != nil {
return
}

var got map[string]interface{}
if err := yaml.Unmarshal(output, &got); err != nil {
t.Fatalf("failed to unmarshal output YAML: %v", err)
}

if !reflect.DeepEqual(got, tc.expected) {
t.Errorf("TrimExtraFields() got = %v, expected %v", got, tc.expected)
}
})
}
}
Loading