-
Notifications
You must be signed in to change notification settings - Fork 30
/
main_test.go
254 lines (202 loc) · 5.09 KB
/
main_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"bytes"
"errors"
"io"
"os"
"time"
"github.com/miquella/vaulted/lib"
"github.com/miquella/vaulted/lib/legacy"
)
func CaptureStdout(f func()) []byte {
// Save/restore stdout
stdout := os.Stdout
defer func() {
os.Stdout = stdout
}()
// Capture stdout
r, w, _ := os.Pipe()
defer w.Close()
os.Stdout = w
captured := make(chan []byte)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
captured <- buf.Bytes()
close(captured)
}()
f()
w.Close()
return <-captured
}
func WriteStdin(b []byte, f func()) {
// Save/restore stdin
stdin := os.Stdin
defer func() {
os.Stdin = stdin
}()
// Write to stdin
r, w, _ := os.Pipe()
defer r.Close()
os.Stdin = r
go func() {
vr := bytes.NewReader(b)
io.Copy(w, vr)
w.Close()
}()
f()
}
func NewTestStore() *TestStore {
return &TestStore{
Passwords: make(map[string]string),
Vaults: make(map[string]*vaulted.Vault),
Sessions: make(map[string]*vaulted.Session),
}
}
type TestStore struct {
Passwords map[string]string
Vaults map[string]*vaulted.Vault
Sessions map[string]*vaulted.Session
LegacyPassword string
LegacyEnvironments map[string]legacy.Environment
}
func (ts TestStore) GetPassword(operation vaulted.Operation, name string) (string, error) {
switch operation {
case vaulted.OpenOperation:
return "prompted open password", nil
case vaulted.SealOperation:
return "prompted seal password", nil
default:
return "", errors.New("Unknown operation")
}
}
func (ts TestStore) GetMFAToken(name string) (string, error) {
return "123456", nil
}
func (ts TestStore) Steward() vaulted.Steward {
return ts
}
func (ts TestStore) VaultExists(name string) bool {
_, exists := ts.Vaults[name]
return exists
}
func (ts TestStore) ListVaults() ([]string, error) {
var vaults []string
for name := range ts.Vaults {
vaults = append(vaults, name)
}
return vaults, nil
}
func (ts TestStore) SealVault(vault *vaulted.Vault, name string) error {
return ts.SealVaultWithPassword(vault, name, "sealed")
}
func (ts TestStore) SealVaultWithPassword(vault *vaulted.Vault, name, password string) error {
ts.Passwords[name] = password
ts.Vaults[name] = cloneVault(vault)
return nil
}
func (ts TestStore) OpenVault(name string) (*vaulted.Vault, string, error) {
return ts.OpenVaultWithPassword(name, "prompted password")
}
func (ts TestStore) OpenVaultWithPassword(name, password string) (*vaulted.Vault, string, error) {
if !ts.VaultExists(name) {
return nil, "", os.ErrNotExist
}
return cloneVault(ts.Vaults[name]), ts.Passwords[name], nil
}
func (ts TestStore) RemoveVault(name string) error {
if !ts.VaultExists(name) {
return os.ErrNotExist
}
delete(ts.Passwords, name)
delete(ts.Vaults, name)
return nil
}
func (ts TestStore) GetSession(vault *vaulted.Vault, name, password string) (*vaulted.Session, error) {
if !ts.VaultExists(name) {
return nil, os.ErrNotExist
}
s := &vaulted.Session{
Expiration: time.Unix(1136239445, 0),
Vars: make(map[string]string),
SSHKeys: make(map[string]string),
SSHOptions: &vaulted.SSHOptions{},
}
if _, exists := ts.Sessions[name]; exists {
cachedSession := ts.Sessions[name]
s.Name = cachedSession.Name
s.Expiration = cachedSession.Expiration
creds := *cachedSession.AWSCreds
s.AWSCreds = &creds
for key, value := range cachedSession.Vars {
s.Vars[key] = value
}
for key, value := range cachedSession.SSHKeys {
s.SSHKeys[key] = value
}
if cachedSession.SSHOptions != nil {
s.SSHOptions = cachedSession.SSHOptions
}
} else {
s.Name = name
for key, value := range vault.Vars {
s.Vars[key] = value
}
for key, value := range vault.SSHKeys {
s.SSHKeys[key] = value
}
if vault.SSHOptions != nil {
s.SSHOptions = vault.SSHOptions
}
}
return s, nil
}
func (ts TestStore) CreateSession(vault *vaulted.Vault, name, password string) (*vaulted.Session, error) {
if !ts.VaultExists(name) {
return nil, os.ErrNotExist
}
s := &vaulted.Session{
Expiration: time.Unix(1136239446, 0),
Vars: make(map[string]string),
SSHKeys: make(map[string]string),
SSHOptions: &vaulted.SSHOptions{},
}
s.Name = name
for key, value := range vault.Vars {
s.Vars[key] = value
}
for key, value := range vault.SSHKeys {
s.SSHKeys[key] = value
}
return s, nil
}
func (ts TestStore) OpenLegacyVault() (environments map[string]legacy.Environment, password string, err error) {
return ts.LegacyEnvironments, ts.LegacyPassword, nil
}
func cloneVault(vault *vaulted.Vault) *vaulted.Vault {
newVault := &vaulted.Vault{
Vars: make(map[string]string),
SSHKeys: make(map[string]string),
SSHOptions: &vaulted.SSHOptions{},
}
if vault.AWSKey != nil {
newVault.AWSKey = &vaulted.AWSKey{
AWSCredentials: vaulted.AWSCredentials{
ID: vault.AWSKey.ID,
Secret: vault.AWSKey.Secret,
},
MFA: vault.AWSKey.MFA,
Role: vault.AWSKey.Role,
}
}
for key, value := range vault.Vars {
newVault.Vars[key] = value
}
for key, value := range vault.SSHKeys {
newVault.SSHKeys[key] = value
}
if vault.SSHOptions != nil {
newVault.SSHOptions = vault.SSHOptions
}
return newVault
}