Skip to content

Commit c5fb200

Browse files
committed
strict decoder for dynconfig
1 parent d12570b commit c5fb200

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

api/v1alpha1/configuration.go

+13-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1alpha1
22

33
import (
4+
"bytes"
45
"crypto/sha256"
56
"fmt"
67
"path"
@@ -89,7 +90,7 @@ func tryFillMissingSections(
8990
}
9091
}
9192

92-
func buildConfiguration(cr *Storage, crDB *Database) (string, error) {
93+
func buildConfiguration(cr *Storage, crDB *Database) ([]byte, error) {
9394
// If any kind of configuration exists on Database object, then
9495
// it will be used to fully override storage object.
9596
// This is a temporary solution that should go away when it would
@@ -101,34 +102,24 @@ func buildConfiguration(cr *Storage, crDB *Database) (string, error) {
101102
rawYamlConfiguration = cr.Spec.Configuration
102103
}
103104

104-
dynconfig, err := TryParseDynconfig(rawYamlConfiguration)
105-
if err == nil {
106-
config, err := yaml.Marshal(dynconfig.Config)
107-
return string(config), err
105+
config := make(map[string]interface{})
106+
if err := yaml.Unmarshal([]byte(rawYamlConfiguration), &config); err != nil {
107+
return []byte(""), err
108108
}
109109

110-
config := make(map[string]interface{})
111-
err = yaml.Unmarshal([]byte(rawYamlConfiguration), &config)
112-
if err != nil {
113-
return "", err
110+
dynconfig := schema.Dynconfig{}
111+
if err := TryParseDynconfig(rawYamlConfiguration, &dynconfig); err == nil {
112+
return yaml.Marshal(dynconfig.Config)
114113
}
115114

116115
generatedConfig := generateSomeDefaults(cr, crDB)
117116
tryFillMissingSections(config, generatedConfig)
118117

119-
data, err := yaml.Marshal(config)
120-
if err != nil {
121-
return "", err
122-
}
123-
124-
return string(data), nil
118+
return yaml.Marshal(config)
125119
}
126120

127-
func TryParseDynconfig(rawYamlConfiguration string) (schema.Dynconfig, error) {
128-
dynconfig := schema.Dynconfig{}
129-
err := yaml.Unmarshal([]byte(rawYamlConfiguration), &dynconfig)
130-
if err != nil {
131-
return schema.Dynconfig{}, err
132-
}
133-
return dynconfig, nil
121+
func TryParseDynconfig(rawYamlConfiguration string, dynconfig *schema.Dynconfig) error {
122+
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
123+
dec.KnownFields(true)
124+
return dec.Decode(&dynconfig)
134125
}

api/v1alpha1/database_webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (r *DatabaseDefaulter) Default(ctx context.Context, obj runtime.Object) err
145145
if err != nil {
146146
return err
147147
}
148-
database.Spec.Configuration = configuration
148+
database.Spec.Configuration = string(configuration)
149149

150150
return nil
151151
}

api/v1alpha1/storage_webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (r *StorageDefaulter) Default(ctx context.Context, obj runtime.Object) erro
170170
if err != nil {
171171
return err
172172
}
173-
storage.Spec.Configuration = configuration
173+
storage.Spec.Configuration = string(configuration)
174174

175175
return nil
176176
}

internal/configuration/schema/schema_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func TestSchema(t *testing.T) {
7070

7171
var _ = Describe("Testing schema", func() {
7272
It("Parse dynconfig", func() {
73-
dynconfig, err := v1alpha1.TryParseDynconfig(dynconfigExample)
73+
dynconfig := schema.Dynconfig{}
74+
err := v1alpha1.TryParseDynconfig(configurationExample, &dynconfig)
7475
Expect(err).ShouldNot(HaveOccurred())
7576
Expect(*dynconfig.Metadata).Should(BeEquivalentTo(schema.Metadata{
7677
Version: 1,
@@ -79,6 +80,12 @@ var _ = Describe("Testing schema", func() {
7980
Expect(dynconfig.Config["yaml_config_enabled"]).Should(BeTrue())
8081
})
8182

83+
It("Try parse static config as dynconfig", func() {
84+
dynconfig := schema.Dynconfig{}
85+
err := v1alpha1.TryParseDynconfig(configurationExample, &dynconfig)
86+
Expect(err).Should(HaveOccurred())
87+
})
88+
8289
It("Parse static config", func() {
8390
yamlConfig := schema.Configuration{}
8491
err := yaml.Unmarshal([]byte(configurationExample), &yamlConfig)

0 commit comments

Comments
 (0)