From eb507dca899a2f9e5f7fde5d60dfa619282d6601 Mon Sep 17 00:00:00 2001 From: Carl Bergquist Date: Fri, 10 Mar 2023 13:57:29 +0100 Subject: [PATCH] Remotecache: rename setbytearray/getbytearray to set/get and remove codec (#64470) Signed-off-by: bergquist --- pkg/infra/remotecache/database_storage.go | 32 +---- .../remotecache/database_storage_test.go | 9 +- pkg/infra/remotecache/memcached_storage.go | 39 +----- pkg/infra/remotecache/redis_storage.go | 43 +------ pkg/infra/remotecache/remotecache.go | 114 +++++++----------- pkg/infra/remotecache/remotecache_test.go | 112 ++++++++++++----- pkg/middleware/middleware_test.go | 2 +- pkg/services/anonymous/anonimpl/impl.go | 2 +- pkg/services/auth/jwt/key_sets.go | 4 +- pkg/services/authn/clients/proxy.go | 8 +- pkg/services/authn/clients/proxy_test.go | 4 +- .../contexthandler/auth_proxy_test.go | 4 +- .../contexthandler/authproxy/authproxy.go | 6 +- .../authproxy/authproxy_test.go | 4 +- pkg/services/rendering/auth.go | 4 +- 15 files changed, 164 insertions(+), 223 deletions(-) diff --git a/pkg/infra/remotecache/database_storage.go b/pkg/infra/remotecache/database_storage.go index 2d2ed1d34ca..af834da3b61 100644 --- a/pkg/infra/remotecache/database_storage.go +++ b/pkg/infra/remotecache/database_storage.go @@ -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=?" diff --git a/pkg/infra/remotecache/database_storage_test.go b/pkg/infra/remotecache/database_storage_test.go index 57769a05647..b3a64e79bb0 100644 --- a/pkg/infra/remotecache/database_storage_test.go +++ b/pkg/infra/remotecache/database_storage_test.go @@ -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 diff --git a/pkg/infra/remotecache/memcached_storage.go b/pkg/infra/remotecache/memcached_storage.go index ef3ba900d12..6149c82245d 100644 --- a/pkg/infra/remotecache/memcached_storage.go +++ b/pkg/infra/remotecache/memcached_storage.go @@ -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 diff --git a/pkg/infra/remotecache/redis_storage.go b/pkg/infra/remotecache/redis_storage.go index 6d386b592f8..13684530525 100644 --- a/pkg/infra/remotecache/redis_storage.go +++ b/pkg/infra/remotecache/redis_storage.go @@ -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() } diff --git a/pkg/infra/remotecache/remotecache.go b/pkg/infra/remotecache/remotecache.go index 6ecf712e586..039b962d087 100644 --- a/pkg/infra/remotecache/remotecache.go +++ b/pkg/infra/remotecache/remotecache.go @@ -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) } diff --git a/pkg/infra/remotecache/remotecache_test.go b/pkg/infra/remotecache/remotecache_test.go index 585154af647..7c99f3d6a90 100644 --- a/pkg/infra/remotecache/remotecache_test.go +++ b/pkg/infra/remotecache/remotecache_test.go @@ -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)) +} diff --git a/pkg/middleware/middleware_test.go b/pkg/middleware/middleware_test.go index 0331a31b82e..fcbe9edc190 100644 --- a/pkg/middleware/middleware_test.go +++ b/pkg/middleware/middleware_test.go @@ -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", "/") diff --git a/pkg/services/anonymous/anonimpl/impl.go b/pkg/services/anonymous/anonimpl/impl.go index c6677086e49..df055628821 100644 --- a/pkg/services/anonymous/anonimpl/impl.go +++ b/pkg/services/anonymous/anonimpl/impl.go @@ -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) } diff --git a/pkg/services/auth/jwt/key_sets.go b/pkg/services/auth/jwt/key_sets.go index 6fc366191f0..05b746f4ce0 100644 --- a/pkg/services/auth/jwt/key_sets.go +++ b/pkg/services/auth/jwt/key_sets.go @@ -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 } diff --git a/pkg/services/authn/clients/proxy.go b/pkg/services/authn/clients/proxy.go index a53fb754edb..c7384eb898b 100644 --- a/pkg/services/authn/clients/proxy.go +++ b/pkg/services/authn/clients/proxy.go @@ -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) } diff --git a/pkg/services/authn/clients/proxy_test.go b/pkg/services/authn/clients/proxy_test.go index a0f39ec0425..a0b19dc1a0d 100644 --- a/pkg/services/authn/clients/proxy_test.go +++ b/pkg/services/authn/clients/proxy_test.go @@ -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 } diff --git a/pkg/services/contexthandler/auth_proxy_test.go b/pkg/services/contexthandler/auth_proxy_test.go index 2a233b97b62..124fa98a980 100644 --- a/pkg/services/contexthandler/auth_proxy_test.go +++ b/pkg/services/contexthandler/auth_proxy_test.go @@ -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) diff --git a/pkg/services/contexthandler/authproxy/authproxy.go b/pkg/services/contexthandler/authproxy/authproxy.go index 847ae84c0b9..5f42274e6c7 100644 --- a/pkg/services/contexthandler/authproxy/authproxy.go +++ b/pkg/services/contexthandler/authproxy/authproxy.go @@ -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 } diff --git a/pkg/services/contexthandler/authproxy/authproxy_test.go b/pkg/services/contexthandler/authproxy/authproxy_test.go index 361d0972a5c..57f3f55b170 100644 --- a/pkg/services/contexthandler/authproxy/authproxy_test.go +++ b/pkg/services/contexthandler/authproxy/authproxy_test.go @@ -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) { diff --git a/pkg/services/rendering/auth.go b/pkg/services/rendering/auth.go index 307430333a8..32ef4e26d79 100644 --- a/pkg/services/rendering/auth.go +++ b/pkg/services/rendering/auth.go @@ -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) {