Skip to content

Commit 60be78b

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 eb80be8 commit 60be78b

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(ctx context.Context, 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
@@ -450,6 +450,14 @@ func TestFirewallDBMigration(t *testing.T) {
450450
name: "multiple sessions and privacy pairs",
451451
populateDB: multipleSessionsAndPrivacyPairs,
452452
},
453+
{
454+
name: "deleted session with privacy pair",
455+
populateDB: deletedSessionWithPrivPair,
456+
},
457+
{
458+
name: "deleted and existing sessions with privacy pairs",
459+
populateDB: deletedAndExistingSessionsWithPrivPairs,
460+
},
453461
{
454462
name: "random privacy pairs",
455463
populateDB: randomPrivacyPairs,
@@ -997,6 +1005,53 @@ func oneSessionAndPrivPair(t *testing.T, ctx context.Context,
9971005
return createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
9981006
}
9991007

1008+
// deletedSessionWithPrivPair inserts 1 session with a linked 1 privacy pair
1009+
// into the boltDB, and then deletes the session from the sessions store, to
1010+
// simulate the case where a session has been deleted, but the privacy pairs
1011+
// still exist. This can happen if the user deletes their session db but not
1012+
// their firewall db.
1013+
func deletedSessionWithPrivPair(t *testing.T, ctx context.Context,
1014+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
1015+
_ *rootKeyMockStore) *expectedResult {
1016+
1017+
_ = createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
1018+
1019+
// Now we delete the session that the privacy pair was linked to.
1020+
err := sessionStore.DeleteReservedSessions(ctx)
1021+
require.NoError(t, err)
1022+
1023+
return &expectedResult{
1024+
kvEntries: []*kvEntry{},
1025+
// Since the session the privacy pair was linked to has been
1026+
// deleted, we expect no privacy pairs to be migrated.
1027+
privPairs: make(privacyPairs),
1028+
actions: []*Action{},
1029+
}
1030+
}
1031+
1032+
// deletedAndExistingSessionsWithPrivPairs generates 2 different privacy pairs,
1033+
// each linked to a different sessions. However, one of the sessions is deleted
1034+
// prior to the migration, to test that only one of the privacy pairs should be
1035+
// migrated, while the other one should be ignored since its session has been
1036+
// deleted.
1037+
func deletedAndExistingSessionsWithPrivPairs(t *testing.T, ctx context.Context,
1038+
boltDB *BoltDB, sessionStore session.Store, _ accounts.Store,
1039+
_ *rootKeyMockStore) *expectedResult {
1040+
1041+
// First generate one privacy pair linked to a session that will be
1042+
// deleted.
1043+
_ = createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
1044+
1045+
// Delete the linked session.
1046+
err := sessionStore.DeleteReservedSessions(ctx)
1047+
require.NoError(t, err)
1048+
1049+
// Now generate another privacy pair linked to a session that won't be
1050+
// deleted prior to the migration. Therefore, this privacy pair should
1051+
// be migrated.
1052+
return createPrivacyPairs(t, ctx, boltDB, sessionStore, 1, 1)
1053+
}
1054+
10001055
// oneSessionsMultiplePrivPairs inserts 1 session with 10 privacy pairs into the
10011056
// boltDB.
10021057
func oneSessionsMultiplePrivPairs(t *testing.T, ctx context.Context,

0 commit comments

Comments
 (0)