-
Notifications
You must be signed in to change notification settings - Fork 31
/
tests
executable file
·3407 lines (2908 loc) · 102 KB
/
tests
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# vim:et:ts=2:sts=2:sw=2
set -u
done_testing=0
rc=0
platform=
arch=amd64
old_home="$HOME"
vault_pid=
root_token=
unseal_key=
declare -a versions=(
"1.9.10"
"1.10.10"
"1.11.7"
"1.12.3"
)
case $OSTYPE in
(darwin*)
platform=darwin
;;
(linux*)
platform=linux
;;
(*)
echo >&2 "UNRECOGNIZED OSTYPE '$OSTYPE'"
exit 1
;;
esac
bail() {
echo >&2 $*
exit 2
}
ok() {
if [[ -t 1 ]]; then
echo -e "\033[1;32mok\033[0m"
else
echo "ok"
fi
}
failed() {
if [[ -t 1 ]]; then
echo -e "\033[1;31mfail\033[0m"
else
echo "fail"
fi
}
tests_passed() {
if [[ -t 1 ]]; then
echo -e "\033[1;32mALL TESTS PASSED\033[0m"
else
echo "ALL TESTS PASSED"
fi
}
tests_failed() {
if [[ -t 1 ]]; then
echo -e "\033[1;31mTESTS FAILED\033[0m"
else
echo "TESTS FAILED"
fi
}
test=""
testing() {
if [[ $rc -ne 0 && -n "${FAIL_FAST:-}" ]] ; then
dump_log 1
echo "Failure encountered, stopping tests!"
exit 4
fi
rm -f t/home/got t/home/want t/home/diff t/home/errors
echo
if [[ -t 1 ]]; then
echo -e "[\033[1;36m$version\033[0m|\033[1;35mkv$kvversion\033[0m] \033[1;34m$*\033[0m"
else
echo "[$v] $*"
fi
test="$*"
}
clearvault() {
(./safe rm -rdaf secret) &>/dev/null
}
now() {
if [[ -n "${1:-}" ]]; then
test="$*"
fi
}
run() {
rm -f t/tmp/errors
exec &>t/tmp/errors
}
step() {
echo -n " $*... "
}
diffok() {
step "$test"
if [[ -f t/home/errors && -s t/home/errors ]]; then
failed
rc=1
cat t/home/errors
echo ; echo
elif ! diff -Bu t/home/got t/home/want >t/home/diff 2>&1; then
failed
rc=1
cat t/home/diff
echo ; echo
else
ok
fi
}
jsonok() {
cat t/home/got | jq -MS '.' > t/home/got.x ; mv t/home/got.x t/home/got
cat t/home/want | jq -MS '.' > t/home/want.x ; mv t/home/want.x t/home/want
diffok
}
yamlok() {
[ -s t/home/got ] || echo "--- {}" > t/home/got
spruce merge t/home/got > t/home/got.x ; mv t/home/got.x t/home/got
spruce merge t/home/want > t/home/want.x ; mv t/home/want.x t/home/want
diffok
}
exitok() {
step "$test"
if [[ $1 -eq ${2:-0} ]]; then
ok
else
failed
rc=1
echo " expected process to exit $2"
echo " but it actually exited $1"
if [[ -f t/tmp/errors ]]; then
echo
echo "---[ command output ]------------------"
cat t/tmp/errors
echo "---------------------------------------"
rm t/tmp/errors
else
echo " (no output from command)"
fi
fi
}
eq() {
got=$1
want=$2
step $test
if [[ $got != $want ]]; then
failed
rc=1
echo " got $got"
echo " expected $want"
else
ok
fi
}
eq_or_diff() {
got=$1
want=$2
step $test
if [[ $got != $want ]]; then
failed
rc=1
echo " values were different:"
diff -y <(echo "$got") \
<(echo "$want")
else
ok
fi
}
ne() {
got=$1
want=$2
step $test
if [[ $got == $want ]]; then
failed
rc=1
echo " got $got"
echo " expected literally anything else..."
else
ok
fi
}
eq_key() {
key1=$1
key2=$2
now retrieving "$key1" from vault
(run; ./safe get "$key1" >t/home/got) ; exitok $? 0
cp t/home/got t/home/want
now retrieving "$key2" from vault
(run; ./safe get "$key2" >t/home/got) ; exitok $? 0
now checking that "$key1" == "$key2"
diffok
}
is_key() {
key=$1
val=$2
alt=${3:-}
if [[ -z "${alt}" ]]; then
alt="'$2'"
fi
now retrieving "$key" from vault
(run; ./safe get "$key" >t/home/got) ; exitok $? 0
now checking that "$key" == "$alt"
cat >t/home/want <<EOF ; diffok
$val
EOF
}
ok_key() {
for key in "$@"; do
now checking that "$key" exists in vault
(run; ./safe exists "$key"); exitok $? 0
done
}
no_key() {
for key in "$@"; do
now checking that "$key" does not exist in vault
(run; ./safe exists "$key"); exitok $? 1
done
}
generate() {
now generating secret "'$1'" to test with
(run; ./safe set $*); exitok $? 0
}
dump_log() {
if [[ -f t/home/log ]] ; then
if grep -q 'goroutine' t/home/log; then
exit 77 # ./try will pick up on this error code
fi
if [[ ${rc} -ne 0 || -n ${1:-} ]]; then
echo
echo >&2 "---[ VAULT LOG ]------------------------------------------"
cat >&2 t/home/log
echo >&2 "----------------------------------------------------------"
echo
fi
fi
}
kill_running_vault() {
if [[ -n $vault_pid ]] ; then
kill $vault_pid
wait $vault_pid
vault_pid=
fi
}
cleanup() {
export HOME=${old_home}
rm -rf t/
kill_running_vault
if [[ ${done_testing} -eq 1 && ${rc} -eq 0 ]]; then
tests_passed
else
tests_failed
fi
exit ${rc}
}
restart_vault_server() {
kill_running_vault
rm -f t/home/log
mkdir -p ./vaults/bin
cp ./vaults/vault-${platform}-${version} ./vaults/bin/vault
./vaults/bin/vault server -dev -dev-listen-address 127.0.0.1:8198 >t/home/log 2>&1 &
vault_pid=$!
waitfor=600
while ! grep -iq '^root token: ' t/home/log; do
if [[ $waitfor -gt 0 ]]; then
waitfor=$((waitfor - 1))
sleep 0.1
else
failed "timed out waiting for vault server (-dev) to start"
dump_log 1
exit 3 # FIXME: this is the wrong thing to do
fi
done
root_token=$(awk '/^Root Token:/ { print $3 }' < t/home/log | head -n1)
unseal_key=$(awk '/^Unseal Key:/ { print $3 }' < t/home/log | head -n1)
now targeting test vault server
(run; ./safe target unit-tests http://127.0.0.1:8198) ; exitok $? 0
now authenticating with ${root_token} root token
(run; echo "$root_token" | ./safe auth token) ; exitok $? 0
local engine="kv"
local versionflag="-version $kvversion"
local mountcmd="secrets enable"
local unmountcmd="secrets disable"
if [[ $version =~ ^0\.[6-9].* ]]; then versionflag=""; fi
now unmounting the existing secret/ mount
(run; PATH="${PWD}/vaults/bin:${PATH}" ./safe vault $unmountcmd "secret/"); exitok $? 0
now mounting a kv v$kvversion engine
(run; PATH="${PWD}/vaults/bin:${PATH}" ./safe vault $mountcmd -path="secret/" $versionflag "$engine"); exitok $? 0
}
create_test_policy() {
./safe curl POST /sys/policy/test-policy '{"policy": "path \"secret/*\" { capabilities = [\"create\", \"read\", \"update\", \"delete\", \"list\"]}"}' >/dev/null
}
setup_approle() {
create_test_policy
role_name="test-role"
./safe vault auth enable approle >/dev/null #Mount the approle backend
./safe curl POST "auth/approle/role/${role_name}" '{"policies": "test-policy"}' >/dev/null #Create a role
role_id="$(./safe curl --data-only GET "auth/approle/role/${role_name}/role-id" | jq -Mr '.data.role_id')"
secret_id="$(./safe curl --data-only POST "auth/approle/role/${role_name}/secret-id" | jq -Mr '.data.secret_id')"
echo "$role_id" "$secret_id"
}
mkdir -p vaults t/tmp
trap 'cleanup' INT QUIT TERM EXIT
for version in "${versions[@]}"; do
killall vault-${platform}-${version} >/dev/null 2>&1 || true
done
for version in "${versions[@]}"; do
for kvversion in 1 2; do
# KV v2 was introduced in 0.10
if [[ kvversion -eq 2 && $version =~ ^0\.[6-9].* ]]; then
continue
fi
echo "VAULT ${version}" -- KV v${kvversion}
killall vault-${platform}-${version} >/dev/null 2>&1 || true
echo "----------------------------------------------"
if [[ ! -f vaults/vault-${platform}-${version} ]]; then
echo "Downloading Vault ${version} CLI..."
curl --fail -L > t/tmp/archive.zip \
https://releases.hashicorp.com/vault/${version}/vault_${version}_${platform}_${arch}.zip \
|| bail "download of vault ${version} failed"
unzip -d t/tmp t/tmp/archive.zip
mv t/tmp/vault vaults/vault-${platform}-${version}
echo "DONE"
echo
fi
old_home=$HOME
export HOME=${PWD}/t/home
rm -rf t/home ; mkdir -p t/home
#trap "rm -rf t/home" INT TERM QUIT EXIT
###### ######## ######## ## ## ########
## ## ## ## ## ## ## ##
## ## ## ## ## ## ##
###### ###### ## ## ## ########
## ## ## ## ## ##
## ## ## ## ## ## ##
###### ######## ## ####### ##
#######
testing setup
restart_vault_server
clearvault
######## ### ######## ###### ######## ########
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ## ## ######## ## #### ###### ##
## ######### ## ## ## ## ## ##
## ## ## ## ## ## ## ## ##
## ## ## ## ## ###### ######## ##
#######
testing targeting by ip
(run; ./safe target http://127.0.0.1:8198) ; exitok $? 0
(run; ./safe -T http://127.0.0.1:8198 env) ; exitok $? 0
testing targeting by alias
(run; ./safe target unit-tests) ; exitok $? 0
(run; ./safe -T unit-tests env) ; exitok $? 0
testing targeting a bad ip
(run; ./safe target http://127.0.0.1:8200) ; exitok $? 1
(run; ./safe -T http://127.0.0.1:8200 env) ; exitok $? 1
testing ambiguous targets
(run; ./safe target alternate \
http://127.0.0.1:8198) ; exitok $? 0
(run; ./safe target http://127.0.0.1:8198) ; exitok $? 1
(run; ./safe -T http://127.0.0.1:8198 env) ; exitok $? 1
restart_vault_server
###### ## ## ######## ##
## ## ## ## ## ## ##
## ## ## ## ## ##
## ## ## ######## ##
## ## ## ## ## ##
## ## ## ## ## ## ##
###### ####### ## ## ########
#######
clearvault
testing safe curl plumbing
now trying to curl without a leading slash
(run; ./safe curl sys/seal-status) ; exitok $? 0
now trying to curl with a leading slash
(run; ./safe curl /sys/seal-status) ; exitok $? 0
now trying to curl with embedded duplicate slashes
(run; ./safe curl /sys///seal-status) ; exitok $? 0
## ## ## ######## ## ##
#### ## ## ## ## ##
## ## ## ## ## ## ##
######## ## ## ## ########
## ## ## ## ## ## ##
## ## ## ## ## ## ##
## ## ####### ## ## ##
########
clearvault
testing approle login
now making an approle
approle_output="$(setup_approle)"
echo "$approle_output" >&2
role_id="$(awk '{print $1}' <<<"$approle_output")"
secret_id="$(awk '{print $2}' <<<"$approle_output")"
now trying to login with approle
(run; ./safe auth approle <<<"$role_id
$secret_id
") ; exitok $? 0;
now checking that we can do stuff with the Vault
(run; ./safe set secret/beep/boop foo=bar) ; exitok $? 0
(run; ./safe get secret/beep/boop) ; exitok $? 0
restart_vault_server
###### ######## ######## ## ###### ######## ########
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ##
## #### ###### ## ## ###### ###### ##
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ##
###### ######## ## ## ###### ######## ##
#######
clearvault
testing basic set/get operation
now setting a key=value on a new path
(run; ./safe set secret/handshake knock=knock) ; exitok $? 0
now asking for a key, but providing the value anyway
(run; ./safe ask secret/handshake whos=there) ; exitok $? 0
now retrieving the test path
(run; ./safe get secret/handshake >t/home/got) ; exitok $? 0
now checking that we got the expected secrets in the output
cat >t/home/want <<EOF ; yamlok
--- # secret/handshake
knock: knock
whos: there
EOF
for p in /secret/handshake \
secret///handshake \
/////secret//////handshake; do
now retrieving non-canonical $p path
(run; ./safe get $p >t/home/got) ; exitok $? 0
now checking that we got the expected secrets in the output
cat >t/home/want <<EOF ; yamlok
--- # secret/handshake
knock: knock
whos: there
EOF
done
now checking that we cannot set sub-keys on keyed path
(run; ./safe set secret/inner:secret key=value) ; exitok $? 1
now checking that we can set non-canonical paths
(run; ./safe set /secret///non/canonical key=value) ; exitok $? 0
now retrieving the secret from its canonical path
(run; ./safe get secret/non/canonical >t/home/got) ; exitok $? 0
now checking that the response is what we wrote
cat >t/home/want <<EOF ; yamlok
--- # secret/non/canonical
key: value
EOF
#######
#clearvault
testing get operation with --yaml on single path
now retrieving secret/handshake as --yaml
(run; ./safe get --yaml secret/handshake >t/home/got) ; exitok $? 0
now checking that the response is properly formatted
cat >t/home/want <<EOF ; yamlok
---
secret/handshake:
knock: knock
whos: there
EOF
#######
#clearvault
testing get operation: multiple requests, some missing
now setting secret/other/thing to a valid value
(run; ./safe set secret/other/thing get=this); exitok $? 0
now checking that a get where some of the paths are missing fails
(run; ./safe get secret/handshake \
secret/other/thing \
secret/non-existent \
secret/handshake:passcode >t/home/got 2>&1) ; exitok $? 1
now checking that we get errors about missing paths and keys
cat >t/home/want <<'EOF' ; diffok
!! Multiple errors found:
- no secret exists at path `secret/non-existent`
- no key `passcode` exists in secret `secret/handshake`
EOF
#######
#clearvault
testing get operation: multiple requests
now checking that a get of multiple existing paths succeeds
(run; ./safe get secret/handshake secret/other/thing >t/home/got) ; exitok $? 0
now checking that we get back the paths we want
cat >t/home/want <<'EOF' ; yamlok
---
secret/handshake:
knock: knock
whos: there
secret/other/thing:
get: this
EOF
#######
#clearvault
testing get --keys operation: multiple requests, some missing
now checking that a get where some of the keys are missing still works
(run; ./safe get --keys secret/handshake secret/other/thing secret/code:pssst >t/home/got 2>t/home/warn); exitok $? 0
now checking that we get back the paths we want
cat >t/home/want <<'EOF' ; yamlok
---
secret/handshake:
- knock
- whos
secret/other/thing:
- get
secret/code: []
EOF
mv t/home/warn t/home/got
now checking that we got a warning about the missing secrets
cat >t/home/want <<'EOF'; diffok
WARNING: no secret exists at path `secret/code`
EOF
#######
#clearvault
testing get --keys operation: single key
now retrieving a single path and all of its --keys
(run;./safe get --keys secret/handshake >t/home/got) ; exitok $? 0
now checking that we got the values of the keys
cat >t/home/want <<'EOF' ; diffok
knock
whos
EOF
#######
#clearvault
testing get --keys operation: single key with --yaml
now retrieving a single path and all of its --keys in --yaml format
(run; ./safe get --keys --yaml secret/handshake >t/home/got) ; exitok $? 0
now checking that we got the yaml output we wanted
cat >t/home/want <<'EOF' ; yamlok
---
secret/handshake:
- knock
- whos
EOF
#######
clearvault
testing multiple-attribute set operation
now setting multiple keys on the same path
(run; ./safe set secret/account username=admin password=sekrit) ; exitok $? 0
now retrieving secret/account to check for our keys
(run; ./safe get secret/account >t/home/got) ; exitok $? 0
now checking that our keys are found in the output
cat >t/home/want <<EOF ; yamlok
--- # secret/account
username: admin
password: sekrit
EOF
clearvault
testing versions - simple
now checking versions errs when the secret does not exist
(run; ./safe versions secret/notasecret); exitok $? 1
now putting a single version into the vault
(run; ./safe set secret/versions beep=boop); exitok $? 0
now checking that one versions does not err on an existing secret
(run; ./safe versions secret/versions >t/home/got); exitok $? 0
now checking that the output of versions was version 1
eq "$(tail -n +2 < t/home/got | awk '{print $1}')" 1
now checking that the output of versions was labeled as alive
eq "$(tail -n +2 < t/home/got | awk '{print $2}')" alive
###### ## ## ######## ###### ## ##
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ##
## ######### ###### ## #####
## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ##
###### ## ## ######## ###### ## ##
#######
clearvault
testing basic check/exists operation
now setting secret/handshake
(run; ./safe set secret/handshake knock=knock) ; exitok $? 0
now checking that secret/handshake:knock exists
(run; ./safe exists secret/handshake:knock) ; exitok $? 0
now checking that secret/handshake:enoent does not exist
(run; ./safe exists secret/handshake:enoent) ; exitok $? 1
now checking that secret/enoent does not exist
(run; ./safe exists secret/enoent) ; exitok $? 1
now checking that secret/enoent:sub does not exist
(run; ./safe exists secret/enoent:sub) ; exitok $? 1
###### ######## ## ##
## ## ## ### ##
## ## #### ##
## #### ###### ## ## ##
## ## ## ## ####
## ## ## ## ###
###### ######## ## ##
#######
clearvault
testing password randomization
now generating some random secrets
(run; ./safe gen secret/random one ) ; exitok $? 0
(run; ./safe gen secret/random another) ; exitok $? 0
now retrieving generated credentials
(run; ./safe get secret/random | spruce json >t/home/got) ; exitok $? 0
one=$(jq -r .one < t/home/got)
another=$(jq -r .another < t/home/got)
step "checking that the two generated passwords are different"
if [[ ${one} == ${another} ]]; then
failed
rc=1
echo " ... expected generated password '${one}'"
echo " to not be the same as '${another}'"
else
ok
fi
###### ## ## ### #### ## ##
## ## ## ## ## ## ## ### ##
## ## ## ## ## ## #### ##
## ######### ## ## ## ## ## ##
## ## ## ######### ## ## ####
## ## ## ## ## ## ## ## ###
###### ## ## ## ## #### ## ##
#######
clearvault
testing command chaining
now generating three credentials via a command chain
(run; ./safe gen secret/chained a -- \
gen secret/chained b -- \
gen secret/chained c) ; exitok $? 0
now retrieving our generated credentials
(run; ./safe get secret/chained | spruce json > t/home/got) ; exitok $? 0
for k in a b c; do
step "checking that each credential exists"
v=$(jq -r .$k < t/home/got)
if [[ -z "$v" ]]; then
failed
rc=1
echo " ... expected secret/chained/$k to exist"
else
ok
fi
done
#######
clearvault
testing custom password runes randomization
now generating credentials consisting of only the digits 1 - 9
(run; ./safe gen --policy "1-9" 64 secret/random one) ; exitok $? 0
now retrieving our generated credentials
(run; ./safe get secret/random | spruce json >t/home/runed) ; exitok $? 0
step "checking that the generated credential only consists of the digits 1-9"
one=$(jq -r .one < t/home/runed)
if [[ "$one" =~ ^[1-9]+$ ]]; then
ok
else
failed
rc=1
echo " expected generated password: '$one'"
echo " to only contain: [1-9]"
fi
#######
clearvault
testing password with secret:key syntax
now generating a random secret at secret/random:two
(run; ./safe gen 64 secret/random:two) ; exitok $? 0
now retrieving our generated credentials
(run; ./safe get secret/random:two) ; exitok $? 0
#######
clearvault
testing gen with multiple passwords to make
now generating passwords
(run; ./safe gen 64 secret/random:three secret/random four secret/random:five) ; exitok $? 0
ok_key secret/random:{three,four,five}
#######
clearvault
testing gen with length flag
now generating a 10-character password
(run; ./safe gen secret/random:six -l 10) ; exitok $? 0
now retrieving our 10-character password
(run; ./safe get secret/random:six | tr -d '\n' >t/home/got) ; exitok $? 0
step "testing that the password is only 10 characters long"
if [[ $(cat t/home/got | wc -m) -eq 10 ]]; then
ok
else
failed
rc=1
echo "Expected generated password to have 10 characters"
echo "Got $(cat t/home/got | wc -m) instead"
fi
#######
clearvault
testing gen with incorrectly mixed path:key should fail
(run; ./safe gen secret/random secret/random:seven) ; exitok $? 1
no_key secret/random
#######
clearvault
testing gen with missing key should fail
(run; ./safe gen secret/random) ; exitok $? 1
no_key secret/random
#######
clearvault
testing single-value retrieval
(run; ./safe set secret/single/value foo=bar baz=quux) ; exitok $? 0
foo=$(./safe get secret/single/value:foo)
step "retrieving our single value"
if [[ ${foo} = "bar" ]]; then
ok
else
failed
rc=1
echo " ... expected foo='bar'"
echo " got '${foo}'"
fi
#######
clearvault
testing set key from file
echo -n "this is a value" >t/home/value-file
echo -n "" >t/home/empty-file
cat >t/home/long-file <<EOF
this
is
a
multiline
value
EOF
now setting values from files
(run; ./safe set secret/from-files/content \
value@t/home/value-file \
blank@t/home/empty-file \
long@t/home/long-file) ; exitok $? 0
now retrieving set values
(run; ./safe get secret/from-files/content --yaml >t/home/got) ; exitok $? 0
now checking that we got back the correct multiline and single-line values
cat >t/home/want <<EOF ; yamlok
---
secret/from-files/content:
value: this is a value
blank: ""
long: |
this
is
a
multiline
value
EOF
#######
clearvault
testing set key to empty value
now setting values
(run; ./safe set secret/setec/astronomy too= many="") ; exitok $? 0
is_key secret/setec/astronomy:too ""
is_key secret/setec/astronomy:many ""
#######
clearvault
testing set key from file - error conditions
now setting values from a non-existent file - should fail
(run; ./safe set secret/from-file/content nothing@non-existent-file 2>t/home/got) ; exitok $? 1
now checking the output error
cat > t/home/want<<EOF ; diffok
!! Failed to read contents of non-existent-file: open non-existent-file: no such file or directory
EOF
now checking that we did not set a secret in vault
(run; ./safe get secret/from-file/content:nothing) ; exitok $? 1
now setting values using file syntax, but missing the file argument
(run; ./safe set secret/from-file/content no-file@ 2>t/home/got) ; exitok $? 1
now checking the output error
cat > t/home/want<<EOF ; diffok
!! No file specified: expecting no-file@<filename>
EOF
now checking that we did not set a secret in vault
(run; ./safe get secret/from-file/content:no-file) ; exitok $? 1
######## ## ## ########
## ### ### ##
## #### #### ##
###### ## ### ## ##
## ## ## ##
## ## ## ##
## ## ## ##
#######
clearvault
testing fmt - all formats
now generating a test secret to format
(run; ./safe gen secret/fmt/formats original) ; exitok $? 0
now verifying that our secret got created
(run; ./safe get secret/fmt/formats:original) ; exitok $? 0
for f in base64 bcrypt crypt-{md5,sha256,sha512}; do
now formatting $f
(run; ./safe fmt $f secret/fmt/formats original $f) ; exitok $? 0
now verifying that we generated a secret for $f
(run; ./safe get secret/fmt/formats:$f) ; exitok $? 0
done
# note that the tests do not support the optional parameters in crypt format
# (i.e. `$<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]]`)
now checking bcrypt format
(run; ./safe get secret/fmt/formats:bcrypt \
| grep -qE '^\$2[axy]?\$[a-zA-Z0-9+/=]+\$.+') ; exitok $? 0
now checking crypt-md5 format
(run; ./safe get secret/fmt/formats:crypt-md5 \
| grep -qE '^\$1\$[a-zA-Z0-9+/=]+\$.+') ; exitok $? 0
now checking crypt-sha256 format
(run; ./safe get secret/fmt/formats:crypt-sha256 \
| grep -qE '^\$5\$[a-zA-Z0-9+/=]+\$.+') ; exitok $? 0
now checking crypt-sha512 format
(run; ./safe get secret/fmt/formats:crypt-sha512 \
| grep -qE '^\$6\$[a-zA-Z0-9+/=]+\$.+') ; exitok $? 0
######## ######## ## ######## ######## ########
## ## ## ## ## ## ##
## ## ## ## ## ## ##
## ## ###### ## ###### ## ######
## ## ## ## ## ## ##
## ## ## ## ## ## ##
######## ######## ######## ######## ## ########
#######
clearvault
testing secret deletion
no_key secret/delete
generate secret/delete hello=world
is_key secret/delete:hello world
now removing the secret
(run; ./safe delete secret/delete) ; exitok $? 0
no_key secret/delete
#######
clearvault
testing recursive secret deletion
no_key secret/a/b/c/d/e/f/delete
generate secret/a/b/c/d/e/f/delete hello=world
generate secret/a/b/c/1/2/3/delete goodbye=cruel-world
generate secret/a/a/a/a/a/a/a/h real=monsters
is_key secret/a/b/c/d/e/f/delete:hello world
is_key secret/a/b/c/1/2/3/delete:goodbye cruel-world
is_key secret/a/a/a/a/a/a/a/h:real monsters
now removing the secret
(run; ./safe delete -Rf secret/a/b) ; exitok $? 0
no_key secret/a/b/c/d/e/f/delete
no_key secret/a/b/c/1/2/3/delete
is_key secret/a/a/a/a/a/a/a/h:real monsters # still got it!
#######
clearvault
testing multiple secret deletion
no_key secret/delete/{two,three}
generate secret/delete/two foo=bar
generate secret/delete/three wom=bat
now deleting both secrets
(run; ./safe delete secret/delete/two secret/delete/three) ; exitok $? 0
no_key secret/delete/two
no_key secret/delete/three
#######
clearvault
testing key deletion with key remaining within secret afterwards
generate secret/delete foo=bar wom=bat
now deleting a single key in the multi-key path
(run; ./safe delete secret/delete:wom) ; exitok $? 0
no_key secret/delete:wom
is_key secret/delete:foo "bar"