diff --git a/session/interface.go b/session/interface.go index 41bd354c..08b3c0cb 100644 --- a/session/interface.go +++ b/session/interface.go @@ -164,6 +164,9 @@ type Store interface { // ListSessions returns all sessions currently known to the store. ListSessions(filterFn func(s *Session) bool) ([]*Session, error) + // ListSessionsByType returns all sessions of the given type. + ListSessionsByType(t Type) ([]*Session, error) + // RevokeSession updates the state of the session with the given local // public key to be revoked. RevokeSession(*btcec.PublicKey) error diff --git a/session/kvdb_store.go b/session/kvdb_store.go index e5862056..2fc714c2 100644 --- a/session/kvdb_store.go +++ b/session/kvdb_store.go @@ -371,6 +371,16 @@ func (db *BoltStore) ListSessions(filterFn func(s *Session) bool) ([]*Session, e return db.listSessions(filterFn) } +// ListSessionsByType returns all sessions currently known to the store that +// have the given type. +// +// NOTE: this is part of the Store interface. +func (db *BoltStore) ListSessionsByType(t Type) ([]*Session, error) { + return db.listSessions(func(s *Session) bool { + return s.Type == t + }) +} + // listSessions returns all sessions currently known to the store that pass the // given filter function. func (db *BoltStore) listSessions(filterFn func(s *Session) bool) ([]*Session, diff --git a/session/store_test.go b/session/store_test.go index 051e3187..6057e9fe 100644 --- a/session/store_test.go +++ b/session/store_test.go @@ -31,7 +31,7 @@ func TestBasicSessionStore(t *testing.T) { clock.SetTime(testTime.Add(time.Second)) s2 := newSession(t, db, clock, "session 2") clock.SetTime(testTime.Add(2 * time.Second)) - s3 := newSession(t, db, clock, "session 3") + s3 := newSession(t, db, clock, "session 3", withType(TypeAutopilot)) clock.SetTime(testTime.Add(3 * time.Second)) s4 := newSession(t, db, clock, "session 4") @@ -64,6 +64,22 @@ func TestBasicSessionStore(t *testing.T) { assertEqualSessions(t, s2, sessions[1]) assertEqualSessions(t, s3, sessions[2]) + // Test the ListSessionsByType method. + sessions, err = db.ListSessionsByType(TypeMacaroonAdmin) + require.NoError(t, err) + require.Equal(t, 2, len(sessions)) + assertEqualSessions(t, s1, sessions[0]) + assertEqualSessions(t, s2, sessions[1]) + + sessions, err = db.ListSessionsByType(TypeAutopilot) + require.NoError(t, err) + require.Equal(t, 1, len(sessions)) + assertEqualSessions(t, s3, sessions[0]) + + sessions, err = db.ListSessionsByType(TypeMacaroonReadonly) + require.NoError(t, err) + require.Empty(t, sessions) + // Ensure that we can retrieve each session by both its local pub key // and by its ID. for _, s := range []*Session{s1, s2, s3} { @@ -310,6 +326,12 @@ func withLinkedGroupID(groupID *ID) testSessionModifier { } } +func withType(t Type) testSessionModifier { + return func(s *Session) { + s.Type = t + } +} + func newSession(t *testing.T, db Store, clock clock.Clock, label string, mods ...testSessionModifier) *Session { diff --git a/session_rpcserver.go b/session_rpcserver.go index 666744cd..d3a8b18a 100644 --- a/session_rpcserver.go +++ b/session_rpcserver.go @@ -1259,9 +1259,7 @@ func (s *sessionRpcServer) ListAutopilotSessions(_ context.Context, _ *litrpc.ListAutopilotSessionsRequest) ( *litrpc.ListAutopilotSessionsResponse, error) { - sessions, err := s.cfg.db.ListSessions(func(s *session.Session) bool { - return s.Type == session.TypeAutopilot - }) + sessions, err := s.cfg.db.ListSessionsByType(session.TypeAutopilot) if err != nil { return nil, fmt.Errorf("error fetching sessions: %v", err) }