Skip to content

Commit 73528e3

Browse files
committed
refactor(api): replace specific session methods with store standard
1 parent 6280e2e commit 73528e3

File tree

8 files changed

+537
-416
lines changed

8 files changed

+537
-416
lines changed

api/services/auth.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,35 @@ func (s *service) AuthDevice(ctx context.Context, req requests.DeviceAuth) (*mod
187187
}
188188

189189
for _, sessionUID := range req.Sessions {
190-
if err := s.store.SessionSetLastSeen(ctx, models.UID(sessionUID)); err != nil {
190+
session, err := s.store.SessionResolve(ctx, store.SessionUIDResolver, sessionUID)
191+
if err != nil {
192+
log.WithError(err).WithField("session_uid", sessionUID).Warn("cannot resolve session")
193+
194+
continue
195+
}
196+
197+
if session.Closed {
198+
continue
199+
}
200+
201+
session.LastSeen = clock.Now()
202+
if err := s.store.SessionUpdate(ctx, session); err != nil {
191203
log.WithError(err).WithField("session_uid", sessionUID).Warn("cannot set session's last seen")
192204

193205
continue
194206
}
207+
208+
activeSession, err := s.store.ActiveSessionResolve(ctx, store.SessionUIDResolver, sessionUID)
209+
if err != nil {
210+
log.WithError(err).WithField("session_uid", sessionUID).Warn("cannot resolve active session")
211+
212+
continue
213+
}
214+
215+
activeSession.LastSeen = session.LastSeen
216+
if err := s.store.ActiveSessionUpdate(ctx, activeSession); err != nil {
217+
log.WithError(err).WithField("session_uid", sessionUID).Warn("cannot update active session's last seen")
218+
}
195219
}
196220

197221
cachedData["device_name"] = device.Name

api/services/auth_test.go

Lines changed: 137 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,47 +302,122 @@ func TestAuthDevice(t *testing.T) {
302302
},
303303
},
304304
{
305-
description: "[device exists] succeeds to authenticate device with sessions",
305+
description: "[device exists] succeeds to authenticate device with closed sessions",
306306
req: requests.DeviceAuth{
307307
TenantID: "00000000-0000-4000-0000-000000000000",
308308
Hostname: "hostname",
309309
Identity: &requests.DeviceIdentity{MAC: ""},
310-
Info: nil,
311310
PublicKey: "",
312311
Sessions: []string{"session_1", "session_2"},
313312
RealIP: "127.0.0.1",
314313
},
315314
requiredMocks: func(ctx context.Context) {
316315
uid := toUID("00000000-0000-4000-0000-000000000000", "hostname", "", "")
317316
device := &models.Device{UID: uid, Name: "hostname"}
317+
expectedDevice := *device
318+
expectedDevice.LastSeen = clock.Now()
318319

320+
cacheMock.
321+
On("Get", ctx, "auth_device/"+uid, testifymock.AnythingOfType("*map[string]string")).
322+
Return(store.ErrNoDocuments).
323+
Once()
319324
storeMock.
320325
On("NamespaceResolve", ctx, store.NamespaceTenantIDResolver, "00000000-0000-4000-0000-000000000000").
321326
Return(&models.Namespace{TenantID: "00000000-0000-4000-0000-000000000000", Name: "test"}, nil).
322327
Once()
328+
storeMock.
329+
On("DeviceResolve", ctx, store.DeviceUIDResolver, uid).
330+
Return(device, nil).
331+
Once()
332+
storeMock.
333+
On("DeviceUpdate", ctx, &expectedDevice).
334+
Return(nil).
335+
Once()
336+
storeMock.
337+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_1").
338+
Return(&models.Session{UID: "session_1", Closed: true}, nil).
339+
Once()
340+
storeMock.
341+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_2").
342+
Return(&models.Session{UID: "session_2", Closed: true}, nil).
343+
Once()
323344
cacheMock.
324-
On("Get", ctx, "auth_device/"+uid, testifymock.Anything).
345+
On("Set", ctx, "auth_device/"+uid, map[string]string{"device_name": "hostname", "namespace_name": "test"}, time.Second*30).
325346
Return(nil).
326347
Once()
348+
},
349+
expected: Expected{
350+
res: &models.DeviceAuthResponse{
351+
UID: toUID("00000000-0000-4000-0000-000000000000", "hostname", "", ""),
352+
Token: toToken("00000000-0000-4000-0000-000000000000", toUID("00000000-0000-4000-0000-000000000000", "hostname", "", "")),
353+
Name: "hostname",
354+
Namespace: "test",
355+
},
356+
err: nil,
357+
},
358+
},
359+
{
360+
description: "[device exists] succeeds to authenticate device with open sessions",
361+
req: requests.DeviceAuth{
362+
TenantID: "00000000-0000-4000-0000-000000000000",
363+
Hostname: "hostname",
364+
Identity: &requests.DeviceIdentity{MAC: ""},
365+
PublicKey: "",
366+
Sessions: []string{"session_1", "session_2"},
367+
RealIP: "127.0.0.1",
368+
},
369+
requiredMocks: func(ctx context.Context) {
370+
uid := toUID("00000000-0000-4000-0000-000000000000", "hostname", "", "")
371+
device := &models.Device{UID: uid, Name: "hostname"}
372+
expectedDevice := *device
373+
expectedDevice.LastSeen = clock.Now()
374+
375+
cacheMock.
376+
On("Get", ctx, "auth_device/"+uid, testifymock.AnythingOfType("*map[string]string")).
377+
Return(store.ErrNoDocuments).
378+
Once()
379+
storeMock.
380+
On("NamespaceResolve", ctx, store.NamespaceTenantIDResolver, "00000000-0000-4000-0000-000000000000").
381+
Return(&models.Namespace{TenantID: "00000000-0000-4000-0000-000000000000", Name: "test"}, nil).
382+
Once()
327383
storeMock.
328384
On("DeviceResolve", ctx, store.DeviceUIDResolver, uid).
329385
Return(device, nil).
330386
Once()
331-
332-
expectedDevice := *device
333-
expectedDevice.LastSeen = now
334-
expectedDevice.DisconnectedAt = nil
335-
336387
storeMock.
337388
On("DeviceUpdate", ctx, &expectedDevice).
338389
Return(nil).
339390
Once()
340391
storeMock.
341-
On("SessionSetLastSeen", ctx, models.UID("session_1")).
392+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_1").
393+
Return(&models.Session{UID: "session_1", Closed: false}, nil).
394+
Once()
395+
storeMock.
396+
On("SessionUpdate", ctx, testifymock.MatchedBy(func(s *models.Session) bool { return s.UID == "session_1" })).
397+
Return(nil).
398+
Once()
399+
storeMock.
400+
On("ActiveSessionResolve", ctx, store.SessionUIDResolver, "session_1").
401+
Return(&models.ActiveSession{UID: "session_1"}, nil).
402+
Once()
403+
storeMock.
404+
On("ActiveSessionUpdate", ctx, testifymock.MatchedBy(func(as *models.ActiveSession) bool { return as.UID == "session_1" })).
405+
Return(nil).
406+
Once()
407+
storeMock.
408+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_2").
409+
Return(&models.Session{UID: "session_2", Closed: false}, nil).
410+
Once()
411+
storeMock.
412+
On("SessionUpdate", ctx, testifymock.MatchedBy(func(s *models.Session) bool { return s.UID == "session_2" })).
342413
Return(nil).
343414
Once()
344415
storeMock.
345-
On("SessionSetLastSeen", ctx, models.UID("session_2")).
416+
On("ActiveSessionResolve", ctx, store.SessionUIDResolver, "session_2").
417+
Return(&models.ActiveSession{UID: "session_2"}, nil).
418+
Once()
419+
storeMock.
420+
On("ActiveSessionUpdate", ctx, testifymock.MatchedBy(func(as *models.ActiveSession) bool { return as.UID == "session_2" })).
346421
Return(nil).
347422
Once()
348423
cacheMock.
@@ -685,11 +760,35 @@ func TestAuthDevice(t *testing.T) {
685760
Return(nil).
686761
Once()
687762
storeMock.
688-
On("SessionSetLastSeen", ctx, models.UID("session_1")).
763+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_1").
764+
Return(&models.Session{UID: "session_1", Closed: false}, nil).
765+
Once()
766+
storeMock.
767+
On("ActiveSessionResolve", ctx, store.SessionUIDResolver, "session_1").
768+
Return(&models.ActiveSession{UID: "session_1"}, nil).
769+
Once()
770+
storeMock.
771+
On("ActiveSessionUpdate", ctx, testifymock.MatchedBy(func(as *models.ActiveSession) bool { return as.UID == "session_1" })).
772+
Return(nil).
773+
Once()
774+
storeMock.
775+
On("SessionUpdate", ctx, testifymock.MatchedBy(func(s *models.Session) bool { return s.UID == "session_1" })).
776+
Return(nil).
777+
Once()
778+
storeMock.
779+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_2").
780+
Return(&models.Session{UID: "session_2", Closed: false}, nil).
781+
Once()
782+
storeMock.
783+
On("ActiveSessionResolve", ctx, store.SessionUIDResolver, "session_2").
784+
Return(&models.ActiveSession{UID: "session_2"}, nil).
785+
Once()
786+
storeMock.
787+
On("ActiveSessionUpdate", ctx, testifymock.MatchedBy(func(as *models.ActiveSession) bool { return as.UID == "session_2" })).
689788
Return(nil).
690789
Once()
691790
storeMock.
692-
On("SessionSetLastSeen", ctx, models.UID("session_2")).
791+
On("SessionUpdate", ctx, testifymock.MatchedBy(func(s *models.Session) bool { return s.UID == "session_2" })).
693792
Return(nil).
694793
Once()
695794
cacheMock.
@@ -921,11 +1020,35 @@ func TestAuthDevice(t *testing.T) {
9211020
Return(nil).
9221021
Once()
9231022
storeMock.
924-
On("SessionSetLastSeen", ctx, models.UID("session_1")).
1023+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_1").
1024+
Return(&models.Session{UID: "session_1", Closed: false}, nil).
1025+
Once()
1026+
storeMock.
1027+
On("ActiveSessionResolve", ctx, store.SessionUIDResolver, "session_1").
1028+
Return(&models.ActiveSession{UID: "session_1"}, nil).
1029+
Once()
1030+
storeMock.
1031+
On("ActiveSessionUpdate", ctx, testifymock.MatchedBy(func(as *models.ActiveSession) bool { return as.UID == "session_1" })).
1032+
Return(nil).
1033+
Once()
1034+
storeMock.
1035+
On("SessionUpdate", ctx, testifymock.MatchedBy(func(s *models.Session) bool { return s.UID == "session_1" })).
1036+
Return(nil).
1037+
Once()
1038+
storeMock.
1039+
On("SessionResolve", ctx, store.SessionUIDResolver, "session_2").
1040+
Return(&models.Session{UID: "session_2", Closed: false}, nil).
1041+
Once()
1042+
storeMock.
1043+
On("ActiveSessionResolve", ctx, store.SessionUIDResolver, "session_2").
1044+
Return(&models.ActiveSession{UID: "session_2"}, nil).
1045+
Once()
1046+
storeMock.
1047+
On("ActiveSessionUpdate", ctx, testifymock.MatchedBy(func(as *models.ActiveSession) bool { return as.UID == "session_2" })).
9251048
Return(nil).
9261049
Once()
9271050
storeMock.
928-
On("SessionSetLastSeen", ctx, models.UID("session_2")).
1051+
On("SessionUpdate", ctx, testifymock.MatchedBy(func(s *models.Session) bool { return s.UID == "session_2" })).
9291052
Return(nil).
9301053
Once()
9311054
cacheMock.

api/services/session.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/shellhub-io/shellhub/api/store"
88
"github.com/shellhub-io/shellhub/pkg/api/query"
99
"github.com/shellhub-io/shellhub/pkg/api/requests"
10+
"github.com/shellhub-io/shellhub/pkg/clock"
1011
"github.com/shellhub-io/shellhub/pkg/models"
1112
)
1213

@@ -33,7 +34,7 @@ func (s *service) ListSessions(ctx context.Context, req *requests.ListSessions)
3334
}
3435

3536
func (s *service) GetSession(ctx context.Context, uid models.UID) (*models.Session, error) {
36-
session, err := s.store.SessionGet(ctx, uid)
37+
session, err := s.store.SessionResolve(ctx, store.SessionUIDResolver, string(uid))
3738
if err != nil {
3839
return nil, NewErrSessionNotFound(uid, err)
3940
}
@@ -44,7 +45,7 @@ func (s *service) GetSession(ctx context.Context, uid models.UID) (*models.Sessi
4445
func (s *service) CreateSession(ctx context.Context, session requests.SessionCreate) (*models.Session, error) {
4546
position, _ := s.locator.GetPosition(net.ParseIP(session.IPAddress))
4647

47-
return s.store.SessionCreate(ctx, models.Session{
48+
uid, err := s.store.SessionCreate(ctx, models.Session{
4849
UID: session.UID,
4950
DeviceUID: models.UID(session.DeviceUID),
5051
Username: session.Username,
@@ -56,35 +57,57 @@ func (s *service) CreateSession(ctx context.Context, session requests.SessionCre
5657
Latitude: position.Latitude,
5758
},
5859
})
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
return s.store.SessionResolve(ctx, store.SessionUIDResolver, uid)
5965
}
6066

6167
func (s *service) DeactivateSession(ctx context.Context, uid models.UID) error {
62-
sess, err := s.store.SessionGet(ctx, uid)
68+
sess, err := s.store.SessionResolve(ctx, store.SessionUIDResolver, string(uid))
6369
if err != nil {
6470
return NewErrSessionNotFound(uid, err)
6571
}
6672

67-
return s.store.SessionDeleteActives(ctx, models.UID(sess.UID))
73+
return s.store.ActiveSessionDelete(ctx, models.UID(sess.UID))
6874
}
6975

7076
func (s *service) KeepAliveSession(ctx context.Context, uid models.UID) error {
71-
return s.store.SessionSetLastSeen(ctx, uid)
77+
session, err := s.store.SessionResolve(ctx, store.SessionUIDResolver, string(uid))
78+
if err != nil {
79+
return NewErrSessionNotFound(uid, err)
80+
}
81+
82+
session.LastSeen = clock.Now()
83+
84+
return s.store.SessionUpdate(ctx, session)
7285
}
7386

7487
func (s *service) UpdateSession(ctx context.Context, uid models.UID, model models.SessionUpdate) error {
75-
sess, err := s.store.SessionGet(ctx, uid)
88+
sess, err := s.store.SessionResolve(ctx, store.SessionUIDResolver, string(uid))
7689
if err != nil {
7790
return NewErrSessionNotFound(uid, err)
7891
}
7992

80-
return s.store.SessionUpdate(ctx, uid, sess, &model)
93+
if model.Authenticated != nil {
94+
sess.Authenticated = *model.Authenticated
95+
}
96+
if model.Type != nil {
97+
sess.Type = *model.Type
98+
}
99+
if model.Recorded != nil {
100+
sess.Recorded = *model.Recorded
101+
}
102+
103+
return s.store.SessionUpdate(ctx, sess)
81104
}
82105

83106
func (s *service) EventSession(ctx context.Context, uid models.UID, event *models.SessionEvent) error {
84-
sess, err := s.store.SessionGet(ctx, uid)
107+
sess, err := s.store.SessionResolve(ctx, store.SessionUIDResolver, string(uid))
85108
if err != nil {
86109
return NewErrSessionNotFound(uid, err)
87110
}
88111

89-
return s.store.SessionEvent(ctx, models.UID(sess.UID), event)
112+
return s.store.SessionEventsCreate(ctx, models.UID(sess.UID), event)
90113
}

0 commit comments

Comments
 (0)