Remotecache: Migrates get/set calls to use bytearrays and remove get/set functions (#63525)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
Carl Bergquist
2023-03-08 17:08:57 +01:00
committed by GitHub
parent a5133d61b5
commit 7c55dbf37d
5 changed files with 34 additions and 54 deletions

View File

@@ -96,5 +96,5 @@ func (a *AnonSessionService) TagSession(ctx context.Context, httpReq *http.Reque
a.localCache.SetDefault(key, struct{}{})
return a.remoteCache.Set(ctx, key, key, thirtyDays)
return a.remoteCache.SetByteArray(ctx, key, []byte(key), thirtyDays)
}

View File

@@ -2,6 +2,7 @@ package clients
import (
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"hash/fnv"
@@ -49,8 +50,8 @@ func ProvideProxy(cfg *setting.Cfg, cache proxyCache, userSrv user.Service, clie
}
type proxyCache interface {
Get(ctx context.Context, key string) (interface{}, error)
Set(ctx context.Context, key string, value interface{}, expire time.Duration) error
GetByteArray(ctx context.Context, key string) ([]byte, error)
SetByteArray(ctx context.Context, key string, value []byte, expire time.Duration) error
}
type Proxy struct {
@@ -82,14 +83,16 @@ func (c *Proxy) Authenticate(ctx context.Context, r *authn.Request) (*authn.Iden
if ok {
// See if we have cached the user id, in that case we can fetch the signed-in user and skip sync.
// Error here means that we could not find anything in cache, so we can proceed as usual
if entry, err := c.cache.Get(ctx, cacheKey); err == nil {
if entry, err := c.cache.GetByteArray(ctx, cacheKey); err == nil {
uid := int64(binary.LittleEndian.Uint64(entry))
usr, err := c.userSrv.GetSignedInUserWithCacheCtx(ctx, &user.GetSignedInUserQuery{
UserID: entry.(int64),
UserID: uid,
OrgID: r.OrgID,
})
if err != nil {
c.log.FromContext(ctx).Warn("Could not resolved cached user", "error", err, "userId", entry.(int64))
c.log.FromContext(ctx).Warn("Could not resolved cached user", "error", err, "userId", string(entry))
}
// if we for some reason cannot find the user we proceed with the normal flow, authenticate with ProxyClient
@@ -133,8 +136,10 @@ func (c *Proxy) Hook(ctx context.Context, identity *authn.Identity, r *authn.Req
}
c.log.FromContext(ctx).Debug("Cache proxy user", "userId", id)
if err := c.cache.Set(ctx, identity.ClientParams.CacheAuthProxyKey, id, time.Duration(c.cfg.AuthProxySyncTTL)*time.Minute); err != nil {
c.log.FromContext(ctx).Warn("Failed to cache proxy user", "error", err, "userId", id)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(id))
if err := c.cache.SetByteArray(ctx, identity.ClientParams.CacheAuthProxyKey, bytes, time.Duration(c.cfg.AuthProxySyncTTL)*time.Minute); err != nil {
c.log.Warn("failed to cache proxy user", "error", err, "userId", id)
}
return nil

View File

@@ -178,13 +178,13 @@ var _ proxyCache = new(fakeCache)
type fakeCache struct {
expectedErr error
expectedItem interface{}
expectedItem []byte
}
func (f fakeCache) Get(ctx context.Context, key string) (interface{}, error) {
func (f fakeCache) GetByteArray(ctx context.Context, key string) ([]byte, error) {
return f.expectedItem, f.expectedErr
}
func (f fakeCache) Set(ctx context.Context, key string, value interface{}, expire time.Duration) error {
func (f fakeCache) SetByteArray(ctx context.Context, key string, value []byte, expire time.Duration) error {
return f.expectedErr
}