-
Notifications
You must be signed in to change notification settings - Fork 10
/
main_e2e_test.go
453 lines (388 loc) · 13.5 KB
/
main_e2e_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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright 2018 SumUp Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
stdOs "os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/sumup-oss/go-pkgs/os"
"github.com/sumup-oss/go-pkgs/testutils"
gopkgsTestUtils "github.com/sumup-oss/go-pkgs/testutils"
"github.com/sumup-oss/vaulted/internal/e2e"
vaultedTestUtils "github.com/sumup-oss/vaulted/pkg/testutils"
"github.com/sumup-oss/vaulted/pkg/vaulted/payload"
)
var (
osExecutor = &os.RealOsExecutor{}
binaryPath string
)
func TestMain(m *testing.M) {
binaryPath = e2e.GoBuild(osExecutor)
runTests := m.Run()
osExecutor.Remove(binaryPath)
stdOs.Exit(runTests)
}
// NOTE: Test the most-valuable functionality and workflow, excluding the `legacy` and `terraform `commands.
// The flow is:
// 1. encrypt
// 2. decrypt
// 3. rotate
// 4. decrypt
// 5. encrypt
// 6. rekey
// 7. decrypt
// TODO: Investigate why commands that are run do not output to neither stderr nor stdout.
// Tests intentionally expect blank stdout/stderr, even though it's wrong,
// to pass and later be able to correct to expected output.
func TestMvpWorkflow(t *testing.T) {
t.Parallel()
tmpDir := gopkgsTestUtils.TestDir(t, "vaulted")
defer stdOs.RemoveAll(tmpDir)
// NOTE: Create a build before switching dirs
build := e2e.NewBuild(binaryPath, tmpDir)
// NOTE: Switch to tmp dir to make sure we're not
// relying on content inside the non-temporary dir (previous cwd).
gopkgsTestUtils.TestChdir(t, tmpDir)
privKeyPath, privKey := testutils.GenerateAndWritePrivateKey(t, tmpDir, "priv.key")
pubKeyPath := testutils.GenerateAndWritePublicKey(t, tmpDir, "pub.key", privKey)
// NOTE: Start of `1. encrypt`
inFileContent := []byte("mysecret")
encryptInPath := filepath.Join(tmpDir, "1-encrypt-in.raw")
encryptOutPath := filepath.Join(tmpDir, "1-encrypt-out.enc")
err := ioutil.WriteFile(encryptInPath, inFileContent, 0644)
require.Nil(t, err)
stdout, stderr, err := build.Run(
"encrypt",
"--public-key-path",
pubKeyPath,
"--in",
encryptInPath,
"--out",
encryptOutPath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
encryptedOutContent, err := osExecutor.ReadFile(encryptOutPath)
require.Nil(t, err)
// NOTE: Make sure we've actually encrypted something
// and it has a version header, passphrase and content parts
encryptedOutContentParts := strings.Split(
string(encryptedOutContent),
payload.EncryptionPayloadSeparator,
)
assert.Equal(t, 3, len(encryptedOutContentParts))
// NOTE: Verify header, passphrase and content are not empty.
assert.NotEqual(t, "", encryptedOutContentParts[0])
assert.NotEqual(t, "", encryptedOutContentParts[1])
assert.NotEqual(t, "", encryptedOutContentParts[2])
// NOTE: Start of `2. decrypt`.
// Take the output of `1. encrypt` to make sure
// we're still able to decrypt it.
decryptInPath := encryptOutPath
decryptOutPath := filepath.Join(tmpDir, "2-decrypt-out")
stdout, stderr, err = build.Run(
"decrypt",
"--private-key-path",
privKeyPath,
"--in",
decryptInPath,
"--out",
decryptOutPath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
decryptedOutput, err := osExecutor.ReadFile(decryptOutPath)
require.Nil(t, err)
// NOTE: Make sure we're not adding/losing content
assert.Equal(t, inFileContent, decryptedOutput)
// NOTE: Start of `3. rotate`.
// Take the output of `1. encrypt` as input,
// since we need already encrypted payload.
rotateInPath := encryptOutPath
rotateOutPath := filepath.Join(tmpDir, "3-rotate-out.enc")
stdout, stderr, err = build.Run(
"rotate",
"--public-key-path",
pubKeyPath,
"--private-key-path",
privKeyPath,
"--in",
rotateInPath,
"--out",
rotateOutPath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
rotatedOutContent, err := osExecutor.ReadFile(rotateOutPath)
require.Nil(t, err)
rotatedOutContentParts := strings.Split(
string(rotatedOutContent),
payload.EncryptionPayloadSeparator,
)
// NOTE: Make sure we've actually encrypted something
// and it has a version header, passphrase and content parts
assert.Equal(t, 3, len(rotatedOutContentParts))
// NOTE: Make sure the header is still the same,
// and we didn't attempt to change versions.
assert.Equal(t, encryptedOutContentParts[0], rotatedOutContentParts[0])
// NOTE: Make sure we rotated the encrypted passphrase
assert.NotEqual(t, encryptedOutContentParts[1], rotatedOutContentParts[1])
// NOTE: Make sure we rotated the encrypted content
assert.NotEqual(t, encryptedOutContentParts[2], rotatedOutContentParts[2])
// NOTE: Start of `4. decrypt`.
// We take the rotate encrypted payload output
// and make sure we're still able to decrypt it.
rotatedDecryptInPath := rotateOutPath
rotatedDecryptOutPath := filepath.Join(tmpDir, "4-decrypt-out")
stdout, stderr, err = build.Run(
"decrypt",
"--private-key-path",
privKeyPath,
"--in",
rotatedDecryptInPath,
"--out",
rotatedDecryptOutPath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
// NOTE: Since we're still using the plaintext input from `1. encrypt`
// make sure we're still decrypting the same result and not adding/removing content.
rotatedDecryptOutContent, err := osExecutor.ReadFile(rotatedDecryptOutPath)
require.Nil(t, err)
assert.Equal(t, inFileContent, rotatedDecryptOutContent)
// NOTE: Start of `5. encrypt`.
// Encrypt again the recently decrypted rotated output,
// to make sure we cover the `decrypt` -> `encrypt` again flow.
encryptAgainInPath := rotatedDecryptOutPath
encryptAgainOutPath := filepath.Join(tmpDir, "5-encrypt-out.enc")
stdout, stderr, err = build.Run(
"encrypt",
"--public-key-path",
pubKeyPath,
"--in",
encryptAgainInPath,
"--out",
encryptAgainOutPath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
encryptAgainOutContent, err := osExecutor.ReadFile(encryptAgainOutPath)
require.Nil(t, err)
// NOTE: Make sure we've actually encrypted something
// and it has a version header, passphrase and content parts
encryptAgainOutContentParts := strings.Split(
string(encryptAgainOutContent),
payload.EncryptionPayloadSeparator,
)
assert.Equal(t, 3, len(encryptAgainOutContentParts))
// NOTE: Verify header, passphrase and content are not empty.
assert.NotEqual(t, "", encryptAgainOutContentParts[0])
assert.NotEqual(t, "", encryptAgainOutContentParts[1])
assert.NotEqual(t, "", encryptAgainOutContentParts[2])
// NOTE: Start of `6. rekey`.
// Make sure that we can rotate the keypair used to
// generate a previously encrypted payload.
newPrivKeyPath, newPrivKey := testutils.GenerateAndWritePrivateKey(
t,
tmpDir,
"new-priv.key",
)
newPubKeyPath := testutils.GenerateAndWritePublicKey(
t,
tmpDir,
"new-pub.key",
newPrivKey,
)
// NOTE: Use the recently encrypted again payload
rekeyInPath := encryptAgainOutPath
rekeyOutPath := filepath.Join(tmpDir, "6-rekey-out.enc")
stdout, stderr, err = build.Run(
"rekey",
"--old-private-key-path",
privKeyPath,
"--new-public-key-path",
newPubKeyPath,
"--in",
rekeyInPath,
"--out",
rekeyOutPath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
rekeyOutContent, err := osExecutor.ReadFile(rekeyOutPath)
require.Nil(t, err)
// NOTE: Make sure we've actually encrypted something
// and it has a version header, passphrase and content parts
rekeyOutContentParts := strings.Split(
string(rekeyOutContent),
payload.EncryptionPayloadSeparator,
)
assert.Equal(t, 3, len(rekeyOutContentParts))
// NOTE: Verify header is the same, since we didn't
// want to increment versions and modify it at all.
assert.Equal(t, encryptAgainOutContentParts[0], rekeyOutContentParts[0])
// NOTE: Verify passphrase and content are actually rotated
// and not the same as the `in` encrypted payload's.
assert.NotEqual(t, encryptAgainOutContentParts[1], rekeyOutContentParts[1])
assert.NotEqual(t, encryptAgainOutContentParts[2], rekeyOutContentParts[2])
// NOTE: Verify that rekeyed payload is not decryptable using the old keypair
stdout, stderr, err = build.Run(
"decrypt",
"--private-key-path",
privKeyPath,
"--in",
rekeyOutPath,
// NOTE: `out` intentionally left-out,
// since this must not pass either way.
// No need to write to a file, since printing to stdout
// is sufficient if unexpected behavior occurs.
)
require.NotNil(t, err)
assert.Equal(t, "", stdout)
assert.Contains(t, stderr, "failed to decrypt encrypted payload using specified RSA key")
// NOTE: Start of `7. decrypt`.
// Verifies that the rekeyed content is still decryptable
// using the new keypair.
rekeyDecryptInPath := rekeyOutPath
rekeyDecryptOutPath := filepath.Join(tmpDir, "7-decrypt-out")
stdout, stderr, err = build.Run(
"decrypt",
"--private-key-path",
newPrivKeyPath,
"--in",
rekeyDecryptInPath,
"--out",
rekeyDecryptOutPath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
// NOTE: Since we're still using the plaintext input from `1. encrypt`
// make sure we're still decrypting the same result and not adding/removing content.
rekeyDecryptOutContent, err := osExecutor.ReadFile(rekeyDecryptOutPath)
require.Nil(t, err)
assert.Equal(t, inFileContent, rekeyDecryptOutContent)
}
// NOTE: Test the terraform workflow that is feasible when you're producing terraform resources for `terraform-provider-vaulted`.
// The flow is:
// TODO: When `terraform view` is added this will verify that the content is still decryptable and viewable.
// 1. terraform vault new-resource
// 2. terraform vault rotate
// 3. terraform vault rekey
// TODO: Investigate why commands that are run do not output to neither stderr nor stdout.
// Tests intentionally expect blank stdout/stderr, even though it's wrong,
// to pass and later be able to correct to expected output.
func TestV1TerraformWorkflow(t *testing.T) {
t.Parallel()
tmpDir := gopkgsTestUtils.TestDir(t, "vaulted")
defer stdOs.RemoveAll(tmpDir)
// NOTE: Create a build before switching dirs
build := e2e.NewBuild(binaryPath, tmpDir)
// NOTE: Switch to tmp dir to make sure we're not
// relying on content inside the non-temporary dir (previous cwd).
gopkgsTestUtils.TestChdir(t, tmpDir)
privKeyPath, privKey := testutils.GenerateAndWritePrivateKey(t, tmpDir, "priv.key")
pubKeyPath := testutils.GenerateAndWritePublicKey(t, tmpDir, "pub.key", privKey)
// NOTE: Start of `4. terraform new-resource`
inFilePath := path.Join(tmpDir, "in.raw")
newResourceOutFilePath := path.Join(tmpDir, "out.tf")
inFileContent := []byte("mynewsecret")
newResourcePathArg := "secret/new-resource/example"
newResourceResourceName := "myresource"
err := osExecutor.WriteFile(inFilePath, inFileContent, 0644)
require.Nil(t, err)
// NOTE: Append to the same output file as migrated one
stdout, stderr, err := build.Run(
"terraform",
"vault",
"new-resource",
"--public-key-path",
pubKeyPath,
"--path",
newResourcePathArg,
"--resource-name",
newResourceResourceName,
"--in",
inFilePath,
"--out",
newResourceOutFilePath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
tfFileContent, err := osExecutor.ReadFile(newResourceOutFilePath)
require.Nil(t, err)
// NOTE: Make sure we actually wrote valid terraform resources
regexMatches := vaultedTestUtils.NewTerraformRegex.FindAllStringSubmatch(string(tfFileContent), -1)
assert.Equal(t, 1, len(regexMatches))
rotatedTfFilePath := path.Join(tmpDir, "rotated.tf")
// NOTE: Start of `5. terraform vault rotate`
stdout, stderr, err = build.Run(
"terraform",
"vault",
"rotate",
"--public-key-path",
pubKeyPath,
"--private-key-path",
privKeyPath,
"--in",
newResourceOutFilePath,
"--out",
rotatedTfFilePath,
)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
require.Nil(t, err)
rotatedTfFileContent, err := osExecutor.ReadFile(rotatedTfFilePath)
require.Nil(t, err)
// NOTE: Make sure we actually wrote valid terraform resources
regexMatches = vaultedTestUtils.NewTerraformRegex.FindAllStringSubmatch(string(rotatedTfFileContent), -1)
assert.Equal(t, 1, len(regexMatches))
rekeyedTfFilePath := path.Join(tmpDir, "rekeyed.tf")
_, newPrivKey := testutils.GenerateAndWritePrivateKey(t, tmpDir, "newpriv.key")
newPubKeyPath := testutils.GenerateAndWritePublicKey(t, tmpDir, "newpub.key", newPrivKey)
// NOTE: Start of `6. terraform vault rekey`
stdout, stderr, err = build.Run(
"terraform",
"vault",
"rekey",
"--new-public-key-path",
newPubKeyPath,
"--old-private-key-path",
privKeyPath,
"--in",
rotatedTfFilePath,
"--out",
rekeyedTfFilePath,
)
require.Nil(t, err)
assert.Equal(t, "", stdout)
assert.Equal(t, "", stderr)
rekeyedTfFileContent, err := osExecutor.ReadFile(rekeyedTfFilePath)
require.Nil(t, err)
// NOTE: Make sure we actually wrote valid terraform resources
regexMatches = vaultedTestUtils.NewTerraformRegex.FindAllStringSubmatch(string(rekeyedTfFileContent), -1)
assert.Equal(t, 1, len(regexMatches))
}