-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsecrets_test.go
More file actions
692 lines (587 loc) · 16.9 KB
/
Copy pathsecrets_test.go
File metadata and controls
692 lines (587 loc) · 16.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package pipeline
import (
"encoding/json"
"testing"
"github.com/buildkite/go-pipeline/ordered"
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v3"
)
func TestSecretsUnmarshalJSON(t *testing.T) {
t.Parallel()
jsonData := `[
"DATABASE_URL",
"API_TOKEN"
]`
var secrets Secrets
err := json.Unmarshal([]byte(jsonData), &secrets)
if err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if len(secrets) != 2 {
t.Fatalf("len(secrets) = %d, want 2", len(secrets))
}
// Check first secret
if secrets[0].Key != "DATABASE_URL" {
t.Errorf("secrets[0].Key = %q, want %q", secrets[0].Key, "DATABASE_URL")
}
if secrets[0].EnvironmentVariable != "DATABASE_URL" {
t.Errorf("secrets[0].EnvironmentVariable = %q, want %q", secrets[0].EnvironmentVariable, "DATABASE_URL")
}
// Check second secret
if secrets[1].Key != "API_TOKEN" {
t.Errorf("secrets[1].Key = %q, want %q", secrets[1].Key, "API_TOKEN")
}
if secrets[1].EnvironmentVariable != "API_TOKEN" {
t.Errorf("secrets[1].EnvironmentVariable = %q, want %q", secrets[1].EnvironmentVariable, "API_TOKEN")
}
}
func TestSecretsUnmarshalYAML(t *testing.T) {
t.Parallel()
yamlData := `
- DATABASE_URL
- API_TOKEN
`
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err != nil {
t.Fatalf("ordered.Unmarshal() error = %v", err)
}
if len(secrets) != 2 {
t.Fatalf("len(secrets) = %d, want 2", len(secrets))
}
// Check first secret
if secrets[0].Key != "DATABASE_URL" {
t.Errorf("secrets[0].Key = %q, want %q", secrets[0].Key, "DATABASE_URL")
}
if secrets[0].EnvironmentVariable != "DATABASE_URL" {
t.Errorf("secrets[0].EnvironmentVariable = %q, want %q", secrets[0].EnvironmentVariable, "DATABASE_URL")
}
// Check second secret
if secrets[1].Key != "API_TOKEN" {
t.Errorf("secrets[1].Key = %q, want %q", secrets[1].Key, "API_TOKEN")
}
if secrets[1].EnvironmentVariable != "API_TOKEN" {
t.Errorf("secrets[1].EnvironmentVariable = %q, want %q", secrets[1].EnvironmentVariable, "API_TOKEN")
}
}
func TestSecretsUnmarshalNull(t *testing.T) {
t.Parallel()
var secrets Secrets
err := secrets.UnmarshalOrdered(nil)
if err == nil {
t.Fatalf("UnmarshalOrdered(nil) should return error, got nil")
}
expectedErr := "unmarshaling secrets: secrets cannot be null"
if err.Error() != expectedErr {
t.Errorf("UnmarshalOrdered(nil) error = %q, want %q", err.Error(), expectedErr)
}
}
func TestSecretsUnmarshalNullYAML(t *testing.T) {
t.Parallel()
// Test that `secrets: null` in YAML returns an error
yamlData := `null`
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err == nil {
t.Fatalf("ordered.Unmarshal() should return error for null secrets, got nil")
}
expectedErr := "unmarshaling secrets: secrets cannot be null"
if err.Error() != expectedErr {
t.Errorf("ordered.Unmarshal() error = %q, want %q", err.Error(), expectedErr)
}
}
func TestSecretsMergeWith(t *testing.T) {
t.Parallel()
baseSecrets := Secrets{
Secret{Key: "DB_KEY", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "REDIS_KEY", EnvironmentVariable: "REDIS_URL"},
}
stepSecrets := Secrets{
Secret{Key: "API_KEY", EnvironmentVariable: "API_TOKEN"},
Secret{Key: "DB_OVERRIDE", EnvironmentVariable: "DATABASE_URL"},
}
merged := baseSecrets.MergeWith(stepSecrets)
if len(merged) != 3 {
t.Fatalf("len(merged) = %d, want 3", len(merged))
}
want := Secrets{
Secret{Key: "API_KEY", EnvironmentVariable: "API_TOKEN"},
Secret{Key: "DB_OVERRIDE", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "REDIS_KEY", EnvironmentVariable: "REDIS_URL"},
}
if diff := cmp.Diff(merged, want); diff != "" {
t.Errorf("merged mismatch (-got +want):\n%s", diff)
}
}
func TestSecretsMergeWithEmptySlices(t *testing.T) {
t.Parallel()
baseSecrets := Secrets{
Secret{Key: "DB_KEY", EnvironmentVariable: "DATABASE_URL"},
}
want := Secrets{
Secret{Key: "DB_KEY", EnvironmentVariable: "DATABASE_URL"},
}
merged1 := baseSecrets.MergeWith(Secrets{})
if diff := cmp.Diff(merged1, want); diff != "" {
t.Errorf("merged1 mismatch (-got +want):\n%s", diff)
}
// Empty base should return other
merged2 := Secrets{}.MergeWith(baseSecrets)
if diff := cmp.Diff(merged2, want); diff != "" {
t.Errorf("merged2 mismatch (-got +want):\n%s", diff)
}
// Both empty should return empty
merged3 := Secrets{}.MergeWith(Secrets{})
if diff := cmp.Diff(merged3, Secrets{}); diff != "" {
t.Errorf("merged3 mismatch (-got +want):\n%s", diff)
}
}
func TestSecretsUnmarshalMapSyntax(t *testing.T) {
t.Parallel()
yamlData := `
CUSTOM_ENV: SECRET_KEY
API_TOKEN: api-secret
DATABASE_URL: db-key
`
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err != nil {
t.Fatalf("ordered.Unmarshal() error = %v", err)
}
want := Secrets{
Secret{Key: "SECRET_KEY", EnvironmentVariable: "CUSTOM_ENV"},
Secret{Key: "api-secret", EnvironmentVariable: "API_TOKEN"},
Secret{Key: "db-key", EnvironmentVariable: "DATABASE_URL"},
}
if diff := cmp.Diff(secrets, want); diff != "" {
t.Errorf("secrets mismatch (-got +want):\n%s", diff)
}
}
func TestSecretsUnmarshalMapSyntaxJSON(t *testing.T) {
t.Parallel()
jsonData := `{
"CUSTOM_ENV": "SECRET_KEY",
"API_TOKEN": "api-secret"
}`
var secrets Secrets
err := json.Unmarshal([]byte(jsonData), &secrets)
if err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
want := Secrets{
Secret{Key: "SECRET_KEY", EnvironmentVariable: "CUSTOM_ENV"},
Secret{Key: "api-secret", EnvironmentVariable: "API_TOKEN"},
}
if diff := cmp.Diff(secrets, want); diff != "" {
t.Errorf("secrets mismatch (-got +want):\n%s", diff)
}
}
func TestSecretsUnmarshalMapSyntaxErrors(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
yamlData string
expectedErr string
}{
{
name: "non-string secret key",
yamlData: "ENV_VAR: 123",
expectedErr: "unmarshaling secrets: secret key must be a string, but was int",
},
{
name: "empty secret key",
yamlData: "ENV_VAR: \"\"",
expectedErr: "unmarshaling secrets: secret key cannot be empty",
},
{
name: "empty environment variable name",
yamlData: "\"\": SECRET_KEY",
expectedErr: "unmarshaling secrets: environment variable name cannot be empty",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(tc.yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err == nil {
t.Fatalf("ordered.Unmarshal() should return error, got nil")
}
if err.Error() != tc.expectedErr {
t.Errorf("ordered.Unmarshal() error = %q, want %q", err.Error(), tc.expectedErr)
}
})
}
}
func TestSecretsUnmarshalMixedFormats(t *testing.T) {
t.Parallel()
// Test that array and map formats can't be mixed at the top level
// This should use the array format (existing behavior)
yamlData := `
- DATABASE_URL
- key: API_TOKEN
environment_variable: API_KEY
`
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err != nil {
t.Fatalf("ordered.Unmarshal() error = %v", err)
}
want := Secrets{
Secret{Key: "DATABASE_URL", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "API_TOKEN", EnvironmentVariable: "API_KEY"},
}
if diff := cmp.Diff(secrets, want); diff != "" {
t.Errorf("secrets mismatch (-got +want):\n%s", diff)
}
}
func TestSecretsUnmarshalEmptyMap(t *testing.T) {
t.Parallel()
yamlData := `{}`
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err != nil {
t.Fatalf("ordered.Unmarshal() error = %v", err)
}
if secrets != nil && len(secrets) != 0 {
t.Errorf("Expected empty secrets, got %v", secrets)
}
}
func TestSecretsMapSyntaxPreservesOrder(t *testing.T) {
t.Parallel()
yamlData := `
FIRST: first-key
SECOND: second-key
THIRD: third-key
`
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err != nil {
t.Fatalf("ordered.Unmarshal() error = %v", err)
}
// Verify order is preserved
want := Secrets{
Secret{Key: "first-key", EnvironmentVariable: "FIRST"},
Secret{Key: "second-key", EnvironmentVariable: "SECOND"},
Secret{Key: "third-key", EnvironmentVariable: "THIRD"},
}
if diff := cmp.Diff(secrets, want); diff != "" {
t.Errorf("secrets mismatch (-got +want):\n%s", diff)
}
}
func TestSecretsBackwardCompatibility(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
yamlData string
want Secrets
}{
{
name: "simple array format",
yamlData: `
- DATABASE_URL
- API_TOKEN
- REDIS_URL
`,
want: Secrets{
Secret{Key: "DATABASE_URL", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "API_TOKEN", EnvironmentVariable: "API_TOKEN"},
Secret{Key: "REDIS_URL", EnvironmentVariable: "REDIS_URL"},
},
},
{
name: "simple map format",
yamlData: `
DATABASE_URL: DATABASE_URL
API_TOKEN: API_TOKEN
REDIS_URL: REDIS_URL
`,
want: Secrets{
Secret{Key: "DATABASE_URL", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "API_TOKEN", EnvironmentVariable: "API_TOKEN"},
Secret{Key: "REDIS_URL", EnvironmentVariable: "REDIS_URL"},
},
},
{
name: "complex map format (different keys)",
yamlData: `
DIRECT_DATABASE_URL: DATABASE_URL
BUILDKITE_API_TOKEN: API_TOKEN
REDIS_URL_DIRECT: REDIS_URL
`,
want: Secrets{
Secret{Key: "DATABASE_URL", EnvironmentVariable: "DIRECT_DATABASE_URL"},
Secret{Key: "API_TOKEN", EnvironmentVariable: "BUILDKITE_API_TOKEN"},
Secret{Key: "REDIS_URL", EnvironmentVariable: "REDIS_URL_DIRECT"},
},
},
{
name: "backend object format",
yamlData: `
- key: database-secret
environment_variable: DATABASE_URL
- key: api-secret
environment_variable: API_TOKEN
`,
want: Secrets{
Secret{Key: "database-secret", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "api-secret", EnvironmentVariable: "API_TOKEN"},
},
},
{
name: "backend object format with missing environment_variable",
yamlData: `
- key: database-secret
`,
want: Secrets{
Secret{Key: "database-secret", EnvironmentVariable: ""},
},
},
{
name: "mixed array formats",
yamlData: `
- DATABASE_URL
- key: api-secret
environment_variable: API_TOKEN
- REDIS_URL
`,
want: Secrets{
Secret{Key: "DATABASE_URL", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "api-secret", EnvironmentVariable: "API_TOKEN"},
Secret{Key: "REDIS_URL", EnvironmentVariable: "REDIS_URL"},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(tc.yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err != nil {
t.Fatalf("ordered.Unmarshal() error = %v", err)
}
if diff := cmp.Diff(secrets, tc.want); diff != "" {
t.Errorf("secrets mismatch (-got +want):\n%s", diff)
}
})
}
}
func TestSecretsInvalidFormatErrors(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
yamlData string
expectedErr string
}{
{
name: "invalid array item type",
yamlData: "- 123",
expectedErr: "unmarshaling secrets: secret type int, want string, map[string]any, or *ordered.Map",
},
{
name: "backend format with invalid key type",
yamlData: "- key: 123",
expectedErr: "unmarshaling secret: key must be a non-empty string, but was int 123",
},
{
name: "backend format with empty key",
yamlData: "- key: \"\"",
expectedErr: "unmarshaling secret: key must be a non-empty string, but was string ",
},
{
name: "backend format with invalid environment_variable type",
yamlData: "- key: test\n environment_variable: 123",
expectedErr: "unmarshaling secret: environment_variable must be a string, but was int",
},
{
name: "invalid top-level type",
yamlData: "\"invalid\"",
expectedErr: "unmarshaling secrets: got string, want []any or map[string]any",
},
{
name: "invalid top-level number",
yamlData: "123",
expectedErr: "unmarshaling secrets: got int, want []any or map[string]any",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(tc.yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err == nil {
t.Fatalf("ordered.Unmarshal() should return error, got nil")
}
if err.Error() != tc.expectedErr {
t.Errorf("ordered.Unmarshal() error = %q, want %q", err.Error(), tc.expectedErr)
}
})
}
}
func TestSecretsUnmarshalMapFormat(t *testing.T) {
t.Parallel()
yamlData := `
ENV_VAR: secret-key
API_TOKEN: api-secret-key
`
var secrets Secrets
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlData), &node)
if err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
err = ordered.Unmarshal(&node, &secrets)
if err != nil {
t.Fatalf("ordered.Unmarshal() error = %v", err)
}
want := Secrets{
Secret{Key: "secret-key", EnvironmentVariable: "ENV_VAR"},
Secret{Key: "api-secret-key", EnvironmentVariable: "API_TOKEN"},
}
if diff := cmp.Diff(secrets, want); diff != "" {
t.Errorf("map format unmarshaling mismatch (-got +want):\n%s", diff)
}
}
func TestSecretsMarshalYAML(t *testing.T) {
t.Parallel()
secrets := Secrets{
Secret{Key: "database-secret-key", EnvironmentVariable: "DATABASE_URL"},
Secret{Key: "api-secret-key", EnvironmentVariable: "API_TOKEN"},
}
// Test that it actually marshals to the expected YAML format
actualYAML, err := yaml.Marshal(secrets)
if err != nil {
t.Fatalf("yaml.Marshal() error = %v", err)
}
expectedYAML := `- key: database-secret-key
environment_variable: DATABASE_URL
- key: api-secret-key
environment_variable: API_TOKEN
`
if string(actualYAML) != expectedYAML {
t.Errorf("YAML output mismatch:\nGot:\n%s\nWant:\n%s", string(actualYAML), expectedYAML)
}
}
func TestSecretsMarshalYAMLEmptyEnvironmentVariable(t *testing.T) {
t.Parallel()
secrets := Secrets{
Secret{Key: "database-secret-key", EnvironmentVariable: ""},
Secret{Key: "api-secret-key", EnvironmentVariable: "API_TOKEN"},
}
// Test that it actually marshals to the expected YAML format
actualYAML, err := yaml.Marshal(secrets)
if err != nil {
t.Fatalf("yaml.Marshal() error = %v", err)
}
expectedYAML := `- key: database-secret-key
- key: api-secret-key
environment_variable: API_TOKEN
`
if string(actualYAML) != expectedYAML {
t.Errorf("YAML output mismatch:\nGot:\n%s\nWant:\n%s", string(actualYAML), expectedYAML)
}
}
func TestSecretsMarshalYAMLEmptyKey(t *testing.T) {
t.Parallel()
secrets := Secrets{
Secret{Key: "", EnvironmentVariable: "ENV_VARIABLE"},
Secret{Key: "api-secret-key", EnvironmentVariable: "API_TOKEN"},
}
// Test that it actually marshals to the expected YAML format
actualYAML, err := yaml.Marshal(secrets)
if err != nil {
t.Fatalf("yaml.Marshal() error = %v", err)
}
expectedYAML := `- key: ""
environment_variable: ENV_VARIABLE
- key: api-secret-key
environment_variable: API_TOKEN
`
if string(actualYAML) != expectedYAML {
t.Errorf("YAML output mismatch:\nGot:\n%s\nWant:\n%s", string(actualYAML), expectedYAML)
}
}
func TestSecretsMarshalYAMLUnsupportedConfiguration(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
secrets Secrets
}{
{
name: "secret with RemainingFields",
secrets: Secrets{
Secret{
Key: "secret-key",
EnvironmentVariable: "ENV_VAR",
RemainingFields: map[string]any{"custom": "field"},
},
},
},
{
name: "secret with empty EnvironmentVariable",
secrets: Secrets{
Secret{
Key: "secret-key",
EnvironmentVariable: "",
},
},
},
{
name: "mixed valid and invalid secrets",
secrets: Secrets{
Secret{Key: "valid-secret", EnvironmentVariable: "VALID_ENV"},
Secret{Key: "invalid-secret", EnvironmentVariable: ""},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.secrets.MarshalYAML()
if err != nil {
t.Fatalf("MarshalYAML() should return error for unsupported configuration, got nil")
}
})
}
}