-
Notifications
You must be signed in to change notification settings - Fork 11
/
strongbox_test.go
340 lines (270 loc) · 12 KB
/
strongbox_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//go:build integration
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"testing"
yaml "gopkg.in/yaml.v2"
"github.com/stretchr/testify/assert"
)
var (
HOME = os.Getenv("HOME")
)
func command(dir, name string, arg ...string) (out []byte, err error) {
cmd := exec.Command(name, arg...)
cmd.Dir = dir
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
return
}
func assertCommand(t *testing.T, dir, name string, arg ...string) (out []byte) {
out, err := command(dir, name, arg...)
if err != nil {
t.Fatal(string(out))
}
return
}
func assertWriteFile(t *testing.T, filename string, data []byte, perm os.FileMode) {
err := os.WriteFile(filename, data, perm)
if err != nil {
t.Fatal(err)
}
}
func assertReadFile(t *testing.T, filename string) string {
data, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
return string(data)
}
func keysFromKR(t *testing.T, name string) (key, keyID string) {
kr := make(map[string]interface{})
krf, err := os.ReadFile(HOME + "/.strongbox_keyring")
if err != nil {
t.Fatal(err)
}
err = yaml.Unmarshal(krf, kr)
if err != nil {
t.Fatal(err)
}
kes := kr["keyentries"].([]interface{})
for k := range kes {
desc := kes[k].(map[interface{}]interface{})["description"].(string)
if name == desc {
return kes[k].(map[interface{}]interface{})["key"].(string),
kes[k].(map[interface{}]interface{})["key-id"].(string)
}
}
t.Fatal(fmt.Sprintf("no keyId for give desc: %s", name))
return "", ""
}
func recipients() [][][]byte {
f, _ := os.ReadFile(HOME + "/.strongbox_identity")
r := regexp.MustCompile(`(?m)^.*public key.*(age.*)$`)
return r.FindAllSubmatch(f, -1)
}
func TestMain(m *testing.M) {
out, err := command("/", "git", "config", "--global", "user.email", "[email protected]")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
out, err = command("/", "git", "config", "--global", "user.name", "test")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
out, err = command("/", "strongbox", "-git-config")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
out, err = command("/", "strongbox", "-gen-key", "test00")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
out, err = command("/", "strongbox", "-gen-identity", "ident1")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
out, err = command("/", "strongbox", "-gen-identity", "ident2")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
out, err = command("/", "mkdir", HOME+"/test-proj")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
out, err = command(HOME+"/test-proj", "git", "init")
if err != nil {
fmt.Fprintf(os.Stderr, "%v", string(out))
os.Exit(1)
}
os.Exit(m.Run())
}
func TestSimpleEnc(t *testing.T) {
repoDir := HOME + "/test-proj"
_, keyID := keysFromKR(t, "test00")
secVal := "secret123wombat"
ga := `secret filter=strongbox diff=strongbox
secrets/* filter=strongbox diff=strongbox`
assertWriteFile(t, repoDir+"/.gitattributes", []byte(ga), 0644)
assertWriteFile(t, repoDir+"/.strongbox-keyid", []byte(keyID), 0644)
assertWriteFile(t, repoDir+"/secret", []byte(secVal), 0644)
assertCommand(t, repoDir, "git", "add", ".")
assertCommand(t, repoDir, "git", "commit", "-m", "\"TestSimpleEnc\"")
ptOut, _ := command(repoDir, "git", "show", "--", "secret")
encOut, _ := command(repoDir, "git", "show", "HEAD:secret")
assert.Contains(t, string(ptOut), secVal, "no plaintext")
assert.Contains(t, string(encOut), "STRONGBOX ENCRYPTED RESOURCE", "no plaintext")
}
func TestNestedEnc(t *testing.T) {
repoDir := HOME + "/test-proj"
secVal := "secret123croc"
assertCommand(t, repoDir, "mkdir", "-p", "secrets/dir0")
assertWriteFile(t, repoDir+"/secrets/dir0/sec0", []byte(secVal), 0644)
assertCommand(t, repoDir, "git", "add", ".")
assertCommand(t, repoDir, "git", "commit", "-m", "\"TestNestedEnc\"")
ptOut, _ := command(repoDir, "git", "show")
encOut, _ := command(repoDir, "git", "show", "HEAD:secret")
assert.Contains(t, string(ptOut), secVal, "no plaintext")
assert.Contains(t, string(encOut), "STRONGBOX ENCRYPTED RESOURCE", "no plaintext")
}
func TestMissingKey(t *testing.T) {
repoDir := HOME + "/test-proj"
secVal := "secret-missing-key"
// remove the key for encryption
assertCommand(t, "/", "mv", HOME+"/.strongbox_keyring", HOME+"/.strongbox_keyring.bkup")
assertCommand(t, "/", "strongbox", "-gen-key", "tmp")
assertWriteFile(t, repoDir+"/secrets/sec-missing-key", []byte(secVal), 0644)
_, err := command(repoDir, "git", "add", ".")
assert.Error(t, err, "Should error on add attempt")
// clean up
assertCommand(t, "/", "mv", HOME+"/.strongbox_keyring.bkup", HOME+"/.strongbox_keyring")
// as the correct is now present, should not error and present untracked changes
assertCommand(t, repoDir, "git", "status")
// remove the file
assertCommand(t, "/", "rm", repoDir+"/secrets/sec-missing-key")
}
func TestRecursiveDecryption(t *testing.T) {
repoDir := HOME + "/test-rec-dec"
assertCommand(t, "/", "mkdir", "-p", repoDir+"/secrets/")
assertCommand(t, "/", "mkdir", "-p", repoDir+"/app/secrets/")
assertCommand(t, repoDir, "git", "init")
// generate new private keys
assertCommand(t, "/", "strongbox", "-gen-key", "rec-dec-01")
assertCommand(t, "/", "strongbox", "-gen-key", "rec-dec-02")
pKey1, keyID1 := keysFromKR(t, "rec-dec-01")
pKey2, keyID2 := keysFromKR(t, "rec-dec-02")
secVal := "secret123wombat"
ga := `secret filter=strongbox diff=strongbox
secrets/* filter=strongbox diff=strongbox
*/secrets/* filter=strongbox diff=strongbox`
// setup root keyID and nested app folder with different keyID
assertWriteFile(t, repoDir+"/.gitattributes", []byte(ga), 0644)
assertWriteFile(t, repoDir+"/.strongbox-keyid", []byte(keyID1), 0644)
assertWriteFile(t, repoDir+"/app/.strongbox-keyid", []byte(keyID2), 0644)
// Write plan secrets
assertWriteFile(t, repoDir+"/secret", []byte(secVal+"01"), 0644)
assertWriteFile(t, repoDir+"/secrets/s2", []byte(secVal+"02"), 0644)
assertWriteFile(t, repoDir+"/app/secrets/s3", []byte(secVal+"03"), 0644)
// set test dir as Home because git command will use default strongbox key
assertCommand(t, repoDir, "git", "add", ".")
assertCommand(t, repoDir, "git", "commit", "-m", "\"TestSimpleEnc\"")
// Make sure files are encrypted
ptOut01, _ := command(repoDir, "git", "show", "--", "secret")
encOut01, _ := command(repoDir, "git", "show", "HEAD:secret")
assert.Contains(t, string(ptOut01), secVal+"01", "should be in plain text")
assert.Contains(t, string(encOut01), "STRONGBOX ENCRYPTED RESOURCE", "should be encrypted")
ptOut02, _ := command(repoDir, "git", "show", "--", "secrets/s2")
encOut02, _ := command(repoDir, "git", "show", "HEAD:secrets/s2")
assert.Contains(t, string(ptOut02), secVal+"02", "should be in plain text")
assert.Contains(t, string(encOut02), "STRONGBOX ENCRYPTED RESOURCE", "should be encrypted")
ptOut03, _ := command(repoDir, "git", "show", "--", "app/secrets/s3")
encOut03, _ := command(repoDir, "git", "show", "HEAD:app/secrets/s3")
assert.Contains(t, string(ptOut03), secVal+"03", "should be in plain text")
assert.Contains(t, string(encOut03), "STRONGBOX ENCRYPTED RESOURCE", "should be encrypted")
// TEST 1 (using default keyring file location)
//override local file with encrypted content
assertWriteFile(t, repoDir+"/secret", encOut01, 0644)
assertWriteFile(t, repoDir+"/secrets/s2", encOut02, 0644)
assertWriteFile(t, repoDir+"/app/secrets/s3", encOut03, 0644)
// run command from the root of the target folder without path arg
assertCommand(t, repoDir, "strongbox", "-decrypt", "-recursive")
// make sure all files are decrypted
assert.Contains(t, assertReadFile(t, repoDir+"/secret"), secVal+"01", "should be in plain text")
assert.Contains(t, assertReadFile(t, repoDir+"/secrets/s2"), secVal+"02", "should be in plain text")
assert.Contains(t, assertReadFile(t, repoDir+"/app/secrets/s3"), secVal+"03", "should be in plain text")
// TEST 2 (using custom keyring file location)
// override local file with encrypted content
assertWriteFile(t, repoDir+"/secret", encOut01, 0644)
assertWriteFile(t, repoDir+"/secrets/s2", encOut02, 0644)
assertWriteFile(t, repoDir+"/app/secrets/s3", encOut03, 0644)
keyRingPath := repoDir + "/.keyring"
// move keyring file
assertCommand(t, "/", "mv", HOME+"/.strongbox_keyring", keyRingPath)
// run command from outside of the target folder
assertCommand(t, "/", "strongbox", "-keyring", keyRingPath, "-decrypt", "-recursive", repoDir)
// make sure all files are decrypted
assert.Contains(t, assertReadFile(t, repoDir+"/secret"), secVal+"01", "should be in plain text")
assert.Contains(t, assertReadFile(t, repoDir+"/secrets/s2"), secVal+"02", "should be in plain text")
assert.Contains(t, assertReadFile(t, repoDir+"/app/secrets/s3"), secVal+"03", "should be in plain text")
// TEST 3.1 (using given private key)
// override local file with encrypted content
assertWriteFile(t, repoDir+"/secret", encOut01, 0644)
assertWriteFile(t, repoDir+"/secrets/s2", encOut02, 0644)
assertWriteFile(t, repoDir+"/app/secrets/s3", encOut03, 0644)
//since rec-dec-01 is not used to encrypt app folders secret so expect error
command(repoDir, "strongbox", "-key", pKey1, "-decrypt", "-recursive", ".")
assert.Contains(t, assertReadFile(t, repoDir+"/secret"), secVal+"01", "should be in plain text")
assert.Contains(t, assertReadFile(t, repoDir+"/secrets/s2"), secVal+"02", "should be in plain text")
assert.Contains(t, assertReadFile(t, repoDir+"/app/secrets/s3"), "STRONGBOX ENCRYPTED RESOURCE", "should be encrypted")
// TEST 3.2 (using custom keyring file location)
// override local file with encrypted content
assertWriteFile(t, repoDir+"/secret", encOut01, 0644)
assertWriteFile(t, repoDir+"/secrets/s2", encOut02, 0644)
assertWriteFile(t, repoDir+"/app/secrets/s3", encOut03, 0644)
//since rec-dec-02 is not used to encrypt root folders secrets so expect error
command(repoDir, "strongbox", "-key", pKey2, "-decrypt", "-recursive", ".")
assert.Contains(t, assertReadFile(t, repoDir+"/secret"), "STRONGBOX ENCRYPTED RESOURCE", "should be encrypted")
assert.Contains(t, assertReadFile(t, repoDir+"/secrets/s2"), "STRONGBOX ENCRYPTED RESOURCE", "should be encrypted")
assert.Contains(t, assertReadFile(t, repoDir+"/app/secrets/s3"), secVal+"03", "should be in plain text")
}
func TestAgeEnc(t *testing.T) {
repoDir := HOME + "/test-proj"
secVal := "age_secret1"
assertCommand(t, repoDir, "mkdir", "-p", "age/secrets")
assertWriteFile(t, repoDir+"/age/.gitattributes", []byte(`secrets/* filter=strongbox diff=strongbox`), 0644)
assertWriteFile(t, repoDir+"/.strongbox_recipient", recipients()[0][1], 0644)
assertWriteFile(t, repoDir+"/age/secrets/secret", []byte(secVal), 0644)
assertCommand(t, repoDir, "git", "add", ".")
assertCommand(t, repoDir, "git", "commit", "-m", "\"TestAgeEnc\"")
ptOut, _ := command(repoDir, "git", "show")
encOut, _ := command(repoDir, "git", "show", "HEAD:age/secrets/secret")
assert.Contains(t, string(ptOut), secVal, "no plaintext")
assert.Contains(t, string(encOut), "-----BEGIN AGE ENCRYPTED FILE-----", "missing age header")
}
func TestAgeKeyUpdate(t *testing.T) {
repoDir := HOME + "/test-proj"
// update recipient
assertWriteFile(t, repoDir+"/.strongbox_recipient", recipients()[1][1], 0644)
assertCommand(t, repoDir, "touch", "age/secrets/secret")
assertCommand(t, repoDir, "git", "add", ".")
assertCommand(t, repoDir, "git", "commit", "-m", "\"TestAgeKeyUpdate\"")
encOut, _ := command(repoDir, "git", "show", "HEAD:age/secrets/secret")
encOutPrevCommit, _ := command(repoDir, "git", "show", "HEAD^1:age/secrets/secret")
assert.Contains(t, string(encOut), "-----BEGIN AGE ENCRYPTED FILE-----", "missing age header")
assert.NotEqual(t, string(encOut), string(encOutPrevCommit), "cipher text hasn't changed")
}
func TestAgeSimulateDeterministic(t *testing.T) {
repoDir := HOME + "/test-proj"
assertCommand(t, repoDir, "touch", "age/secrets/secret")
status, _ := command(repoDir, "git", "status")
assert.NotContains(t, string(status), "age/secrets/secret", "secret file showing up in diff")
}