Skip to content

Commit a9c9366

Browse files
Merge pull request #1129 from ViktorTigerstrom/2025-08-session-migration-ceavat-order-fix
[sql-48] session: sort MacaroonRecipe.caveats in migration
2 parents f71840e + 1af12f4 commit a9c9366

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

session/sql_migration.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"reflect"
10+
"sort"
1011
"time"
1112

1213
"github.com/davecgh/go-spew/spew"
@@ -380,17 +381,69 @@ func overrideSessionTimeZone(session *Session) {
380381
// as nil in the bbolt store. Therefore, we also override the permissions
381382
// or caveats to nil for the migrated session in that scenario, so that the
382383
// deep equals check does not fail in this scenario either.
384+
//
385+
// Additionally, we sort the caveats & permissions of both the kv and sql
386+
// sessions by their ID, so that they are always comparable in a deterministic
387+
// way with deep equals.
383388
func overrideMacaroonRecipe(kvSession *Session, migratedSession *Session) {
384389
if kvSession.MacaroonRecipe != nil {
385390
kvPerms := kvSession.MacaroonRecipe.Permissions
386391
kvCaveats := kvSession.MacaroonRecipe.Caveats
387392

393+
// If the kvSession has a MacaroonRecipe with nil set for any
394+
// of the fields, we need to override the migratedSession
395+
// MacaroonRecipe to match that.
388396
if kvPerms == nil && kvCaveats == nil {
389397
migratedSession.MacaroonRecipe = &MacaroonRecipe{}
390398
} else if kvPerms == nil {
391399
migratedSession.MacaroonRecipe.Permissions = nil
392400
} else if kvCaveats == nil {
393401
migratedSession.MacaroonRecipe.Caveats = nil
394402
}
403+
404+
sqlCaveats := migratedSession.MacaroonRecipe.Caveats
405+
sqlPerms := migratedSession.MacaroonRecipe.Permissions
406+
407+
// If there have been caveats set for the MacaroonRecipe,
408+
// the order of the postgres db caveats will in very rare cases
409+
// differ from the kv store caveats. Therefore, we sort
410+
// both the kv and sql caveats by their ID, so that we can
411+
// compare them in a deterministic way.
412+
if kvCaveats != nil {
413+
sort.Slice(kvCaveats, func(i, j int) bool {
414+
return bytes.Compare(
415+
kvCaveats[i].Id, kvCaveats[j].Id,
416+
) < 0
417+
})
418+
419+
sort.Slice(sqlCaveats, func(i, j int) bool {
420+
return bytes.Compare(
421+
sqlCaveats[i].Id, sqlCaveats[j].Id,
422+
) < 0
423+
})
424+
}
425+
426+
// Similarly, we sort the macaroon permissions for both the kv
427+
// and sql sessions, so that we can compare them in a
428+
// deterministic way.
429+
if kvPerms != nil {
430+
sort.Slice(kvPerms, func(i, j int) bool {
431+
if kvPerms[i].Entity == kvPerms[j].Entity {
432+
return kvPerms[i].Action <
433+
kvPerms[j].Action
434+
}
435+
436+
return kvPerms[i].Entity < kvPerms[j].Entity
437+
})
438+
439+
sort.Slice(sqlPerms, func(i, j int) bool {
440+
if sqlPerms[i].Entity == sqlPerms[j].Entity {
441+
return sqlPerms[i].Action <
442+
sqlPerms[j].Action
443+
}
444+
445+
return sqlPerms[i].Entity < sqlPerms[j].Entity
446+
})
447+
}
395448
}
396449
}

0 commit comments

Comments
 (0)