mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-08-04 02:08:59 -04:00

* feat(config): add Thresholds to the top level config file Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(config): make String() on ExpressionOrList join the component expressions Signed-off-by: Xe Iaso <me@xeiaso.net> * test(config): ensure unparseable json fails Signed-off-by: Xe Iaso <me@xeiaso.net> * fix(config): if no thresholds are set, use the default thresholds Signed-off-by: Xe Iaso <me@xeiaso.net> * feat(policy): half implement thresholds Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(policy): continue wiring things up Signed-off-by: Xe Iaso <me@xeiaso.net> * feat(lib): wire up thresholds Signed-off-by: Xe Iaso <me@xeiaso.net> * test(lib): handle behavior from legacy configurations Signed-off-by: Xe Iaso <me@xeiaso.net> * docs: document thresholds Signed-off-by: Xe Iaso <me@xeiaso.net> * docs: update CHANGELOG, refer to threshold configuration Signed-off-by: Xe Iaso <me@xeiaso.net> * fix(lib): fix build Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(lib): fix U1000 Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net> Signed-off-by: Jason Cameron <git@jasoncameron.dev> Co-authored-by: Jason Cameron <git@jasoncameron.dev>
267 lines
4.9 KiB
Go
267 lines
4.9 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
|
|
yaml "sigs.k8s.io/yaml/goyaml.v3"
|
|
)
|
|
|
|
func TestExpressionOrListMarshalJSON(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
input *ExpressionOrList
|
|
output []byte
|
|
err error
|
|
}{
|
|
{
|
|
name: "single expression",
|
|
input: &ExpressionOrList{
|
|
Expression: "true",
|
|
},
|
|
output: []byte(`"true"`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "all",
|
|
input: &ExpressionOrList{
|
|
All: []string{"true", "true"},
|
|
},
|
|
output: []byte(`{"all":["true","true"]}`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "all one",
|
|
input: &ExpressionOrList{
|
|
All: []string{"true"},
|
|
},
|
|
output: []byte(`"true"`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "any",
|
|
input: &ExpressionOrList{
|
|
Any: []string{"true", "false"},
|
|
},
|
|
output: []byte(`{"any":["true","false"]}`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "any one",
|
|
input: &ExpressionOrList{
|
|
Any: []string{"true"},
|
|
},
|
|
output: []byte(`"true"`),
|
|
err: nil,
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := json.Marshal(tt.input)
|
|
if !errors.Is(err, tt.err) {
|
|
t.Errorf("wanted marshal error: %v but got: %v", tt.err, err)
|
|
}
|
|
|
|
if !bytes.Equal(result, tt.output) {
|
|
t.Logf("wanted: %s", string(tt.output))
|
|
t.Logf("got: %s", string(result))
|
|
t.Error("mismatched output")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpressionOrListMarshalYAML(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
input *ExpressionOrList
|
|
output []byte
|
|
err error
|
|
}{
|
|
{
|
|
name: "single expression",
|
|
input: &ExpressionOrList{
|
|
Expression: "true",
|
|
},
|
|
output: []byte(`"true"`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "all",
|
|
input: &ExpressionOrList{
|
|
All: []string{"true", "true"},
|
|
},
|
|
output: []byte(`all:
|
|
- "true"
|
|
- "true"`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "all one",
|
|
input: &ExpressionOrList{
|
|
All: []string{"true"},
|
|
},
|
|
output: []byte(`"true"`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "any",
|
|
input: &ExpressionOrList{
|
|
Any: []string{"true", "false"},
|
|
},
|
|
output: []byte(`any:
|
|
- "true"
|
|
- "false"`),
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "any one",
|
|
input: &ExpressionOrList{
|
|
Any: []string{"true"},
|
|
},
|
|
output: []byte(`"true"`),
|
|
err: nil,
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := yaml.Marshal(tt.input)
|
|
if !errors.Is(err, tt.err) {
|
|
t.Errorf("wanted marshal error: %v but got: %v", tt.err, err)
|
|
}
|
|
|
|
result = bytes.TrimSpace(result)
|
|
|
|
if !bytes.Equal(result, tt.output) {
|
|
t.Logf("wanted: %q", string(tt.output))
|
|
t.Logf("got: %q", string(result))
|
|
t.Error("mismatched output")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpressionOrListUnmarshalJSON(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
err error
|
|
validErr error
|
|
result *ExpressionOrList
|
|
name string
|
|
inp string
|
|
}{
|
|
{
|
|
name: "simple",
|
|
inp: `"\"User-Agent\" in headers"`,
|
|
result: &ExpressionOrList{
|
|
Expression: `"User-Agent" in headers`,
|
|
},
|
|
},
|
|
{
|
|
name: "object-and",
|
|
inp: `{
|
|
"all": ["\"User-Agent\" in headers"]
|
|
}`,
|
|
result: &ExpressionOrList{
|
|
All: []string{
|
|
`"User-Agent" in headers`,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "object-or",
|
|
inp: `{
|
|
"any": ["\"User-Agent\" in headers"]
|
|
}`,
|
|
result: &ExpressionOrList{
|
|
Any: []string{
|
|
`"User-Agent" in headers`,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "both-or-and",
|
|
inp: `{
|
|
"all": ["\"User-Agent\" in headers"],
|
|
"any": ["\"User-Agent\" in headers"]
|
|
}`,
|
|
validErr: ErrExpressionCantHaveBoth,
|
|
},
|
|
{
|
|
name: "expression-empty",
|
|
inp: `{
|
|
"any": []
|
|
}`,
|
|
validErr: ErrExpressionEmpty,
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var eol ExpressionOrList
|
|
|
|
if err := json.Unmarshal([]byte(tt.inp), &eol); !errors.Is(err, tt.err) {
|
|
t.Errorf("wanted unmarshal error: %v but got: %v", tt.err, err)
|
|
}
|
|
|
|
if tt.result != nil && !eol.Equal(tt.result) {
|
|
t.Logf("wanted: %#v", tt.result)
|
|
t.Logf("got: %#v", &eol)
|
|
t.Fatal("parsed expression is not what was expected")
|
|
}
|
|
|
|
if err := eol.Valid(); !errors.Is(err, tt.validErr) {
|
|
t.Errorf("wanted validation error: %v but got: %v", tt.err, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpressionOrListString(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
in ExpressionOrList
|
|
out string
|
|
}{
|
|
{
|
|
name: "single expression",
|
|
in: ExpressionOrList{
|
|
Expression: "true",
|
|
},
|
|
out: "true",
|
|
},
|
|
{
|
|
name: "all",
|
|
in: ExpressionOrList{
|
|
All: []string{"true"},
|
|
},
|
|
out: "( true )",
|
|
},
|
|
{
|
|
name: "all with &&",
|
|
in: ExpressionOrList{
|
|
All: []string{"true", "true"},
|
|
},
|
|
out: "( true ) && ( true )",
|
|
},
|
|
{
|
|
name: "any",
|
|
in: ExpressionOrList{
|
|
All: []string{"true"},
|
|
},
|
|
out: "( true )",
|
|
},
|
|
{
|
|
name: "any with ||",
|
|
in: ExpressionOrList{
|
|
Any: []string{"true", "true"},
|
|
},
|
|
out: "( true ) || ( true )",
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.in.String()
|
|
if result != tt.out {
|
|
t.Errorf("wanted %q, got: %q", tt.out, result)
|
|
}
|
|
})
|
|
}
|
|
}
|