Skip to content

Commit

Permalink
feat(selector) add test case and example
Browse files Browse the repository at this point in the history
Signed-off-by: aimuz <mr.imuz@gmail.com>
  • Loading branch information
aimuz committed Sep 5, 2022
1 parent 923dae4 commit acbec6b
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 47 deletions.
4 changes: 2 additions & 2 deletions interceptors/selector/doc.go
Expand Up @@ -4,8 +4,8 @@ Package selector
`selector` a generic server-side selector middleware for gRPC.
# Server Side Selector Middleware
It allows to set check rules to whitelist or blacklist middleware such as Auth
Requests that match the rules will go to the next level of middleware
It allows to set check rules to allowlist or blocklist middleware such as Auth
interceptors to toggle behavior on or off based on the request path.
Please see examples for simple examples of use.
*/
Expand Down
43 changes: 8 additions & 35 deletions interceptors/selector/selector.go
Expand Up @@ -8,49 +8,22 @@ import (

type MatchFunc func(ctx context.Context, fullMethod string) bool

type options struct {
match MatchFunc
}

type Option func(o *options)

// WithMatch ...
func WithMatch(fn MatchFunc) Option {
return func(o *options) {
o.match = fn
}
}

func evaluateServerOpt(opts []Option) *options {
optCopy := &options{}
optCopy.match = DefaultMatch
for _, o := range opts {
o(optCopy)
}
return optCopy
}

// DefaultMatch is the default implementation, always selected
func DefaultMatch(_ context.Context, _ string) bool {
return true
}

// UnaryServerInterceptor returns a new unary server interceptors that performs per-request selector.
func UnaryServerInterceptor(interceptors grpc.UnaryServerInterceptor, opts ...Option) grpc.UnaryServerInterceptor {
o := evaluateServerOpt(opts)
// UnaryServerInterceptor returns a new unary server interceptor that will decide whether to call
// the interceptor on the behavior of the MatchFunc.
func UnaryServerInterceptor(interceptors grpc.UnaryServerInterceptor, match MatchFunc) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if o.match(ctx, info.FullMethod) {
if match(ctx, info.FullMethod) {
return interceptors(ctx, req, info, handler)
}
return handler(ctx, req)
}
}

// StreamServerInterceptor returns a new unary server interceptors that performs per-request selector.
func StreamServerInterceptor(interceptors grpc.StreamServerInterceptor, opts ...Option) grpc.StreamServerInterceptor {
o := evaluateServerOpt(opts)
// StreamServerInterceptor returns a new stream server interceptor that will decide whether to call
// the interceptor on the behavior of the MatchFunc.
func StreamServerInterceptor(interceptors grpc.StreamServerInterceptor, match MatchFunc) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if o.match(ss.Context(), info.FullMethod) {
if match(ss.Context(), info.FullMethod) {
return interceptors(srv, ss, info, handler)
}
return handler(srv, ss)
Expand Down
79 changes: 79 additions & 0 deletions interceptors/selector/selector_example_test.go
@@ -0,0 +1,79 @@
package selector_test

import (
"context"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/ratelimit"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// alwaysPassLimiter is an example limiter which implements Limiter interface.
// It does not limit any request because Limit function always returns false.
type alwaysPassLimiter struct{}

func (*alwaysPassLimiter) Limit(_ context.Context) error {
return nil
}

func healthSkip(ctx context.Context, fullMethod string) bool {
return fullMethod != "/ping.v1.PingService/Health"
}

func Example_ratelimit() {
limiter := &alwaysPassLimiter{}
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
selector.UnaryServerInterceptor(ratelimit.UnaryServerInterceptor(limiter), healthSkip),
),
grpc.ChainStreamInterceptor(
selector.StreamServerInterceptor(ratelimit.StreamServerInterceptor(limiter), healthSkip),
),
)
}

var tokenInfoKey struct{}

func parseToken(token string) (struct{}, error) {
return struct{}{}, nil
}

func userClaimFromToken(struct{}) string {
return "foobar"
}

// exampleAuthFunc is used by a middleware to authenticate requests
func exampleAuthFunc(ctx context.Context) (context.Context, error) {
token, err := auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}

tokenInfo, err := parseToken(token)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
}

ctx = logging.InjectFields(ctx, logging.Fields{"auth.sub", userClaimFromToken(tokenInfo)})

// WARNING: In production define your own type to avoid context collisions.
return context.WithValue(ctx, tokenInfoKey, tokenInfo), nil
}

func loginSkip(ctx context.Context, fullMethod string) bool {
return fullMethod != "/auth.v1.AuthService/Login"
}

func Example_login() {
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
selector.UnaryServerInterceptor(auth.UnaryServerInterceptor(exampleAuthFunc), loginSkip),
),
grpc.ChainStreamInterceptor(
selector.StreamServerInterceptor(auth.StreamServerInterceptor(exampleAuthFunc), loginSkip),
),
)
}
152 changes: 152 additions & 0 deletions interceptors/selector/selector_test.go
@@ -0,0 +1,152 @@
package selector

import (
"context"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"testing"
)

var blockList = []string{"/auth.v1beta1.AuthService/Login"}

const errMsgFake = "fake error"

var ctxKey = struct{}{}

// allow After the method is matched, the interceptor is run
func allow(methods []string) MatchFunc {
return func(ctx context.Context, fullMethod string) bool {
for _, s := range methods {
if s == fullMethod {
return true
}
}
return false
}
}

// Block the interceptor will not run after the method matches
func block(methods []string) MatchFunc {
allow := allow(methods)
return func(ctx context.Context, fullMethod string) bool {
return !allow(ctx, fullMethod)
}
}

type mockGRPCServerStream struct {
grpc.ServerStream

ctx context.Context
}

func (m *mockGRPCServerStream) Context() context.Context {
return m.ctx
}

func TestUnaryServerInterceptor(t *testing.T) {
ctx := context.Background()
interceptor := UnaryServerInterceptor(auth.UnaryServerInterceptor(
func(ctx context.Context) (context.Context, error) {
newCtx := context.WithValue(ctx, ctxKey, true)
return newCtx, nil
},
), block(blockList))
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
val := ctx.Value(ctxKey)
if b, ok := val.(bool); ok && b {
return "good", nil
}
return nil, errors.New(errMsgFake)
}

t.Run("nextStep", func(t *testing.T) {
info := &grpc.UnaryServerInfo{
FullMethod: "FakeMethod",
}
resp, err := interceptor(ctx, nil, info, handler)
assert.Nil(t, err)
assert.Equal(t, resp, "good")
})

t.Run("skipped", func(t *testing.T) {
info := &grpc.UnaryServerInfo{
FullMethod: "/auth.v1beta1.AuthService/Login",
}
resp, err := interceptor(ctx, nil, info, handler)
assert.Nil(t, resp)
assert.EqualError(t, err, errMsgFake)
})
}

func TestStreamServerInterceptor(t *testing.T) {
ctx := context.Background()
interceptor := StreamServerInterceptor(auth.StreamServerInterceptor(
func(ctx context.Context) (context.Context, error) {
newCtx := context.WithValue(ctx, ctxKey, true)
return newCtx, nil
},
), block(blockList))

handler := func(srv interface{}, stream grpc.ServerStream) error {
ctx := stream.Context()
val := ctx.Value(ctxKey)
if b, ok := val.(bool); ok && b {
return nil
}
return errors.New(errMsgFake)
}

t.Run("nextStep", func(t *testing.T) {
info := &grpc.StreamServerInfo{
FullMethod: "FakeMethod",
}
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler)
assert.Nil(t, err)
})

t.Run("skipped", func(t *testing.T) {
info := &grpc.StreamServerInfo{
FullMethod: "/auth.v1beta1.AuthService/Login",
}
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler)
assert.EqualError(t, err, errMsgFake)
})
}

func TestAllow(t *testing.T) {
type args struct {
methods []string
}
tests := []struct {
name string
args args
method string
want bool
}{
{
name: "false",
args: args{
methods: []string{"/auth.v1beta1.AuthService/Login"},
},
method: "/testing.testpb.v1.TestService/PingList",
want: false,
},
{
name: "true",
args: args{
methods: []string{"/auth.v1beta1.AuthService/Login"},
},
method: "/auth.v1beta1.AuthService/Login",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
allow := allow(tt.args.methods)
want := allow(context.Background(), tt.method)
assert.Equalf(t, tt.want, want, "Allow(%v)(ctx, %v)", tt.args.methods, tt.method)
})
}
}
12 changes: 3 additions & 9 deletions testing/testpb/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion testing/testpb/test_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit acbec6b

Please sign in to comment.