2021-10-07 16:33:50 +02:00
|
|
|
package ualert
|
2016-11-12 23:26:33 +01:00
|
|
|
|
|
|
|
|
import (
|
2021-10-26 17:36:24 +02:00
|
|
|
"os"
|
|
|
|
|
|
2019-05-13 14:45:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2016-11-12 23:26:33 +01:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-15 11:11:17 +02:00
|
|
|
// SecureJsonData is used to store encrypted data (for example in data_source table). Only values are separately
|
|
|
|
|
// encrypted.
|
2016-11-12 23:26:33 +01:00
|
|
|
type SecureJsonData map[string][]byte
|
|
|
|
|
|
2021-11-08 17:56:56 +01:00
|
|
|
var seclogger = log.New("securejsondata")
|
|
|
|
|
|
2019-04-15 11:11:17 +02:00
|
|
|
// DecryptedValue returns single decrypted value from SecureJsonData. Similar to normal map access second return value
|
|
|
|
|
// is true if the key exists and false if not.
|
|
|
|
|
func (s SecureJsonData) DecryptedValue(key string) (string, bool) {
|
|
|
|
|
if value, ok := s[key]; ok {
|
|
|
|
|
decryptedData, err := util.Decrypt(value, setting.SecretKey)
|
|
|
|
|
if err != nil {
|
2021-11-08 17:56:56 +01:00
|
|
|
seclogger.Error(err.Error())
|
2021-10-26 17:36:24 +02:00
|
|
|
os.Exit(1)
|
2019-04-15 11:11:17 +02:00
|
|
|
}
|
|
|
|
|
return string(decryptedData), true
|
|
|
|
|
}
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decrypt returns map of the same type but where the all the values are decrypted. Opposite of what
|
|
|
|
|
// GetEncryptedJsonData is doing.
|
2016-11-12 23:26:33 +01:00
|
|
|
func (s SecureJsonData) Decrypt() map[string]string {
|
|
|
|
|
decrypted := make(map[string]string)
|
|
|
|
|
for key, data := range s {
|
2017-04-25 03:14:29 -04:00
|
|
|
decryptedData, err := util.Decrypt(data, setting.SecretKey)
|
|
|
|
|
if err != nil {
|
2021-11-08 17:56:56 +01:00
|
|
|
seclogger.Error(err.Error())
|
2021-10-26 17:36:24 +02:00
|
|
|
os.Exit(1)
|
2017-04-25 03:14:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted[key] = string(decryptedData)
|
2016-11-12 23:26:33 +01:00
|
|
|
}
|
|
|
|
|
return decrypted
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 11:11:17 +02:00
|
|
|
// GetEncryptedJsonData returns map where all keys are encrypted.
|
2016-11-12 23:26:33 +01:00
|
|
|
func GetEncryptedJsonData(sjd map[string]string) SecureJsonData {
|
|
|
|
|
encrypted := make(SecureJsonData)
|
|
|
|
|
for key, data := range sjd {
|
2017-04-25 03:14:29 -04:00
|
|
|
encryptedData, err := util.Encrypt([]byte(data), setting.SecretKey)
|
|
|
|
|
if err != nil {
|
2021-11-08 17:56:56 +01:00
|
|
|
seclogger.Error(err.Error())
|
2021-10-26 17:36:24 +02:00
|
|
|
os.Exit(1)
|
2017-04-25 03:14:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encrypted[key] = encryptedData
|
2016-11-12 23:26:33 +01:00
|
|
|
}
|
|
|
|
|
return encrypted
|
|
|
|
|
}
|