Skip to content

Commit 9d789eb

Browse files
authored
Merge pull request #1070 from ellemouton/sql37
[sql-37] firewalldb: more preparations for SQL actions store
2 parents 57789b0 + 9018cd4 commit 9d789eb

22 files changed

+336
-133
lines changed

app/src/types/generated/firewall_pb.d.ts

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/types/generated/firewall_pb.js

Lines changed: 55 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config_dev.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
108108

109109
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
110110
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
111-
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB)
111+
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock)
112112

113113
stores.accounts = acctStore
114114
stores.sessions = sessStore
@@ -123,7 +123,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
123123

124124
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
125125
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
126-
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB)
126+
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock)
127127

128128
stores.accounts = acctStore
129129
stores.sessions = sessStore
@@ -154,7 +154,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
154154
}
155155

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

config_prod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ 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,
59+
networkDir, firewalldb.DBFilename, sessStore, clock,
6060
)
6161
if err != nil {
6262
return stores, fmt.Errorf("error creating firewall DB: %v", err)

firewall/request_logger.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"strings"
77
"sync"
8-
"time"
98

109
"github.com/lightninglabs/lightning-terminal/firewalldb"
1110
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
@@ -183,21 +182,20 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
183182
withPayloadData bool) error {
184183

185184
// If no macaroon is provided, then an empty 4-byte array is used as the
186-
// session ID. Otherwise, the macaroon is used to derive a session ID.
187-
var sessionID [4]byte
185+
// macaroon ID. Otherwise, the last 4 bytes of the macaroon's root key
186+
// ID are used.
187+
var macaroonID [4]byte
188188
if ri.Macaroon != nil {
189189
var err error
190-
sessionID, err = session.IDFromMacaroon(ri.Macaroon)
190+
macaroonID, err = session.IDFromMacaroon(ri.Macaroon)
191191
if err != nil {
192192
return fmt.Errorf("could not extract ID from macaroon")
193193
}
194194
}
195195

196-
action := &firewalldb.Action{
197-
SessionID: sessionID,
198-
RPCMethod: ri.URI,
199-
AttemptedAt: time.Now(),
200-
State: firewalldb.ActionStateInit,
196+
actionReq := &firewalldb.AddActionReq{
197+
MacaroonIdentifier: macaroonID,
198+
RPCMethod: ri.URI,
201199
}
202200

203201
if withPayloadData {
@@ -211,19 +209,19 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
211209
return fmt.Errorf("unable to decode response: %v", err)
212210
}
213211

214-
action.RPCParamsJson = jsonBytes
212+
actionReq.RPCParamsJson = jsonBytes
215213

216214
meta := ri.MetaInfo
217215
if meta != nil {
218-
action.ActorName = meta.ActorName
219-
action.FeatureName = meta.Feature
220-
action.Trigger = meta.Trigger
221-
action.Intent = meta.Intent
222-
action.StructuredJsonData = meta.StructuredJsonData
216+
actionReq.ActorName = meta.ActorName
217+
actionReq.FeatureName = meta.Feature
218+
actionReq.Trigger = meta.Trigger
219+
actionReq.Intent = meta.Intent
220+
actionReq.StructuredJsonData = meta.StructuredJsonData
223221
}
224222
}
225223

226-
locator, err := r.actionsDB.AddAction(ctx, action)
224+
locator, err := r.actionsDB.AddAction(ctx, actionReq)
227225
if err != nil {
228226
return err
229227
}

firewalldb/actions.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/lightninglabs/lightning-terminal/session"
8+
"github.com/lightningnetwork/lnd/fn"
89
)
910

1011
// ActionState represents the state of an action.
@@ -28,12 +29,21 @@ const (
2829
ActionStateError ActionState = 3
2930
)
3031

31-
// Action represents an RPC call made through the firewall.
32-
type Action struct {
33-
// SessionID is the ID of the session that this action belongs to.
34-
// Note that this is not serialized on persistence since the action is
35-
// already stored under a bucket identified by the session ID.
36-
SessionID session.ID
32+
// AddActionReq is the request that is used to add a new Action to the database.
33+
// It contains all the information that is needed to create a new Action in the
34+
// ActionStateInit State.
35+
type AddActionReq struct {
36+
// MacaroonIdentifier is a 4 byte identifier created from the last 4
37+
// bytes of the root key ID of the macaroon used to perform the action.
38+
MacaroonIdentifier [4]byte
39+
40+
// SessionID holds the optional session ID of the session that this
41+
// action was performed with.
42+
//
43+
// NOTE: for our BoltDB impl, this is not persisted in any way, and we
44+
// populate it by casting the macaroon ID to a session.ID and so is not
45+
// guaranteed to be linked to an existing session.
46+
SessionID fn.Option[session.ID]
3747

3848
// ActorName is the name of the entity who performed the Action.
3949
ActorName string
@@ -59,6 +69,11 @@ type Action struct {
5969

6070
// RPCParams is the method parameters of the request in JSON form.
6171
RPCParamsJson []byte
72+
}
73+
74+
// Action represents an RPC call made through the firewall.
75+
type Action struct {
76+
AddActionReq
6277

6378
// AttemptedAt is the time at which this action was created.
6479
AttemptedAt time.Time
@@ -181,7 +196,7 @@ func WithActionState(state ActionState) ListActionOption {
181196
// ActionsWriteDB is an abstraction over the Actions DB that will allow a
182197
// caller to add new actions as well as change the values of an existing action.
183198
type ActionsWriteDB interface {
184-
AddAction(ctx context.Context, action *Action) (ActionLocator, error)
199+
AddAction(ctx context.Context, req *AddActionReq) (ActionLocator, error)
185200
SetActionState(ctx context.Context, al ActionLocator,
186201
state ActionState, errReason string) error
187202
}

firewalldb/actions_kvdb.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/lightninglabs/lightning-terminal/session"
13+
"github.com/lightningnetwork/lnd/fn"
1314
"github.com/lightningnetwork/lnd/tlv"
1415
"go.etcd.io/bbolt"
1516
)
@@ -53,8 +54,14 @@ var (
5354
)
5455

5556
// AddAction serialises and adds an Action to the DB under the given sessionID.
56-
func (db *BoltDB) AddAction(_ context.Context, action *Action) (ActionLocator,
57-
error) {
57+
func (db *BoltDB) AddAction(_ context.Context,
58+
req *AddActionReq) (ActionLocator, error) {
59+
60+
action := &Action{
61+
AddActionReq: *req,
62+
AttemptedAt: db.clock.Now().UTC(),
63+
State: ActionStateInit,
64+
}
5865

5966
var buf bytes.Buffer
6067
if err := SerializeAction(&buf, action); err != nil {
@@ -74,7 +81,7 @@ func (db *BoltDB) AddAction(_ context.Context, action *Action) (ActionLocator,
7481
}
7582

7683
sessBucket, err := actionsBucket.CreateBucketIfNotExists(
77-
action.SessionID[:],
84+
action.MacaroonIdentifier[:],
7885
)
7986
if err != nil {
8087
return err
@@ -103,7 +110,7 @@ func (db *BoltDB) AddAction(_ context.Context, action *Action) (ActionLocator,
103110
}
104111

105112
locator = kvdbActionLocator{
106-
sessionID: action.SessionID,
113+
sessionID: action.MacaroonIdentifier,
107114
actionID: nextActionIndex,
108115
}
109116

@@ -543,7 +550,8 @@ func DeserializeAction(r io.Reader, sessionID session.ID) (*Action, error) {
543550
return nil, err
544551
}
545552

546-
action.SessionID = sessionID
553+
action.MacaroonIdentifier = sessionID
554+
action.SessionID = fn.Some(sessionID)
547555
action.ActorName = string(actor)
548556
action.FeatureName = string(featureName)
549557
action.Trigger = string(trigger)

0 commit comments

Comments
 (0)