Encryption: Start move to data keys
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package secrets
|
||||
|
||||
// EncryptedJSON is used to store encrypted data (for example in data_source table). Only values are separately
|
||||
// encrypted.
|
||||
type EncryptedJSON struct {
|
||||
s *Secrets
|
||||
Map map[string][]byte
|
||||
}
|
||||
|
||||
// DecryptedValue returns single decrypted value from EncryptedJSON. Similar to normal map access second return value
|
||||
// is true if the key exists and false if not.
|
||||
func (j EncryptedJSON) DecryptedValue(key string) (string, bool) {
|
||||
if value, ok := j.Map[key]; ok {
|
||||
decryptedData, err := j.s.Decrypt(value, "")
|
||||
if err != nil {
|
||||
logger.Error("could not decrypt value", "key", key, "err", err.Error())
|
||||
return "", false
|
||||
}
|
||||
return string(decryptedData), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Decrypt returns map of the same type but where the all the values are decrypted. Opposite of what
|
||||
// EncryptJSONMap is doing.
|
||||
func (j EncryptedJSON) DecryptJSONMap() (map[string]string, error) {
|
||||
decrypted := make(map[string]string)
|
||||
for key, data := range j.Map {
|
||||
decryptedData, err := j.s.Decrypt(data, "")
|
||||
if err != nil {
|
||||
logger.Error("could not decrypt map", "err", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decrypted[key] = string(decryptedData)
|
||||
}
|
||||
return decrypted, nil
|
||||
}
|
||||
|
||||
// EncryptJSONMap returns map where all values are encrypted.
|
||||
func (s *Secrets) EncryptJSONMap(m map[string]string) (EncryptedJSON, error) {
|
||||
encrypted := EncryptedJSON{
|
||||
s: s,
|
||||
Map: make(map[string][]byte),
|
||||
}
|
||||
for key, data := range m {
|
||||
encryptedData, err := s.Encrypt([]byte(data), "")
|
||||
if err != nil {
|
||||
return EncryptedJSON{}, err
|
||||
}
|
||||
|
||||
encrypted.Map[key] = encryptedData
|
||||
}
|
||||
return encrypted, nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"golang.org/x/crypto/pbkdf2"
|
||||
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
type secretKey struct {
|
||||
key func() []byte
|
||||
}
|
||||
|
||||
func (s *secretKey) Encrypt(blob []byte) ([]byte, error) {
|
||||
return encrypt(blob, s.key())
|
||||
}
|
||||
|
||||
func (s *secretKey) Decrypt(blob []byte) ([]byte, error) {
|
||||
return decrypt(blob, s.key())
|
||||
}
|
||||
|
||||
const saltLength = 8
|
||||
|
||||
// Decrypt decrypts a payload with a given secret.
|
||||
func decrypt(payload, secret []byte) ([]byte, error) {
|
||||
salt := payload[:saltLength]
|
||||
key, err := encryptionKeyToBytes(secret, salt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The IV needs to be unique, but not secure. Therefore it's common to
|
||||
// include it at the beginning of the ciphertext.
|
||||
if len(payload) < aes.BlockSize {
|
||||
return nil, errors.New("payload too short")
|
||||
}
|
||||
iv := payload[saltLength : saltLength+aes.BlockSize]
|
||||
payload = payload[saltLength+aes.BlockSize:]
|
||||
payloadDst := make([]byte, len(payload))
|
||||
|
||||
stream := cipher.NewCFBDecrypter(block, iv)
|
||||
|
||||
// XORKeyStream can work in-place if the two arguments are the same.
|
||||
stream.XORKeyStream(payloadDst, payload)
|
||||
return payloadDst, nil
|
||||
}
|
||||
|
||||
// Encrypt encrypts a payload with a given secret.
|
||||
func encrypt(payload, secret []byte) ([]byte, error) {
|
||||
salt, err := util.GetRandomString(saltLength)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
key, err := encryptionKeyToBytes(secret, []byte(salt))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The IV needs to be unique, but not secure. Therefore it's common to
|
||||
// include it at the beginning of the ciphertext.
|
||||
ciphertext := make([]byte, saltLength+aes.BlockSize+len(payload))
|
||||
copy(ciphertext[:saltLength], salt)
|
||||
iv := ciphertext[saltLength : saltLength+aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stream := cipher.NewCFBEncrypter(block, iv)
|
||||
stream.XORKeyStream(ciphertext[saltLength+aes.BlockSize:], payload)
|
||||
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
// Key needs to be 32bytes
|
||||
func encryptionKeyToBytes(secret, salt []byte) ([]byte, error) {
|
||||
return pbkdf2.Key(secret, salt, 10000, 32, sha256.New), nil
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
var logger = log.New("secrets")
|
||||
|
||||
type Secrets struct {
|
||||
store *sqlstore.SqlStore `inject:""`
|
||||
|
||||
providers map[string]Provider
|
||||
dataKeyCache map[string]dataKeyCacheItem
|
||||
}
|
||||
|
||||
type dataKeyCacheItem struct {
|
||||
expiry time.Time
|
||||
dataKey []byte
|
||||
}
|
||||
|
||||
type Provider interface {
|
||||
Encrypt(blob []byte) ([]byte, error)
|
||||
Decrypt(blob []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
func (s *Secrets) Init() error {
|
||||
s.providers = map[string]Provider{
|
||||
"": &secretKey{
|
||||
key: func() []byte {
|
||||
return []byte(setting.SecretKey)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Secrets) Encrypt(payload []byte, key string) ([]byte, error) {
|
||||
dataKey, err := s.dataKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b64 := base64.StdEncoding
|
||||
prefix := make([]byte, b64.EncodedLen(len(key))+2)
|
||||
b64.Encode(prefix[1:], []byte(key))
|
||||
prefix[0] = '#'
|
||||
prefix[len(prefix)-1] = '#'
|
||||
|
||||
blob := make([]byte, len(prefix)+len(payload))
|
||||
copy(blob, prefix)
|
||||
copy(blob[len(prefix):], payload)
|
||||
|
||||
return encrypt(blob, dataKey)
|
||||
}
|
||||
|
||||
func (s *Secrets) Decrypt(payload []byte, key string) ([]byte, error) {
|
||||
dataKey, err := s.dataKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return decrypt(payload, dataKey)
|
||||
}
|
||||
|
||||
func (s *Secrets) dataKey(key string) ([]byte, error) {
|
||||
if key == "" {
|
||||
return []byte(setting.SecretKey), nil
|
||||
}
|
||||
|
||||
if item, exists := s.dataKeyCache[key]; exists {
|
||||
if item.expiry.Before(time.Now()) && !item.expiry.IsZero() {
|
||||
delete(s.dataKeyCache, key)
|
||||
} else {
|
||||
return item.dataKey, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 1. get encrypted data key from database
|
||||
dataKey, err := s.store.GetDataKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. decrypt data key
|
||||
provider, exists := s.providers[dataKey.Provider]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("could not find encryption provider '%s'", dataKey.Provider)
|
||||
}
|
||||
|
||||
decrypted, err := provider.Decrypt(dataKey.EncryptedData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 3. cache data key
|
||||
s.dataKeyCache[key] = dataKeyCacheItem{
|
||||
expiry: time.Now().Add(15 * time.Minute),
|
||||
dataKey: decrypted,
|
||||
}
|
||||
|
||||
return decrypted, nil
|
||||
}
|
||||
Reference in New Issue
Block a user