Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v15] fix: support set.add on nil sets in traits expression parser #49432

Merged
merged 1 commit into from
Nov 26, 2024
Merged
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
66 changes: 60 additions & 6 deletions lib/expression/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ import (
"github.com/gravitational/teleport/lib/utils/typical"
)

func TestEvaluateTraitsMap(t *testing.T) {
t.Parallel()

baseInputTraits := map[string][]string{
var (
baseInputTraits = map[string][]string{
"groups": []string{"devs", "security"},
"username": []string{"alice"},
}

tests := []struct {
testCases = []struct {
desc string
expressions map[string][]string
inputTraits map[string][]string
Expand Down Expand Up @@ -253,7 +251,31 @@ func TestEvaluateTraitsMap(t *testing.T) {
"localEmails": {"alice", "bob", "charlie", "darrell", "esther", "frank"},
},
},
{
desc: "methods on nil set from nonexistent map key",
expressions: map[string][]string{
"a": {`user.spec.traits["a"].add("a")`},
"b": {`ifelse(user.spec.traits["b"].contains("b"), set("z"), set("b"))`},
"c": {`ifelse(user.spec.traits["c"].contains_any(set("c")), set("z"), set("c"))`},
"d": {`ifelse(user.spec.traits["d"].isempty(), set("d"), set("z"))`},
"e": {`user.spec.traits["e"].remove("e")`},
"f": {`user.spec.traits["f"].remove("f").add("f")`},
},
inputTraits: baseInputTraits,
expectedTraits: map[string][]string{
"a": {"a"},
"b": {"b"},
"c": {"c"},
"d": {"d"},
"e": {},
"f": {"f"},
},
},
}
)

func TestEvaluateTraitsMap(t *testing.T) {
t.Parallel()

type evaluationEnv struct {
Traits Dict
Expand All @@ -270,7 +292,7 @@ func TestEvaluateTraitsMap(t *testing.T) {
attributeParser, err := NewTraitsExpressionParser[evaluationEnv](typicalEnvVar)
require.NoError(t, err)

for _, tc := range tests {
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result, err := EvaluateTraitsMap[evaluationEnv](
evaluationEnv{
Expand All @@ -292,3 +314,35 @@ func TestEvaluateTraitsMap(t *testing.T) {
})
}
}

func FuzzTraitsExpressionParser(f *testing.F) {
type evaluationEnv struct {
Traits Dict
}
parser, err := NewTraitsExpressionParser[evaluationEnv](map[string]typical.Variable{
"true": true,
"false": false,
"user.spec.traits": typical.DynamicMap[evaluationEnv, Set](func(env evaluationEnv) (Dict, error) {
return env.Traits, nil
}),
})
require.NoError(f, err)
for _, tc := range testCases {
for _, expressions := range tc.expressions {
for _, expression := range expressions {
f.Add(expression)
}
}
}
f.Fuzz(func(t *testing.T, expression string) {
expr, err := parser.Parse(expression)
if err != nil {
// Many/most fuzzed expressions won't parse, as long as we didn't
// panic that's okay.
return
}
// If the expression parsed, try to evaluate it, errors are okay just
// make sure we don't panic.
_, _ = expr.Evaluate(evaluationEnv{DictFromStringSliceMap(baseInputTraits)})
})
}
3 changes: 3 additions & 0 deletions lib/expression/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func NewSet(values ...string) Set {
}

func (s Set) add(values ...string) Set {
if len(s) == 0 {
return NewSet(values...)
}
out := s.clone()
for _, value := range values {
out[value] = struct{}{}
Expand Down
Loading