This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkms_test.go
98 lines (77 loc) · 2.67 KB
/
kms_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
const mockKmsKey = `Q1PuWtB1E7F1sLpvfBGjL+ZuH+fSCOvMDqTyRQE4GTg=`
type MockKmsClient struct {
}
func (k *MockKmsClient) GenerateDataKey(keyID string) (*[32]byte, []byte, error) {
if keyID != "123" {
return nil, nil, errors.New("Expected keyID=123")
}
key, err := decode(mockKmsKey)
if err != nil {
return nil, nil, err
}
dataKey, err := asKey(key)
if err != nil {
return nil, nil, err
}
return dataKey, []byte(mockKmsKey), nil
}
func (k *MockKmsClient) Decrypt(data []byte) (*[32]byte, error) {
key, err := decode(string(data))
if err != nil {
return nil, err
}
dataKey, err := asKey(key)
if err != nil {
return nil, err
}
return dataKey, nil
}
func newMockKmsClient() *MockKmsClient {
return &MockKmsClient{}
}
func TestKms(t *testing.T) {
client := newMockKmsClient()
encryption := newKmsEncryptionStrategy(client, "123")
decryption := newKmsDecryptionStrategy(client)
envelope, err := encryption.Encrypt([]byte("secret"))
assert.Nil(t, err)
plaintext, err := decryption.Decrypt(envelope)
assert.Nil(t, err)
assert.Equal(t, "secret", string(plaintext))
}
func TestCompositeDecryptionStrategy(t *testing.T) {
composite := newCompositeDecryptionStrategy()
composite.Add("KMS", newKmsDecryptionStrategy(newMockKmsClient()))
composite.Add("NACL", newKeyDecryptionStrategy(
pemRead("./resources/test/keys/config-public-key.pem"),
pemRead("./resources/test/keys/master-private-key.pem")))
{
plaintext, err := composite.Decrypt("ENC[KMS,RP+BAwEBCmttc1BheWxvYWQB/4IAAQMBEEVuY3J5cHRlZERhdGFLZXkBCgABBU5vbmNlAf+EAAEHTWVzc2FnZQEKAAAAGf+DAQEBCVsyNF11aW50OAH/hAABBgEwAABw/4IBLFExUHVXdEIxRTdGMXNMcHZmQkdqTCtadUgrZlNDT3ZNRHFUeVJRRTRHVGc9ARgr/502fv/vQP+S/5H/k//gOf/gWDNh/53/3in/uf/L/5r/mTxbARYoewY+qb+skiPKwGUnT/2GADtui80vAA==]")
assert.Nil(t, err)
assert.Equal(t, "secret", string(plaintext))
}
{
plaintext, err := composite.Decrypt("ENC[NACL,fB7RSmpONiUGzaHtd8URiTSKqfBhor6BsJLSQErHH9NSgLTnxNLF60YS8ZT2IQ==]")
assert.Nil(t, err)
assert.Equal(t, "secret", string(plaintext))
}
{
plaintext, err := composite.Decrypt("ENC[ACL,fB7RSmpONiUGzaHtd8URiTSKqfBhor6BsJLSQErHH9NSgLTnxNLF60YS8ZT2IQ==]")
assert.Nil(t, plaintext)
assert.NotNil(t, err)
assert.Equal(t, "Not configured for decrypting ENC[,..] values", err.Error())
}
}
func TestUnsupportedDecryptionStrategy(t *testing.T) {
composite := newCompositeDecryptionStrategy()
plaintext, err := composite.Decrypt("ENC[NACL,fB7RSmpONiUGzaHtd8URiTSKqfBhor6BsJLSQErHH9NSgLTnxNLF60YS8ZT2IQ==]")
assert.Nil(t, plaintext)
assert.NotNil(t, err)
assert.Equal(t, "Not configured for decrypting ENC[NACL,..] values", err.Error())
}