mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Config Array Syntax (#8204)
* refactor util encryption library so it doesn't have to import log * add util.SplitString to handle space and/or comma-separated config lines * go fmt
This commit is contained in:
committed by
Torkel Ödegaard
parent
d085aaad41
commit
b489e93d94
@@ -5,26 +5,25 @@ import (
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
)
|
||||
|
||||
const saltLength = 8
|
||||
|
||||
func Decrypt(payload []byte, secret string) []byte {
|
||||
func Decrypt(payload []byte, secret string) ([]byte, error) {
|
||||
salt := payload[:saltLength]
|
||||
key := encryptionKeyToBytes(secret, string(salt))
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
log.Fatal(4, err.Error())
|
||||
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 {
|
||||
log.Fatal(4, "payload too short")
|
||||
return nil, errors.New("payload too short")
|
||||
}
|
||||
iv := payload[saltLength : saltLength+aes.BlockSize]
|
||||
payload = payload[saltLength+aes.BlockSize:]
|
||||
@@ -33,16 +32,16 @@ func Decrypt(payload []byte, secret string) []byte {
|
||||
|
||||
// XORKeyStream can work in-place if the two arguments are the same.
|
||||
stream.XORKeyStream(payload, payload)
|
||||
return payload
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func Encrypt(payload []byte, secret string) []byte {
|
||||
func Encrypt(payload []byte, secret string) ([]byte, error) {
|
||||
salt := GetRandomString(saltLength)
|
||||
|
||||
key := encryptionKeyToBytes(secret, salt)
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
log.Fatal(4, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The IV needs to be unique, but not secure. Therefore it's common to
|
||||
@@ -51,13 +50,13 @@ func Encrypt(payload []byte, secret string) []byte {
|
||||
copy(ciphertext[:saltLength], []byte(salt))
|
||||
iv := ciphertext[saltLength : saltLength+aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
log.Fatal(4, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stream := cipher.NewCFBEncrypter(block, iv)
|
||||
stream.XORKeyStream(ciphertext[saltLength+aes.BlockSize:], payload)
|
||||
|
||||
return ciphertext
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
// Key needs to be 32bytes
|
||||
|
||||
@@ -18,9 +18,11 @@ func TestEncryption(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("When decrypting basic payload", t, func() {
|
||||
encrypted := Encrypt([]byte("grafana"), "1234")
|
||||
decrypted := Decrypt(encrypted, "1234")
|
||||
encrypted, encryptErr := Encrypt([]byte("grafana"), "1234")
|
||||
decrypted, decryptErr := Decrypt(encrypted, "1234")
|
||||
|
||||
So(encryptErr, ShouldBeNil)
|
||||
So(decryptErr, ShouldBeNil)
|
||||
So(string(decrypted), ShouldEqual, "grafana")
|
||||
})
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func StringsFallback2(val1 string, val2 string) string {
|
||||
return stringsFallback(val1, val2)
|
||||
}
|
||||
@@ -16,3 +20,11 @@ func stringsFallback(vals ...string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func SplitString(str string) []string {
|
||||
if len(str) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
return regexp.MustCompile("[, ]+").Split(str, -1)
|
||||
}
|
||||
|
||||
@@ -13,3 +13,14 @@ func TestStringsUtil(t *testing.T) {
|
||||
So(StringsFallback3("", "", "3"), ShouldEqual, "3")
|
||||
})
|
||||
}
|
||||
|
||||
func TestSplitString(t *testing.T) {
|
||||
Convey("Splits strings correctly", t, func() {
|
||||
So(SplitString(""), ShouldResemble, []string{})
|
||||
So(SplitString("test"), ShouldResemble, []string{"test"})
|
||||
So(SplitString("test1 test2 test3"), ShouldResemble, []string{"test1", "test2", "test3"})
|
||||
So(SplitString("test1,test2,test3"), ShouldResemble, []string{"test1", "test2", "test3"})
|
||||
So(SplitString("test1, test2, test3"), ShouldResemble, []string{"test1", "test2", "test3"})
|
||||
So(SplitString("test1 , test2 test3"), ShouldResemble, []string{"test1", "test2", "test3"})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user