open-consul/agent/consul/authmethod/testing.go
R.B. Boyer 3ac5a841ec
acl: refactor the authmethod.Validator interface (#7760)
This is a collection of refactors that make upcoming PRs easier to digest.

The main change is the introduction of the authmethod.Identity struct.
In the one and only current auth method (type=kubernetes) all of the
trusted identity attributes are both selectable and projectable, so they
were just passed around as a map[string]string.

When namespaces were added, this was slightly changed so that the
enterprise metadata can also come back from the login operation, so
login now returned two fields.

Now with some upcoming auth methods it won't be true that all identity
attributes will be both selectable and projectable, so rather than
update the login function to return 3 pieces of data it seemed worth it
to wrap those fields up and give them a proper name.
2020-05-01 17:35:28 -05:00

46 lines
1.1 KiB
Go

package authmethod
import (
"sort"
"github.com/hashicorp/go-bexpr"
"github.com/mitchellh/go-testing-interface"
"github.com/stretchr/testify/require"
)
// RequireIdentityMatch tests to see if the given Identity matches the provided
// projected vars and filters for testing purpose.
func RequireIdentityMatch(t testing.T, id *Identity, projectedVars map[string]string, filters ...string) {
t.Helper()
gotNames := id.ProjectedVarNames()
require.Equal(t, projectedVars, id.ProjectedVars)
expectNames := make([]string, 0, len(projectedVars))
for k, _ := range projectedVars {
expectNames = append(expectNames, k)
}
sort.Strings(expectNames)
sort.Strings(gotNames)
require.Equal(t, expectNames, gotNames)
require.Nil(t, id.EnterpriseMeta)
for _, filter := range filters {
eval, err := bexpr.CreateEvaluatorForType(filter, nil, id.SelectableFields)
if err != nil {
t.Fatalf("filter %q got err: %v", filter, err)
}
result, err := eval.Evaluate(id.SelectableFields)
if err != nil {
t.Fatalf("filter %q got err: %v", filter, err)
}
if !result {
t.Fatalf("filter %q did not match", filter)
}
}
}