grafana/pkg/services/secrets/kvstore/cache.go
Michael Mandrus 72d9de3a0f
Secrets: Implement Secret Plugin required flag and fatal crash on startup (#52552)
* add special handling on the plugin gathering side to check whether secrets manager plugins are enabled or not

* show disabled badge in front end if the plugin is not enabled

* Only show error in disabled badge hover if one is present (otherwise it shows "undefined")

* refactor to make use of fields already available in the DTO

* fix typo

* if there is no error returned for the plugin, just show 'disabled'

* fix typo

* Update public/app/features/plugins/admin/components/Badges/PluginDisabledBadge.tsx

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Update frontendsettings.go

add clarifying comment

* fix unit test

* rework task to use new frontend property combined with plugin type to determine if the plugin should be disabled

* Update helpers.test.ts

revert test change

* fix unit test

* show custom uninstall message if the plugin is a secrets manager

* bogus commit to trigger precommit

* undo commit

* run precommit manually

* add some consts

* refactor a bit to pull plugin error management up a level

* re-add code squashed in merge

* fix compile issues

* add code to set plugin error fatal flag after secret migration

* refactor to move plugin startup out of Should Check func

* re-add important check

* make plugin startup errors fatal the first time we set a secret on the plugin

* rename func to make intent clearler

* remove unnecessary duplicate code from plugin mig

* fix compile error

* fix more compile errors

* add some extra logging to secrets migration

* have remote_plugin secret service managed plugin error fatal flag directly

* add blank file for eventual unit tests

* fix linting issues

* changes from PR review

* quick bit of cleanup

* add comment explaining design decision

* move more common test helpers to file

* slightly update to first time Get secret call

* add unit tests

* remove override func from provider

* fix linting issues

* add test cleanup step

* add some comments about refactoring to hacky test function

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
2022-07-25 12:37:47 -04:00

83 lines
2.2 KiB
Go

package kvstore
import (
"context"
"fmt"
"time"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/infra/log"
)
type CachedKVStore struct {
log log.Logger
cache *localcache.CacheService
store SecretsKVStore
}
func NewCachedKVStore(store SecretsKVStore, defaultExpiration time.Duration, cleanupInterval time.Duration) *CachedKVStore {
return &CachedKVStore{
log: log.New("secrets.kvstore"),
cache: localcache.New(defaultExpiration, cleanupInterval),
store: store,
}
}
func (kv *CachedKVStore) Get(ctx context.Context, orgId int64, namespace string, typ string) (string, bool, error) {
key := fmt.Sprint(orgId, namespace, typ)
if value, ok := kv.cache.Get(key); ok {
kv.log.Debug("got secret value from cache", "orgId", orgId, "type", typ, "namespace", namespace)
return fmt.Sprint(value), true, nil
}
value, ok, err := kv.store.Get(ctx, orgId, namespace, typ)
if err != nil {
return "", false, err
}
if ok {
kv.cache.SetDefault(key, value)
}
return value, ok, err
}
func (kv *CachedKVStore) Set(ctx context.Context, orgId int64, namespace string, typ string, value string) error {
err := kv.store.Set(ctx, orgId, namespace, typ, value)
if err != nil {
return err
}
key := fmt.Sprint(orgId, namespace, typ)
kv.cache.SetDefault(key, value)
return nil
}
func (kv *CachedKVStore) Del(ctx context.Context, orgId int64, namespace string, typ string) error {
err := kv.store.Del(ctx, orgId, namespace, typ)
if err != nil {
return err
}
key := fmt.Sprint(orgId, namespace, typ)
kv.cache.Delete(key)
return nil
}
func (kv *CachedKVStore) Keys(ctx context.Context, orgId int64, namespace string, typ string) ([]Key, error) {
return kv.store.Keys(ctx, orgId, namespace, typ)
}
func (kv *CachedKVStore) Rename(ctx context.Context, orgId int64, namespace string, typ string, newNamespace string) error {
err := kv.store.Rename(ctx, orgId, namespace, typ, newNamespace)
if err != nil {
return err
}
key := fmt.Sprint(orgId, namespace, typ)
if value, ok := kv.cache.Get(key); ok {
newKey := fmt.Sprint(orgId, newNamespace, typ)
kv.cache.SetDefault(newKey, value)
kv.cache.Delete(key)
}
return nil
}
func (kv *CachedKVStore) GetUnwrappedStore() SecretsKVStore {
return kv.store
}