|
7 | 7 | "errors"
|
8 | 8 | "fmt"
|
9 | 9 | "reflect"
|
| 10 | + "sort" |
10 | 11 | "time"
|
11 | 12 |
|
12 | 13 | "github.com/davecgh/go-spew/spew"
|
@@ -380,17 +381,44 @@ func overrideSessionTimeZone(session *Session) {
|
380 | 381 | // as nil in the bbolt store. Therefore, we also override the permissions
|
381 | 382 | // or caveats to nil for the migrated session in that scenario, so that the
|
382 | 383 | // deep equals check does not fail in this scenario either.
|
| 384 | +// |
| 385 | +// Additionally, we sort the caveats of both the kv and sql sessions by |
| 386 | +// their ID, so that they are always comparable in a deterministic way with deep |
| 387 | +// equals. |
383 | 388 | func overrideMacaroonRecipe(kvSession *Session, migratedSession *Session) {
|
384 | 389 | if kvSession.MacaroonRecipe != nil {
|
385 | 390 | kvPerms := kvSession.MacaroonRecipe.Permissions
|
386 | 391 | kvCaveats := kvSession.MacaroonRecipe.Caveats
|
| 392 | + sqlCaveats := migratedSession.MacaroonRecipe.Caveats |
387 | 393 |
|
| 394 | + // If the kvSession has a MacaroonRecipe with nil set for any |
| 395 | + // of the fields, we need to override the migratedSession |
| 396 | + // MacaroonRecipe to match that. |
388 | 397 | if kvPerms == nil && kvCaveats == nil {
|
389 | 398 | migratedSession.MacaroonRecipe = &MacaroonRecipe{}
|
390 | 399 | } else if kvPerms == nil {
|
391 | 400 | migratedSession.MacaroonRecipe.Permissions = nil
|
392 | 401 | } else if kvCaveats == nil {
|
393 | 402 | migratedSession.MacaroonRecipe.Caveats = nil
|
394 | 403 | }
|
| 404 | + |
| 405 | + // If there have been caveats set for the MacaroonRecipe, |
| 406 | + // the order of the postgres db caveats will in very rare cases |
| 407 | + // differ from the kv store caveats. Therefore, we sort |
| 408 | + // both the kv and sql caveats by their ID, so that we can |
| 409 | + // compare them in a deterministic way. |
| 410 | + if kvCaveats != nil { |
| 411 | + sort.Slice(kvCaveats, func(i, j int) bool { |
| 412 | + return bytes.Compare( |
| 413 | + kvCaveats[i].Id, kvCaveats[j].Id, |
| 414 | + ) < 0 |
| 415 | + }) |
| 416 | + |
| 417 | + sort.Slice(sqlCaveats, func(i, j int) bool { |
| 418 | + return bytes.Compare( |
| 419 | + sqlCaveats[i].Id, sqlCaveats[j].Id, |
| 420 | + ) < 0 |
| 421 | + }) |
| 422 | + } |
395 | 423 | }
|
396 | 424 | }
|
0 commit comments