Skip to content

Commit ef90ce7

Browse files
session: update un-set MacaroonRecipe field in kvdb
The KVDB implementation could previously create sessions with a non‐nil `MacaroonRecipe` whose `Permissions` and `Caveats` fields were both nil. However, the SQL session store cannot represent a `MacaroonRecipe` if both `Permissions` and `Caveats` are missing—because in SQL they are stored in separate tables, and without any entries in those tables we can't represent a `MacaroonRecipe` record at all. This commit therefore changes the implementation for the KVDB session store, so that such sessions will have a nil value set for the `MacaroonRecipe` field, if no `Permissions` and `Caveats` are set. Additionally, when a session has a `MacaroonRecipe` set but one of the `Permissions` or `Caveats` fields is unset, the KVDB session store would represent that field as `nil`, whereas the SQL store would represent it as an empty array. Therefore, we update the KVDB session store implementation so that in this scenario, those fields are also set to an empty array instead of `nil`, matching the SQL store’s behavior. This change is important because the KVDB→SQL migration code expects sessions in both stores to be equivalent. Without it, comparing sessions would fail, since the `MacaroonRecipe` field would be represented differently in each store.
1 parent 0c8300c commit ef90ce7

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

session/interface.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
153153
groupID = *opts.linkedGroupID
154154
}
155155

156+
if opts.macaroonRecipe != nil {
157+
perms := opts.macaroonRecipe.Permissions
158+
caveats := opts.macaroonRecipe.Caveats
159+
160+
if len(perms) == 0 && len(caveats) == 0 {
161+
opts.macaroonRecipe = nil
162+
}
163+
}
164+
156165
sess := &Session{
157166
ID: id,
158167
Label: label,

session/tlv.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,23 @@ func DeserializeSession(r io.Reader) (*Session, error) {
283283
// any) is linked implicitly via the macaroon recipe caveat. So we
284284
// need to extract it from there.
285285
if session.MacaroonRecipe != nil {
286-
session.AccountID, err = accounts.IDFromCaveats(
287-
session.MacaroonRecipe.Caveats,
288-
)
289-
if err != nil {
290-
return nil, err
286+
caveats := session.MacaroonRecipe.Caveats
287+
perms := session.MacaroonRecipe.Permissions
288+
289+
// If there are no caveats or permissions, we set the
290+
// MacaroonRecipe to nil. This ensures that different store
291+
// implementations exhibit consistent behavior in this scenario.
292+
if len(caveats) == 0 && len(perms) == 0 {
293+
session.MacaroonRecipe = nil
294+
} else {
295+
// If there are caveats, we attempt to extract the
296+
// AccountID if one exists.
297+
session.AccountID, err = accounts.IDFromCaveats(
298+
session.MacaroonRecipe.Caveats,
299+
)
300+
if err != nil {
301+
return nil, err
302+
}
291303
}
292304
}
293305

@@ -472,6 +484,16 @@ func macaroonRecipeDecoder(r io.Reader, val interface{}, buf *[8]byte,
472484
return err
473485
}
474486

487+
// If either the permissions or caveats are nil, initialize them
488+
// to empty slices. This ensures that different store
489+
// implementations exhibit consistent behavior in this scenario.
490+
if perms == nil {
491+
perms = make([]bakery.Op, 0)
492+
}
493+
if caveats == nil {
494+
caveats = make([]macaroon.Caveat, 0)
495+
}
496+
475497
*v = MacaroonRecipe{
476498
Permissions: perms,
477499
Caveats: caveats,

0 commit comments

Comments
 (0)