Skip to content

Commit 5ed5fd2

Browse files
committed
session: use error variables
In preparation for having the unit tests pass against a different Store implementation, we standardize some of the errors that get returned.
1 parent aa8080d commit 5ed5fd2

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

itest/litd_firewall_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,9 @@ func testSessionLinking(net *NetworkHarness, t *harnessTest) {
866866
LinkedGroupId: sessResp.Session.GroupId,
867867
},
868868
)
869-
require.ErrorContains(t.t, err, "is still active")
869+
require.ErrorContains(
870+
t.t, err, session.ErrSessionsInGroupStillActive.Error(),
871+
)
870872

871873
// Revoke the previous one and repeat.
872874
_, err = litAutopilotClient.RevokeAutopilotSession(

session/errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@ var (
66
// ErrSessionNotFound is an error returned when we attempt to retrieve
77
// information about a session but it is not found.
88
ErrSessionNotFound = errors.New("session not found")
9+
10+
// ErrUnknownGroup is returned when an attempt is made to insert a
11+
// session and link it to an existing group where the group is not
12+
// known.
13+
ErrUnknownGroup = errors.New("unknown group")
14+
15+
// ErrSessionsInGroupStillActive is returned when an attempt is made to
16+
// insert a session and link it to a group that still has other active
17+
// sessions.
18+
ErrSessionsInGroupStillActive = errors.New(
19+
"group has active sessions",
20+
)
921
)

session/kvdb_store.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
229229
if session.ID != session.GroupID {
230230
_, err = getKeyForID(sessionBucket, session.GroupID)
231231
if err != nil {
232-
return fmt.Errorf("unknown linked session "+
233-
"%x: %w", session.GroupID, err)
232+
return fmt.Errorf("%w: unknown linked "+
233+
"session %x: %w", ErrUnknownGroup,
234+
session.GroupID, err)
234235
}
235236

236237
// Fetch all the session IDs for this group. This will
@@ -242,18 +243,22 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
242243
return err
243244
}
244245

246+
// Ensure that the all the linked sessions are no longer
247+
// active.
245248
for _, id := range sessionIDs {
246249
sess, err := getSessionByID(sessionBucket, id)
247250
if err != nil {
248251
return err
249252
}
250253

251-
// Ensure that the session is no longer active.
252-
if !sess.State.Terminal() {
253-
return fmt.Errorf("session (id=%x) "+
254-
"in group %x is still active",
255-
sess.ID, sess.GroupID)
254+
if sess.State.Terminal() {
255+
continue
256256
}
257+
258+
return fmt.Errorf("%w: session (id=%x) in "+
259+
"group %x is still active",
260+
ErrSessionsInGroupStillActive, sess.ID,
261+
sess.GroupID)
257262
}
258263
}
259264

@@ -630,14 +635,14 @@ func (db *BoltStore) GetGroupID(sessionID ID) (ID, error) {
630635

631636
sessionIDBkt := idIndex.Bucket(sessionID[:])
632637
if sessionIDBkt == nil {
633-
return fmt.Errorf("no index entry for session ID: %x",
634-
sessionID)
638+
return fmt.Errorf("%w: no index entry for session "+
639+
"ID: %x", ErrUnknownGroup, sessionID)
635640
}
636641

637642
groupIDBytes := sessionIDBkt.Get(groupIDKey)
638643
if len(groupIDBytes) == 0 {
639-
return fmt.Errorf("group ID not found for session "+
640-
"ID %x", sessionID)
644+
return fmt.Errorf("%w: group ID not found for "+
645+
"session ID %x", ErrUnknownGroup, sessionID)
641646
}
642647

643648
copy(groupID[:], groupIDBytes)
@@ -806,7 +811,7 @@ func addIDToGroupIDPair(sessionBkt *bbolt.Bucket, id, groupID ID) error {
806811
func getSessionByID(bucket *bbolt.Bucket, id ID) (*Session, error) {
807812
keyBytes, err := getKeyForID(bucket, id)
808813
if err != nil {
809-
return nil, err
814+
return nil, fmt.Errorf("%w: %w", ErrSessionNotFound, err)
810815
}
811816

812817
v := bucket.Get(keyBytes)

session/store_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func TestBasicSessionStore(t *testing.T) {
2222
_ = db.Close()
2323
})
2424

25+
// Try fetch a session that doesn't exist yet.
26+
_, err = db.GetSessionByID(ID{1, 3, 4, 4})
27+
require.ErrorIs(t, err, ErrSessionNotFound)
28+
2529
// Reserve a session. This should succeed.
2630
s1, err := reserveSession(db, "session 1")
2731
require.NoError(t, err)
@@ -183,7 +187,7 @@ func TestBasicSessionStore(t *testing.T) {
183187
require.Empty(t, sessions)
184188

185189
_, err = db.GetGroupID(s4.ID)
186-
require.ErrorContains(t, err, "no index entry")
190+
require.ErrorIs(t, err, ErrUnknownGroup)
187191

188192
// Only session 1 should remain in this group.
189193
sessIDs, err = db.GetSessionIDs(s4.GroupID)
@@ -211,7 +215,7 @@ func TestLinkingSessions(t *testing.T) {
211215
_, err = reserveSession(
212216
db, "session 2", withLinkedGroupID(&groupID),
213217
)
214-
require.ErrorContains(t, err, "unknown linked session")
218+
require.ErrorIs(t, err, ErrUnknownGroup)
215219

216220
// Create a new session with no previous link.
217221
s1 := createSession(t, db, "session 1")
@@ -220,7 +224,7 @@ func TestLinkingSessions(t *testing.T) {
220224
// session. This should fail due to the first session still being
221225
// active.
222226
_, err = reserveSession(db, "session 2", withLinkedGroupID(&s1.GroupID))
223-
require.ErrorContains(t, err, "is still active")
227+
require.ErrorIs(t, err, ErrSessionsInGroupStillActive)
224228

225229
// Revoke the first session.
226230
require.NoError(t, db.ShiftState(s1.ID, StateRevoked))

0 commit comments

Comments
 (0)