Skip to content

Commit b99a4f8

Browse files
committed
firewall+firewalldb: move mac ID serialisation to kvdb impl
For our kvdb firewalldb, we use an empty 4 byte array as the macaroon identifier even if no macaroon was used to create the action. This is so that we have some sort of "session ID" bucket to store these set of actions under. For our SQL impl, however, this is not needed and we will likely just use a nullable field for the macaroon ID. So in preparation for this, we move the kvdb specific logic to the kvdb impl.
1 parent 9fea538 commit b99a4f8

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

firewall/request_logger.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/lightninglabs/lightning-terminal/firewalldb"
1010
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
1111
"github.com/lightninglabs/lightning-terminal/session"
12+
"github.com/lightningnetwork/lnd/fn"
1213
"github.com/lightningnetwork/lnd/lnrpc"
1314
"github.com/lightningnetwork/lnd/macaroons"
1415
)
@@ -181,16 +182,15 @@ func (r *RequestLogger) Intercept(ctx context.Context,
181182
func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
182183
withPayloadData bool) error {
183184

184-
// If no macaroon is provided, then an empty 4-byte array is used as the
185-
// macaroon ID. Otherwise, the last 4 bytes of the macaroon's root key
186-
// ID are used.
187-
var macaroonID [4]byte
185+
var macaroonID fn.Option[[4]byte]
188186
if ri.Macaroon != nil {
189187
var err error
190-
macaroonID, err = session.IDFromMacaroon(ri.Macaroon)
188+
macID, err := session.IDFromMacaroon(ri.Macaroon)
191189
if err != nil {
192190
return fmt.Errorf("could not extract ID from macaroon")
193191
}
192+
193+
macaroonID = fn.Some([4]byte(macID))
194194
}
195195

196196
actionReq := &firewalldb.AddActionReq{

firewalldb/actions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const (
3636
type AddActionReq struct {
3737
// MacaroonIdentifier is a 4 byte identifier created from the last 4
3838
// bytes of the root key ID of the macaroon used to perform the action.
39-
MacaroonIdentifier [4]byte
39+
// If no macaroon was used for the action, then this will not be set.
40+
MacaroonIdentifier fn.Option[[4]byte]
4041

4142
// SessionID holds the optional session ID of the session that this
4243
// action was performed with.

firewalldb/actions_kvdb.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ var (
5858
func (db *BoltDB) AddAction(ctx context.Context,
5959
req *AddActionReq) (ActionLocator, error) {
6060

61+
// If no macaroon is provided, then an empty 4-byte array is used as the
62+
// macaroon ID.
63+
var macaroonID [4]byte
64+
req.MacaroonIdentifier.WhenSome(func(id [4]byte) {
65+
macaroonID = id
66+
})
67+
6168
// If the new action links to a session, the session must exist.
6269
// For the bbolt impl of the store, this is our best effort attempt
6370
// at ensuring each action links to a session. If the session is
@@ -105,7 +112,7 @@ func (db *BoltDB) AddAction(ctx context.Context,
105112
}
106113

107114
sessBucket, err := actionsBucket.CreateBucketIfNotExists(
108-
action.MacaroonIdentifier[:],
115+
macaroonID[:],
109116
)
110117
if err != nil {
111118
return err
@@ -134,7 +141,7 @@ func (db *BoltDB) AddAction(ctx context.Context,
134141
}
135142

136143
locator = kvdbActionLocator{
137-
sessionID: action.MacaroonIdentifier,
144+
sessionID: macaroonID,
138145
actionID: nextActionIndex,
139146
}
140147

@@ -574,7 +581,7 @@ func DeserializeAction(r io.Reader, sessionID session.ID) (*Action, error) {
574581
return nil, err
575582
}
576583

577-
action.MacaroonIdentifier = sessionID
584+
action.MacaroonIdentifier = fn.Some([4]byte(sessionID))
578585
action.SessionID = fn.Some(sessionID)
579586
action.ActorName = string(actor)
580587
action.FeatureName = string(featureName)

firewalldb/actions_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestActionStorage(t *testing.T) {
6767
action1Req := &AddActionReq{
6868
SessionID: fn.Some(sess1.ID),
6969
AccountID: fn.Some(acct1.ID),
70-
MacaroonIdentifier: sess1.ID,
70+
MacaroonIdentifier: fn.Some([4]byte(sess1.ID)),
7171
ActorName: "Autopilot",
7272
FeatureName: "auto-fees",
7373
Trigger: "fee too low",
@@ -85,7 +85,7 @@ func TestActionStorage(t *testing.T) {
8585

8686
action2Req := &AddActionReq{
8787
SessionID: fn.Some(sess2.ID),
88-
MacaroonIdentifier: sess2.ID,
88+
MacaroonIdentifier: fn.Some([4]byte(sess2.ID)),
8989
ActorName: "Autopilot",
9090
FeatureName: "rebalancer",
9191
Trigger: "channels not balanced",
@@ -223,7 +223,7 @@ func TestListActions(t *testing.T) {
223223
actionIds++
224224

225225
actionReq := &AddActionReq{
226-
MacaroonIdentifier: sessionID,
226+
MacaroonIdentifier: fn.Some(sessionID),
227227
ActorName: "Autopilot",
228228
FeatureName: fmt.Sprintf("%d", actionIds),
229229
Trigger: "fee too low",
@@ -245,9 +245,11 @@ func TestListActions(t *testing.T) {
245245
assertActions := func(dbActions []*Action, al []*action) {
246246
require.Len(t, dbActions, len(al))
247247
for i, a := range al {
248-
require.EqualValues(
249-
t, a.sessionID, dbActions[i].MacaroonIdentifier,
248+
mID, err := dbActions[i].MacaroonIdentifier.UnwrapOrErr(
249+
fmt.Errorf("macaroon identifier is none"),
250250
)
251+
require.NoError(t, err)
252+
require.EqualValues(t, a.sessionID, mID)
251253
require.Equal(t, a.actionID, dbActions[i].FeatureName)
252254
}
253255
}
@@ -433,7 +435,7 @@ func TestListGroupActions(t *testing.T) {
433435

434436
action1Req := &AddActionReq{
435437
SessionID: fn.Some(sess1.ID),
436-
MacaroonIdentifier: sess1.ID,
438+
MacaroonIdentifier: fn.Some([4]byte(sess1.ID)),
437439
ActorName: "Autopilot",
438440
FeatureName: "auto-fees",
439441
Trigger: "fee too low",
@@ -451,7 +453,7 @@ func TestListGroupActions(t *testing.T) {
451453

452454
action2Req := &AddActionReq{
453455
SessionID: fn.Some(sess2.ID),
454-
MacaroonIdentifier: sess2.ID,
456+
MacaroonIdentifier: fn.Some([4]byte(sess2.ID)),
455457
ActorName: "Autopilot",
456458
FeatureName: "rebalancer",
457459
Trigger: "channels not balanced",

session_rpcserver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,14 @@ func (s *sessionRpcServer) ListActions(ctx context.Context,
806806
sessionID = id
807807
})
808808

809+
var macID [4]byte
810+
a.MacaroonIdentifier.WhenSome(func(id [4]byte) {
811+
macID = id
812+
})
813+
809814
resp[i] = &litrpc.Action{
810815
SessionId: sessionID[:],
811-
MacaroonIdentifier: a.MacaroonIdentifier[:],
816+
MacaroonIdentifier: macID[:],
812817
ActorName: a.ActorName,
813818
FeatureName: a.FeatureName,
814819
Trigger: a.Trigger,

0 commit comments

Comments
 (0)