-
Notifications
You must be signed in to change notification settings - Fork 107
[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
Conversation
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.
Summary of Changes
Hello @ViktorTigerstrom, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request implements a foundational update to the SQL actions store, enabling it to record the complete 8-byte macaroon root key ID. This enhancement is a crucial prerequisite for the larger migration effort to transition the actions store from a KVDB to a SQL backend. By storing the full root key ID, the system gains improved accuracy and future-proofing for macaroon-related actions. The current design ensures backward compatibility by not altering the KVDB implementation, with plans to fully leverage the extended ID once the KVDB is phased out.
Highlights
- Enhanced Macaroon ID Storage: The SQL actions store now persists the full 8-byte macaroon root key ID (uint64) instead of just the last 4 bytes, improving data fidelity for macaroon identification.
- Migration Preparation: This change is a critical preparatory step for the ongoing migration of the actions store from KVDB to SQL, ensuring the SQL store is ready for more complete macaroon data.
- Backward Compatibility: The KVDB implementation remains unchanged and will ignore the full root key ID, maintaining compatibility with existing systems until the KVDB store is fully deprecated.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point by creating a comment using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands on the current page.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request prepares for a database migration by changing the SQL actions store to persist the full 8-byte macaroon root key ID. The changes are mostly correct and well-tested, but I've found a critical issue in the data retrieval logic that would cause a panic. I've also suggested a minor refactoring in the test code to improve maintainability. Please see my detailed comments.
37fa599
to
87cf67e
Compare
@ViktorTigerstrom - what's the main reasoning around storing the full thing? i guess no harm in doing so, but just checking understanding |
firewall/request_logger.go
Outdated
"sync" | ||
|
||
"github.com/lightninglabs/lightning-terminal/firewalldb" | ||
litd_macaroons "github.com/lightninglabs/lightning-terminal/macaroons" |
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
?
firewall/request_logger.go
Outdated
MacaroonIdentifier: macaroonID, | ||
MacaroonRootKeyID: rootKeyID, |
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.
ok and we need both cause kvdb still stores old identifier but now sql stores full rootkey id?
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.
Correct, the kvdb
store will only store the 4 byte MacaroonID, and not the full 8 bytes. Therefore all callsites that uses the persisted AddActionReq
will still only use the MacaroonIdentifier
still.
firewalldb/actions.go
Outdated
// 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 |
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.
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 comment
The 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.
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.
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.
Hmmm I'm not sure if I misunderstand you here, but as this is also used as the return value for an action
, we would need to set the MacRootKeyID
when returning an kvdb
action if we were to remove the MacaroonIdentifier
field. However, since we can't regenerated the full MacRootKeyID
from just the MacaroonIdentifier
that wouldn't be possible. OR am I misunderstanding your suggestion?
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
Good idea. I added a warning to the MacaroonRootKeyID
that it will not be set for kvdb
database backends.
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.
Potentially, we could make the struct returned from a persisted action, different from the struct used when persisting a new action. I.e. not use the AddActionReq
struct for both.
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.
Perhaps you could move MacaroonIdentifier
from AddActionReq
to Action
and keep the full root key unpopulated on retrieval?
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.
we would need to set the MacRootKeyID when returning an kvdb action if we were to remove the MacaroonIdentifier field
but we only ever use 4 of the bytes right? and i think we only ever use it in ListActions for the RPC?
so could potentially just let the bbolt impl append 4 zero bytes to whatever it has stored in order to construct the full rootkeyId?.
bitromortac's idea is also good - we can also just completely remove the Req struct from being embedded in the action. We could rather just have an ActionsInfo
struct which both the ActionsReq and Actions structs can embed. then it makes things clearer and doesnt require that we store everything that we needed to construct the ActionsReq.
Anyways, gonna leave this up to you - but i do think it makes things easier to read. So leaving as non-blocking comment
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.
so could potentially just let the bbolt impl append 4 zero bytes to whatever it has stored in order to construct the full rootkeyId?.
Ah good idea! I actually liked that version most, so I went a head and implemented that. I therefore also added a last commit that removes the MacaroonIdentifier
field. If we want to further change the structure of the Action
& AddActionReq
struct, I'd suggest we take that in a future follow-up once we've decided how we'd like it structured :)
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.
Those changes look good! Just a small suggestion to add a method to cut down on code repetition: patch.txt.
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.
Cool thanks 🙏! Applied the patch as an extra helper function commit :)
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 comment
The 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)
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.
Idid not set the field previously to currently only have one field set when reading a persisted action, to make it as little confusing as possible at callsites, and ensure no callsites used the MacaroonRootKeyID
field yet. But I agree that this potentially even makes it more confusing. So therefore changed so that we set the MacaroonRootKeyID
field when reading persisted actions on a an sql databasebackend :)
Thanks for the review @ellemouton. I'm answering this clarification first, as I believe that's the deciding factor regarding wether we want to go ahead of with this PR or not:
So after working through the actions migration, it has become clear that the only real reason we'd need to persist the full macaroon root key id (8 bytes), compared to the short version (just the last 4 bytes), is to make it possible to link which exact macaroon was used to when the action was generated. However, I'm not super firm that this protection is totally needed, and IMO it's worth doing for the the sake of it being more clean than opening up for the collision risk. If we do not care about that collision protection however, I don't think we actually need to determine the full root key ID during the migration time as it's not really needed to link possible linked sessions/actions. If so, we can drop the whole short mac key ID -> full root key mapping in the actions migration. Let me know if you think we should proceed with this, or just not try to solve the collision issue :) |
I've realized one scenario where persisting the full root key ID miiiiight be useful is if the user deletes their Not sure if there are many scenarios where this would be useful though :) |
Thanks for the info @ViktorTigerstrom - i think yes it makes sense to store the full thing 🙏 |
firewall/request_logger.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Still a bit unsure why it's needed, looking at buildSession
, we derive the full root key id from the last four bytes of the local pubkey and a common prefix, see NewSuperMacaroonRootKeyID
. So it seems like we can always reconstruct the full macaroon root key id from the four bytes that are stored?
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.
Thanks a lot for the deep investigation! Unfortunately though, s also communicated offline we an have actions listed in litd which have a different prefix than the SuperMacaroonRootKeyPrefix
.
If you run litd with the firewall.request-logger.level=all
config option set, any calls using the pure lnd admin.macaroon
, will log actions that start with bytes of just zeros.
Additionally, if users have created a macaroon with lncli bakemacaroon
command and set a value for the root_key_id
param that uses another prefix than the SuperMacaroonRootKeyPrefix
, calls using that macaroon will also use another prefix than the SuperMacaroonRootKeyPrefix
.
I do however think that no macaroons that were used for litd sessions nor accounts should ever start with any other prefix than the SuperMacaroonRootKeyPrefix
.
We therefore decided to go ahead with this PR.
firewalldb/actions_test.go
Outdated
acct1, err := accountsDB.NewAccount(ctx, 0, time.Time{}, "foo") | ||
require.NoError(t, err) | ||
|
||
sess1RootKeyID := macIDToRootKeyID(sess1.ID) |
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.
so here we could use NewSuperMacaroonRootKeyID
instead?
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.
Thanks, updated to use your suggestion!
b805fb4
to
df09427
Compare
Thanks a lot for the reviews @ellemouton & @bitromortac! I've addressed your feedback in the best way I could. I do agree that it's a bit confusing that there's both a |
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.
🙏
df09427
to
0c9ef5a
Compare
Thanks for the review @ellemouton! Addressed your latest feedback with: |
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.
LGTM 🎉 (just have a minor suggestion to cut down code repetition a bit)
firewalldb/actions.go
Outdated
// 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 |
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.
Those changes look good! Just a small suggestion to add a method to cut down on code repetition: patch.txt.
The upcoming commit will update the `AddActionReq` struct to include an extra field which the `kvdb` actions store will ignore. Therefore the `assertEqualActions` for the `kvdb` version will need to be update to ignore this field. In preparation for that change, we also do another optimization of the `assertEqualActions` function under kvdb builds, to not mutate the passed action references.
When migrating the actions store from kvdb to sql, we will update the existing actions to include the full mac root key, instead of just the last 4 bytes (currently called `MacaroonIdentifier`). In order to do so, we change the sql implementation of the `actions` store to persist the full mac root key, instead of just the last 4 bytes. As no production data in the sql actions store exists for users yet, it's fine for us to change this without having to address old sql actions which only stored the last 4 bytes. Note though that since old actions stored in the kvdb implementation only have the last 4 bytes of the mac root key persisted, we will only ever persist the last 4 byte of the mac root key ID for kvdb actions. When the actions are later read back from the kvdb store, the first 4 bytes of the mac root key ID will be padded with zeroes to make up the full 8 bytes. As no call site currently utilizes the full 8 bytes of the mac root key ID, this is okay for now. When we later deprecate and remove the kvdb implementation, we can then update the rest of `litd` to also use the full mac root key ID.
As the `MacaroonRootKeyID` field of the `AddActionReq` struct also contains the 4 bytes of the `MacaroonIdentifier`, we change all call sites to instead use the last 4 bytes of the `MacaroonRootKeyID` field. As the `MacaroonIdentifier` field therefore becomes redundant, we also remove it.
Add helper method to `AddActionReq` returns the 4 byte macaroon ID that is derived from the MacaroonRootKeyID. Using the helper removes some code repetition at call sites, and makes the intended usage clearer.
0c9ef5a
to
bd59605
Compare
Part of #917
This PR addresses an issue I noticed we need to address prior to #1143:
When migrating the actions store from kvdb to sql, we will need to update the existing actions to include the full mac root key, instead of just the last 4 bytes (currently called
MacaroonIdentifier
). In order to do so, we need to change the sql implementation of theactions
store to persist the full mac root key, instead of just the last 4 bytes. As no production data in the sql actions store exists for users yet, it's fine for us to change this without having to address old sql actions which only stored the last 4 bytes.This PR adds this change.
Note though that this PR doesn't update the kvdb implementation, and the full macaroon root key will be ignored by the kvdb store even if set. Therefore, the rest of
litd
will still have to just expect the last 4 bytes of the mac root key when accessing anAction
's MacaroonIdentifier. Therefore, we we currently never expose the rest of the mac root key outside of the sql actions store.Once the kvdb store has been fully deprecated and removed, we can then update the rest of
litd
to also use the full mac root key, and change theAction
struct's field to reflect this.