|
| 1 | +package firewall |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 8 | + "gopkg.in/macaroon.v2" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + // MWRequestTypeStreamAuth represents the type name for a stream |
| 13 | + // authentication interception message. |
| 14 | + MWRequestTypeStreamAuth = "stream_auth" |
| 15 | + |
| 16 | + // MWRequestTypeRequest represents the type name for a request |
| 17 | + // interception message. |
| 18 | + MWRequestTypeRequest = "request" |
| 19 | + |
| 20 | + // MWRequestTypeResponse represents the type name for a response |
| 21 | + // interception message. |
| 22 | + MWRequestTypeResponse = "response" |
| 23 | +) |
| 24 | + |
| 25 | +// RequestInfo stores the parsed representation of an incoming RPC middleware |
| 26 | +// request. |
| 27 | +type RequestInfo struct { |
| 28 | + MsgID uint64 |
| 29 | + RequestID uint64 |
| 30 | + MWRequestType string |
| 31 | + URI string |
| 32 | + GRPCMessageType string |
| 33 | + Streaming bool |
| 34 | + Macaroon *macaroon.Macaroon |
| 35 | + Caveats []string |
| 36 | + MetaInfo *InterceptMetaInfo |
| 37 | + Rules []*InterceptRule |
| 38 | +} |
| 39 | + |
| 40 | +// NewInfoFromRequest parses the given RPC middleware interception request and |
| 41 | +// returns a RequestInfo struct. |
| 42 | +func NewInfoFromRequest(req *lnrpc.RPCMiddlewareRequest) (*RequestInfo, error) { |
| 43 | + var ri *RequestInfo |
| 44 | + switch t := req.InterceptType.(type) { |
| 45 | + case *lnrpc.RPCMiddlewareRequest_StreamAuth: |
| 46 | + ri = &RequestInfo{ |
| 47 | + MWRequestType: MWRequestTypeStreamAuth, |
| 48 | + URI: t.StreamAuth.MethodFullUri, |
| 49 | + Streaming: true, |
| 50 | + } |
| 51 | + |
| 52 | + case *lnrpc.RPCMiddlewareRequest_Request: |
| 53 | + ri = &RequestInfo{ |
| 54 | + MWRequestType: MWRequestTypeRequest, |
| 55 | + URI: t.Request.MethodFullUri, |
| 56 | + GRPCMessageType: t.Request.TypeName, |
| 57 | + Streaming: t.Request.StreamRpc, |
| 58 | + } |
| 59 | + |
| 60 | + case *lnrpc.RPCMiddlewareRequest_Response: |
| 61 | + ri = &RequestInfo{ |
| 62 | + MWRequestType: MWRequestTypeResponse, |
| 63 | + URI: t.Response.MethodFullUri, |
| 64 | + GRPCMessageType: t.Response.TypeName, |
| 65 | + Streaming: t.Response.StreamRpc, |
| 66 | + } |
| 67 | + |
| 68 | + default: |
| 69 | + return nil, fmt.Errorf("invalid request type: %T", t) |
| 70 | + } |
| 71 | + |
| 72 | + ri.MsgID = req.MsgId |
| 73 | + ri.RequestID = req.RequestId |
| 74 | + |
| 75 | + ri.Macaroon = &macaroon.Macaroon{} |
| 76 | + if err := ri.Macaroon.UnmarshalBinary(req.RawMacaroon); err != nil { |
| 77 | + return nil, fmt.Errorf("error parsing macaroon: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + ri.Caveats = make([]string, len(ri.Macaroon.Caveats())) |
| 81 | + for idx, cav := range ri.Macaroon.Caveats() { |
| 82 | + ri.Caveats[idx] = string(cav.Id) |
| 83 | + |
| 84 | + // Apply any meta information sent as a custom caveat. Only the |
| 85 | + // last one will be considered if there are multiple caveats. |
| 86 | + metaInfo, err := ParseMetaInfoCaveat(ri.Caveats[idx]) |
| 87 | + if err == nil { |
| 88 | + ri.MetaInfo = metaInfo |
| 89 | + |
| 90 | + // The same caveat can't be a meta info and rule list. |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + // Also apply the rule list sent as a custom caveat. Only the |
| 95 | + // last set of rules will be considered if there are multiple |
| 96 | + // caveats. |
| 97 | + rules, err := ParseRuleCaveat(ri.Caveats[idx]) |
| 98 | + if err == nil { |
| 99 | + ri.Rules = rules |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return ri, nil |
| 104 | +} |
| 105 | + |
| 106 | +// String returns the string representation of the request info struct. |
| 107 | +func (ri *RequestInfo) String() string { |
| 108 | + return fmt.Sprintf("Request={msg_id=%d, request_id=%d, type=%v, "+ |
| 109 | + "uri=%v, grpc_message_type=%v, streaming=%v, caveats=[%v], "+ |
| 110 | + "meta_info=%v, rules=[%v]}", |
| 111 | + ri.MsgID, ri.RequestID, ri.MWRequestType, ri.URI, |
| 112 | + ri.GRPCMessageType, ri.Streaming, strings.Join(ri.Caveats, ","), |
| 113 | + ri.MetaInfo, ri.Rules) |
| 114 | +} |
0 commit comments