Commit graph

127 commits

Author SHA1 Message Date
ziggie
2a4a57b55b
build: bump sqldb/v2 to v2.0.0 2026-07-10 19:34:19 -03:00
Viktor Torstensson
9016bbb3d2
multi: confirm kvdb migration at startup
Prompt before automatically migrating legacy kvdb state to SQL when
litd starts with a SQL backend and active bbolt data is still present.

Detect prior migrations by checking for the SQL tombstone marker so
already-migrated stores can start without prompting. Add unit coverage
for the prompt flow and wire stdin through the itest harness so the
migration restart path can acknowledge the prompt automatically.
2026-06-08 21:11:51 +02:00
Viktor Torstensson
29b3a9b8db
session: guard nil migrated macaroon recipe
Initialize the migrated session's MacaroonRecipe before applying the
nil-perms and nil-caveats normalization used during migration
validation.

This is needed because the SQL store can represent a session with no
macaroon permission or caveat rows as a nil MacaroonRecipe, while the
KV store may still have a non-nil recipe wrapper. Without this guard,
overrideMacaroonRecipe can dereference a nil migrated recipe and panic
instead of letting the migration validation complete normally.

Using an empty MacaroonRecipe preserves the intended normalization
behavior for the nil/empty recipe cases while still allowing genuine
mismatches to fail through DeepEqual.
2026-06-08 17:21:03 +02:00
Viktor Torstensson
b61f62d7d9
multi: preserve macaroon recipe order in SQL
Store explicit positions for session macaroon caveats and permissions
in the SQL schema and read them back in position order. Also remove the
migration-time sorting workaround in session comparison, so migration
validation now checks the actual persisted order instead of masking it.

This is needed because session caveat order is not just presentation
data. LiT adds caveats to the baked macaroon in slice order, and the
macaroon library updates the signature hash chain for each added caveat.
Reordering caveats can therefore change the resulting macaroon bytes and
signature.

The previous schema split caveats and permissions into child tables
without any position column, and the SQL reads had no ORDER BY. The KV
store preserves slice order, but SQL had no explicit way to reproduce
that order after migration or on later reads. The migration code’s old
sorting step was only making validation deterministic; it did not
preserve the original recipe order.

Permissions are canonicalized by lnd when baking, so their order is less
semantically important for the final macaroon. They still get positions
here so the stored recipe remains faithful to the original session data
and both child tables behave consistently.

Why it was needed:

- caveats needed explicit order preservation because they are appended
  and signed in order.
- The old SQL schema did not store order, and the read queries did not
  request one.
- Adding position makes the SQL representation faithful to the KV/TLV
  recipe instead of relying on incidental row order.

- Adding it to permissions too keeps the stored recipe lossless and
  consistent, even though lnd. canonicalizes permissions before baking.

NOTE: This commit explicitly edits the previous migration instead of
adding a new one. This is ok as SQL dbs are not yet supported in
production, so there are no live deployments to worry about.
2026-06-08 17:21:03 +02:00
Viktor Torstensson
36eb65c526
Merge pull request #1312 from ViktorT-11/2026-05-sql-migration-fixes
[sql-74] Handle empty VerificationIDs for sessions & normalize action timestamp handling to UTC
2026-05-19 11:54:47 +02:00
Viktor Torstensson
7b3916596f
session: cover empty caveat verification ids
Add migration coverage for nested empty caveat verification IDs
and normalize the migrated SQL representation so empty byte
slices compare consistently with the KV store.
2026-05-19 11:27:12 +02:00
Viktor Torstensson
2f5de800c8
multi: improve kvdb migration progress logging
Add clearer progress logging across the KVDB to SQL migration paths
for accounts, sessions, and firewalldb.

Log the total number of items to migrate where it is cheap to
determine up front, and emit progress updates every 100 migrated
items.
2026-05-19 11:22:45 +02:00
Viktor Torstensson
000d28a4fb
multi: deprecate kvdb stores after SQL migration
Mark the legacy kvdb stores as deprecated once the kvdb -> SQL
migration commits successfully. This prevents normal bbolt startup
from reopening accounts.db, session.db, or rules.db after their data
has already been migrated.

Add explicit deprecation checks to the three kvdb store open paths and
provide migration-only constructors that can still reopen deprecated
files when the SQL database is deleted or downgraded and the migration
must be rerun.

Use store-specific tombstones for the deprecation markers and add
tests that verify deprecated stores are rejected while migration
reruns continue to work.
2026-05-14 11:39:14 +02:00
Viktor Torstensson
f78ddf1702
multi: rename MigrationStream names to MigrationSet 2026-05-14 11:39:09 +02:00
Viktor Torstensson
d1743c3b29
session: add database backend benchmark test 2026-05-14 11:38:55 +02:00
Viktor Torstensson
f77ae5dcf2
multi: introduce migration stream for unit tests
In upcoming commits, we will introduce a new migration stream package
that will need to reference the db package, as well as the accounts,
session and firewalldb package in future commits. To avoid circular
dependencies, we therefore introduce a new migration stream that unit
tests can use, in order to avoid having to import the new migration
stream package.
2026-05-14 11:38:24 +02:00
Viktor Torstensson
80dead031f
session: use sqlcmig6 for kvdb to sql migration
This commit updates the session package to use the new `sqlcmig6`
package for kvdb to SQL migration.
2026-05-14 11:38:06 +02:00
Viktor Torstensson
eb26c2b1c5
mutli: rename db.NewTestPostgresV2DB function
As the legacy `NewTestPostgresDB` function is no longer used and has
been removed, it no longer makes sense to have a `V2` suffix on the
`NewTestPostgresV2DB` function. This commit renames it to
`NewTestPostgresDB`, to indicate that this function now replaces the
legacy function.
2026-05-14 11:37:54 +02:00
Viktor Torstensson
252d1206b6
multi: use sqldb/v2 in session package
Update the session package to use the `sqldb/v2` package instead of the
older version.
2026-05-14 11:37:43 +02:00
Boris Nagaev
1629e3106a
session: fix mailbox TLS ALPN regression
Starting in grpc-go v1.67.0, clients and servers reject TLS handshakes when
ALPN is not negotiated. This changed the default value of
GRPC_ENFORCE_ALPN_ENABLED from false to true.

The default flip is in https://github.com/grpc/grpc-go/pull/7535

Our mailbox transport handshake can reach endpoints that currently do not
negotiate ALPN, so LNC session setup started failing and the lnc_auth flow
timed out, with malformed header/content-type errors showing up later on the
stream path.

This adds mailbox-specific TLS transport credentials that allow a missing
negotiated ALPN value for mailbox links. The mailbox server path and the
integration-test mailbox clients now use these credentials, so session
establishment works again.
2026-04-05 17:21:21 -05:00
ffranr
a0e63124c0
multi: reformat long lines for readability
- Replace occurrences of `// nolint:lll` with `// nolint:ll` across
  files for consistency.
- Reformat multiline strings, comments, and function parameters to
  improve clarity and adhere to style guidelines.
- Add `// nolint:ll` comments where necessary to prevent linter
  warnings.
2025-12-09 16:12:03 +00:00
Viktor Torstensson
d14d7668f6
Merge pull request #1174 from bitromortac/2511-delete-session-after-fail
sessions: delete session after registration failure
2025-11-26 15:49:17 +01:00
bitromortac
a18c0b656f
session: implement DeleteReservedSession 2025-11-25 19:35:42 +01:00
Viktor Torstensson
3d10572280
session: fix empty config for features in sql mig
In the bbolt store, an empty config for a feature is represented as an
empty array, while in for the SQL store, the same config is represented
as nil. Therefore, in the scenario where a specific feature has an empty
config, we override the SQL FeatureConfig for that feature to also be
set to an empty array. This is needed to ensure that the deep equals
check in the migration validation does not fail in this scenario.
2025-11-10 13:40:51 +01:00
Viktor Torstensson
b4b36f653f
session: handle removed linked account
If a user creates a session linked to an account, but later deletes that
account using the `litcli accounts remove` command, the KVDB session
will still contain the account ID of the removed account.

During the KVDB to SQL migration, the accounts migration runs before the
sessions migration. Since the deleted account no longer exists, it
cannot be migrated. As a result, the migrated SQL session cannot link to
the now non-existent account in the SQL store.

In such cases, we will migrate the session and not link to any account
in the SQL session.
2025-09-26 20:46:50 +02:00
Elle
bce4c049a9
Merge pull request #1133 from ViktorTigerstrom/2025-08-session-migration-duplicate-id-fix
[sql-50] session migration duplicate ID fix
2025-09-02 16:20:37 +02:00
Viktor Tigerström
dfe0fa8b07
session: handle multiple sessions with the same ID
The logic to avoid duplicate session IDs was first added with the
session linking linking functionality. That means that there may be old
legacy sessions that were added prior to that which may have duplicate
IDs. Before this commit, such sessions would cause the kvdb to SQL
migration to error, as there is a uniqueness constraint on the legacy
session alias in the SQL sessions table.

We want to keep that constraint, so in such rare cases, we update the
kvdb to SQL migration logic to drop all but the session with the
latest CreatedAt time if for sessions that share the same ID.
That follows the logic in c8b78bd10d as
closely as possible, as that migration ensured that all sessions but the
latest one was revoked if multiple sessions shared the same ID.
2025-08-21 16:03:31 +02:00
Viktor Tigerström
2d3564b36b
session: add expected results to migration tests
In the upcoming commit, we will update the kvdb to sql migration to not
always include all sessions in the kvdb store. Therefore, we update the
kvdb to migration tests in order to be able to handle such cases by
including the expected results for each test case.
2025-08-19 23:22:15 +02:00
Viktor Tigerström
1af12f4fb9
session: sort MacaroonRecipe.Permissions in migration
Similar to the previous commit, we also sort the
`MacaroonRecipe.Permissions` slice to ensure it can be compared in a
deterministic manner during migrations.
2025-08-15 17:19:23 +02:00
Viktor Tigerström
d3eb3cd3fc
session: sort MacaroonRecipe.caveats in migration
In the kvdb to sql migration, if there have been caveats set for the
MacaroonRecipe, the order of the postgres db caveats will in very rare
cases differ from the kv store caveats. Therefore, we sort both the kv
and sql caveats by their ID, so that we can compare them in a
deterministic way.
2025-08-07 15:54:16 +02:00
Oliver Gugger
c417adf318
mod+accounts+session: update to latest lnd RC 2025-07-11 18:41:16 +02:00
Viktor Tigerström
2007d538ec
session: add randomized session migration test 2025-06-12 13:06:27 +02:00
Viktor Tigerström
8f1aa99ee5
session: add migration code from kvdb to SQL
This commit introduces the migration logic for transitioning the
sessions store from kvdb to SQL.

Note that as of this commit, the migration is not yet triggered by any
production code, i.e. only tests execute the migration logic.
2025-06-12 11:52:39 +02:00
Viktor Tigerström
6c4c6a4319
session: ensure that test SQL store is closed
We add a helper function to the functions that creates the test SQL
stores, in order to ensure that the store is properly closed when the
test is cleaned up.
2025-06-12 11:52:39 +02:00
Viktor Tigerström
b48e40f000
session: update NewTestDB funcs to return Store
In preparation for upcoming migration tests from a kvdb to an SQL store,
this commit updates the NewTestDB function to return the Store interface
rather than a concrete store implementation.

This change ensures that migration tests can call NewTestDB under any
build tag while receiving a consistent return type.
2025-06-12 11:52:38 +02:00
Elle Mouton
87bef069e3
session: add session ID to grpc metadata via context
Add grpc interceptors that inject an LNC session's ID into the context
as gRPC metadata. By injecting it as such, it will be transported over
the wire in any outgoing gRPC calls.

This lets us be sure that any session call sent to the RPCMiddleware
interceptor in LND will continue to be grouped along with the
appropriate session ID. This gives LND a way to send the metadata we
include back to LiT meaning that we will later on be able to extract the
session ID again.
2025-05-15 06:15:10 +02:00
Elle Mouton
4f8c3aec16
firewalldb+rpcserver: refactor ListActions
Here we move the filter logic behind the interface so that our sql
implementation can make use of indexes.
2025-05-09 13:57:10 +02:00
Elle Mouton
7cb22cead9
session: embed db.BaseDB in SQLStore
So that we can extract the same BaseDB in other packages and use it for
initialising other SQL stores during tests.
2025-04-15 11:24:49 +02:00
Elle Mouton
34a202f61e
session: fix and test some return values
GetGroupID is given a session ID as a param and so should return
ErrSessionNotFound if that session is not found. GetSessionIDs is given
a groupID as a param and so should return ErrUnknownGroup if that group
does not exist.

This commit updates the kvstore and sql implementations to return the
correct error values and also ensures that this is properly tested now.
2025-04-07 13:41:27 +02:00
George Tsagkarelis
443b5f4170
Merge pull request #998 from lightninglabs/lnd-19
multi: update LiT and all deps to use LND 0.19
2025-03-26 18:29:21 +01:00
Elle Mouton
dbec9da31b
multi: update LND and friends
This commit updates:
- LND
- pool
- loop
- taproot-assets
- faraday
- lighting-node-connect
- aperture
- lndclient
- btclog

We start using the new btclog v2 library and the associated breaking
changes in the lnd/build package.

With this commit, we deprecate the `--remote.lit-maxlogfiles` and
`--remote.lit-maxlogfilesize` options and introduce new logging options
under the `--remote.lit-logging` namespace.

Finally, the LND update introduced a new `MaxBlocksMinedPerTest`
variable in the `lntest` package that we now need to override in order
for our itests to pass.
2025-03-26 09:20:52 -05:00
Elle Mouton
60da67d50a
session: add SQL CRUD
This commit adds the SQL implementation of the session.Store interface.
This can be run against all session unit tests via `make unit
pkg=session tags=test_db_sqlite` and `make unit pkg=session
tags=test_db_postgres`.
2025-03-25 16:58:01 +02:00
Elle Mouton
b82e01edf4
session: update session remote pub by session ID
It's a better pattern to refer to sessions in the same way consistently.
So we update the UpdateSessionRemotePubKey method to use a session ID as
a reference to the session instead of local pub key.
2025-03-04 17:39:03 +02:00
Elle Mouton
190d3dc828
multi: rename GetSessionByID to GetSession
By default, we fetch records by an ID.
2025-03-04 17:34:31 +02:00
Elle Mouton
1ba381cead
session: rename GetSession method
Rename it to GetSessionByLocalPub so that it is more accurately named
and so that we free up the GetSession name for future use.
2025-03-04 17:30:43 +02:00
Elle Mouton
8a34189b45
session: assert account exists before linking a session to it
In this commit, we more tightly & explicitly link a session to an
account. At a persitance layer, we have always only linked a session to
an account by encoding the AccountID within the macaroon caveat that we
store with the session. We still keep this persistence the same but now
we first ensure that the account exists and we also add an AccountID
field to the Session struct.
2025-03-04 13:43:42 +02:00
Elle Mouton
2717aa0bc2
session+terminal: give session Store access to the accounts store
When we link a session to an account, we want to be able to validate
that the account actually does exist. So in preparation for that, we
first give the session store access to the accounts store.
2025-03-04 13:43:13 +02:00
Elle Mouton
b3bc6df4e4
session: fix test timestamp
Before we add the SQL implementation of the store, we fix this timestamp
to be a more realistic number. This is needed since in sqlite,
timestamps are serialised as strings and golang actually cant parse the
string for the current timestamp back into a time.Time. This is the
error that will be encountered:
`parsing time "99999-01-01 00:00:00 +0000 UTC" as "2006-01-02
15:04:05.999999999 -0700 MST": cannot parse "9-01-01 00:00:00 +0000 UTC"
as "-"`
2025-03-02 13:28:34 +02:00
Elle Mouton
306519fd6d
session: remove variadic State param from ListSessionsByState
We only really ever use it in one place and even then, only for a
session State that we no longer use anymore.

This is done to make the SQL queries we will need to implement the SQL
Store more simple.
2025-03-02 13:27:14 +02:00
Elle Mouton
01c19c5172
session: add context to UpdateSessionRemotePubKey 2025-02-28 09:26:02 +02:00
Elle Mouton
f93093e7c9
session: add context to ShiftState 2025-02-28 09:26:02 +02:00
Elle Mouton
aa7b1db207
session: pass contexts through to all IDToGroupIndex methods 2025-02-28 09:26:02 +02:00
Elle Mouton
9642ce1656
session+firewall: pass context to GetSessionByID 2025-02-28 09:26:02 +02:00
Elle Mouton
aa5674cabd
session: add context to DeleteReservedSessions 2025-02-28 09:24:42 +02:00
Elle Mouton
7c30c510a1
session: add context to various session List methods 2025-02-28 09:24:42 +02:00