-
Notifications
You must be signed in to change notification settings - Fork 34
/
rotate_test.go
138 lines (118 loc) · 3.13 KB
/
rotate_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package sneaker
import (
"bytes"
"io/ioutil"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/s3"
)
func TestRotate(t *testing.T) {
oldKey := func() []byte {
return make([]byte, 32)
}
newKey := func() []byte {
k := oldKey()
k[0] = 100
return k
}
oldCiphertext := []byte{
0x00, 0x00, 0x00, 0x0d, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65,
0x64, 0x20, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xba, 0xcf, 0x29, 0x4e, 0x6d, 0x09, 0x18,
0x4e, 0x66, 0x6e, 0xb1, 0xb6, 0xc9, 0x87, 0x65, 0xcc, 0xe1, 0x06, 0x8c,
0xbf, 0x7f, 0xdd, 0x5d, 0x70, 0x4e, 0x3d, 0xbf, 0xd5, 0x44, 0xec,
}
fakeS3 := &FakeS3{
ListOutputs: []s3.ListObjectsOutput{
{
Contents: []*s3.Object{
{
Key: aws.String("secrets/weeble.txt"),
ETag: aws.String(`"etag1"`),
Size: aws.Int64(1004),
LastModified: aws.Time(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)),
},
},
},
},
GetOutputs: []s3.GetObjectOutput{
{
Body: ioutil.NopCloser(bytes.NewReader(oldCiphertext)),
},
},
PutOutputs: []s3.PutObjectOutput{
{},
{},
},
}
fakeKMS := &FakeKMS{
DecryptOutputs: []kms.DecryptOutput{
{
KeyId: aws.String("key1"),
Plaintext: oldKey(),
},
},
GenerateOutputs: []kms.GenerateDataKeyOutput{
{
CiphertextBlob: []byte("encrypted new key"),
KeyId: aws.String("key1"),
Plaintext: newKey(),
},
},
}
man := Manager{
Objects: fakeS3,
Envelope: Envelope{
KMS: fakeKMS,
},
KeyId: "key1",
Bucket: "bucket",
Prefix: "secrets",
}
if err := man.Rotate("", nil); err != nil {
t.Fatal(err)
}
// KMS request
genReq := fakeKMS.GenerateInputs[0]
if v, want := *genReq.KeyId, "key1"; v != want {
t.Errorf("Key ID was %q, but expected %q", v, want)
}
if v, want := *genReq.KeySpec, "AES_256"; v != want {
t.Errorf("Key spec was %v, but expected %v", v, want)
}
putReq := fakeS3.PutInputs[0]
if v, want := *putReq.Bucket, "bucket"; v != want {
t.Errorf("Bucket was %q, but expected %q", v, want)
}
if v, want := *putReq.Key, "secrets/weeble.txt"; v != want {
t.Errorf("Key was %q, but expected %q", v, want)
}
if v, want := *putReq.ContentLength, int64(63); v != want {
t.Errorf("ContentLength was %d, but expected %d", v, want)
}
if v, want := *putReq.ContentType, "application/octet-stream"; v != want {
t.Errorf("ContentType was %q, but expected %q", v, want)
}
actual, err := ioutil.ReadAll(putReq.Body)
if err != nil {
t.Fatal(err)
}
header := actual[:4]
if v := []byte{0x00, 0x00, 0x00, 0x11}; !bytes.Equal(header, v) {
t.Errorf("Header was %x but expected %x", header, v)
}
blob := actual[4 : 17+4]
if v := []byte("encrypted new key"); !bytes.Equal(blob, v) {
t.Errorf("Blob was %x but expected %x", blob, v)
}
ciphertext := actual[17+4:]
plaintext, err := decrypt(newKey(), ciphertext, []byte("key1"))
if err != nil {
t.Fatal(err)
}
if v, want := string(plaintext), "this is a test"; v != want {
t.Errorf("Plaintext was %x but expected %x", v, want)
}
}