Skip to content

Commit 667e513

Browse files
committed
firewall: extract SessionID from gRPC metadata
In this commit, we update our various firewall interceptors so that they rely on the session ID passed via gRPC metadata to extract a session ID. For the PrivacyMapper and RuleEnforcer, these _MUST_ always contain a session ID and so we error out if one was not found. For the request logger, the session ID is optional and so we pass it to the new SessionID field in the AddActionReq - our bbolt actions DB will not make use of this field on persistence (but our incoming SQL version will).
1 parent cbfb11f commit 667e513

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

firewall/privacy_mapper.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ func (p *PrivacyMapper) Intercept(ctx context.Context,
106106
"interception request: %v", err)
107107
}
108108

109-
sessionID, err := session.IDFromMacaroon(ri.Macaroon)
109+
sessionID, err := ri.SessionID.UnwrapOrErr(
110+
fmt.Errorf("no session ID found in macaroon"),
111+
)
110112
if err != nil {
111-
return nil, fmt.Errorf("could not extract ID from macaroon")
113+
return nil, err
112114
}
113115

114116
log.Tracef("PrivacyMapper: Intercepting %v", ri)

firewall/request_info.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/lightninglabs/lightning-terminal/session"
8+
"github.com/lightningnetwork/lnd/fn"
79
"github.com/lightningnetwork/lnd/lnrpc"
10+
"google.golang.org/grpc/metadata"
811
"gopkg.in/macaroon.v2"
912
)
1013

@@ -25,6 +28,7 @@ const (
2528
// RequestInfo stores the parsed representation of an incoming RPC middleware
2629
// request.
2730
type RequestInfo struct {
31+
SessionID fn.Option[session.ID]
2832
MsgID uint64
2933
RequestID uint64
3034
MWRequestType string
@@ -43,13 +47,27 @@ type RequestInfo struct {
4347
// NewInfoFromRequest parses the given RPC middleware interception request and
4448
// returns a RequestInfo struct.
4549
func NewInfoFromRequest(req *lnrpc.RPCMiddlewareRequest) (*RequestInfo, error) {
50+
md := make(metadata.MD)
51+
for k, vs := range req.MetadataPairs {
52+
for _, v := range vs.Values {
53+
md.Append(k, v)
54+
}
55+
}
56+
57+
sessionID, err := session.FromGRPCMetadata(md)
58+
if err != nil {
59+
return nil, fmt.Errorf("error extracting session ID "+
60+
"from request: %v", err)
61+
}
62+
4663
var ri *RequestInfo
4764
switch t := req.InterceptType.(type) {
4865
case *lnrpc.RPCMiddlewareRequest_StreamAuth:
4966
ri = &RequestInfo{
5067
MWRequestType: MWRequestTypeStreamAuth,
5168
URI: t.StreamAuth.MethodFullUri,
5269
Streaming: true,
70+
SessionID: sessionID,
5371
}
5472

5573
case *lnrpc.RPCMiddlewareRequest_Request:
@@ -60,6 +78,7 @@ func NewInfoFromRequest(req *lnrpc.RPCMiddlewareRequest) (*RequestInfo, error) {
6078
IsError: t.Request.IsError,
6179
Serialized: t.Request.Serialized,
6280
Streaming: t.Request.StreamRpc,
81+
SessionID: sessionID,
6382
}
6483

6584
case *lnrpc.RPCMiddlewareRequest_Response:
@@ -70,6 +89,7 @@ func NewInfoFromRequest(req *lnrpc.RPCMiddlewareRequest) (*RequestInfo, error) {
7089
IsError: t.Response.IsError,
7190
Serialized: t.Response.Serialized,
7291
Streaming: t.Response.StreamRpc,
92+
SessionID: sessionID,
7393
}
7494

7595
default:

firewall/request_logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
194194
}
195195

196196
actionReq := &firewalldb.AddActionReq{
197+
SessionID: ri.SessionID,
197198
MacaroonIdentifier: macaroonID,
198199
RPCMethod: ri.URI,
199200
}

firewall/rule_enforcer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ func (r *RuleEnforcer) Intercept(ctx context.Context,
237237
func (r *RuleEnforcer) handleRequest(ctx context.Context,
238238
ri *RequestInfo) (proto.Message, error) {
239239

240-
sessionID, err := session.IDFromMacaroon(ri.Macaroon)
240+
sessionID, err := ri.SessionID.UnwrapOrErr(
241+
fmt.Errorf("no session ID found in macaroon"),
242+
)
241243
if err != nil {
242-
return nil, fmt.Errorf("could not extract ID from macaroon")
244+
return nil, err
243245
}
244246

245247
rules, err := r.collectEnforcers(ctx, ri, sessionID)

0 commit comments

Comments
 (0)