Merge 627a37647c
into e85e0592dd
This commit is contained in:
commit
802f7650ba
21
hook/hook.go
21
hook/hook.go
@ -384,15 +384,28 @@ func (h *Hooks) LoadFromFile(path string) error {
|
|||||||
|
|
||||||
// parse hook file for hooks
|
// parse hook file for hooks
|
||||||
file, e := ioutil.ReadFile(path)
|
file, e := ioutil.ReadFile(path)
|
||||||
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
e = json.Unmarshal(file, h)
|
if e = json.Unmarshal(file, h); e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.checkDeprecations()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkDeprecations crawls through a Hooks definition checking for and handling
|
||||||
|
// deprecated values.
|
||||||
|
func (h Hooks) checkDeprecations() {
|
||||||
|
for i := range h {
|
||||||
|
if h[i].TriggerRule != nil && h[i].TriggerRule.Match != nil && h[i].TriggerRule.Match.Type == MatchRegex && h[i].TriggerRule.Match.Regex != "" && h[i].TriggerRule.Match.Value == "" {
|
||||||
|
h[i].TriggerRule.Match.Value = h[i].TriggerRule.Match.Regex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Match iterates through Hooks and returns first one that matches the given ID,
|
// Match iterates through Hooks and returns first one that matches the given ID,
|
||||||
// if no hook matches the given ID, nil is returned
|
// if no hook matches the given ID, nil is returned
|
||||||
func (h *Hooks) Match(id string) *Hook {
|
func (h *Hooks) Match(id string) *Hook {
|
||||||
@ -503,7 +516,7 @@ func (r NotRule) Evaluate(headers, query, payload *map[string]interface{}, body
|
|||||||
// MatchRule will evaluate to true based on the type
|
// MatchRule will evaluate to true based on the type
|
||||||
type MatchRule struct {
|
type MatchRule struct {
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
Regex string `json:"regex,omitempty"`
|
Regex string `json:"regex,omitempty"` // Regex is deprecated. Use Value instead.
|
||||||
Secret string `json:"secret,omitempty"`
|
Secret string `json:"secret,omitempty"`
|
||||||
Value string `json:"value,omitempty"`
|
Value string `json:"value,omitempty"`
|
||||||
Parameter Argument `json:"parameter,omitempty"`
|
Parameter Argument `json:"parameter,omitempty"`
|
||||||
@ -523,7 +536,7 @@ func (r MatchRule) Evaluate(headers, query, payload *map[string]interface{}, bod
|
|||||||
case MatchValue:
|
case MatchValue:
|
||||||
return arg == r.Value, nil
|
return arg == r.Value, nil
|
||||||
case MatchRegex:
|
case MatchRegex:
|
||||||
return regexp.MatchString(r.Regex, arg)
|
return regexp.MatchString(r.Value, arg)
|
||||||
case MatchHashSHA1:
|
case MatchHashSHA1:
|
||||||
_, err := CheckPayloadSignature(*body, r.Secret, arg)
|
_, err := CheckPayloadSignature(*body, r.Secret, arg)
|
||||||
return err == nil, err
|
return err == nil, err
|
||||||
|
@ -183,14 +183,14 @@ var matchRuleTests = []struct {
|
|||||||
err bool
|
err bool
|
||||||
}{
|
}{
|
||||||
{"value", "", "", "z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true, false},
|
{"value", "", "", "z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true, false},
|
||||||
{"regex", "^z", "", "z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true, false},
|
{"regex", "", "", "^z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true, false},
|
||||||
{"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": "b17e04cbb22afa8ffbff8796fc1894ed27badd9e"}, nil, nil, []byte(`{"a": "z"}`), true, false},
|
{"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": "b17e04cbb22afa8ffbff8796fc1894ed27badd9e"}, nil, nil, []byte(`{"a": "z"}`), true, false},
|
||||||
// failures
|
// failures
|
||||||
{"value", "", "", "X", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, false},
|
{"value", "", "", "X", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, false},
|
||||||
{"regex", "^X", "", "", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, false},
|
{"regex", "", "", "^X", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, false},
|
||||||
{"value", "", "2", "X", Argument{"header", "a"}, &map[string]interface{}{"y": "z"}, nil, nil, []byte{}, false, false}, // reference invalid header
|
{"value", "", "2", "X", Argument{"header", "a"}, &map[string]interface{}{"y": "z"}, nil, nil, []byte{}, false, false}, // reference invalid header
|
||||||
// errors
|
// errors
|
||||||
{"regex", "*", "", "", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, true}, // invalid regex
|
{"regex", "", "", "*", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, true}, // invalid regex
|
||||||
{"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": ""}, nil, nil, []byte{}, false, true}, // invalid hmac
|
{"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": ""}, nil, nil, []byte{}, false, true}, // invalid hmac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +49,18 @@
|
|||||||
"name": "ref"
|
"name": "ref"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match":
|
||||||
|
{
|
||||||
|
"type": "regex",
|
||||||
|
"value": "refs.*",
|
||||||
|
"parameter":
|
||||||
|
{
|
||||||
|
"source": "payload",
|
||||||
|
"name": "ref"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user