-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathage_test.go
More file actions
435 lines (367 loc) · 11.9 KB
/
age_test.go
File metadata and controls
435 lines (367 loc) · 11.9 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
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
package vault_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/flowexec/vault"
)
func TestAgeIdentityResolver(t *testing.T) {
tempDir := t.TempDir()
testIdentity := "AGE-SECRET-KEY-1LC563A3EG4TLDL5EQE0YP5ZSJW8NADURXLZ8WVM00DMKG60URRNQ5TRZH0"
t.Setenv("TEST_AGE_IDENTITY", testIdentity)
resolver := vault.NewIdentityResolver([]vault.IdentitySource{
{Type: "env", Name: "TEST_AGE_IDENTITY"},
})
identities, err := resolver.ResolveIdentities()
if err != nil {
t.Fatalf("Failed to resolve identities: %v", err)
}
if len(identities) != 1 {
t.Errorf("Expected 1 identity, got %d", len(identities))
}
// Test identity from file
keyFile := filepath.Join(tempDir, "test-identity.txt")
err = os.WriteFile(keyFile, []byte(testIdentity), 0600)
if err != nil {
t.Fatalf("Failed to write identity file: %v", err)
}
resolver = vault.NewIdentityResolver([]vault.IdentitySource{
{Type: "file", Path: keyFile},
})
identities, err = resolver.ResolveIdentities()
if err != nil {
t.Fatalf("Failed to resolve identities from file: %v", err)
}
if len(identities) != 1 {
t.Errorf("Expected 1 identity from file, got %d", len(identities))
}
// Test multiple sources
resolver = vault.NewIdentityResolver([]vault.IdentitySource{
{Type: "env", Name: "NONEXISTENT_IDENTITY"},
{Type: "file", Path: keyFile},
{Type: "env", Name: "TEST_AGE_IDENTITY"},
})
identities, err = resolver.ResolveIdentities()
if err != nil {
t.Fatalf("Failed to resolve identities from multiple sources: %v", err)
}
if len(identities) != 2 { // Should find both the file and env identity
t.Errorf("Expected 2 identities from multiple sources, got %d", len(identities))
}
}
func TestAgeIdentityResolverErrors(t *testing.T) {
// Test invalid identity
t.Setenv("INVALID_AGE_IDENTITY", "not-a-valid-age-key")
resolver := vault.NewIdentityResolver([]vault.IdentitySource{
{Type: "env", Name: "INVALID_AGE_IDENTITY"},
})
identities, err := resolver.ResolveIdentities()
if err == nil {
t.Error("Expected error when no valid identities found")
}
if len(identities) != 0 {
t.Errorf("Expected 0 identities for invalid key, got %d", len(identities))
}
// Test nonexistent file
resolver = vault.NewIdentityResolver([]vault.IdentitySource{
{Type: "file", Path: "/nonexistent/path/key.txt"},
})
_, err = resolver.ResolveIdentities()
if err == nil {
t.Error("Expected error for nonexistent file")
}
// Test empty file path
resolver = vault.NewIdentityResolver([]vault.IdentitySource{
{Type: "file", Path: ""},
})
_, err = resolver.ResolveIdentities()
if err == nil {
t.Error("Expected error for empty file path")
}
// Test no valid identities
resolver = vault.NewIdentityResolver([]vault.IdentitySource{
{Type: "env", Name: "NONEXISTENT_KEY"},
})
_, err = resolver.ResolveIdentities()
if err == nil {
t.Error("Expected error when no valid identities found")
}
}
func TestAgeVaultCreation(t *testing.T) {
tempDir := t.TempDir()
// Test creating vault without Age config
_, err := vault.NewAgeVault(&vault.Config{
ID: "test",
Type: vault.ProviderTypeAge,
})
if err == nil {
t.Error("Expected error when creating vault without Age config")
}
testIdentity := "AGE-SECRET-KEY-1LC563A3EG4TLDL5EQE0YP5ZSJW8NADURXLZ8WVM00DMKG60URRNQ5TRZH0"
testRecipient := "age1wnhg53pg2qfsfxwvxvlg6pygw5uzwcyhj2dqhg0k83fvjexf9pzsxqdvs0"
keyFile := filepath.Join(tempDir, "test-key.txt")
err = os.WriteFile(keyFile, []byte(testIdentity), 0600)
if err != nil {
t.Fatalf("Failed to write identity file: %v", err)
}
config := &vault.Config{
ID: "test-age-vault",
Type: vault.ProviderTypeAge,
Age: &vault.AgeConfig{
StoragePath: tempDir,
IdentitySources: []vault.IdentitySource{
{Type: "file", Path: keyFile},
},
Recipients: []string{testRecipient},
},
}
v, err := vault.NewAgeVault(config)
if err != nil {
t.Fatalf("Failed to create Age vault: %v", err)
}
defer v.Close()
if v.ID() != "test-age-vault" {
t.Errorf("Expected vault ID 'test-age-vault', got %s", v.ID())
}
// Verify vault file was created
expectedFile := filepath.Join(tempDir, "vault-test-age-vault.age")
if _, err := os.Stat(expectedFile); os.IsNotExist(err) {
t.Errorf("Expected vault file %s was not created", expectedFile)
}
}
func TestAgeVaultRecipientManagement(t *testing.T) {
tempDir := t.TempDir()
testIdentity := "AGE-SECRET-KEY-1LC563A3EG4TLDL5EQE0YP5ZSJW8NADURXLZ8WVM00DMKG60URRNQ5TRZH0"
testRecipient1 := "age1wnhg53pg2qfsfxwvxvlg6pygw5uzwcyhj2dqhg0k83fvjexf9pzsxqdvs0"
testRecipient2 := "age1u7rkgxlu26y68m3ky0aesxtls9g33zy5zcy0wuehtwua6lssmpus4xszw6"
keyFile := filepath.Join(tempDir, "test-key.txt")
err := os.WriteFile(keyFile, []byte(testIdentity), 0600)
if err != nil {
t.Fatalf("Failed to write identity file: %v", err)
}
config := &vault.Config{
ID: "recipient-test",
Type: vault.ProviderTypeAge,
Age: &vault.AgeConfig{
StoragePath: tempDir,
IdentitySources: []vault.IdentitySource{
{Type: "file", Path: keyFile},
},
Recipients: []string{testRecipient1},
},
}
v, err := vault.NewAgeVault(config)
if err != nil {
t.Fatalf("Failed to create Age vault: %v", err)
}
defer v.Close()
// Test initial recipients
recipients, err := v.ListRecipients()
if err != nil {
t.Fatalf("Failed to list recipients: %v", err)
}
if len(recipients) != 1 {
t.Errorf("Expected 1 initial recipient, got %d", len(recipients))
}
if recipients[0] != testRecipient1 {
t.Errorf("Expected recipient %s, got %s", testRecipient1, recipients[0])
}
// Test adding recipient
err = v.AddRecipient(testRecipient2)
if err != nil {
t.Fatalf("Failed to add recipient: %v", err)
}
recipients, err = v.ListRecipients()
if err != nil {
t.Fatalf("Failed to list recipients after add: %v", err)
}
if len(recipients) != 2 {
t.Errorf("Expected 2 recipients after add, got %d", len(recipients))
}
// Test adding duplicate recipient (should not fail)
err = v.AddRecipient(testRecipient1)
if err != nil {
t.Fatalf("Failed to add duplicate recipient: %v", err)
}
recipients, err = v.ListRecipients()
if err != nil {
t.Fatalf("Failed to list recipients after duplicate add: %v", err)
}
if len(recipients) != 2 { // Should still be 2, duplicate doesn't increase count
t.Errorf("Expected 2 recipients after duplicate add, got %d", len(recipients))
}
// Test removing recipient (should succeed now that we have 2)
err = v.RemoveRecipient(testRecipient2)
if err != nil {
t.Fatalf("Failed to remove recipient: %v", err)
}
recipients, err = v.ListRecipients()
if err != nil {
t.Fatalf("Failed to list recipients after remove: %v", err)
}
if len(recipients) != 1 {
t.Errorf("Expected 1 recipient after remove, got %d", len(recipients))
}
// Test removing the last recipient (should fail)
err = v.RemoveRecipient(testRecipient1)
if err == nil {
t.Error("Expected error when removing the last recipient")
}
// Test removing nonexistent recipient
err = v.RemoveRecipient("age1nonexistent123456789")
if err == nil {
t.Error("Expected error when removing nonexistent recipient")
}
}
func TestAgeVaultInvalidRecipient(t *testing.T) {
tempDir := t.TempDir()
testIdentity := "AGE-SECRET-KEY-1LC563A3EG4TLDL5EQE0YP5ZSJW8NADURXLZ8WVM00DMKG60URRNQ5TRZH0"
keyFile := filepath.Join(tempDir, "test-key.txt")
err := os.WriteFile(keyFile, []byte(testIdentity), 0600)
if err != nil {
t.Fatalf("Failed to write identity file: %v", err)
}
config := &vault.Config{
ID: "invalid-recipient-test",
Type: vault.ProviderTypeAge,
Age: &vault.AgeConfig{
StoragePath: tempDir,
IdentitySources: []vault.IdentitySource{
{Type: "file", Path: keyFile},
},
Recipients: []string{"invalid-recipient-key"},
},
}
// Should fail during vault creation due to invalid recipient
_, err = vault.NewAgeVault(config)
if err == nil {
t.Error("Expected error when creating vault with invalid recipient")
}
}
func TestAgeVaultFileFormat(t *testing.T) {
tempDir := t.TempDir()
testIdentity := "AGE-SECRET-KEY-1LC563A3EG4TLDL5EQE0YP5ZSJW8NADURXLZ8WVM00DMKG60URRNQ5TRZH0"
testRecipient := "age1wnhg53pg2qfsfxwvxvlg6pygw5uzwcyhj2dqhg0k83fvjexf9pzsxqdvs0"
keyFile := filepath.Join(tempDir, "test-key.txt")
err := os.WriteFile(keyFile, []byte(testIdentity), 0600)
if err != nil {
t.Fatalf("Failed to write identity file: %v", err)
}
config := &vault.Config{
ID: "format-test",
Type: vault.ProviderTypeAge,
Age: &vault.AgeConfig{
StoragePath: tempDir,
IdentitySources: []vault.IdentitySource{
{Type: "file", Path: keyFile},
},
Recipients: []string{testRecipient},
},
}
vault1, err := vault.NewAgeVault(config)
if err != nil {
t.Fatalf("Failed to create vault: %v", err)
}
_ = vault1.SetSecret("key1", vault.NewSecretValue([]byte("value1")))
_ = vault1.SetSecret("key2", vault.NewSecretValue([]byte("value2")))
_ = vault1.Close()
// Verify the encrypted file exists and has content
vaultFile := filepath.Join(tempDir, "vault-format-test.age")
data, err := os.ReadFile(vaultFile)
if err != nil {
t.Fatalf("Failed to read vault file: %v", err)
}
if len(data) == 0 {
t.Error("Vault file should not be empty")
}
// Verify the file starts with age format header
dataStr := string(data)
if !strings.HasPrefix(dataStr, "age-encryption.org/v1") {
t.Error("Age vault file should start with age format header")
}
// Verify the file is encrypted (should not contain plain text)
if strings.Contains(dataStr, "key1") ||
strings.Contains(dataStr, "value1") ||
strings.Contains(dataStr, "key2") ||
strings.Contains(dataStr, "value2") {
t.Error("Age vault file should not contain plain text secrets")
}
// Verify file can be decrypted by creating new vault
vault2, err := vault.NewAgeVault(config)
if err != nil {
t.Fatalf("Failed to recreate vault: %v", err)
}
defer vault2.Close()
secret1, err := vault2.GetSecret("key1")
if err != nil {
t.Fatalf("Failed to decrypt secret: %v", err)
}
if secret1.PlainTextString() != "value1" {
t.Errorf("Expected 'value1', got %s", secret1.PlainTextString())
}
}
func TestAgeVaultNoRecipients(t *testing.T) {
tempDir := t.TempDir()
testIdentity := "AGE-SECRET-KEY-1LC563A3EG4TLDL5EQE0YP5ZSJW8NADURXLZ8WVM00DMKG60URRNQ5TRZH0"
keyFile := filepath.Join(tempDir, "test-key.txt")
err := os.WriteFile(keyFile, []byte(testIdentity), 0600)
if err != nil {
t.Fatalf("Failed to write identity file: %v", err)
}
config := &vault.Config{
ID: "no-recipients-test",
Type: vault.ProviderTypeAge,
Age: &vault.AgeConfig{
StoragePath: tempDir,
IdentitySources: []vault.IdentitySource{
{Type: "file", Path: keyFile},
},
Recipients: []string{},
},
}
_, err = vault.NewAgeVault(config)
if err == nil {
t.Error("Expected error when creating vault with no recipients")
}
}
func TestAgeVaultPathExpansion(t *testing.T) {
tempDir := t.TempDir()
testIdentity := "AGE-SECRET-KEY-1LC563A3EG4TLDL5EQE0YP5ZSJW8NADURXLZ8WVM00DMKG60URRNQ5TRZH0"
relativeKeyFile := "./test-key.txt"
// Change to temp dir so relative path works
oldDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
defer os.Chdir(oldDir)
err = os.Chdir(tempDir)
if err != nil {
t.Fatalf("Failed to change to temp directory: %v", err)
}
err = os.WriteFile(relativeKeyFile, []byte(testIdentity), 0600)
if err != nil {
t.Fatalf("Failed to write identity file: %v", err)
}
testRecipient := "age1wnhg53pg2qfsfxwvxvlg6pygw5uzwcyhj2dqhg0k83fvjexf9pzsxqdvs0"
config := &vault.Config{
ID: "path-expansion-test",
Type: vault.ProviderTypeAge,
Age: &vault.AgeConfig{
StoragePath: tempDir,
IdentitySources: []vault.IdentitySource{
{Type: "file", Path: relativeKeyFile},
},
Recipients: []string{testRecipient},
},
}
v, err := vault.NewAgeVault(config)
if err != nil {
t.Fatalf("Failed to create vault with relative path: %v", err)
}
defer v.Close()
err = v.SetSecret("test", vault.NewSecretValue([]byte("value")))
if err != nil {
t.Fatalf("Failed to set secret with relative path identity: %v", err)
}
}