From 6682639b73779a2795cb13f10b7ebae1dd807476 Mon Sep 17 00:00:00 2001 From: Tim Ross Date: Fri, 26 Jan 2024 17:52:18 -0500 Subject: [PATCH] Use cache for listing app sessions ListAppSessions was never added to the `auth.Cache` interface or implemented by `cache.Cache` which means all session listing was using the backend as the data source. This could cause backend throttling if the number of sessions was large enough. --- lib/auth/api.go | 3 +++ lib/cache/cache.go | 13 +++++++++++++ lib/cache/collections.go | 1 + 3 files changed, 17 insertions(+) diff --git a/lib/auth/api.go b/lib/auth/api.go index e3259efd5c40a..04fcdb2424ec6 100644 --- a/lib/auth/api.go +++ b/lib/auth/api.go @@ -923,6 +923,9 @@ type Cache interface { // GetAppSession gets an application web session. GetAppSession(context.Context, types.GetAppSessionRequest) (types.WebSession, error) + // ListAppSessions returns a page of application web sessions. + ListAppSessions(ctx context.Context, pageSize int, pageToken, user string) ([]types.WebSession, string, error) + // GetSnowflakeSession gets a Snowflake web session. GetSnowflakeSession(context.Context, types.GetSnowflakeSessionRequest) (types.WebSession, error) diff --git a/lib/cache/cache.go b/lib/cache/cache.go index fa4374df7366d..3e51e0bc39193 100644 --- a/lib/cache/cache.go +++ b/lib/cache/cache.go @@ -2152,6 +2152,19 @@ func (c *Cache) GetAppSession(ctx context.Context, req types.GetAppSessionReques return sess, trace.Wrap(err) } +// ListAppSessions returns a page of application web sessions. +func (c *Cache) ListAppSessions(ctx context.Context, pageSize int, pageToken, user string) ([]types.WebSession, string, error) { + ctx, span := c.Tracer.Start(ctx, "cache/ListAppSessions") + defer span.End() + + rg, err := readCollectionCache(c, c.collections.appSessions) + if err != nil { + return nil, "", trace.Wrap(err) + } + defer rg.Release() + return rg.reader.ListAppSessions(ctx, pageSize, pageToken, user) +} + // GetSnowflakeSession gets Snowflake web session. func (c *Cache) GetSnowflakeSession(ctx context.Context, req types.GetSnowflakeSessionRequest) (types.WebSession, error) { ctx, span := c.Tracer.Start(ctx, "cache/GetSnowflakeSession") diff --git a/lib/cache/collections.go b/lib/cache/collections.go index 48296ce219464..8c1bd704ea527 100644 --- a/lib/cache/collections.go +++ b/lib/cache/collections.go @@ -1427,6 +1427,7 @@ func (appSessionExecutor) getReader(cache *Cache, cacheOK bool) appSessionGetter type appSessionGetter interface { GetAppSession(ctx context.Context, req types.GetAppSessionRequest) (types.WebSession, error) + ListAppSessions(ctx context.Context, pageSize int, pageToken, user string) ([]types.WebSession, string, error) } var _ executor[types.WebSession, appSessionGetter] = appSessionExecutor{}