selector

package
v2.0.0-rc.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2022 License: Apache-2.0 Imports: 2 Imported by: 28

Documentation

Overview

Package selector

`selector` a generic server-side selector middleware for gRPC.

# Server Side Selector 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.

Example (Login)
package main

import (
	"context"

	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"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/selector"
)

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)})

	return context.WithValue(ctx, tokenInfoKey, tokenInfo), nil
}

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

func main() {
	_ = grpc.NewServer(
		grpc.ChainUnaryInterceptor(
			selector.UnaryServerInterceptor(auth.UnaryServerInterceptor(exampleAuthFunc), loginSkip),
		),
		grpc.ChainStreamInterceptor(
			selector.StreamServerInterceptor(auth.StreamServerInterceptor(exampleAuthFunc), loginSkip),
		),
	)
}
Output:

Example (Ratelimit)
package main

import (
	"context"

	"google.golang.org/grpc"

	"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/ratelimit"
	"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
)

// 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 main() {
	limiter := &alwaysPassLimiter{}
	_ = grpc.NewServer(
		grpc.ChainUnaryInterceptor(
			selector.UnaryServerInterceptor(ratelimit.UnaryServerInterceptor(limiter), healthSkip),
		),
		grpc.ChainStreamInterceptor(
			selector.StreamServerInterceptor(ratelimit.StreamServerInterceptor(limiter), healthSkip),
		),
	)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamServerInterceptor

func StreamServerInterceptor(interceptors grpc.StreamServerInterceptor, match MatchFunc) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new stream server interceptor that will decide whether to call the interceptor on the behavior of the MatchFunc.

func UnaryServerInterceptor

func UnaryServerInterceptor(interceptors grpc.UnaryServerInterceptor, match MatchFunc) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor that will decide whether to call the interceptor on the behavior of the MatchFunc.

Types

type MatchFunc

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL