-
Notifications
You must be signed in to change notification settings - Fork 105
[sql-53] firewalldb: actions migration prep 2 - persist full macaroon root key ID in the SQL actions store #1146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"sync" | ||
|
||
"github.com/lightninglabs/lightning-terminal/firewalldb" | ||
litd_macaroons "github.com/lightninglabs/lightning-terminal/macaroons" | ||
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware" | ||
"github.com/lightninglabs/lightning-terminal/session" | ||
"github.com/lightningnetwork/lnd/fn" | ||
|
@@ -182,21 +183,33 @@ func (r *RequestLogger) Intercept(ctx context.Context, | |
func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo, | ||
withPayloadData bool) error { | ||
|
||
var macaroonID fn.Option[[4]byte] | ||
var ( | ||
rootKeyID fn.Option[uint64] | ||
macaroonID fn.Option[[4]byte] | ||
) | ||
|
||
if ri.Macaroon != nil { | ||
var err error | ||
macID, err := session.IDFromMacaroon(ri.Macaroon) | ||
|
||
fullRootKeyID, err := litd_macaroons.RootKeyIDFromMacaroon( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still a bit unsure why it's needed, looking at |
||
ri.Macaroon, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("could not extract ID from macaroon") | ||
return fmt.Errorf("could not extract root key ID from "+ | ||
"macaroon: %w", err) | ||
} | ||
|
||
macID := session.IDFromMacRootKeyID(fullRootKeyID) | ||
|
||
rootKeyID = fn.Some(fullRootKeyID) | ||
macaroonID = fn.Some([4]byte(macID)) | ||
} | ||
|
||
actionReq := &firewalldb.AddActionReq{ | ||
SessionID: ri.SessionID, | ||
AccountID: ri.AccountID, | ||
MacaroonIdentifier: macaroonID, | ||
MacaroonRootKeyID: rootKeyID, | ||
Comment on lines
211
to
+212
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok and we need both cause kvdb still stores old identifier but now sql stores full rootkey id? |
||
RPCMethod: ri.URI, | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,11 @@ type AddActionReq struct { | |
// If no macaroon was used for the action, then this will not be set. | ||
MacaroonIdentifier fn.Option[[4]byte] | ||
|
||
// MacaroonRootKeyID is the uint64 / full 8 bytes of the root key ID of | ||
// the macaroon used to perform the action. | ||
// If no macaroon was used for the action, then this will not be set. | ||
MacaroonRootKeyID fn.Option[uint64] | ||
|
||
// SessionID holds the optional session ID of the session that this | ||
Comment on lines
39
to
47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. related to above question, if one of these is stored in kvdb only and the other in sql only, lets add this warning in the comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or actually, to keep the API intuitive, cant se instead only have the MacRootKeyID field here & remove the MacID field. Then extract the relevant format at the DB layer. ie, in the kvdb layer, take the root key ID and compute the mac ID. |
||
// action was performed with. | ||
// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package firewalldb | |
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
"math" | ||
|
@@ -140,8 +141,11 @@ func (s *SQLDB) AddAction(ctx context.Context, | |
} | ||
|
||
var macID []byte | ||
req.MacaroonIdentifier.WhenSome(func(id [4]byte) { | ||
macID = id[:] | ||
req.MacaroonRootKeyID.WhenSome(func(rootKeyID uint64) { | ||
rootKeyBytes := make([]byte, 8) | ||
binary.BigEndian.PutUint64(rootKeyBytes[:], rootKeyID) | ||
|
||
macID = rootKeyBytes | ||
}) | ||
|
||
id, err := db.InsertAction(ctx, sqlc.InsertActionParams{ | ||
|
@@ -393,9 +397,17 @@ func unmarshalAction(ctx context.Context, db SQLActionQueries, | |
legacyAcctID = fn.Some(acctID) | ||
} | ||
|
||
// While we store the full 8 byte macaroon root key ID in the sql | ||
// actions DB, the kvdb version only stored the last 4 bytes. So | ||
// we'll only return that here to maintain compatibility with any | ||
// existing callers. | ||
// | ||
// TODO(viktor): Remove this when we no longer need to be compatible | ||
// with the kvdb version. | ||
var macID fn.Option[[4]byte] | ||
if len(dbAction.MacaroonIdentifier) > 0 { | ||
macID = fn.Some([4]byte(dbAction.MacaroonIdentifier)) | ||
if len(dbAction.MacaroonIdentifier) >= 4 { | ||
dbMacID := dbAction.MacaroonIdentifier | ||
macID = fn.Some([4]byte(dbMacID[len(dbMacID)-4:])) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not populating the new field below? (see comment about only needing 1 field potentially anyways) |
||
|
||
return &Action{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ package firewalldb | |
import ( | ||
"testing" | ||
|
||
"github.com/lightninglabs/lightning-terminal/accounts" | ||
"github.com/lightninglabs/lightning-terminal/session" | ||
"github.com/lightningnetwork/lnd/clock" | ||
"github.com/lightningnetwork/lnd/fn" | ||
|
@@ -59,8 +58,16 @@ func newDBFromPathWithSessions(t *testing.T, dbPath string, | |
|
||
func assertEqualActions(t *testing.T, expected, got *Action) { | ||
// Accounts are not explicitly linked in our bbolt DB implementation. | ||
actualAccountID := got.AccountID | ||
got.AccountID = expected.AccountID | ||
|
||
// As the kvdb implementation doesn't store the Macaroon Root Key ID, | ||
// we clear the expected value before comparison, and restore it after. | ||
expectedMacRootKey := expected.MacaroonRootKeyID | ||
expected.MacaroonRootKeyID = fn.None[uint64]() | ||
|
||
require.Equal(t, expected, got) | ||
|
||
got.AccountID = fn.None[accounts.AccountID]() | ||
got.AccountID = actualAccountID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
expected.MacaroonRootKeyID = expectedMacRootKey | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest
litmac
?