-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvault_test.go
More file actions
162 lines (133 loc) · 4.15 KB
/
vault_test.go
File metadata and controls
162 lines (133 loc) · 4.15 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package vcrypt
import (
"bytes"
"crypto/rand"
"io"
"testing"
"github.com/vcrypt/vcrypt/internal/test"
"github.com/vcrypt/vcrypt/secret"
"golang.org/x/crypto/openpgp"
)
var (
twoManVault, twoManSecret = buildVault(twoManPlan, twoManDriver)
twoPartyVault, twoPartySecret = buildVault(twoPartyPlan, twoPartyDriver)
diamondVault, diamondSecret = buildVault(diamondPlan, diamondDriver)
dnsSecVault, dnsSecSecret = buildVault(dnsSecPlan, dnsSecDriver)
acmeBankVault, acmeBankSecret = buildVault(acmeBankPlan, acmeBankDriver)
twoManDriver = test.Driver(map[string][]byte{
"op 1 secret": []byte("key #1"),
"op 2 secret": []byte("key #2"),
})
twoPartyDriver = test.Driver(map[string][]byte{
"party 1 password 2": []byte("step #3 secret"),
"party 2 password": []byte("step #2 secret"),
"party 1 password 1": []byte("step #1 secret"),
})
diamondDriver = test.Driver(map[string][]byte{
"step 3 password": []byte("step #3 password"),
"step 2a password": []byte("step #2a password"),
"step 2b password": []byte("step #2b password"),
"step 1 password": []byte("step #1 password"),
})
dnsSecDriver = test.Driver(map[string][]byte{
test.Users["alice"].OpenPGPKey.KeyID: mustOpenPGPKey(test.Users["alice"].OpenPGPKey.Private),
test.Users["bob"].OpenPGPKey.KeyID: mustOpenPGPKey(test.Users["bob"].OpenPGPKey.Private),
test.Users["claire"].OpenPGPKey.KeyID: mustOpenPGPKey(test.Users["claire"].OpenPGPKey.Private),
test.Users["david"].OpenPGPKey.KeyID: mustOpenPGPKey(test.Users["david"].OpenPGPKey.Private),
test.Users["emily"].OpenPGPKey.KeyID: mustOpenPGPKey(test.Users["emily"].OpenPGPKey.Private),
test.Users["frank"].OpenPGPKey.KeyID: mustOpenPGPKey(test.Users["frank"].OpenPGPKey.Private),
test.Users["gloria"].OpenPGPKey.KeyID: mustOpenPGPKey(test.Users["gloria"].OpenPGPKey.Private),
})
acmeBankDriver = test.Driver(map[string][]byte{})
)
func TestVault(t *testing.T) {
tests := []struct {
vault *Vault
drv Driver
secret []byte
}{
{twoManVault, twoManDriver, twoManSecret},
{twoPartyVault, twoPartyDriver, twoPartySecret},
{diamondVault, diamondDriver, diamondSecret},
{dnsSecVault, dnsSecDriver, dnsSecSecret},
{acmeBankVault, acmeBankDriver, acmeBankSecret},
}
for _, test := range tests {
var got bytes.Buffer
if _, err := test.vault.Unlock(&got, test.drv); err != nil {
t.Error(err)
continue
}
want := test.secret
if !bytes.Equal(want, got.Bytes()) {
t.Errorf("vault unlocked bad secret: want %v, got %v", want, got.Bytes())
}
}
}
type skipDriver struct {
test.Driver
skippable map[string]bool
}
func (d skipDriver) LoadSecret(sec secret.Secret) ([][]byte, bool, error) {
if _, ok := d.Driver[sec.Comment()]; !ok {
return [][]byte{[]byte{}}, true, nil
}
return d.Driver.LoadSecret(sec)
}
func TestMultiPassVault(t *testing.T) {
vault := dnsSecVault
want, got := dnsSecSecret, bytes.Buffer{}
driver := skipDriver{
Driver: test.Driver{},
}
step := 0
for _, userdata := range test.Users {
key := userdata.OpenPGPKey
driver.Driver[key.KeyID] = mustOpenPGPKey(key.Private)
ok, err := vault.Unlock(&got, driver)
if err != nil {
t.Fatal(err)
}
if step++; step == 5 {
if !ok {
t.Fatalf("vault failed to unlock after 5th pass")
}
break
}
if ok {
t.Fatalf("vault opened too early")
}
if len(got.Bytes()) > 0 {
t.Fatalf("vault unlock wrote secret prior to successful unlock")
}
delete(driver.Driver, key.KeyID)
}
if !bytes.Equal(want, got.Bytes()) {
t.Errorf("vault unlocked bad secret: want %v, got %v", want, got.Bytes())
}
}
func buildVault(plan *Plan, drv Driver) (*Vault, []byte) {
secret := make([]byte, 256)
if _, err := io.ReadFull(rand.Reader, secret); err != nil {
panic(err)
}
vault, err := NewVault(plan, plan.Comment())
if err != nil {
panic(err)
}
if err := vault.Lock(bytes.NewBuffer(secret), drv); err != nil {
panic(err)
}
return vault, secret
}
func mustOpenPGPKey(keyPEM string) []byte {
el, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(keyPEM))
if err != nil {
panic(err)
}
buf := bytes.NewBuffer(nil)
if err := el[0].SerializePrivate(buf, nil); err != nil {
panic(err)
}
return buf.Bytes()
}