mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Encryption: Refactor securejsondata.SecureJsonData to stop relying on global functions (#38865)
* Encryption: Add support to encrypt/decrypt sjd * Add datasources.Service as a proxy to datasources db operations * Encrypt ds.SecureJsonData before calling SQLStore * Move ds cache code into ds service * Fix tlsmanager tests * Fix pluginproxy tests * Remove some securejsondata.GetEncryptedJsonData usages * Add pluginsettings.Service as a proxy for plugin settings db operations * Add AlertNotificationService as a proxy for alert notification db operations * Remove some securejsondata.GetEncryptedJsonData usages * Remove more securejsondata.GetEncryptedJsonData usages * Fix lint errors * Minor fixes * Remove encryption global functions usages from ngalert * Fix lint errors * Minor fixes * Minor fixes * Remove securejsondata.DecryptedValue usage * Refactor the refactor * Remove securejsondata.DecryptedValue usage * Move securejsondata to migrations package * Move securejsondata to migrations package * Minor fix * Fix integration test * Fix integration tests * Undo undesired changes * Fix tests * Add context.Context into encryption methods * Fix tests * Fix tests * Fix tests * Trigger CI * Fix test * Add names to params of encryption service interface * Remove bus from CacheServiceImpl * Add logging * Add keys to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Add missing key to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Undo changes in markdown files * Fix formatting * Add context to secrets service * Rename decryptSecureJsonData to decryptSecureJsonDataFn * Name args in GetDecryptedValueFn * Add template back to NewAlertmanagerNotifier * Copy GetDecryptedValueFn to ngalert * Add logging to pluginsettings * Fix pluginsettings test Co-authored-by: Tania B <yalyna.ts@gmail.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
This commit is contained in:
committed by
GitHub
parent
da813877fb
commit
722c414fef
@@ -5,49 +5,17 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
|
||||
"xorm.io/xorm"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/components/securejsondata"
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetDataSources)
|
||||
bus.AddHandler("sql", GetDataSourcesByType)
|
||||
bus.AddHandler("sql", GetDataSource)
|
||||
bus.AddHandler("sql", AddDataSource)
|
||||
bus.AddHandler("sql", DeleteDataSource)
|
||||
bus.AddHandler("sql", UpdateDataSource)
|
||||
bus.AddHandler("sql", GetDefaultDataSource)
|
||||
}
|
||||
|
||||
// GetDataSource returns a datasource by org_id and either uid (preferred), id, or name.
|
||||
// Zero values (0, or "") should be used for the parameters that will not be queried.
|
||||
func (ss *SQLStore) GetDataSource(uid string, id int64, name string, orgID int64) (*models.DataSource, error) {
|
||||
query := &models.GetDataSourceQuery{
|
||||
Id: id,
|
||||
Uid: uid,
|
||||
Name: name,
|
||||
OrgId: orgID,
|
||||
}
|
||||
|
||||
if err := GetDataSource(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return query.Result, nil
|
||||
}
|
||||
|
||||
// GetDataSource adds a datasource to the query model by querying by org_id as well as
|
||||
// either uid (preferred), id, or name and is added to the bus.
|
||||
func GetDataSource(query *models.GetDataSourceQuery) error {
|
||||
func (ss *SQLStore) GetDataSource(query *models.GetDataSourceQuery) error {
|
||||
metrics.MDBDataSourceQueryByID.Inc()
|
||||
if query.OrgId == 0 || (query.Id == 0 && len(query.Name) == 0 && len(query.Uid) == 0) {
|
||||
return models.ErrDataSourceIdentifierNotSet
|
||||
@@ -67,7 +35,7 @@ func GetDataSource(query *models.GetDataSourceQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDataSources(query *models.GetDataSourcesQuery) error {
|
||||
func (ss *SQLStore) GetDataSources(query *models.GetDataSourcesQuery) error {
|
||||
var sess *xorm.Session
|
||||
if query.DataSourceLimit <= 0 {
|
||||
sess = x.Where("org_id=?", query.OrgId).Asc("name")
|
||||
@@ -80,7 +48,7 @@ func GetDataSources(query *models.GetDataSourcesQuery) error {
|
||||
}
|
||||
|
||||
// GetDataSourcesByType returns all datasources for a given type or an error if the specified type is an empty string
|
||||
func GetDataSourcesByType(query *models.GetDataSourcesByTypeQuery) error {
|
||||
func (ss *SQLStore) GetDataSourcesByType(query *models.GetDataSourcesByTypeQuery) error {
|
||||
if query.Type == "" {
|
||||
return fmt.Errorf("datasource type cannot be empty")
|
||||
}
|
||||
@@ -90,7 +58,7 @@ func GetDataSourcesByType(query *models.GetDataSourcesByTypeQuery) error {
|
||||
}
|
||||
|
||||
// GetDefaultDataSource is used to get the default datasource of organization
|
||||
func GetDefaultDataSource(query *models.GetDefaultDataSourceQuery) error {
|
||||
func (ss *SQLStore) GetDefaultDataSource(query *models.GetDefaultDataSourceQuery) error {
|
||||
datasource := models.DataSource{}
|
||||
|
||||
exists, err := x.Where("org_id=? AND is_default=?", query.OrgId, true).Get(&datasource)
|
||||
@@ -103,26 +71,9 @@ func GetDefaultDataSource(query *models.GetDefaultDataSourceQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteDataSource deletes a datasource by org_id and either uid (preferred), id, or name.
|
||||
// Zero values (0, or "") should be used for the parameters that will not be queried.
|
||||
func (ss *SQLStore) DeleteDataSource(uid string, id int64, name string, orgID int64) (int64, error) {
|
||||
cmd := &models.DeleteDataSourceCommand{
|
||||
ID: id,
|
||||
UID: uid,
|
||||
Name: name,
|
||||
OrgID: orgID,
|
||||
}
|
||||
|
||||
if err := DeleteDataSource(cmd); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return cmd.DeletedDatasourcesCount, nil
|
||||
}
|
||||
|
||||
// DeleteDataSource removes a datasource by org_id as well as either uid (preferred), id, or name
|
||||
// and is added to the bus.
|
||||
func DeleteDataSource(cmd *models.DeleteDataSourceCommand) error {
|
||||
func (ss *SQLStore) DeleteDataSource(cmd *models.DeleteDataSourceCommand) error {
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
makeQuery := func(sql string, p ...interface{}) {
|
||||
@@ -159,7 +110,7 @@ func DeleteDataSource(cmd *models.DeleteDataSourceCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func AddDataSource(cmd *models.AddDataSourceCommand) error {
|
||||
func (ss *SQLStore) AddDataSource(cmd *models.AddDataSourceCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
existing := models.DataSource{OrgId: cmd.OrgId, Name: cmd.Name}
|
||||
has, _ := sess.Get(&existing)
|
||||
@@ -195,7 +146,7 @@ func AddDataSource(cmd *models.AddDataSourceCommand) error {
|
||||
BasicAuthPassword: cmd.BasicAuthPassword,
|
||||
WithCredentials: cmd.WithCredentials,
|
||||
JsonData: cmd.JsonData,
|
||||
SecureJsonData: securejsondata.GetEncryptedJsonData(cmd.SecureJsonData),
|
||||
SecureJsonData: cmd.EncryptedSecureJsonData,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
Version: 1,
|
||||
@@ -237,7 +188,7 @@ func updateIsDefaultFlag(ds *models.DataSource, sess *DBSession) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDataSource(cmd *models.UpdateDataSourceCommand) error {
|
||||
func (ss *SQLStore) UpdateDataSource(cmd *models.UpdateDataSourceCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
if cmd.JsonData == nil {
|
||||
cmd.JsonData = simplejson.New()
|
||||
@@ -259,7 +210,7 @@ func UpdateDataSource(cmd *models.UpdateDataSourceCommand) error {
|
||||
BasicAuthPassword: cmd.BasicAuthPassword,
|
||||
WithCredentials: cmd.WithCredentials,
|
||||
JsonData: cmd.JsonData,
|
||||
SecureJsonData: securejsondata.GetEncryptedJsonData(cmd.SecureJsonData),
|
||||
SecureJsonData: cmd.EncryptedSecureJsonData,
|
||||
Updated: time.Now(),
|
||||
ReadOnly: cmd.ReadOnly,
|
||||
Version: cmd.Version + 1,
|
||||
|
||||
Reference in New Issue
Block a user