Skip to content

Commit beab4e0

Browse files
authored
Merge pull request #1071 from ellemouton/sql38
[sql-38] multi: couple sessions and accounts to actions
2 parents a513aae + 642a69f commit beab4e0

File tree

12 files changed

+220
-123
lines changed

12 files changed

+220
-123
lines changed

accounts/interceptor.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (s *InterceptorService) Intercept(ctx context.Context,
8181
return mid.RPCErrString(req, "error parsing macaroon: %v", err)
8282
}
8383

84-
acctID, err := accountFromMacaroon(mac)
84+
acctID, err := IDFromCaveats(mac.Caveats())
8585
if err != nil {
8686
return mid.RPCErrString(
8787
req, "error parsing account from macaroon: %v", err,
@@ -91,15 +91,17 @@ func (s *InterceptorService) Intercept(ctx context.Context,
9191
// No account lock in the macaroon, something's weird. The interceptor
9292
// wouldn't have been triggered if there was no caveat, so we do expect
9393
// a macaroon here.
94-
if acctID == nil {
95-
return mid.RPCErrString(req, "expected account ID in "+
96-
"macaroon caveat")
94+
accountID, err := acctID.UnwrapOrErr(
95+
fmt.Errorf("expected account ID in macaroon caveat"),
96+
)
97+
if err != nil {
98+
return mid.RPCErr(req, err)
9799
}
98100

99-
acct, err := s.Account(ctx, *acctID)
101+
acct, err := s.Account(ctx, accountID)
100102
if err != nil {
101103
return mid.RPCErrString(
102-
req, "error getting account %x: %v", acctID[:], err,
104+
req, "error getting account %x: %v", accountID[:], err,
103105
)
104106
}
105107

@@ -208,27 +210,6 @@ func parseRPCMessage(msg *lnrpc.RPCMessage) (proto.Message, error) {
208210
return parsedMsg, nil
209211
}
210212

211-
// accountFromMacaroon attempts to extract an account ID from the custom account
212-
// caveat in the macaroon.
213-
func accountFromMacaroon(mac *macaroon.Macaroon) (*AccountID, error) {
214-
if mac == nil {
215-
return nil, nil
216-
}
217-
218-
// Extract the account caveat from the macaroon.
219-
accountID, err := IDFromCaveats(mac.Caveats())
220-
if err != nil {
221-
return nil, err
222-
}
223-
224-
var id *AccountID
225-
accountID.WhenSome(func(aID AccountID) {
226-
id = &aID
227-
})
228-
229-
return id, nil
230-
}
231-
232213
// CaveatFromID creates a custom caveat that can be used to bind a macaroon to
233214
// a certain account.
234215
func CaveatFromID(id AccountID) macaroon.Caveat {

config_dev.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
154154
}
155155

156156
firewallBoltDB, err := firewalldb.NewBoltDB(
157-
networkDir, firewalldb.DBFilename, stores.sessions, clock,
157+
networkDir, firewalldb.DBFilename, stores.sessions,
158+
stores.accounts, clock,
158159
)
159160
if err != nil {
160161
return stores, fmt.Errorf("error creating firewall BoltDB: %v",

config_prod.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
5656
stores.closeFns["sessions"] = sessStore.Close
5757

5858
firewallDB, err := firewalldb.NewBoltDB(
59-
networkDir, firewalldb.DBFilename, sessStore, clock,
59+
networkDir, firewalldb.DBFilename, stores.sessions,
60+
stores.accounts, clock,
6061
)
6162
if err != nil {
6263
return stores, fmt.Errorf("error creating firewall DB: %v", err)

firewall/request_info.go

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

7+
"github.com/lightninglabs/lightning-terminal/accounts"
78
"github.com/lightninglabs/lightning-terminal/session"
89
"github.com/lightningnetwork/lnd/fn"
910
"github.com/lightningnetwork/lnd/lnrpc"
@@ -29,6 +30,7 @@ const (
2930
// request.
3031
type RequestInfo struct {
3132
SessionID fn.Option[session.ID]
33+
AccountID fn.Option[accounts.AccountID]
3234
MsgID uint64
3335
RequestID uint64
3436
MWRequestType string
@@ -140,6 +142,12 @@ func NewInfoFromRequest(req *lnrpc.RPCMiddlewareRequest) (*RequestInfo, error) {
140142
}
141143
}
142144

145+
ri.AccountID, err = accounts.IDFromCaveats(ri.Macaroon.Caveats())
146+
if err != nil {
147+
return nil, fmt.Errorf("error extracting account ID "+
148+
"from macaroon: %v", err)
149+
}
150+
143151
return ri, nil
144152
}
145153

firewall/request_logger.go

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

196196
actionReq := &firewalldb.AddActionReq{
197197
SessionID: ri.SessionID,
198+
AccountID: ri.AccountID,
198199
MacaroonIdentifier: macaroonID,
199200
RPCMethod: ri.URI,
200201
}

firewalldb/actions.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
"github.com/lightninglabs/lightning-terminal/accounts"
78
"github.com/lightninglabs/lightning-terminal/session"
89
"github.com/lightningnetwork/lnd/fn"
910
)
@@ -45,6 +46,13 @@ type AddActionReq struct {
4546
// guaranteed to be linked to an existing session.
4647
SessionID fn.Option[session.ID]
4748

49+
// AccountID holds the optional account ID of the account that this
50+
// action was performed on.
51+
//
52+
// NOTE: for our BoltDB impl, this is not persisted in any way, and we
53+
// do not populate it on reading from disk.
54+
AccountID fn.Option[accounts.AccountID]
55+
4856
// ActorName is the name of the entity who performed the Action.
4957
ActorName string
5058

firewalldb/actions_kvdb.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"time"
1111

12+
"github.com/lightninglabs/lightning-terminal/accounts"
1213
"github.com/lightninglabs/lightning-terminal/session"
1314
"github.com/lightningnetwork/lnd/fn"
1415
"github.com/lightningnetwork/lnd/tlv"
@@ -54,9 +55,32 @@ var (
5455
)
5556

5657
// AddAction serialises and adds an Action to the DB under the given sessionID.
57-
func (db *BoltDB) AddAction(_ context.Context,
58+
func (db *BoltDB) AddAction(ctx context.Context,
5859
req *AddActionReq) (ActionLocator, error) {
5960

61+
// If the new action links to a session, the session must exist.
62+
// For the bbolt impl of the store, this is our best effort attempt
63+
// at ensuring each action links to a session. If the session is
64+
// deleted later on, however, then the action will still exist.
65+
var err error
66+
req.SessionID.WhenSome(func(id session.ID) {
67+
_, err = db.sessionIDIndex.GetSession(ctx, id)
68+
})
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
// If the new action links to an account, the account must exist.
74+
// For the bbolt impl of the store, this is our best effort attempt
75+
// at ensuring each action links to an account. If the account is
76+
// deleted later on, however, then the action will still exist.
77+
req.AccountID.WhenSome(func(id accounts.AccountID) {
78+
_, err = db.accountsDB.Account(ctx, id)
79+
})
80+
if err != nil {
81+
return nil, err
82+
}
83+
6084
action := &Action{
6185
AddActionReq: *req,
6286
AttemptedAt: db.clock.Now().UTC(),
@@ -69,7 +93,7 @@ func (db *BoltDB) AddAction(_ context.Context,
6993
}
7094

7195
var locator kvdbActionLocator
72-
err := db.DB.Update(func(tx *bbolt.Tx) error {
96+
err = db.DB.Update(func(tx *bbolt.Tx) error {
7397
mainActionsBucket, err := getBucket(tx, actionsBucketKey)
7498
if err != nil {
7599
return err

0 commit comments

Comments
 (0)