rpcmiddleware

package
v0.12.5-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultInterceptTimeout is the default maximum time we're allowed to
	// take to respond to an RPC middleware interception request.
	DefaultInterceptTimeout = time.Second * 2
)
View Source
const Subsystem = "RMID"

Variables

This section is empty.

Functions

func MessageTypeOf

func MessageTypeOf(typeName string) (protoreflect.MessageType, error)

MessageTypeOf return the protobuf reflection type of the given fully qualified message type name.

func ParseProtobuf

func ParseProtobuf(typeName string, serialized []byte) (proto.Message, error)

ParseProtobuf parses a proto serialized message of the given type into its native version.

func ParseResponseErr

func ParseResponseErr(serialized []byte) error

ParseResponseErr converts a serialized error into an error type.

func RPCErr

RPCErr constructs an rpc middleware response from the given error. The response will result in the message being rejected and the error being given as the reason for rejection.

func RPCErrReplacement

func RPCErrReplacement(req *lnrpc.RPCMiddlewareRequest,
	replacementError error) (*lnrpc.RPCMiddlewareResponse, error)

RPCErrReplacement constructs a new rpc middleware response that will indicate that an error should be replaced by a different one

func RPCErrString

func RPCErrString(req *lnrpc.RPCMiddlewareRequest, format string,
	args ...interface{}) (*lnrpc.RPCMiddlewareResponse, error)

RPCErrString constructs a middleware response. If an empty format param is given then the feedback will be empty meaning that the message can pass through without modification. If the format param is empty, then this is used as the feedback error indicating the reason that the message can not go through.

func RPCOk

RPCOk constructs a middleware response with no error. This results in the original rpc message passing through as is.

func RPCReplacement

func RPCReplacement(req *lnrpc.RPCMiddlewareRequest,
	replacementResponse proto.Message) (*lnrpc.RPCMiddlewareResponse,
	error)

RPCReplacement constructs a new middleware response that will indicate that the message should be replaced with the given replacement message.

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type Config

type Config struct {
	Disabled         bool          `long:"disabled" description:"Disable the RPC middleware"`
	InterceptTimeout time.Duration `` /* 127-byte string literal not displayed */
}

Config is the configuration struct for the RPC middleware.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default RPC middleware configuration.

type DefaultChecker

type DefaultChecker struct {
	// contains filtered or unexported fields
}

DefaultChecker is the default implementation of a round trip checker.

func NewFullChecker

func NewFullChecker(requestSample proto.Message,
	responseSample proto.Message, typedRequestHandler interface{},
	typedResponseHandler interface{},
	errorHandler ErrorHandler) *DefaultChecker

NewFullChecker returns a round trip checker that both inspects the incoming request and response and potentially modifies the response.

func NewFullRewriter

func NewFullRewriter(requestSample proto.Message,
	responseSample proto.Message, typedRequestHandler interface{},
	typedResponseHandler interface{},
	errHandler ErrorHandler) *DefaultChecker

NewFullRewriter returns a round trip checker that both inspects the incoming request and response and potentially modifies the both the request and response.

func NewPassThrough

func NewPassThrough(requestSample proto.Message,
	responseSample proto.Message) *DefaultChecker

NewPassThrough returns a round trip checker that allows the incoming request and passes through the response unmodified.

func NewRequestChecker

func NewRequestChecker(requestSample proto.Message,
	responseSample proto.Message,
	typedRequestHandler interface{}) *DefaultChecker

NewRequestChecker returns a round trip checker that inspects the incoming request and passes through the response unmodified.

func NewRequestDenier

func NewRequestDenier(requestSample proto.Message,
	responseSample proto.Message) *DefaultChecker

NewRequestDenier returns a round trip checker that denies the given requests.

func NewRequestRewriter

func NewRequestRewriter(requestSample proto.Message,
	responseSample proto.Message,
	typedRequestHandler interface{}) *DefaultChecker

NewRequestRewriter returns a round trip checker that inspects and potentially modifies the incoming request and passes through the response unmodified.

func NewResponseEmptier

func NewResponseEmptier[reqT, respT proto.Message]() *DefaultChecker

NewResponseEmptier returns a round trip checker that allows the incoming request and replaces the response with an empty one.

func NewResponseRewriter

func NewResponseRewriter(requestSample proto.Message,
	responseSample proto.Message, typedResponseHandler interface{},
	errorHandler ErrorHandler) *DefaultChecker

NewResponseRewriter returns a round trip checker that allows the incoming request and inspects and potentially modifies the response.

func (*DefaultChecker) HandleErrorResponse

func (r *DefaultChecker) HandleErrorResponse(respErr error) (error, error)

HandleErrorResponse is called for any error response. The handler can pass through the error (=return nil, nil), replace the response error with a new one (=return non-nil error, nil) or abort by returning a non nil error (=return nil, non-nil error).

func (*DefaultChecker) HandleRequest

func (r *DefaultChecker) HandleRequest(ctx context.Context,
	req proto.Message) (proto.Message, error)

HandleRequest is called for each incoming gRPC request message of the type declared to be accepted by HandlesRequest. The handler can accept the request as is (=return nil, nil), replace the request with a new message of the same type (=return non-nil message, nil) or refuse (=return non-nil error with rejection reason) an incoming request.

func (*DefaultChecker) HandleResponse

func (r *DefaultChecker) HandleResponse(ctx context.Context,
	resp proto.Message) (proto.Message, error)

HandleResponse is called for each outgoing gRPC response message of the type declared to be handled by HandlesResponse. The handler can pass through the response (=return nil, nil), replace the response with a new message of the same type (=return non-nil message, nil error) or abort the call by returning a non-nil error.

func (*DefaultChecker) HandlesRequest

func (r *DefaultChecker) HandlesRequest(t protoreflect.MessageType) bool

HandlesRequest returns true if the checker accepts protobuf request messages of the given type. This is mainly a safety feature to make sure a round trip checker is implemented correctly.

func (*DefaultChecker) HandlesResponse

func (r *DefaultChecker) HandlesResponse(t protoreflect.MessageType) bool

HandlesResponse returns true if the checker can handle protobuf response messages of the given type. This is mainly a safety feature to make sure a round trip checker is implemented correctly.

type ErrorHandler

type ErrorHandler func(respErr error) (error, error)

ErrorHandler is a function type for a generic gRPC error handler. It can pass through the error unchanged (=return nil, nil), replace the error with a different one (=return non-nil error, nil error) or abort by returning a non-nil error.

var (
	// ErrNotSupported is returned if a specific method is called that is
	// not supported by the RPC middleware interceptor checker.
	ErrNotSupported = fmt.Errorf("method not supported")

	// PassThroughErrorHandler is an ErrorHandler that does not modify an
	// error and instead just passes it through.
	PassThroughErrorHandler ErrorHandler = func(error) (error, error) {
		return nil, nil
	}
)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is the main middleware manager service.

func NewManager

func NewManager(interceptTimeout time.Duration,
	lndClient lndclient.LightningClient, errChan chan<- error,
	interceptors ...RequestInterceptor) *Manager

NewManager returns a new middleware manager.

func (*Manager) Start

func (f *Manager) Start() error

Start starts the firewall by registering the interceptors with lnd.

func (*Manager) Stop

func (f *Manager) Stop()

Stop shuts down the middleware manager.

type RequestInterceptor

type RequestInterceptor interface {
	// Name returns the name of the interceptor.
	Name() string

	// ReadOnly returns true if this interceptor should be registered in
	// read-only mode. In read-only mode no custom caveat name can be
	// specified.
	ReadOnly() bool

	// CustomCaveatName returns the name of the custom caveat that is
	// expected to be handled by this interceptor. Cannot be specified in
	// read-only mode.
	CustomCaveatName() string

	// Intercept processes an RPC middleware interception request and
	// returns the interception result which either accepts or rejects the
	// intercepted message.
	Intercept(context.Context,
		*lnrpc.RPCMiddlewareRequest) (*lnrpc.RPCMiddlewareResponse,
		error)
}

RequestInterceptor is a type that can intercept an RPC request.

type RoundTripChecker

type RoundTripChecker interface {
	// HandlesRequest returns true if the checker accepts protobuf request
	// messages of the given type. This is mainly a safety feature to make
	// sure a round trip checker is implemented correctly.
	HandlesRequest(protoreflect.MessageType) bool

	// HandlesResponse returns true if the checker can handle protobuf
	// response messages of the given type. This is mainly a safety feature
	// to make sure a round trip checker is implemented correctly.
	HandlesResponse(protoreflect.MessageType) bool

	// HandleRequest is called for each incoming gRPC request message of the
	// type declared to be accepted by HandlesRequest. The handler can
	// accept the request as is (=return nil, nil), replace the request with
	// a new message of the same type (=return non-nil message, nil) or
	// refuse (=return non-nil error with rejection reason) an incoming
	// request.
	HandleRequest(context.Context, proto.Message) (proto.Message, error)

	// HandleResponse is called for each outgoing gRPC response message of
	// the type declared to be handled by HandlesResponse. The handler can
	// pass through the response (=return nil, nil), replace the response
	// with a new message of the same type (=return non-nil message, nil
	// error) or abort the call by returning a non-nil error.
	HandleResponse(context.Context, proto.Message) (proto.Message, error)

	// HandleErrorResponse is called for any error response.
	// The handler can pass through the error (=return nil, nil), replace
	// the response error with a new one (=return non-nil error, nil) or
	// abort by returning a non nil error (=return nil, non-nil error).
	HandleErrorResponse(error) (error, error)
}

RoundTripChecker is a type that represents a basic request/response round trip checker.

Jump to

Keyboard shortcuts

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