2022-06-09 12:19:27 -05:00
|
|
|
package kvstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
smp "github.com/grafana/grafana/pkg/plugins/backendplugin/secretsmanagerplugin"
|
2022-06-27 11:23:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2022-06-09 12:19:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/secrets"
|
|
|
|
)
|
|
|
|
|
|
|
|
// secretsKVStorePlugin provides a key/value store backed by the Grafana plugin gRPC interface
|
|
|
|
type secretsKVStorePlugin struct {
|
|
|
|
log log.Logger
|
|
|
|
secretsPlugin smp.SecretsManagerPlugin
|
|
|
|
secretsService secrets.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an item from the store
|
|
|
|
func (kv *secretsKVStorePlugin) Get(ctx context.Context, orgId int64, namespace string, typ string) (string, bool, error) {
|
2022-06-14 10:53:04 -05:00
|
|
|
req := &smp.GetSecretRequest{
|
2022-06-09 12:19:27 -05:00
|
|
|
KeyDescriptor: &smp.Key{
|
|
|
|
OrgId: orgId,
|
|
|
|
Namespace: namespace,
|
|
|
|
Type: typ,
|
|
|
|
},
|
|
|
|
}
|
2022-06-14 10:53:04 -05:00
|
|
|
res, err := kv.secretsPlugin.GetSecret(ctx, req)
|
2022-06-09 12:19:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
2022-06-14 10:53:04 -05:00
|
|
|
} else if res.UserFriendlyError != "" {
|
2022-06-16 11:26:57 -05:00
|
|
|
err = wrapUserFriendlySecretError(res.UserFriendlyError)
|
2022-06-09 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.DecryptedValue, res.Exists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set an item in the store
|
|
|
|
func (kv *secretsKVStorePlugin) Set(ctx context.Context, orgId int64, namespace string, typ string, value string) error {
|
2022-06-14 10:53:04 -05:00
|
|
|
req := &smp.SetSecretRequest{
|
2022-06-09 12:19:27 -05:00
|
|
|
KeyDescriptor: &smp.Key{
|
|
|
|
OrgId: orgId,
|
|
|
|
Namespace: namespace,
|
|
|
|
Type: typ,
|
|
|
|
},
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:53:04 -05:00
|
|
|
res, err := kv.secretsPlugin.SetSecret(ctx, req)
|
|
|
|
if err == nil && res.UserFriendlyError != "" {
|
2022-06-16 11:26:57 -05:00
|
|
|
err = wrapUserFriendlySecretError(res.UserFriendlyError)
|
2022-06-09 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Del deletes an item from the store.
|
|
|
|
func (kv *secretsKVStorePlugin) Del(ctx context.Context, orgId int64, namespace string, typ string) error {
|
2022-06-14 10:53:04 -05:00
|
|
|
req := &smp.DeleteSecretRequest{
|
2022-06-09 12:19:27 -05:00
|
|
|
KeyDescriptor: &smp.Key{
|
|
|
|
OrgId: orgId,
|
|
|
|
Namespace: namespace,
|
|
|
|
Type: typ,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:53:04 -05:00
|
|
|
res, err := kv.secretsPlugin.DeleteSecret(ctx, req)
|
|
|
|
if err == nil && res.UserFriendlyError != "" {
|
2022-06-16 11:26:57 -05:00
|
|
|
err = wrapUserFriendlySecretError(res.UserFriendlyError)
|
2022-06-09 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys get all keys for a given namespace. To query for all
|
|
|
|
// organizations the constant 'kvstore.AllOrganizations' can be passed as orgId.
|
|
|
|
func (kv *secretsKVStorePlugin) Keys(ctx context.Context, orgId int64, namespace string, typ string) ([]Key, error) {
|
2022-06-14 10:53:04 -05:00
|
|
|
req := &smp.ListSecretsRequest{
|
2022-06-09 12:19:27 -05:00
|
|
|
KeyDescriptor: &smp.Key{
|
|
|
|
OrgId: orgId,
|
|
|
|
Namespace: namespace,
|
|
|
|
Type: typ,
|
|
|
|
},
|
|
|
|
AllOrganizations: orgId == AllOrganizations,
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:53:04 -05:00
|
|
|
res, err := kv.secretsPlugin.ListSecrets(ctx, req)
|
2022-06-09 12:19:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-14 10:53:04 -05:00
|
|
|
} else if res.UserFriendlyError != "" {
|
2022-06-16 11:26:57 -05:00
|
|
|
err = wrapUserFriendlySecretError(res.UserFriendlyError)
|
2022-06-09 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return parseKeys(res.Keys), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename an item in the store
|
|
|
|
func (kv *secretsKVStorePlugin) Rename(ctx context.Context, orgId int64, namespace string, typ string, newNamespace string) error {
|
2022-06-14 10:53:04 -05:00
|
|
|
req := &smp.RenameSecretRequest{
|
2022-06-09 12:19:27 -05:00
|
|
|
KeyDescriptor: &smp.Key{
|
|
|
|
OrgId: orgId,
|
|
|
|
Namespace: namespace,
|
|
|
|
Type: typ,
|
|
|
|
},
|
|
|
|
NewNamespace: newNamespace,
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:53:04 -05:00
|
|
|
res, err := kv.secretsPlugin.RenameSecret(ctx, req)
|
|
|
|
if err == nil && res.UserFriendlyError != "" {
|
2022-06-16 11:26:57 -05:00
|
|
|
err = wrapUserFriendlySecretError(res.UserFriendlyError)
|
2022-06-09 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseKeys(keys []*smp.Key) []Key {
|
|
|
|
var newKeys []Key
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
newKey := Key{OrgId: k.OrgId, Namespace: k.Namespace, Type: k.Type}
|
|
|
|
newKeys = append(newKeys, newKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newKeys
|
|
|
|
}
|
2022-06-16 11:26:57 -05:00
|
|
|
|
2022-06-27 11:23:15 -05:00
|
|
|
func wrapUserFriendlySecretError(ufe string) datasources.ErrDatasourceSecretsPluginUserFriendly {
|
|
|
|
return datasources.ErrDatasourceSecretsPluginUserFriendly{Err: ufe}
|
2022-06-16 11:26:57 -05:00
|
|
|
}
|