Skip to content

Commit 11cb808

Browse files
committed
firewalldb: handle deleted sessions in priv pair mig
If the user has deleted their session.db file, but kept their rules.db file, there can exist privacy mapper pairs that point to a now deleted session ID. Such pairs should be ignored during the migration, as they are cannot be used anymore. This commit updates the migration to handle this case.
1 parent 4591818 commit 11cb808

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

firewalldb/sql_migration.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,19 @@ func collectPrivacyPairs(kvStore *bbolt.DB,
617617

618618
sess, ok := sessMap[groupAlias]
619619
if !ok {
620-
return fmt.Errorf("session with group id %x "+
621-
"not found in sql db", groupId)
620+
// If we can't find the session group in the SQL
621+
// db, that indicates that the session was never
622+
// migrated from KVDB. This likely means that
623+
// the user deleted their session.db file, but
624+
// kept the rules.db file. As the privacy pairs
625+
// are useless when the session no longer
626+
// exists, we can just skip the migration of the
627+
// privacy pairs for this group.
628+
log.Warnf("Skipping migration of privacy "+
629+
"pairs for session group %x, as the "+
630+
"session group was not found", groupId)
631+
632+
return nil
622633
}
623634

624635
if !sess.GroupID.Valid {

firewalldb/sql_migration_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,14 @@ func TestFirewallDBMigration(t *testing.T) {
458458
name: "multiple sessions and privacy pairs",
459459
populateDB: multipleSessionsAndPrivacyPairs,
460460
},
461+
{
462+
name: "deleted session with privacy pair",
463+
populateDB: deletedSessionWithPrivPair,
464+
},
465+
{
466+
name: "deleted and existing sessions with privacy pairs",
467+
populateDB: deletedAndExistingSessionsWithPrivPairs,
468+
},
461469
{
462470
name: "random privacy pairs",
463471
populateDB: randomPrivacyPairs,
@@ -1059,6 +1067,53 @@ func oneSessionAndPrivPair(t *testing.T, ctx context.Context,
10591067
return createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
10601068
}
10611069

1070+
// deletedSessionWithPrivPair inserts 1 session with a linked 1 privacy pair
1071+
// into the boltDB, and then deletes the session from the sessions store, to
1072+
// simulate the case where a session has been deleted, but the privacy pairs
1073+
// still exist. This can happen if the user deletes their session db but not
1074+
// their firewall db.
1075+
func deletedSessionWithPrivPair(t *testing.T, ctx context.Context,
1076+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
1077+
_ *rootKeyMockStore) *expectedResult {
1078+
1079+
_ = createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
1080+
1081+
// Now we delete the session that the privacy pair was linked to.
1082+
err := sessionStore.DeleteReservedSessions(ctx)
1083+
require.NoError(t, err)
1084+
1085+
return &expectedResult{
1086+
kvEntries: []*kvEntry{},
1087+
// Since the session the privacy pair was linked to has been
1088+
// deleted, we expect no privacy pairs to be migrated.
1089+
privPairs: make(privacyPairs),
1090+
actions: []*Action{},
1091+
}
1092+
}
1093+
1094+
// deletedAndExistingSessionsWithPrivPairs generates 2 different privacy pairs,
1095+
// each linked to a different sessions. However, one of the sessions is deleted
1096+
// prior to the migration, to test that only one of the privacy pairs should be
1097+
// migrated, while the other one should be ignored since its session has been
1098+
// deleted.
1099+
func deletedAndExistingSessionsWithPrivPairs(t *testing.T, ctx context.Context,
1100+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
1101+
_ *rootKeyMockStore) *expectedResult {
1102+
1103+
// First generate one privacy pair linked to a session that will be
1104+
// deleted.
1105+
_ = createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
1106+
1107+
// Delete the linked session.
1108+
err := sessionStore.DeleteReservedSessions(ctx)
1109+
require.NoError(t, err)
1110+
1111+
// Now generate another privacy pair linked to a session that won't be
1112+
// deleted prior to the migration. Therefore, this privacy pair should
1113+
// be migrated.
1114+
return createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
1115+
}
1116+
10621117
// oneSessionsMultiplePrivPairs inserts 1 session with 10 privacy pairs into the
10631118
// boltDB.
10641119
func oneSessionsMultiplePrivPairs(t *testing.T, ctx context.Context,

0 commit comments

Comments
 (0)