Skip to content

Commit 6b8c58f

Browse files
committed
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 e8120a2 commit 6b8c58f

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

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)