-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
710 lines (660 loc) · 24.2 KB
/
main_test.go
File metadata and controls
710 lines (660 loc) · 24.2 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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package main
import (
"bytes"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
sopsconfig "github.com/getsops/sops/v3/config"
)
func TestValidateYAMLContent(t *testing.T) {
// Create a rule that requires all keys to be encrypted
ruleAllEncrypted := &sopsconfig.Config{
EncryptedRegex: "", // Empty means all keys should be encrypted
}
// Create a rule that only encrypts keys matching password|secret|token|key
rulePartialEncryption := &sopsconfig.Config{
EncryptedRegex: "^(password|secret|token|key|pass|token|credentials)$",
}
// Create a rule that allows some keys to remain plaintext.
ruleWithUnencryptedRegex := &sopsconfig.Config{
UnencryptedRegex: "^(host|port|username)$",
}
// Create a rule that encrypts only keys ending in _enc.
ruleWithEncryptedSuffix := &sopsconfig.Config{
EncryptedSuffix: "_enc",
}
// Create a rule that leaves keys ending in _plain unencrypted.
ruleWithUnencryptedSuffix := &sopsconfig.Config{
UnencryptedSuffix: "_plain",
}
// Rule with no selectors — SOPS defaults to UnencryptedSuffix: "_unencrypted"
ruleDefault := &sopsconfig.Config{}
tests := []struct {
name string
yaml string
rule *sopsconfig.Config
wantFail []string
wantErr bool
}{
{
name: "all encrypted scalars pass",
yaml: "apiVersion: ENC[AES256_GCM,data:abc]\nkind: ENC[AES256_GCM,data:def]\nspec:\n credentials:\n user: ENC[AES256_GCM,data:ghi]\n pass: ENC[AES256_GCM,data:jkl]\n replicas:\n - ENC[AES256_GCM,data:mno]\n",
rule: ruleAllEncrypted,
wantFail: []string{},
},
{
name: "sops metadata is ignored",
yaml: "secret: ENC[AES256_GCM,data:abc]\nsops:\n version: 3.9.0\n kms:\n - arn:aws:kms:us-east-1:123456789012:key/abcd\n",
rule: ruleAllEncrypted,
wantFail: []string{},
},
{
name: "null and empty string must be encrypted when selected",
yaml: "service:\n token: null\n note: \"\"\n password: ENC[AES256_GCM,data:abc]\n",
rule: ruleAllEncrypted,
wantFail: []string{"3:9: unencrypted value found at 'service.note'"},
},
{
name: "reports nested map and array paths",
yaml: "services:\n db:\n password: plaintext\n api:\n keys:\n - ENC[AES256_GCM,data:abc]\n - not_encrypted\n",
rule: ruleAllEncrypted,
wantFail: []string{"3:15: unencrypted value found at 'services.db.password'", "7:9: unencrypted value found at 'services.api.keys.1'"},
},
{
name: "non-string scalar is unencrypted",
yaml: "count: 3\n",
rule: ruleAllEncrypted,
wantFail: []string{"1:8: unencrypted value found at 'count'"},
},
{
name: "invalid yaml returns error",
yaml: "key: [unclosed\n",
rule: ruleAllEncrypted,
wantErr: true,
wantFail: nil,
},
{
name: "partial encryption: only password/secret/token/key must be encrypted",
yaml: "host: localhost\nport: 5432\nusername: admin\npassword: plaintext\ntoken: ENC[AES256_GCM,data:abc]\n",
rule: rulePartialEncryption,
wantFail: []string{"4:11: unencrypted value found at 'password'"},
},
{
name: "unencrypted_regex: matching keys may remain plaintext",
yaml: "host: localhost\nport: 5432\nusername: admin\npassword: plaintext\n",
rule: ruleWithUnencryptedRegex,
wantFail: []string{"4:11: unencrypted value found at 'password'"},
},
{
name: "unencrypted_regex: empty values allowed only for excluded keys",
yaml: "host: \"\"\nport: null\npassword: null\n",
rule: ruleWithUnencryptedRegex,
wantFail: []string{},
},
{
name: "encrypted_suffix: only matching suffix keys must be encrypted",
yaml: "host: localhost\npassword_enc: plaintext\napi_key_enc: ENC[AES256_GCM,data:abc]\n",
rule: ruleWithEncryptedSuffix,
wantFail: []string{"2:15: unencrypted value found at 'password_enc'"},
},
{
name: "unencrypted_suffix: matching suffix keys may remain plaintext",
yaml: "host_plain: localhost\npassword: plaintext\n",
rule: ruleWithUnencryptedSuffix,
wantFail: []string{"2:11: unencrypted value found at 'password'"},
},
{
name: "default rule: keys ending in _unencrypted are excluded",
yaml: "password: plaintext\ndebug_unencrypted: true\n",
rule: ruleDefault,
wantFail: []string{"1:11: unencrypted value found at 'password'"},
},
{
name: "default rule: all _unencrypted keys pass without encryption",
yaml: "token: ENC[AES256_GCM,data:abc]\nlog_level_unencrypted: info\n",
rule: ruleDefault,
wantFail: []string{},
},
{
name: "empty file returns no failures",
yaml: "",
rule: ruleAllEncrypted,
wantFail: []string{},
},
{
name: "comment-only file returns no failures",
yaml: "# This is a comment\n# Another comment\n",
rule: ruleAllEncrypted,
wantFail: []string{},
},
{
name: "bare document separator returns no failures",
yaml: "---\n",
rule: ruleAllEncrypted,
wantFail: []string{},
},
{
name: "unencrypted_regex excludes matching keys from encryption",
yaml: "runtime: prod\ntags: web\nsecret: plaintext\n",
rule: &sopsconfig.Config{UnencryptedRegex: "^(runtime|tags)$"},
wantFail: []string{"3:9: unencrypted value found at 'secret'"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotFail, err := validateYAMLContent([]byte(tt.yaml), tt.rule)
if (err != nil) != tt.wantErr {
t.Fatalf("validateYAMLContent() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
sort.Strings(gotFail)
sort.Strings(tt.wantFail)
if !reflect.DeepEqual(gotFail, tt.wantFail) {
t.Fatalf("validateYAMLContent() failures = %v, want %v", gotFail, tt.wantFail)
}
})
}
}
func TestRunExitCodes(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
wantExitCode int
wantErrSubstr string
}{
{
name: "file not found",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
return filepath.Join(tempDir, "nonexistent.yaml")
},
wantExitCode: exitFileReadError,
wantErrSubstr: "target not found",
},
{
name: "invalid yaml in matched file",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "invalid.yaml"), []byte("key: [unclosed\n"), 0o600); err != nil {
t.Fatalf("write invalid yaml: %v", err)
}
return tempDir
},
wantExitCode: exitInvalidInput,
wantErrSubstr: "invalid YAML",
},
{
name: "unencrypted value in matched file",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "unencrypted.yaml"), []byte("spec:\n db:\n password: plaintext\n"), 0o600); err != nil {
t.Fatalf("write unencrypted yaml: %v", err)
}
return tempDir
},
wantExitCode: exitUnencryptedValue,
wantErrSubstr: "unencrypted value found",
},
{
name: "valid project",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "valid.yaml"), []byte("key: ENC[AES256_GCM,data:abc]\n"), 0o600); err != nil {
t.Fatalf("write valid yaml: %v", err)
}
return tempDir
},
wantExitCode: exitSuccess,
wantErrSubstr: "All files compliant",
},
{
name: "valid project prints success summary",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "valid.yaml"), []byte("key: ENC[AES256_GCM,data:abc]\n"), 0o600); err != nil {
t.Fatalf("write valid yaml: %v", err)
}
return tempDir
},
wantExitCode: exitSuccess,
wantErrSubstr: "All files compliant",
},
{
name: "unencrypted value prints violation summary",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "bad.yaml"), []byte("a: plain1\nb: plain2\n"), 0o600); err != nil {
t.Fatalf("write bad yaml: %v", err)
}
return tempDir
},
wantExitCode: exitUnencryptedValue,
wantErrSubstr: "SOPS-COP found 2 violations",
},
{
name: "empty yaml file passes",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "empty.yaml"), []byte(""), 0o600); err != nil {
t.Fatalf("write empty yaml: %v", err)
}
return tempDir
},
wantExitCode: exitSuccess,
wantErrSubstr: "All files compliant",
},
{
name: "comment-only yaml file passes",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "comments.yaml"), []byte("# only comments here\n"), 0o600); err != nil {
t.Fatalf("write comments yaml: %v", err)
}
return tempDir
},
wantExitCode: exitSuccess,
wantErrSubstr: "All files compliant",
},
{
name: "only files matching path_regex are validated",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: "^secrets/.*\\.yaml$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.MkdirAll(filepath.Join(tempDir, "secrets"), 0o755); err != nil {
t.Fatalf("mkdir secrets: %v", err)
}
if err := os.MkdirAll(filepath.Join(tempDir, "configs"), 0o755); err != nil {
t.Fatalf("mkdir configs: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "secrets", "bad.yaml"), []byte("password: plaintext\n"), 0o600); err != nil {
t.Fatalf("write secrets bad yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "configs", "ignored.yaml"), []byte("password: plaintext\n"), 0o600); err != nil {
t.Fatalf("write ignored yaml: %v", err)
}
return tempDir
},
wantExitCode: exitUnencryptedValue,
wantErrSubstr: "secrets/bad.yaml",
},
{
name: "multiple creation rules apply different encryption to different paths",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
// Rule 1: secrets/ files must encrypt everything
// Rule 2: configs/ files only encrypt keys matching "password"
sopsConfig := `creation_rules:
- path_regex: "^secrets/.*\\.yaml$"
encrypted_regex: ""
- path_regex: "^configs/.*\\.yaml$"
encrypted_regex: "^password$"
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.MkdirAll(filepath.Join(tempDir, "secrets"), 0o755); err != nil {
t.Fatalf("mkdir secrets: %v", err)
}
if err := os.MkdirAll(filepath.Join(tempDir, "configs"), 0o755); err != nil {
t.Fatalf("mkdir configs: %v", err)
}
// secrets/creds.yaml: fully encrypted — should pass
if err := os.WriteFile(filepath.Join(tempDir, "secrets", "creds.yaml"),
[]byte("token: ENC[AES256_GCM,data:abc]\n"), 0o600); err != nil {
t.Fatalf("write secrets creds.yaml: %v", err)
}
// configs/db.yaml: host is plaintext (allowed), password is plaintext (violation)
if err := os.WriteFile(filepath.Join(tempDir, "configs", "db.yaml"),
[]byte("host: localhost\npassword: plaintext\n"), 0o600); err != nil {
t.Fatalf("write configs db.yaml: %v", err)
}
return tempDir
},
wantExitCode: exitUnencryptedValue,
wantErrSubstr: "configs/db.yaml",
},
{
name: "multiple creation rules all passing",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: "^secrets/.*\\.yaml$"
encrypted_regex: ""
- path_regex: "^configs/.*\\.yaml$"
encrypted_regex: "^password$"
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.MkdirAll(filepath.Join(tempDir, "secrets"), 0o755); err != nil {
t.Fatalf("mkdir secrets: %v", err)
}
if err := os.MkdirAll(filepath.Join(tempDir, "configs"), 0o755); err != nil {
t.Fatalf("mkdir configs: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "secrets", "creds.yaml"),
[]byte("token: ENC[AES256_GCM,data:abc]\n"), 0o600); err != nil {
t.Fatalf("write secrets creds.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "configs", "db.yaml"),
[]byte("host: localhost\npassword: ENC[AES256_GCM,data:xyz]\n"), 0o600); err != nil {
t.Fatalf("write configs db.yaml: %v", err)
}
return tempDir
},
wantExitCode: exitSuccess,
wantErrSubstr: "All files compliant",
},
{
name: "invalid rule when both encrypted_regex and unencrypted_regex are set",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: "^password$"
unencrypted_regex: "^host$"
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "bad.yaml"), []byte("host: localhost\n"), 0o600); err != nil {
t.Fatalf("write bad yaml: %v", err)
}
return tempDir
},
wantExitCode: exitConfigError,
wantErrSubstr: "cannot use more than one of encrypted_suffix",
},
{
name: "invalid rule when both encrypted_suffix and unencrypted_suffix are set",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_suffix: "_enc"
unencrypted_suffix: "_plain"
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "bad.yaml"), []byte("password_enc: plaintext\n"), 0o600); err != nil {
t.Fatalf("write bad yaml: %v", err)
}
return tempDir
},
wantExitCode: exitConfigError,
wantErrSubstr: "cannot use more than one of encrypted_suffix",
},
{
name: "invalid rule when regex and suffix selectors are mixed",
setup: func(t *testing.T) string {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.yaml$"
encrypted_regex: "^password$"
encrypted_suffix: "_enc"
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "bad.yaml"), []byte("password: plaintext\n"), 0o600); err != nil {
t.Fatalf("write bad yaml: %v", err)
}
return tempDir
},
wantExitCode: exitConfigError,
wantErrSubstr: "cannot use more than one of encrypted_suffix",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
target := tt.setup(t)
var stderr bytes.Buffer
gotExitCode := run(target, &stderr)
if gotExitCode != tt.wantExitCode {
t.Fatalf("run() exit code = %d, want %d", gotExitCode, tt.wantExitCode)
}
if tt.wantErrSubstr != "" && !strings.Contains(stderr.String(), tt.wantErrSubstr) {
t.Fatalf("run() stderr = %q, want substring %q", stderr.String(), tt.wantErrSubstr)
}
})
}
}
func TestExampleSecretFixtureWithSopsConfig(t *testing.T) {
// Use the real .sops.yaml and secrets.example.yaml from the repo.
repoRoot, err := filepath.Abs(".")
if err != nil {
t.Fatalf("failed to resolve repo root: %v", err)
}
sopsConfigBytes, err := os.ReadFile(filepath.Join(repoRoot, ".sops.yaml"))
if err != nil {
t.Fatalf("read .sops.yaml: %v", err)
}
encryptedSecretBytes, err := os.ReadFile(filepath.Join(repoRoot, "secrets.example.yaml"))
if err != nil {
t.Fatalf("read secrets.example.yaml: %v", err)
}
t.Run("encrypted fixture passes", func(t *testing.T) {
tempDir := t.TempDir()
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), sopsConfigBytes, 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "secrets.example.yaml"), encryptedSecretBytes, 0o600); err != nil {
t.Fatalf("write secrets.example.yaml: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitSuccess {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitSuccess, stderr.String())
}
})
t.Run("plaintext secret value fails", func(t *testing.T) {
tempDir := t.TempDir()
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), sopsConfigBytes, 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
plaintextSecretYAML := strings.Replace(string(encryptedSecretBytes),
"DB_PASSWORD: ENC[AES256_GCM,data:c3VwZXItc2VjcmV0LXBhc3M=]",
"DB_PASSWORD: plaintext-password", 1)
if err := os.WriteFile(filepath.Join(tempDir, "secrets.example.yaml"), []byte(plaintextSecretYAML), 0o600); err != nil {
t.Fatalf("write secrets.example.yaml: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitUnencryptedValue {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitUnencryptedValue, stderr.String())
}
errOut := stderr.String()
if !strings.Contains(errOut, "secrets.example.yaml") || !strings.Contains(errOut, "stringData.DB_PASSWORD") {
t.Fatalf("run() stderr = %q, want references to secrets.example.yaml and stringData.DB_PASSWORD", errOut)
}
})
}
func TestRunSupportsStructuredFormats(t *testing.T) {
t.Run("json plaintext value fails", func(t *testing.T) {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.json$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "secrets.json"), []byte(`{"password":"plaintext"}`), 0o600); err != nil {
t.Fatalf("write secrets.json: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitUnencryptedValue {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitUnencryptedValue, stderr.String())
}
if !strings.Contains(stderr.String(), "secrets.json") || !strings.Contains(stderr.String(), "unencrypted value found") {
t.Fatalf("run() stderr = %q, want json violation output", stderr.String())
}
})
t.Run("json encrypted value passes", func(t *testing.T) {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.json$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "secrets.json"), []byte(`{"password":"ENC[AES256_GCM,data:abc]"}`), 0o600); err != nil {
t.Fatalf("write secrets.json: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitSuccess {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitSuccess, stderr.String())
}
if !strings.Contains(stderr.String(), "All files compliant") {
t.Fatalf("run() stderr = %q, want success summary", stderr.String())
}
})
t.Run("dotenv plaintext value fails", func(t *testing.T) {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.env$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "app.env"), []byte("PASSWORD=plaintext\n"), 0o600); err != nil {
t.Fatalf("write app.env: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitUnencryptedValue {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitUnencryptedValue, stderr.String())
}
if !strings.Contains(stderr.String(), "app.env") || !strings.Contains(stderr.String(), "unencrypted value found") {
t.Fatalf("run() stderr = %q, want env violation output", stderr.String())
}
})
t.Run("dotenv encrypted value passes", func(t *testing.T) {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.env$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "app.env"), []byte("PASSWORD=ENC[AES256_GCM,data:abc]\n"), 0o600); err != nil {
t.Fatalf("write app.env: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitSuccess {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitSuccess, stderr.String())
}
if !strings.Contains(stderr.String(), "All files compliant") {
t.Fatalf("run() stderr = %q, want success summary", stderr.String())
}
})
t.Run("ini plaintext value fails", func(t *testing.T) {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.ini$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "app.ini"), []byte("[db]\npassword=plaintext\n"), 0o600); err != nil {
t.Fatalf("write app.ini: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitUnencryptedValue {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitUnencryptedValue, stderr.String())
}
if !strings.Contains(stderr.String(), "app.ini") || !strings.Contains(stderr.String(), "unencrypted value found") {
t.Fatalf("run() stderr = %q, want ini violation output", stderr.String())
}
})
t.Run("ini encrypted value passes", func(t *testing.T) {
tempDir := t.TempDir()
sopsConfig := `creation_rules:
- path_regex: ".*\\.ini$"
encrypted_regex: ""
`
if err := os.WriteFile(filepath.Join(tempDir, ".sops.yaml"), []byte(sopsConfig), 0o600); err != nil {
t.Fatalf("write .sops.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(tempDir, "app.ini"), []byte("[db]\npassword=ENC[AES256_GCM,data:abc]\n"), 0o600); err != nil {
t.Fatalf("write app.ini: %v", err)
}
var stderr bytes.Buffer
gotExitCode := run(tempDir, &stderr)
if gotExitCode != exitSuccess {
t.Fatalf("run() exit code = %d, want %d; stderr=%q", gotExitCode, exitSuccess, stderr.String())
}
if !strings.Contains(stderr.String(), "All files compliant") {
t.Fatalf("run() stderr = %q, want success summary", stderr.String())
}
})
}