lightning-terminal/db/sqlc/sessions.sql.go
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

702 lines
17 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: sessions.sql
package sqlc
import (
"context"
"database/sql"
"time"
)
const deleteSession = `-- name: DeleteSession :exec
DELETE FROM sessions
WHERE id = $1
`
func (q *Queries) DeleteSession(ctx context.Context, id int64) error {
_, err := q.db.ExecContext(ctx, deleteSession, id)
return err
}
const deleteSessionsWithState = `-- name: DeleteSessionsWithState :exec
DELETE FROM sessions
WHERE state = $1
`
func (q *Queries) DeleteSessionsWithState(ctx context.Context, state int16) error {
_, err := q.db.ExecContext(ctx, deleteSessionsWithState, state)
return err
}
const getAliasBySessionID = `-- name: GetAliasBySessionID :one
SELECT alias FROM sessions
WHERE id = $1
`
func (q *Queries) GetAliasBySessionID(ctx context.Context, id int64) ([]byte, error) {
row := q.db.QueryRowContext(ctx, getAliasBySessionID, id)
var alias []byte
err := row.Scan(&alias)
return alias, err
}
const getSessionAliasesInGroup = `-- name: GetSessionAliasesInGroup :many
SELECT alias FROM sessions
WHERE group_id = $1
`
func (q *Queries) GetSessionAliasesInGroup(ctx context.Context, groupID sql.NullInt64) ([][]byte, error) {
rows, err := q.db.QueryContext(ctx, getSessionAliasesInGroup, groupID)
if err != nil {
return nil, err
}
defer rows.Close()
var items [][]byte
for rows.Next() {
var alias []byte
if err := rows.Scan(&alias); err != nil {
return nil, err
}
items = append(items, alias)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getSessionByAlias = `-- name: GetSessionByAlias :one
SELECT id, alias, label, state, type, expiry, created_at, revoked_at, server_address, dev_server, macaroon_root_key, pairing_secret, local_private_key, local_public_key, remote_public_key, privacy, account_id, group_id FROM sessions
WHERE alias = $1
`
func (q *Queries) GetSessionByAlias(ctx context.Context, alias []byte) (Session, error) {
row := q.db.QueryRowContext(ctx, getSessionByAlias, alias)
var i Session
err := row.Scan(
&i.ID,
&i.Alias,
&i.Label,
&i.State,
&i.Type,
&i.Expiry,
&i.CreatedAt,
&i.RevokedAt,
&i.ServerAddress,
&i.DevServer,
&i.MacaroonRootKey,
&i.PairingSecret,
&i.LocalPrivateKey,
&i.LocalPublicKey,
&i.RemotePublicKey,
&i.Privacy,
&i.AccountID,
&i.GroupID,
)
return i, err
}
const getSessionByID = `-- name: GetSessionByID :one
SELECT id, alias, label, state, type, expiry, created_at, revoked_at, server_address, dev_server, macaroon_root_key, pairing_secret, local_private_key, local_public_key, remote_public_key, privacy, account_id, group_id FROM sessions
WHERE id = $1
`
func (q *Queries) GetSessionByID(ctx context.Context, id int64) (Session, error) {
row := q.db.QueryRowContext(ctx, getSessionByID, id)
var i Session
err := row.Scan(
&i.ID,
&i.Alias,
&i.Label,
&i.State,
&i.Type,
&i.Expiry,
&i.CreatedAt,
&i.RevokedAt,
&i.ServerAddress,
&i.DevServer,
&i.MacaroonRootKey,
&i.PairingSecret,
&i.LocalPrivateKey,
&i.LocalPublicKey,
&i.RemotePublicKey,
&i.Privacy,
&i.AccountID,
&i.GroupID,
)
return i, err
}
const getSessionByLocalPublicKey = `-- name: GetSessionByLocalPublicKey :one
SELECT id, alias, label, state, type, expiry, created_at, revoked_at, server_address, dev_server, macaroon_root_key, pairing_secret, local_private_key, local_public_key, remote_public_key, privacy, account_id, group_id FROM sessions
WHERE local_public_key = $1
`
func (q *Queries) GetSessionByLocalPublicKey(ctx context.Context, localPublicKey []byte) (Session, error) {
row := q.db.QueryRowContext(ctx, getSessionByLocalPublicKey, localPublicKey)
var i Session
err := row.Scan(
&i.ID,
&i.Alias,
&i.Label,
&i.State,
&i.Type,
&i.Expiry,
&i.CreatedAt,
&i.RevokedAt,
&i.ServerAddress,
&i.DevServer,
&i.MacaroonRootKey,
&i.PairingSecret,
&i.LocalPrivateKey,
&i.LocalPublicKey,
&i.RemotePublicKey,
&i.Privacy,
&i.AccountID,
&i.GroupID,
)
return i, err
}
const getSessionFeatureConfigs = `-- name: GetSessionFeatureConfigs :many
SELECT session_id, feature_name, config FROM session_feature_configs
WHERE session_id = $1
`
func (q *Queries) GetSessionFeatureConfigs(ctx context.Context, sessionID int64) ([]SessionFeatureConfig, error) {
rows, err := q.db.QueryContext(ctx, getSessionFeatureConfigs, sessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SessionFeatureConfig
for rows.Next() {
var i SessionFeatureConfig
if err := rows.Scan(&i.SessionID, &i.FeatureName, &i.Config); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getSessionIDByAlias = `-- name: GetSessionIDByAlias :one
SELECT id FROM sessions
WHERE alias = $1
`
func (q *Queries) GetSessionIDByAlias(ctx context.Context, alias []byte) (int64, error) {
row := q.db.QueryRowContext(ctx, getSessionIDByAlias, alias)
var id int64
err := row.Scan(&id)
return id, err
}
const getSessionMacaroonCaveats = `-- name: GetSessionMacaroonCaveats :many
SELECT id, session_id, caveat_id, verification_id, location, position FROM session_macaroon_caveats
WHERE session_id = $1
ORDER BY position ASC
`
func (q *Queries) GetSessionMacaroonCaveats(ctx context.Context, sessionID int64) ([]SessionMacaroonCaveat, error) {
rows, err := q.db.QueryContext(ctx, getSessionMacaroonCaveats, sessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SessionMacaroonCaveat
for rows.Next() {
var i SessionMacaroonCaveat
if err := rows.Scan(
&i.ID,
&i.SessionID,
&i.CaveatID,
&i.VerificationID,
&i.Location,
&i.Position,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getSessionMacaroonPermissions = `-- name: GetSessionMacaroonPermissions :many
SELECT id, session_id, entity, action, position FROM session_macaroon_permissions
WHERE session_id = $1
ORDER BY position ASC
`
func (q *Queries) GetSessionMacaroonPermissions(ctx context.Context, sessionID int64) ([]SessionMacaroonPermission, error) {
rows, err := q.db.QueryContext(ctx, getSessionMacaroonPermissions, sessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SessionMacaroonPermission
for rows.Next() {
var i SessionMacaroonPermission
if err := rows.Scan(
&i.ID,
&i.SessionID,
&i.Entity,
&i.Action,
&i.Position,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getSessionPrivacyFlags = `-- name: GetSessionPrivacyFlags :many
SELECT session_id, flag FROM session_privacy_flags
WHERE session_id = $1
`
func (q *Queries) GetSessionPrivacyFlags(ctx context.Context, sessionID int64) ([]SessionPrivacyFlag, error) {
rows, err := q.db.QueryContext(ctx, getSessionPrivacyFlags, sessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SessionPrivacyFlag
for rows.Next() {
var i SessionPrivacyFlag
if err := rows.Scan(&i.SessionID, &i.Flag); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getSessionsInGroup = `-- name: GetSessionsInGroup :many
SELECT id, alias, label, state, type, expiry, created_at, revoked_at, server_address, dev_server, macaroon_root_key, pairing_secret, local_private_key, local_public_key, remote_public_key, privacy, account_id, group_id FROM sessions
WHERE group_id = $1
`
func (q *Queries) GetSessionsInGroup(ctx context.Context, groupID sql.NullInt64) ([]Session, error) {
rows, err := q.db.QueryContext(ctx, getSessionsInGroup, groupID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Session
for rows.Next() {
var i Session
if err := rows.Scan(
&i.ID,
&i.Alias,
&i.Label,
&i.State,
&i.Type,
&i.Expiry,
&i.CreatedAt,
&i.RevokedAt,
&i.ServerAddress,
&i.DevServer,
&i.MacaroonRootKey,
&i.PairingSecret,
&i.LocalPrivateKey,
&i.LocalPublicKey,
&i.RemotePublicKey,
&i.Privacy,
&i.AccountID,
&i.GroupID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertSession = `-- name: InsertSession :one
INSERT INTO sessions (
alias, label, state, type, expiry, created_at,
server_address, dev_server, macaroon_root_key, pairing_secret,
local_private_key, local_public_key, remote_public_key, privacy, group_id, account_id
) VALUES (
$1, $2, $3, $4, $5, $6, $7,
$8, $9, $10, $11, $12,
$13, $14, $15, $16
) RETURNING id
`
type InsertSessionParams struct {
Alias []byte
Label string
State int16
Type int16
Expiry time.Time
CreatedAt time.Time
ServerAddress string
DevServer bool
MacaroonRootKey int64
PairingSecret []byte
LocalPrivateKey []byte
LocalPublicKey []byte
RemotePublicKey []byte
Privacy bool
GroupID sql.NullInt64
AccountID sql.NullInt64
}
func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (int64, error) {
row := q.db.QueryRowContext(ctx, insertSession,
arg.Alias,
arg.Label,
arg.State,
arg.Type,
arg.Expiry,
arg.CreatedAt,
arg.ServerAddress,
arg.DevServer,
arg.MacaroonRootKey,
arg.PairingSecret,
arg.LocalPrivateKey,
arg.LocalPublicKey,
arg.RemotePublicKey,
arg.Privacy,
arg.GroupID,
arg.AccountID,
)
var id int64
err := row.Scan(&id)
return id, err
}
const insertSessionFeatureConfig = `-- name: InsertSessionFeatureConfig :exec
INSERT INTO session_feature_configs (
session_id, feature_name, config
) VALUES (
$1, $2, $3
)
`
type InsertSessionFeatureConfigParams struct {
SessionID int64
FeatureName string
Config []byte
}
func (q *Queries) InsertSessionFeatureConfig(ctx context.Context, arg InsertSessionFeatureConfigParams) error {
_, err := q.db.ExecContext(ctx, insertSessionFeatureConfig, arg.SessionID, arg.FeatureName, arg.Config)
return err
}
const insertSessionMacaroonCaveat = `-- name: InsertSessionMacaroonCaveat :exec
INSERT INTO session_macaroon_caveats (
session_id, caveat_id, verification_id, location, position
) VALUES (
$1, $2, $3, $4, $5
)
`
type InsertSessionMacaroonCaveatParams struct {
SessionID int64
CaveatID []byte
VerificationID []byte
Location sql.NullString
Position int32
}
func (q *Queries) InsertSessionMacaroonCaveat(ctx context.Context, arg InsertSessionMacaroonCaveatParams) error {
_, err := q.db.ExecContext(ctx, insertSessionMacaroonCaveat,
arg.SessionID,
arg.CaveatID,
arg.VerificationID,
arg.Location,
arg.Position,
)
return err
}
const insertSessionMacaroonPermission = `-- name: InsertSessionMacaroonPermission :exec
INSERT INTO session_macaroon_permissions (
session_id, entity, action, position
) VALUES (
$1, $2, $3, $4
)
`
type InsertSessionMacaroonPermissionParams struct {
SessionID int64
Entity string
Action string
Position int32
}
func (q *Queries) InsertSessionMacaroonPermission(ctx context.Context, arg InsertSessionMacaroonPermissionParams) error {
_, err := q.db.ExecContext(ctx, insertSessionMacaroonPermission,
arg.SessionID,
arg.Entity,
arg.Action,
arg.Position,
)
return err
}
const insertSessionPrivacyFlag = `-- name: InsertSessionPrivacyFlag :exec
INSERT INTO session_privacy_flags (
session_id, flag
) VALUES (
$1, $2
)
`
type InsertSessionPrivacyFlagParams struct {
SessionID int64
Flag int32
}
func (q *Queries) InsertSessionPrivacyFlag(ctx context.Context, arg InsertSessionPrivacyFlagParams) error {
_, err := q.db.ExecContext(ctx, insertSessionPrivacyFlag, arg.SessionID, arg.Flag)
return err
}
const listSessions = `-- name: ListSessions :many
SELECT id, alias, label, state, type, expiry, created_at, revoked_at, server_address, dev_server, macaroon_root_key, pairing_secret, local_private_key, local_public_key, remote_public_key, privacy, account_id, group_id FROM sessions
ORDER BY created_at
`
func (q *Queries) ListSessions(ctx context.Context) ([]Session, error) {
rows, err := q.db.QueryContext(ctx, listSessions)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Session
for rows.Next() {
var i Session
if err := rows.Scan(
&i.ID,
&i.Alias,
&i.Label,
&i.State,
&i.Type,
&i.Expiry,
&i.CreatedAt,
&i.RevokedAt,
&i.ServerAddress,
&i.DevServer,
&i.MacaroonRootKey,
&i.PairingSecret,
&i.LocalPrivateKey,
&i.LocalPublicKey,
&i.RemotePublicKey,
&i.Privacy,
&i.AccountID,
&i.GroupID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listSessionsByState = `-- name: ListSessionsByState :many
SELECT id, alias, label, state, type, expiry, created_at, revoked_at, server_address, dev_server, macaroon_root_key, pairing_secret, local_private_key, local_public_key, remote_public_key, privacy, account_id, group_id FROM sessions
WHERE state = $1
ORDER BY created_at
`
func (q *Queries) ListSessionsByState(ctx context.Context, state int16) ([]Session, error) {
rows, err := q.db.QueryContext(ctx, listSessionsByState, state)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Session
for rows.Next() {
var i Session
if err := rows.Scan(
&i.ID,
&i.Alias,
&i.Label,
&i.State,
&i.Type,
&i.Expiry,
&i.CreatedAt,
&i.RevokedAt,
&i.ServerAddress,
&i.DevServer,
&i.MacaroonRootKey,
&i.PairingSecret,
&i.LocalPrivateKey,
&i.LocalPublicKey,
&i.RemotePublicKey,
&i.Privacy,
&i.AccountID,
&i.GroupID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listSessionsByType = `-- name: ListSessionsByType :many
SELECT id, alias, label, state, type, expiry, created_at, revoked_at, server_address, dev_server, macaroon_root_key, pairing_secret, local_private_key, local_public_key, remote_public_key, privacy, account_id, group_id FROM sessions
WHERE type = $1
ORDER BY created_at
`
func (q *Queries) ListSessionsByType(ctx context.Context, type_ int16) ([]Session, error) {
rows, err := q.db.QueryContext(ctx, listSessionsByType, type_)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Session
for rows.Next() {
var i Session
if err := rows.Scan(
&i.ID,
&i.Alias,
&i.Label,
&i.State,
&i.Type,
&i.Expiry,
&i.CreatedAt,
&i.RevokedAt,
&i.ServerAddress,
&i.DevServer,
&i.MacaroonRootKey,
&i.PairingSecret,
&i.LocalPrivateKey,
&i.LocalPublicKey,
&i.RemotePublicKey,
&i.Privacy,
&i.AccountID,
&i.GroupID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const setSessionGroupID = `-- name: SetSessionGroupID :exec
UPDATE sessions
SET group_id = $1
WHERE id = $2
`
type SetSessionGroupIDParams struct {
GroupID sql.NullInt64
ID int64
}
func (q *Queries) SetSessionGroupID(ctx context.Context, arg SetSessionGroupIDParams) error {
_, err := q.db.ExecContext(ctx, setSessionGroupID, arg.GroupID, arg.ID)
return err
}
const setSessionRemotePublicKey = `-- name: SetSessionRemotePublicKey :exec
UPDATE sessions
SET remote_public_key = $1
WHERE id = $2
`
type SetSessionRemotePublicKeyParams struct {
RemotePublicKey []byte
ID int64
}
func (q *Queries) SetSessionRemotePublicKey(ctx context.Context, arg SetSessionRemotePublicKeyParams) error {
_, err := q.db.ExecContext(ctx, setSessionRemotePublicKey, arg.RemotePublicKey, arg.ID)
return err
}
const setSessionRevokedAt = `-- name: SetSessionRevokedAt :exec
UPDATE sessions
SET revoked_at = $1
WHERE id = $2
`
type SetSessionRevokedAtParams struct {
RevokedAt sql.NullTime
ID int64
}
func (q *Queries) SetSessionRevokedAt(ctx context.Context, arg SetSessionRevokedAtParams) error {
_, err := q.db.ExecContext(ctx, setSessionRevokedAt, arg.RevokedAt, arg.ID)
return err
}
const updateSessionState = `-- name: UpdateSessionState :exec
UPDATE sessions
SET state = $1
WHERE id = $2
`
type UpdateSessionStateParams struct {
State int16
ID int64
}
func (q *Queries) UpdateSessionState(ctx context.Context, arg UpdateSessionStateParams) error {
_, err := q.db.ExecContext(ctx, updateSessionState, arg.State, arg.ID)
return err
}