Skip to content

[sql-37] firewalldb: more preparations for SQL actions store #1070

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

Merged
merged 6 commits into from
May 15, 2025

Conversation

ellemouton
Copy link
Member

@ellemouton ellemouton commented May 13, 2025

This PR mainly does some refactors in preparation for adding the SQL version of the Actions store.

  • We adjust the AddActions method so that it takes an AddActionsReq instead of an Action. This is more idiomatic since Action includes fields that should not be required by the caller to set.
  • We rename the Action.SessionID field to MacaroonIdentifier to more clearly describe what it actually is and then add an optional Session.ID which is meant to represent an actual session the action is linked to. For our bbolt impl: we continue to only read from the action.MacaroonIdentifier at persistence time (as we did before) and then at read time, we do a best effort attempt at populating the new SessionID field.

Part of #917

In preparation for using the clock to get an Action's AttemptedAt time
in an upcoming commit, we let both the bbolt and SQL impls of the
firewalldb take a clock.
@ellemouton ellemouton added the no-changelog This PR is does not require a release notes entry label May 13, 2025
@ellemouton ellemouton self-assigned this May 13, 2025
Instead of passing an `Action` to the AddAction method, we introduce an
`AddActionReq` type which only holds the fields that are needed to
create a new Action. The rest of the info is determined by the DB layer.
To make it very clear what the data is actually derived from. Then also
add an optional Session.ID. Our bbolt db wont store this real session ID
and will populate it in a best effort manner by casting the persisted
MacaroonIdentifier.
We add a new macaroon_identifier field to the Action proto message an
populate it in the rpc server.
Before this commit, some of the actions store test were using the same
global variables & some tests were also modifying those variables
meaning that we could not run the tests in parallel. So here we instead
make the variables local to the test and add `t.Parallel` to each test.
This will also be useful later on when the session IDs used by the
`AddActionReq` structs are instead derived from a sessions store.
Copy link
Contributor

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ⚡

// NOTE: for our BoltDB impl, this is not persisted in any way, and we
// populate it by casting the macaroon ID to a session.ID and so is not
// guaranteed to be linked to an existing session.
SessionID fn.Option[session.ID]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a test anywhere for an intercepted action, where we don't set this (i.e. it's not related to a session request).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i'll expand on the tests for SQL (where it actually will matter if it is set or not) once we've added the sql impl here 👍

SessionID session.ID
// MacaroonIdentifier is a 4 byte identifier created from the last 4
// bytes of the root key ID of the macaroon used to perform the action.
MacaroonIdentifier [4]byte
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's probably not worth it to have another type alias for [4]byte?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently we dont use this in other places - so i think it's ok for now

@@ -171,7 +174,7 @@ func TestListActions(t *testing.T) {
actionIds++

actionReq := &AddActionReq{
SessionID: sessionID,
MacaroonIdentifier: sessionID,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we set SessionID here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want to keep the tests as close to how they are today as possible - then will expand the tests to cover more cases once the SQL impl is added 👍

@@ -14,11 +14,23 @@ import (
var (
testTime1 = time.Unix(32100, 0)
testTime2 = time.Unix(12300, 0)
)

// TestActionStorage tests that the ActionsListDB CRUD logic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the nice diffs 👍

Copy link
Contributor

@ViktorTigerstrom ViktorTigerstrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT given that my comment isn't an issue 🚀!


action := &Action{
AddActionReq: *req,
AttemptedAt: db.clock.Now().UTC(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that the time is set to the clocks .Now().UTC(), while the previous version set it to time.Now() (i.e. not UTC). I think the new implementation is probably more correct, but wanted to note that. Could this cause any issues, anddo we need to update previous inserted actions to represent that in some way?

Copy link
Member Author

@ellemouton ellemouton May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah - similarly to sessions/accounts - we're going with "it may just be off by a couple of hours and that's ok" since mostly these timestamps are just metadata for display to the user - we dont use them (much) in logic.

So yeah - i think not worth a whole migration just to fix this

@ellemouton ellemouton merged commit 9d789eb into lightninglabs:master May 15, 2025
22 checks passed
@ellemouton ellemouton deleted the sql37 branch May 15, 2025 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog This PR is does not require a release notes entry sql-ize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants