Remotecache: rename setbytearray/getbytearray to set/get and remove codec (#64470)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
Carl Bergquist 2023-03-10 13:57:29 +01:00 committed by GitHub
parent 93b32eec4b
commit eb507dca89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 164 additions and 223 deletions

View File

@ -14,14 +14,12 @@ const databaseCacheType = "database"
type databaseCache struct {
SQLStore db.DB
codec codec
log log.Logger
}
func newDatabaseCache(sqlstore db.DB, codec codec) *databaseCache {
func newDatabaseCache(sqlstore db.DB) *databaseCache {
dc := &databaseCache{
SQLStore: sqlstore,
codec: codec,
log: log.New("remotecache.database"),
}
@ -54,7 +52,7 @@ func (dc *databaseCache) internalRunGC() {
}
}
func (dc *databaseCache) GetByteArray(ctx context.Context, key string) ([]byte, error) {
func (dc *databaseCache) Get(ctx context.Context, key string) ([]byte, error) {
cacheHit := CacheData{}
err := dc.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
@ -85,21 +83,7 @@ func (dc *databaseCache) GetByteArray(ctx context.Context, key string) ([]byte,
return cacheHit.Data, err
}
func (dc *databaseCache) Get(ctx context.Context, key string) (interface{}, error) {
bytes, err := dc.GetByteArray(ctx, key)
if err != nil {
return nil, err
}
item := &cachedItem{}
if err = dc.codec.Decode(ctx, bytes, item); err != nil {
return nil, err
}
return item.Val, err
}
func (dc *databaseCache) SetByteArray(ctx context.Context, key string, data []byte, expire time.Duration) error {
func (dc *databaseCache) Set(ctx context.Context, key string, data []byte, expire time.Duration) error {
return dc.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
var expiresInSeconds int64
if expire != 0 {
@ -129,16 +113,6 @@ func (dc *databaseCache) SetByteArray(ctx context.Context, key string, data []by
})
}
func (dc *databaseCache) Set(ctx context.Context, key string, value interface{}, expire time.Duration) error {
item := &cachedItem{Val: value}
data, err := dc.codec.Encode(ctx, item)
if err != nil {
return err
}
return dc.SetByteArray(ctx, key, data, expire)
}
func (dc *databaseCache) Delete(ctx context.Context, key string) error {
return dc.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
sql := "DELETE FROM cache_data WHERE cache_key=?"

View File

@ -17,11 +17,10 @@ func TestDatabaseStorageGarbageCollection(t *testing.T) {
db := &databaseCache{
SQLStore: sqlstore,
codec: &gobCodec{},
log: log.New("remotecache.database"),
}
obj := &CacheableStruct{String: "foolbar"}
obj := []byte("foolbar")
// set time.now to 2 weeks ago
var err error
@ -66,11 +65,10 @@ func TestSecondSet(t *testing.T) {
db := &databaseCache{
SQLStore: sqlstore,
codec: &gobCodec{},
log: log.New("remotecache.database"),
}
obj := &CacheableStruct{String: "hey!"}
obj := []byte("hey!")
err = db.Set(context.Background(), "killa-gorilla", obj, 0)
assert.Equal(t, err, nil)
@ -84,11 +82,10 @@ func TestDatabaseStorageCount(t *testing.T) {
db := &databaseCache{
SQLStore: sqlstore,
codec: &gobCodec{},
log: log.New("remotecache.database"),
}
obj := &CacheableStruct{String: "foolbar"}
obj := []byte("foolbar")
// set time.now to 2 weeks ago
var err error

View File

@ -15,14 +15,12 @@ const memcachedCacheType = "memcached"
var ErrNotImplemented = errors.New("not implemented")
type memcachedStorage struct {
c *memcache.Client
codec codec
c *memcache.Client
}
func newMemcachedStorage(opts *setting.RemoteCacheOptions, codec codec) *memcachedStorage {
func newMemcachedStorage(opts *setting.RemoteCacheOptions) *memcachedStorage {
return &memcachedStorage{
c: memcache.New(opts.ConnStr),
codec: codec,
c: memcache.New(opts.ConnStr),
}
}
@ -34,19 +32,8 @@ func newItem(sid string, data []byte, expire int32) *memcache.Item {
}
}
// Set sets value to given key in the cache.
func (s *memcachedStorage) Set(ctx context.Context, key string, val interface{}, expires time.Duration) error {
item := &cachedItem{Val: val}
bytes, err := s.codec.Encode(ctx, item)
if err != nil {
return err
}
return s.SetByteArray(ctx, key, bytes, expires)
}
// SetByteArray stores an byte array in the cache
func (s *memcachedStorage) SetByteArray(ctx context.Context, key string, data []byte, expires time.Duration) error {
func (s *memcachedStorage) Set(ctx context.Context, key string, data []byte, expires time.Duration) error {
var expiresInSeconds int64
if expires != 0 {
expiresInSeconds = int64(expires) / int64(time.Second)
@ -56,24 +43,8 @@ func (s *memcachedStorage) SetByteArray(ctx context.Context, key string, data []
return s.c.Set(memcachedItem)
}
// Get gets value by given key in the cache.
func (s *memcachedStorage) Get(ctx context.Context, key string) (interface{}, error) {
bytes, err := s.GetByteArray(ctx, key)
if err != nil {
return nil, err
}
item := &cachedItem{}
err = s.codec.Decode(ctx, bytes, item)
if err != nil {
return nil, err
}
return item.Val, nil
}
// GetByteArray returns the cached value as an byte array
func (s *memcachedStorage) GetByteArray(ctx context.Context, key string) ([]byte, error) {
func (s *memcachedStorage) Get(ctx context.Context, key string) ([]byte, error) {
memcachedItem, err := s.c.Get(key)
if errors.Is(err, memcache.ErrCacheMiss) {
return nil, ErrCacheItemNotFound

View File

@ -16,8 +16,7 @@ import (
const redisCacheType = "redis"
type redisStorage struct {
c *redis.Client
codec codec
c *redis.Client
}
// parseRedisConnStr parses k=v pairs in csv and builds a redis Options object
@ -78,54 +77,22 @@ func parseRedisConnStr(connStr string) (*redis.Options, error) {
return options, nil
}
func newRedisStorage(opts *setting.RemoteCacheOptions, codec codec) (*redisStorage, error) {
func newRedisStorage(opts *setting.RemoteCacheOptions) (*redisStorage, error) {
opt, err := parseRedisConnStr(opts.ConnStr)
if err != nil {
return nil, err
}
return &redisStorage{c: redis.NewClient(opt), codec: codec}, nil
}
// Set sets value to given key in session.
func (s *redisStorage) Set(ctx context.Context, key string, val interface{}, expires time.Duration) error {
item := &cachedItem{Val: val}
value, err := s.codec.Encode(ctx, item)
if err != nil {
return err
}
return s.SetByteArray(ctx, key, value, expires)
return &redisStorage{c: redis.NewClient(opt)}, nil
}
// Set sets value to a given key
func (s *redisStorage) SetByteArray(ctx context.Context, key string, data []byte, expires time.Duration) error {
func (s *redisStorage) Set(ctx context.Context, key string, data []byte, expires time.Duration) error {
status := s.c.Set(ctx, key, data, expires)
return status.Err()
}
// Get gets value by given key in session.
func (s *redisStorage) Get(ctx context.Context, key string) (interface{}, error) {
v, err := s.GetByteArray(ctx, key)
if err != nil {
if err.Error() == "EOF" {
return nil, ErrCacheItemNotFound
}
return nil, err
}
item := &cachedItem{}
err = s.codec.Decode(ctx, v, item)
if err == nil {
return item.Val, nil
}
return nil, err
}
// GetByteArray returns the value as byte array
func (s *redisStorage) GetByteArray(ctx context.Context, key string) ([]byte, error) {
func (s *redisStorage) Get(ctx context.Context, key string) ([]byte, error) {
return s.c.Get(ctx, key).Bytes()
}

View File

@ -1,9 +1,7 @@
package remotecache
import (
"bytes"
"context"
"encoding/gob"
"errors"
"time"
@ -30,13 +28,7 @@ const (
func ProvideService(cfg *setting.Cfg, sqlStore db.DB, usageStats usagestats.Service,
secretsService secrets.Service) (*RemoteCache, error) {
var codec codec
if cfg.RemoteCacheOptions.Encryption {
codec = &encryptionCodec{secretsService}
} else {
codec = &gobCodec{}
}
client, err := createClient(cfg.RemoteCacheOptions, sqlStore, codec)
client, err := createClient(cfg.RemoteCacheOptions, sqlStore)
if err != nil {
return nil, err
}
@ -69,11 +61,11 @@ func (ds *RemoteCache) getUsageStats(ctx context.Context) (map[string]interface{
// so any struct added to the cache needs to be registered with `remotecache.Register`
// ex `remotecache.Register(CacheableStruct{})`
type CacheStorage interface {
// GetByteArray gets the cache value as an byte array
GetByteArray(ctx context.Context, key string) ([]byte, error)
// Get gets the cache value as an byte array
Get(ctx context.Context, key string) ([]byte, error)
// SetByteArray saves the value as an byte array. if `expire` is set to zero it will default to 24h
SetByteArray(ctx context.Context, key string, value []byte, expire time.Duration) error
// Set saves the value as an byte array. if `expire` is set to zero it will default to 24h
Set(ctx context.Context, key string, value []byte, expire time.Duration) error
// Delete object from cache
Delete(ctx context.Context, key string) error
@ -91,18 +83,18 @@ type RemoteCache struct {
Cfg *setting.Cfg
}
// GetByteArray returns the cached value as an byte array
func (ds *RemoteCache) GetByteArray(ctx context.Context, key string) ([]byte, error) {
return ds.client.GetByteArray(ctx, key)
// Get returns the cached value as an byte array
func (ds *RemoteCache) Get(ctx context.Context, key string) ([]byte, error) {
return ds.client.Get(ctx, key)
}
// SetByteArray stored the byte array in the cache
func (ds *RemoteCache) SetByteArray(ctx context.Context, key string, value []byte, expire time.Duration) error {
// Set stored the byte array in the cache
func (ds *RemoteCache) Set(ctx context.Context, key string, value []byte, expire time.Duration) error {
if expire == 0 {
expire = defaultMaxCacheExpiration
}
return ds.client.SetByteArray(ctx, key, value, expire)
return ds.client.Set(ctx, key, value, expire)
}
// Delete object from cache
@ -127,14 +119,14 @@ func (ds *RemoteCache) Run(ctx context.Context) error {
return ctx.Err()
}
func createClient(opts *setting.RemoteCacheOptions, sqlstore db.DB, codec codec) (cache CacheStorage, err error) {
func createClient(opts *setting.RemoteCacheOptions, sqlstore db.DB) (cache CacheStorage, err error) {
switch opts.Name {
case redisCacheType:
cache, err = newRedisStorage(opts, codec)
cache, err = newRedisStorage(opts)
case memcachedCacheType:
cache = newMemcachedStorage(opts, codec)
cache = newMemcachedStorage(opts)
case databaseCacheType:
cache = newDatabaseCache(sqlstore, codec)
cache = newDatabaseCache(sqlstore)
default:
return nil, ErrInvalidCacheType
}
@ -144,61 +136,45 @@ func createClient(opts *setting.RemoteCacheOptions, sqlstore db.DB, codec codec)
if opts.Prefix != "" {
cache = &prefixCacheStorage{cache: cache, prefix: opts.Prefix}
}
if opts.Encryption {
cache = &encryptedCacheStorage{cache: cache}
}
return cache, nil
}
// Register records a type, identified by a value for that type, under its
// internal type name. That name will identify the concrete type of a value
// sent or received as an interface variable. Only types that will be
// transferred as implementations of interface values need to be registered.
// Expecting to be used only during initialization, it panics if the mapping
// between types and names is not a bijection.
func Register(value interface{}) {
gob.Register(value)
type encryptedCacheStorage struct {
cache CacheStorage
secretsService encryptionService
}
type cachedItem struct {
Val interface{}
type encryptionService interface {
Encrypt(ctx context.Context, payload []byte, opt secrets.EncryptionOptions) ([]byte, error)
Decrypt(ctx context.Context, payload []byte) ([]byte, error)
}
type codec interface {
Encode(context.Context, *cachedItem) ([]byte, error)
Decode(context.Context, []byte, *cachedItem) error
}
type gobCodec struct{}
func (c *gobCodec) Encode(_ context.Context, item *cachedItem) ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(item)
return buf.Bytes(), err
}
func (c *gobCodec) Decode(_ context.Context, data []byte, out *cachedItem) error {
buf := bytes.NewBuffer(data)
return gob.NewDecoder(buf).Decode(&out)
}
type encryptionCodec struct {
secretsService secrets.Service
}
func (c *encryptionCodec) Encode(ctx context.Context, item *cachedItem) ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(item)
func (pcs *encryptedCacheStorage) Get(ctx context.Context, key string) ([]byte, error) {
data, err := pcs.cache.Get(ctx, key)
if err != nil {
return nil, err
}
return c.secretsService.Encrypt(ctx, buf.Bytes(), secrets.WithoutScope())
}
func (c *encryptionCodec) Decode(ctx context.Context, data []byte, out *cachedItem) error {
decrypted, err := c.secretsService.Decrypt(ctx, data)
return pcs.secretsService.Decrypt(ctx, data)
}
func (pcs *encryptedCacheStorage) Set(ctx context.Context, key string, value []byte, expire time.Duration) error {
encrypted, err := pcs.secretsService.Encrypt(ctx, value, secrets.WithoutScope())
if err != nil {
return err
}
buf := bytes.NewBuffer(decrypted)
return gob.NewDecoder(buf).Decode(&out)
return pcs.cache.Set(ctx, key, encrypted, expire)
}
func (pcs *encryptedCacheStorage) Delete(ctx context.Context, key string) error {
return pcs.cache.Delete(ctx, key)
}
func (pcs *encryptedCacheStorage) Count(ctx context.Context, prefix string) (int64, error) {
return pcs.cache.Count(ctx, prefix)
}
type prefixCacheStorage struct {
@ -206,16 +182,16 @@ type prefixCacheStorage struct {
prefix string
}
func (pcs *prefixCacheStorage) GetByteArray(ctx context.Context, key string) ([]byte, error) {
return pcs.cache.GetByteArray(ctx, pcs.prefix+key)
func (pcs *prefixCacheStorage) Get(ctx context.Context, key string) ([]byte, error) {
return pcs.cache.Get(ctx, pcs.prefix+key)
}
func (pcs *prefixCacheStorage) SetByteArray(ctx context.Context, key string, value []byte, expire time.Duration) error {
return pcs.cache.SetByteArray(ctx, pcs.prefix+key, value, expire)
func (pcs *prefixCacheStorage) Set(ctx context.Context, key string, value []byte, expire time.Duration) error {
return pcs.cache.Set(ctx, pcs.prefix+key, value, expire)
}
func (pcs *prefixCacheStorage) Delete(ctx context.Context, key string) error {
return pcs.cache.Delete(ctx, pcs.prefix+key)
}
func (pcs *prefixCacheStorage) Count(ctx context.Context, prefix string) (int64, error) {
return pcs.cache.Count(ctx, pcs.prefix)
return pcs.cache.Count(ctx, pcs.prefix+prefix)
}

View File

@ -9,21 +9,12 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/usagestats"
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/services/secrets/fakes"
"github.com/grafana/grafana/pkg/setting"
)
type CacheableStruct struct {
String string
Int64 int64
}
func init() {
Register(CacheableStruct{})
}
func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore db.DB) CacheStorage {
t.Helper()
@ -49,7 +40,7 @@ func TestCachedBasedOnConfig(t *testing.T) {
}
func TestInvalidCacheTypeReturnsError(t *testing.T) {
_, err := createClient(&setting.RemoteCacheOptions{Name: "invalid"}, nil, &gobCodec{})
_, err := createClient(&setting.RemoteCacheOptions{Name: "invalid"}, nil)
assert.Equal(t, err, ErrInvalidCacheType)
}
@ -68,13 +59,13 @@ func runCountTestsForClient(t *testing.T, opts *setting.RemoteCacheOptions, sqls
t.Run("can count items", func(t *testing.T) {
cacheableValue := []byte("hej hej")
err := client.SetByteArray(context.Background(), "pref-key1", cacheableValue, 0)
err := client.Set(context.Background(), "pref-key1", cacheableValue, 0)
require.NoError(t, err)
err = client.SetByteArray(context.Background(), "pref-key2", cacheableValue, 0)
err = client.Set(context.Background(), "pref-key2", cacheableValue, 0)
require.NoError(t, err)
err = client.SetByteArray(context.Background(), "key3-not-pref", cacheableValue, 0)
err = client.Set(context.Background(), "key3-not-pref", cacheableValue, 0)
require.NoError(t, err)
n, errC := client.Count(context.Background(), "pref-")
@ -92,10 +83,10 @@ func runCountTestsForClient(t *testing.T, opts *setting.RemoteCacheOptions, sqls
func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
dataToCache := []byte("some bytes")
err := client.SetByteArray(context.Background(), "key1", dataToCache, 0)
err := client.Set(context.Background(), "key1", dataToCache, 0)
assert.Equal(t, err, nil, "expected nil. got: ", err)
data, err := client.GetByteArray(context.Background(), "key1")
data, err := client.Get(context.Background(), "key1")
assert.Equal(t, err, nil)
assert.Equal(t, string(data), "some bytes")
@ -103,21 +94,21 @@ func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
err = client.Delete(context.Background(), "key1")
assert.Equal(t, err, nil)
_, err = client.GetByteArray(context.Background(), "key1")
_, err = client.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound)
}
func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
dataToCache := []byte("some bytes")
err := client.SetByteArray(context.Background(), "key1", dataToCache, time.Second)
err := client.Set(context.Background(), "key1", dataToCache, time.Second)
assert.Equal(t, err, nil)
// not sure how this can be avoided when testing redis/memcached :/
<-time.After(time.Second + time.Millisecond)
// should not be able to read that value since its expired
_, err = client.GetByteArray(context.Background(), "key1")
_, err = client.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound)
}
@ -140,26 +131,91 @@ func TestCollectUsageStats(t *testing.T) {
}
func TestCachePrefix(t *testing.T) {
db := db.InitTestDB(t)
cache := &databaseCache{
SQLStore: db,
log: log.New("remotecache.database"),
codec: &gobCodec{},
}
cache := NewFakeCacheStorage()
prefixCache := &prefixCacheStorage{cache: cache, prefix: "test/"}
// Set a value (with a prefix)
err := prefixCache.SetByteArray(context.Background(), "foo", []byte("bar"), time.Hour)
err := prefixCache.Set(context.Background(), "foo", []byte("bar"), time.Hour)
require.NoError(t, err)
// Get a value (with a prefix)
v, err := prefixCache.GetByteArray(context.Background(), "foo")
v, err := prefixCache.Get(context.Background(), "foo")
require.NoError(t, err)
require.Equal(t, "bar", string(v))
// Get a value directly from the underlying cache, ensure the prefix is in the key
v, err = cache.GetByteArray(context.Background(), "test/foo")
v, err = cache.Get(context.Background(), "test/foo")
require.NoError(t, err)
require.Equal(t, "bar", string(v))
// Get a value directly from the underlying cache without a prefix, should not be there
_, err = cache.Get(context.Background(), "foo")
require.Error(t, err)
}
func TestEncryptedCache(t *testing.T) {
cache := NewFakeCacheStorage()
encryptedCache := &encryptedCacheStorage{cache: cache, secretsService: &fakeSecretsService{}}
// Set a value in the encrypted cache
err := encryptedCache.Set(context.Background(), "foo", []byte("bar"), time.Hour)
require.NoError(t, err)
// make sure the stored value is not equal to input
v, err := cache.Get(context.Background(), "foo")
require.NoError(t, err)
require.NotEqual(t, "bar", string(v))
// make sure the returned value is the same as orignial
v, err = encryptedCache.Get(context.Background(), "foo")
require.NoError(t, err)
require.Equal(t, "bar", string(v))
}
type fakeCacheStorage struct {
storage map[string][]byte
}
func (fcs fakeCacheStorage) Set(_ context.Context, key string, value []byte, exp time.Duration) error {
fcs.storage[key] = value
return nil
}
func (fcs fakeCacheStorage) Get(_ context.Context, key string) ([]byte, error) {
value, exist := fcs.storage[key]
if !exist {
return nil, ErrCacheItemNotFound
}
return value, nil
}
func (fcs fakeCacheStorage) Delete(_ context.Context, key string) error {
delete(fcs.storage, key)
return nil
}
func (fcs fakeCacheStorage) Count(_ context.Context, prefix string) (int64, error) {
return int64(len(fcs.storage)), nil
}
func NewFakeCacheStorage() CacheStorage {
return fakeCacheStorage{
storage: map[string][]byte{},
}
}
type fakeSecretsService struct{}
func (f fakeSecretsService) Encrypt(_ context.Context, payload []byte, _ secrets.EncryptionOptions) ([]byte, error) {
return f.reverse(payload), nil
}
func (f fakeSecretsService) Decrypt(_ context.Context, payload []byte) ([]byte, error) {
return f.reverse(payload), nil
}
func (f fakeSecretsService) reverse(input []byte) []byte {
r := []rune(string(input))
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return []byte(string(r))
}

View File

@ -615,7 +615,7 @@ func TestMiddlewareContext(t *testing.T) {
require.NoError(t, err)
key := fmt.Sprintf(authproxy.CachePrefix, h)
userIdBytes := []byte(strconv.FormatInt(userID, 10))
err = sc.remoteCacheService.SetByteArray(context.Background(), key, userIdBytes, 0)
err = sc.remoteCacheService.Set(context.Background(), key, userIdBytes, 0)
require.NoError(t, err)
sc.fakeReq("GET", "/")

View File

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

View File

@ -171,7 +171,7 @@ func (ks *keySetHTTP) getJWKS(ctx context.Context) (keySetJWKS, error) {
var jwks keySetJWKS
if ks.cacheExpiration > 0 {
if val, err := ks.cache.GetByteArray(ctx, ks.cacheKey); err == nil {
if val, err := ks.cache.Get(ctx, ks.cacheKey); err == nil {
err := json.Unmarshal(val, &jwks)
return jwks, err
}
@ -200,7 +200,7 @@ func (ks *keySetHTTP) getJWKS(ctx context.Context) (keySetJWKS, error) {
}
if ks.cacheExpiration > 0 {
err = ks.cache.SetByteArray(ctx, ks.cacheKey, jsonBuf.Bytes(), ks.cacheExpiration)
err = ks.cache.Set(ctx, ks.cacheKey, jsonBuf.Bytes(), ks.cacheExpiration)
}
return jwks, err
}

View File

@ -50,8 +50,8 @@ func ProvideProxy(cfg *setting.Cfg, cache proxyCache, userSrv user.Service, clie
}
type proxyCache interface {
GetByteArray(ctx context.Context, key string) ([]byte, error)
SetByteArray(ctx context.Context, key string, value []byte, expire time.Duration) error
Get(ctx context.Context, key string) ([]byte, error)
Set(ctx context.Context, key string, value []byte, expire time.Duration) error
}
type Proxy struct {
@ -83,7 +83,7 @@ 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.GetByteArray(ctx, cacheKey); err == nil {
if entry, err := c.cache.Get(ctx, cacheKey); err == nil {
uid := int64(binary.LittleEndian.Uint64(entry))
usr, err := c.userSrv.GetSignedInUserWithCacheCtx(ctx, &user.GetSignedInUserQuery{
@ -138,7 +138,7 @@ func (c *Proxy) Hook(ctx context.Context, identity *authn.Identity, r *authn.Req
c.log.FromContext(ctx).Debug("Cache proxy user", "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 {
if err := c.cache.Set(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)
}

View File

@ -181,10 +181,10 @@ type fakeCache struct {
expectedItem []byte
}
func (f fakeCache) GetByteArray(ctx context.Context, key string) ([]byte, error) {
func (f fakeCache) Get(ctx context.Context, key string) ([]byte, error) {
return f.expectedItem, f.expectedErr
}
func (f fakeCache) SetByteArray(ctx context.Context, key string, value []byte, expire time.Duration) error {
func (f fakeCache) Set(ctx context.Context, key string, value []byte, expire time.Duration) error {
return f.expectedErr
}

View File

@ -58,7 +58,7 @@ func TestInitContextWithAuthProxy_CachedInvalidUserID(t *testing.T) {
t.Logf("Injecting stale user ID in cache with key %q", key)
userIdPayload := []byte(strconv.FormatInt(int64(33), 10))
err = svc.RemoteCache.SetByteArray(context.Background(), key, userIdPayload, 0)
err = svc.RemoteCache.Set(context.Background(), key, userIdPayload, 0)
require.NoError(t, err)
authEnabled := svc.initContextWithAuthProxy(ctx, orgID)
@ -67,7 +67,7 @@ func TestInitContextWithAuthProxy_CachedInvalidUserID(t *testing.T) {
require.Equal(t, userID, ctx.SignedInUser.UserID)
require.True(t, ctx.IsSignedIn)
cachedByteArray, err := svc.RemoteCache.GetByteArray(context.Background(), key)
cachedByteArray, err := svc.RemoteCache.Get(context.Background(), key)
require.NoError(t, err)
cacheUserId, err := strconv.ParseInt(string(cachedByteArray), 10, 64)

View File

@ -191,7 +191,7 @@ func (auth *AuthProxy) getUserViaCache(reqCtx *contextmodel.ReqContext) (int64,
return 0, err
}
auth.logger.Debug("Getting user ID via auth cache", "cacheKey", cacheKey)
cachedValue, err := auth.remoteCache.GetByteArray(reqCtx.Req.Context(), cacheKey)
cachedValue, err := auth.remoteCache.Get(reqCtx.Req.Context(), cacheKey)
if err != nil {
return 0, err
}
@ -353,7 +353,7 @@ func (auth *AuthProxy) Remember(reqCtx *contextmodel.ReqContext, id int64) error
}
// Check if user already in cache
cachedValue, err := auth.remoteCache.GetByteArray(reqCtx.Req.Context(), key)
cachedValue, err := auth.remoteCache.Get(reqCtx.Req.Context(), key)
if err == nil && len(cachedValue) != 0 {
return nil
}
@ -361,7 +361,7 @@ func (auth *AuthProxy) Remember(reqCtx *contextmodel.ReqContext, id int64) error
expiration := time.Duration(auth.cfg.AuthProxySyncTTL) * time.Minute
userIdPayload := []byte(strconv.FormatInt(id, 10))
if err := auth.remoteCache.SetByteArray(reqCtx.Req.Context(), key, userIdPayload, expiration); err != nil {
if err := auth.remoteCache.Set(reqCtx.Req.Context(), key, userIdPayload, expiration); err != nil {
return err
}

View File

@ -61,7 +61,7 @@ func TestMiddlewareContext(t *testing.T) {
require.NoError(t, err)
key := fmt.Sprintf(CachePrefix, h)
userIdPayload := []byte(strconv.FormatInt(id, 10))
err = cache.SetByteArray(context.Background(), key, userIdPayload, 0)
err = cache.Set(context.Background(), key, userIdPayload, 0)
require.NoError(t, err)
// Set up the middleware
auth, reqCtx := prepareMiddleware(t, cache, nil)
@ -84,7 +84,7 @@ func TestMiddlewareContext(t *testing.T) {
require.NoError(t, err)
key := fmt.Sprintf(CachePrefix, h)
userIdPayload := []byte(strconv.FormatInt(id, 10))
err = cache.SetByteArray(context.Background(), key, userIdPayload, 0)
err = cache.Set(context.Background(), key, userIdPayload, 0)
require.NoError(t, err)
auth, reqCtx := prepareMiddleware(t, cache, func(req *http.Request, cfg *setting.Cfg) {

View File

@ -21,7 +21,7 @@ type RenderUser struct {
}
func (rs *RenderingService) GetRenderUser(ctx context.Context, key string) (*RenderUser, bool) {
val, err := rs.RemoteCacheService.GetByteArray(ctx, fmt.Sprintf(renderKeyPrefix, key))
val, err := rs.RemoteCacheService.Get(ctx, fmt.Sprintf(renderKeyPrefix, key))
if err != nil {
rs.log.Error("Failed to get render key from cache", "error", err)
}
@ -46,7 +46,7 @@ func setRenderKey(cache *remotecache.RemoteCache, ctx context.Context, opts Auth
return err
}
return cache.SetByteArray(ctx, fmt.Sprintf(renderKeyPrefix, renderKey), buf.Bytes(), expiry)
return cache.Set(ctx, fmt.Sprintf(renderKeyPrefix, renderKey), buf.Bytes(), expiry)
}
func generateAndSetRenderKey(cache *remotecache.RemoteCache, ctx context.Context, opts AuthOpts, expiry time.Duration) (string, error) {