Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete stale session trackers #11809

Merged
merged 25 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions lib/services/local/sessiontracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -116,16 +118,43 @@ func (s *sessionTracker) GetActiveSessionTrackers(ctx context.Context) ([]types.
return nil, trace.Wrap(err)
}

sessions := make([]types.SessionTracker, len(result.Items))
for i, item := range result.Items {
sessions := make([]types.SessionTracker, 0, len(result.Items))

// We don't overallocate expired since cleaning up sessions here should be rare.
noExpiry := make([]backend.Item, 0)
xacrimon marked this conversation as resolved.
Show resolved Hide resolved
now := time.Now().UTC()
for _, item := range result.Items {
session, err := unmarshalSession(item.Value)
if err != nil {
return nil, trace.Wrap(err)
}

sessions[i] = session
// NOTE: This is the session expiry timestamp, not the backend timestamp stored in `item.Expires`.
after := session.GetExpires().After(now)
xacrimon marked this conversation as resolved.
Show resolved Hide resolved

switch {
case after:
// Keep any items that aren't expired.
sessions = append(sessions, session)
case !after && item.Expires.IsZero():
// Clear item if expiry is not set on the backend.
// We currently don't set the expiry here but we will when #11551 is merged.
noExpiry = append(noExpiry, item)
default:
// If we take this branch, the expiry is set and the backend is responsible for cleaning up the item.
xacrimon marked this conversation as resolved.
Show resolved Hide resolved
}
}

go func() {
xacrimon marked this conversation as resolved.
Show resolved Hide resolved
for _, item := range noExpiry {
if err := s.bk.Delete(ctx, item.Key); err != nil {
if !trace.IsNotFound(err) {
logrus.WithError(err).Error("Failed to remove stale session tracker")
xacrimon marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}()

return sessions, nil
}

Expand Down
51 changes: 51 additions & 0 deletions lib/services/local/sessiontracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package local
import (
"context"
"testing"
"time"

"github.com/google/uuid"
"github.com/gravitational/teleport/api/client/proto"
Expand Down Expand Up @@ -47,6 +48,7 @@ func TestSessionTrackerStorage(t *testing.T) {
User: "eve",
Mode: string(types.SessionPeerMode),
},
Expires: time.Now().UTC().Add(24 * time.Hour),
})
require.NoError(t, err)

Expand Down Expand Up @@ -87,3 +89,52 @@ func TestSessionTrackerStorage(t *testing.T) {
require.Error(t, err)
require.Nil(t, session)
}

func TestSessionTrackerImplicitExpiry(t *testing.T) {
ctx := context.Background()
bk, err := memory.New(memory.Config{})
require.NoError(t, err)

id := uuid.New().String()
id2 := uuid.New().String()
srv, err := NewSessionTrackerService(bk)
require.NoError(t, err)

_, err = srv.CreateSessionTracker(ctx, &proto.CreateSessionTrackerRequest{
Namespace: defaults.Namespace,
ID: id,
Type: types.KindSSHSession,
Hostname: "hostname",
ClusterName: "cluster",
Login: "root",
Initiator: &types.Participant{
ID: uuid.New().String(),
User: "eve",
Mode: string(types.SessionPeerMode),
},
Expires: time.Now().UTC().Add(time.Second),
})
require.NoError(t, err)

_, err = srv.CreateSessionTracker(ctx, &proto.CreateSessionTrackerRequest{
xacrimon marked this conversation as resolved.
Show resolved Hide resolved
xacrimon marked this conversation as resolved.
Show resolved Hide resolved
Namespace: defaults.Namespace,
ID: id2,
Type: types.KindSSHSession,
Hostname: "hostname",
ClusterName: "cluster",
Login: "root",
Initiator: &types.Participant{
ID: uuid.New().String(),
User: "eve",
Mode: string(types.SessionPeerMode),
},
Expires: time.Now().UTC().Add(24 * time.Hour),
})
require.NoError(t, err)

require.Eventually(t, func() bool {
sessions, err := srv.GetActiveSessionTrackers(ctx)
require.NoError(t, err)
return len(sessions) == 1
xacrimon marked this conversation as resolved.
Show resolved Hide resolved
}, time.Minute, time.Second)
}