Skip to content

Commit 84d93fc

Browse files
authored
fix(configuration): Validate the valuable object when decode it to lookup env variable (#54)
1 parent 898c5eb commit 84d93fc

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

internal/valuable/valuable.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,50 @@ type ValueFromSource struct {
3535
EnvRef *string `json:"envRef,omitempty"`
3636
}
3737

38+
// Validate validates the Valuable object and returns an error if any
39+
// validation fails. In case of envRef, the env variable must exist.
40+
func (v *Valuable) Validate() error {
41+
if v.ValueFrom != nil && v.ValueFrom.EnvRef != nil {
42+
if _, ok := os.LookupEnv(*v.ValueFrom.EnvRef); !ok {
43+
return fmt.Errorf("enviroment variable %s not found", *v.ValueFrom.EnvRef)
44+
}
45+
}
46+
47+
return nil
48+
}
49+
3850
// SerializeValuable serialize anything to a Valuable
3951
// @param data is the data to serialize
4052
// @return the serialized Valuable
4153
func SerializeValuable(data interface{}) (*Valuable, error) {
54+
var v *Valuable = &Valuable{}
4255
switch t := data.(type) {
4356
case string:
44-
return &Valuable{Value: &t}, nil
57+
v.Value = &t
4558
case int, float32, float64, bool:
4659
str := fmt.Sprint(t)
47-
return &Valuable{Value: &str}, nil
60+
v.Value = &str
4861
case nil:
4962
return &Valuable{}, nil
5063
case map[interface{}]interface{}:
5164
var val *Valuable
5265
if err := mapstructure.Decode(data, &val); err != nil {
5366
return nil, err
5467
}
55-
return val, nil
68+
v = val
5669
default:
5770
valuable := Valuable{}
5871
if err := mapstructure.Decode(data, &valuable); err != nil {
5972
return nil, fmt.Errorf("unimplemented valuable type %s", reflect.TypeOf(data).String())
6073
}
61-
return &valuable, nil
74+
v = &valuable
75+
}
76+
77+
if err := v.Validate(); err != nil {
78+
return nil, err
6279
}
80+
81+
return v, nil
6382
}
6483

6584
// Get returns all values of the Valuable as a slice

internal/valuable/valuable_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ func (suite *TestSuiteValuable) BeforeTest(suiteName, testName string) {
2525
os.Setenv(suite.testEnvName, suite.testValue)
2626
}
2727

28+
func (suite *TestSuiteValuable) TestValidate() {
29+
assert := assert.New(suite.T())
30+
31+
tests := []struct {
32+
name string
33+
input *Valuable
34+
wantErr bool
35+
}{
36+
{"a basic value", &Valuable{Value: &suite.testValue}, false},
37+
{"a basic list of values", &Valuable{Values: suite.testValues}, false},
38+
{"a basic value with a basic list", &Valuable{Value: &suite.testValue, Values: suite.testValues}, false},
39+
{"an empty valueFrom", &Valuable{ValueFrom: &ValueFromSource{}}, false},
40+
{"an environment ref with invalid name", &Valuable{ValueFrom: &ValueFromSource{EnvRef: &suite.testInvalidEnvName}}, true},
41+
{"an environment ref with valid name", &Valuable{ValueFrom: &ValueFromSource{EnvRef: &suite.testEnvName}}, false},
42+
}
43+
44+
for _, test := range tests {
45+
err := test.input.Validate()
46+
if test.wantErr && assert.Error(err, "this test must be crash %s", err) {
47+
} else {
48+
assert.NoError(err, "cannot validate test %s", test.name)
49+
}
50+
}
51+
}
52+
2853
func (suite *TestSuiteValuable) TestSerializeValuable() {
2954
assert := assert.New(suite.T())
3055

pkg/factory/mapstructure_decode.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ func DecodeHook(f reflect.Type, t reflect.Type, data interface{}) (interface{},
2828
}
2929
}
3030

31+
if err != nil {
32+
return nil, err
33+
}
34+
3135
return &InputConfig{
3236
Valuable: *v,
3337
Name: name,
34-
}, err
38+
}, nil
3539
}

tests/webhooks.tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ specs:
1515
value: '{{ Outputs.header.value }}'
1616
- name: second
1717
valueFrom:
18-
envRef: SECRET_TOKEN
18+
staticRef: test
1919
storage: []

0 commit comments

Comments
 (0)