-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iammember): add iammember.ChainResolvers
For combining a set of standard resolvers. For example for resolving different types of JWT tokens, or JWT tokens from multiple headers. Context has been added to the resolver API to enable individual resolvers to cache results in the context, for example parsed JWT tokens.
- Loading branch information
Showing
6 changed files
with
153 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package iammember | ||
|
||
import "context" | ||
|
||
// ChainResolvers creates a single resolver out of a chain of many resolvers. | ||
// | ||
// The resulting resolved members will be the union of the members resolved by each resolver. | ||
// | ||
// Execution is done in left-to-right order, including passing of context. | ||
// For example ChainResolvers(one, two, three) will execute one before two before three, and three | ||
// will see context changes of one and two. | ||
// | ||
// If any resolver returns an error, that error is immediately returned and no further resolvers are called. | ||
func ChainResolvers(resolvers ...Resolver) Resolver { | ||
return chainResolver{resolvers: resolvers} | ||
} | ||
|
||
type chainResolver struct { | ||
resolvers []Resolver | ||
} | ||
|
||
func (c chainResolver) ResolveIAMMembers(ctx context.Context) (context.Context, []string, error) { | ||
var result, members []string | ||
var err error | ||
for _, resolver := range c.resolvers { | ||
ctx, members, err = resolver.ResolveIAMMembers(ctx) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
for _, member := range members { | ||
var hasMember bool | ||
for _, resultMember := range result { | ||
if member == resultMember { | ||
hasMember = true | ||
break | ||
} | ||
} | ||
if !hasMember { | ||
result = append(result, member) | ||
} | ||
} | ||
} | ||
return ctx, result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package iammember | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestChainResolvers(t *testing.T) { | ||
t.Run("no resolvers", func(t *testing.T) { | ||
ctx, members, err := ChainResolvers().ResolveIAMMembers(context.Background()) | ||
assert.Equal(t, ctx, context.Background()) | ||
assert.Assert(t, members == nil) | ||
assert.NilError(t, err) | ||
}) | ||
|
||
t.Run("single", func(t *testing.T) { | ||
expected := []string{"foo", "bar"} | ||
ctx, actual, err := ChainResolvers(constantResolver{expected}).ResolveIAMMembers(context.Background()) | ||
assert.Equal(t, ctx, context.Background()) | ||
assert.DeepEqual(t, expected, actual) | ||
assert.NilError(t, err) | ||
}) | ||
|
||
t.Run("multi", func(t *testing.T) { | ||
expected := []string{"foo", "bar", "baz"} | ||
ctx, actual, err := ChainResolvers( | ||
constantResolver{members: []string{"foo", "bar"}}, | ||
constantResolver{members: []string{"baz"}}, | ||
).ResolveIAMMembers(context.Background()) | ||
assert.Equal(t, ctx, context.Background()) | ||
assert.DeepEqual(t, expected, actual) | ||
assert.NilError(t, err) | ||
}) | ||
|
||
t.Run("multi duplicates", func(t *testing.T) { | ||
expected := []string{"foo", "bar", "baz"} | ||
ctx, actual, err := ChainResolvers( | ||
constantResolver{members: []string{"foo", "bar"}}, | ||
constantResolver{members: []string{"bar", "baz"}}, | ||
).ResolveIAMMembers(context.Background()) | ||
assert.Equal(t, ctx, context.Background()) | ||
assert.DeepEqual(t, expected, actual) | ||
assert.NilError(t, err) | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
ctx, actual, err := ChainResolvers( | ||
constantResolver{members: []string{"foo", "bar"}}, | ||
errorResolver{err: errors.New("boom")}, | ||
).ResolveIAMMembers(context.Background()) | ||
assert.Assert(t, ctx == nil) | ||
assert.Assert(t, actual == nil) | ||
assert.Error(t, err, "boom") | ||
}) | ||
} | ||
|
||
type constantResolver struct { | ||
members []string | ||
} | ||
|
||
func (c constantResolver) ResolveIAMMembers(ctx context.Context) (context.Context, []string, error) { | ||
return ctx, c.members, nil | ||
} | ||
|
||
type errorResolver struct { | ||
err error | ||
} | ||
|
||
func (e errorResolver) ResolveIAMMembers(ctx context.Context) (context.Context, []string, error) { | ||
return nil, nil, e.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters