Encryption: Add support for decrypting ciphertexts with algorithm metadata (#49312)

This commit is contained in:
Joan López de la Franca Beltran
2022-05-20 17:11:51 +02:00
committed by GitHub
parent 258b3ab18b
commit 15605b6c80
2 changed files with 80 additions and 4 deletions
@@ -37,6 +37,32 @@ func TestEncryption(t *testing.T) {
_, err := svc.Decrypt(context.Background(), []byte(""), "1234")
require.Error(t, err)
assert.Equal(t, "unable to compute salt", err.Error())
assert.Equal(t, "unable to derive encryption algorithm", err.Error())
})
t.Run("decrypting ciphertext with aes-gcm as encryption algorithm should return error", func(t *testing.T) {
// Raw slice of bytes that corresponds to the following ciphertext:
// - 'grafana' as payload
// - '1234' as secret
// - 'aes-gcm' as encryption algorithm
// With no encryption algorithm metadata.
ciphertext := []byte{42, 89, 87, 86, 122, 76, 87, 100, 106, 98, 81, 42, 48, 99, 55, 50, 51, 48, 83, 66, 20, 99, 47, 238, 61, 44, 129, 125, 14, 37, 162, 230, 47, 31, 104, 70, 144, 223, 26, 51, 180, 17, 76, 52, 36, 93, 17, 203, 99, 158, 219, 102, 74, 173, 74}
_, err := svc.Decrypt(context.Background(), ciphertext, "1234")
require.Error(t, err)
assert.Equal(t, "unsupported encryption algorithm", err.Error())
})
t.Run("decrypting ciphertext with aes-cfb as encryption algorithm do not fail", func(t *testing.T) {
// Raw slice of bytes that corresponds to the following ciphertext:
// - 'grafana' as payload
// - '1234' as secret
// - 'aes-cfb' as encryption algorithm
// With no encryption algorithm metadata.
ciphertext := []byte{42, 89, 87, 86, 122, 76, 87, 78, 109, 89, 103, 42, 73, 71, 50, 57, 121, 110, 90, 109, 115, 23, 237, 13, 130, 188, 151, 118, 98, 103, 80, 209, 79, 143, 22, 122, 44, 40, 102, 41, 136, 16, 27}
decrypted, err := svc.Decrypt(context.Background(), ciphertext, "1234")
require.NoError(t, err)
assert.Equal(t, []byte("grafana"), decrypted)
})
}